Skip to content

Commit

Permalink
Add instrumentation hook for wrapping statements
Browse files Browse the repository at this point in the history
  • Loading branch information
iand675 committed Mar 29, 2021
1 parent 03e794f commit b6d092d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion persistent-mysql/Database/Persist/MySQL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ open' ci logFunc = do
, connLogFunc = logFunc
, connMaxParams = Nothing
, connRepsertManySql = Just repsertManySql
, connStatementMiddleware = const pure
}

-- | Prepare a query. We don't support prepared statements, but
Expand Down Expand Up @@ -1270,7 +1271,8 @@ mockMigration mig = do
connUpsertSql = undefined,
connPutManySql = undefined,
connMaxParams = Nothing,
connRepsertManySql = Nothing
connRepsertManySql = Nothing,
connStatementMiddleware = const pure
}
result = runReaderT . runWriterT . runWriterT $ mig
resp <- result sqlbackend
Expand Down
4 changes: 3 additions & 1 deletion persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ createBackend logFunc serverVersion smap conn = do
, connLogFunc = logFunc
, connMaxParams = Nothing
, connRepsertManySql = upsertFunction repsertManySql serverVersion
, connStatementMiddleware = const pure
}

prepare' :: PG.Connection -> Text -> IO Statement
Expand Down Expand Up @@ -1720,7 +1721,8 @@ mockMigration mig = do
connLimitOffset = undefined,
connLogFunc = undefined,
connMaxParams = Nothing,
connRepsertManySql = Nothing
connRepsertManySql = Nothing,
connStatementMiddleware = const pure
}
result = runReaderT $ runWriterT $ runWriterT mig
resp <- result sqlbackend
Expand Down
2 changes: 2 additions & 0 deletions persistent-sqlite/Database/Persist/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ wrapConnectionInfo connInfo conn logFunc = do
, connLogFunc = logFunc
, connMaxParams = Just 999
, connRepsertManySql = Just repsertManySql
, connStatementMiddleware = const pure
}
where
helper t getter = do
Expand Down Expand Up @@ -482,6 +483,7 @@ mockMigration mig = do
, connPutManySql = undefined
, connMaxParams = Just 999
, connRepsertManySql = Nothing
, connStatementMiddleware = const pure
}
result = runReaderT . runWriterT . runWriterT $ mig
resp <- result sqlbackend
Expand Down
4 changes: 2 additions & 2 deletions persistent/Database/Persist/Sql/Raw.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ getStmtConn :: SqlBackend -> Text -> IO Statement
getStmtConn conn sql = do
smap <- liftIO $ readIORef $ connStmtMap conn
case Map.lookup sql smap of
Just stmt -> return stmt
Just stmt -> connStatementMiddleware conn sql stmt
Nothing -> do
stmt' <- liftIO $ connPrepare conn sql
iactive <- liftIO $ newIORef True
Expand All @@ -100,7 +100,7 @@ getStmtConn conn sql = do
else liftIO $ throwIO $ StatementAlreadyFinalized sql
}
liftIO $ writeIORef (connStmtMap conn) $ Map.insert sql stmt smap
return stmt
connStatementMiddleware conn sql stmt

-- | Execute a raw SQL statement and return its results as a
-- list. If you do not expect a return value, use of
Expand Down
3 changes: 3 additions & 0 deletions persistent/Database/Persist/Sql/Types/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ data SqlBackend = SqlBackend
-- When left as 'Nothing', we default to using 'defaultRepsertMany'.
--
-- @since 2.9.0
, connStatementMiddleware :: Text -> Statement -> IO Statement
-- ^ Provide facilities for injecting middleware into statements
-- to allow for instrumenting queries.
}

instance HasPersistBackend SqlBackend where
Expand Down

0 comments on commit b6d092d

Please sign in to comment.