Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions async_postgres/pg_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,25 @@ proc queryValueOrDefault*[T](
return default
return row.get(0, T)

proc queryValueOrDefault*[T](
conn: PgConnection,
sql: string,
params: seq[PgParam] = @[],
default: T,
timeout: Duration = ZeroDuration,
): Future[T] {.async.} =
## Execute a query and return the first column of the first row as `T`,
## inferring `T` from `default`.
## Returns `default` if no rows or the value is NULL.
## Supported types: int32, int64, float64, bool, string.
let qr = await conn.query(sql, params, timeout = timeout)
if qr.rowCount == 0:
return default
let row = initRow(qr.data, 0)
if row.isNull(0):
return default
return row.get(0, T)

proc queryExists*(
conn: PgConnection,
sql: string,
Expand Down
18 changes: 18 additions & 0 deletions async_postgres/pg_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,24 @@ proc queryValueOrDefault*[T](
return default
return row.get(0, T)

proc queryValueOrDefault*[T](
pool: PgPool,
sql: string,
params: seq[PgParam] = @[],
default: T,
timeout: Duration = ZeroDuration,
): Future[T] {.async.} =
## Execute a query and return the first column of the first row as `T`,
## inferring `T` from `default`.
## Returns `default` if no rows or the value is NULL.
let qr = await pool.query(sql, params, timeout = timeout)
if qr.rowCount == 0:
return default
let row = initRow(qr.data, 0)
if row.isNull(0):
return default
return row.get(0, T)

proc queryExists*(
pool: PgPool,
sql: string,
Expand Down
16 changes: 16 additions & 0 deletions async_postgres/pg_pool_cluster.nim
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ clusterForwards("read"):
timeout: Duration = ZeroDuration,
): Future[T]

proc readQueryValueOrDefault*[T](
cluster: PgPoolCluster,
sql: string,
params: seq[PgParam] = @[],
default: T,
timeout: Duration = ZeroDuration,
): Future[T]

proc readQueryExists*(
cluster: PgPoolCluster,
sql: string,
Expand Down Expand Up @@ -402,6 +410,14 @@ clusterForwards("write"):
timeout: Duration = ZeroDuration,
): Future[T]

proc writeQueryValueOrDefault*[T](
cluster: PgPoolCluster,
sql: string,
params: seq[PgParam] = @[],
default: T,
timeout: Duration = ZeroDuration,
): Future[T]

proc writeQueryExists*(
cluster: PgPoolCluster,
sql: string,
Expand Down
16 changes: 16 additions & 0 deletions async_postgres/pg_sql.nim
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ sqlQueryForwards:
timeout: Duration = ZeroDuration,
): untyped

proc queryValueOrDefault*[T](
conn: PgConnection, sq: SqlQuery, default: T, timeout: Duration = ZeroDuration
): untyped

proc queryExists*(
conn: PgConnection, sq: SqlQuery, timeout: Duration = ZeroDuration
): untyped
Expand Down Expand Up @@ -453,6 +457,10 @@ sqlQueryForwards:
timeout: Duration = ZeroDuration,
): untyped

proc queryValueOrDefault*[T](
pool: PgPool, sq: SqlQuery, default: T, timeout: Duration = ZeroDuration
): untyped

proc queryExists*(
pool: PgPool, sq: SqlQuery, timeout: Duration = ZeroDuration
): untyped
Expand Down Expand Up @@ -533,6 +541,10 @@ sqlQueryForwards:
timeout: Duration = ZeroDuration,
): untyped

proc readQueryValueOrDefault*[T](
cluster: PgPoolCluster, sq: SqlQuery, default: T, timeout: Duration = ZeroDuration
): untyped

proc readQueryExists*(
cluster: PgPoolCluster, sq: SqlQuery, timeout: Duration = ZeroDuration
): untyped
Expand Down Expand Up @@ -614,6 +626,10 @@ sqlQueryForwards:
timeout: Duration = ZeroDuration,
): untyped

proc writeQueryValueOrDefault*[T](
cluster: PgPoolCluster, sq: SqlQuery, default: T, timeout: Duration = ZeroDuration
): untyped

proc writeQueryExists*(
cluster: PgPoolCluster, sq: SqlQuery, timeout: Duration = ZeroDuration
): untyped
Expand Down
22 changes: 22 additions & 0 deletions tests/test_e2e.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5075,6 +5075,17 @@ suite "E2E: Convenience Query Methods":

waitFor t()

test "queryValueOrDefault infers type from default":
proc t() {.async.} =
let conn = await connect(plainConfig())
let val = await conn.queryValueOrDefault("SELECT 1 WHERE false", default = -1'i64)
doAssert val == -1'i64
let val2 = await conn.queryValueOrDefault("SELECT 42", default = 0'i32)
doAssert val2 == 42'i32
await conn.close()

waitFor t()

test "queryValueOpt returns some on value":
proc t() {.async.} =
let conn = await connect(plainConfig())
Expand Down Expand Up @@ -5337,6 +5348,17 @@ suite "E2E: Convenience Query Methods":

waitFor t()

test "pool queryValueOrDefault infers type from default":
proc t() {.async.} =
let pool = await newPool(initPoolConfig(plainConfig(), minSize = 1, maxSize = 2))
let val = await pool.queryValueOrDefault("SELECT 1 WHERE false", default = -1'i32)
doAssert val == -1'i32
let val2 = await pool.queryValueOrDefault("SELECT 7", default = 0'i32)
doAssert val2 == 7'i32
await pool.close()

waitFor t()

test "pool queryValueOpt":
proc t() {.async.} =
let pool = await newPool(initPoolConfig(plainConfig(), minSize = 1, maxSize = 2))
Expand Down