File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -426,6 +426,27 @@ sqlQueryScalar theQuery theParameters = do
426
426
_ -> error " sqlQueryScalar: Expected a scalar result value"
427
427
{-# INLINABLE sqlQueryScalar #-}
428
428
429
+ -- | Runs a raw sql query which results in a single scalar value such as an integer or string, or nothing
430
+ --
431
+ -- __Example:__
432
+ --
433
+ -- > usersCount <- sqlQueryScalarOrNothing "SELECT COUNT(*) FROM users"
434
+ --
435
+ -- Take a look at "IHP.QueryBuilder" for a typesafe approach on building simple queries.
436
+ sqlQueryScalarOrNothing :: (? modelContext :: ModelContext ) => (PG. ToRow q , Show q , FromField value ) => Query -> q -> IO (Maybe value )
437
+ sqlQueryScalarOrNothing theQuery theParameters = do
438
+ result <- measureTimeIfLogging
439
+ (withDatabaseConnection \ connection -> enhanceSqlError theQuery theParameters do
440
+ PG. query connection theQuery theParameters
441
+ )
442
+ theQuery
443
+ theParameters
444
+ pure case result of
445
+ [] -> Nothing
446
+ [PG. Only result] -> Just result
447
+ _ -> error " sqlQueryScalarOrNothing: Expected a scalar result value or an empty result set"
448
+ {-# INLINABLE sqlQueryScalarOrNothing #-}
449
+
429
450
-- | Executes the given block with a database transaction
430
451
--
431
452
-- __Example:__
You can’t perform that action at this time.
0 commit comments