mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 06:20:38 +00:00
add insert,tryInsert unify for postgres that need pk name (#14416)
* add insert,tryInsert unify for postgres that need pk name * add ReadDbEffect to new procs * add .since and changelog * change since to 1.3 * Update src/db_postgres.nim Co-authored-by: bung87 <crc32@qq.com> Co-authored-by: Andreas Rumpf <rumpf_a@web.de> Co-authored-by: alaviss <leorize+oss@disroot.org>
This commit is contained in:
@@ -68,6 +68,8 @@ import strutils, postgres
|
||||
import db_common
|
||||
export db_common
|
||||
|
||||
import std/private/since
|
||||
|
||||
type
|
||||
DbConn* = PPGconn ## encapsulates a database connection
|
||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
||||
@@ -485,6 +487,26 @@ proc insertID*(db: DbConn, query: SqlQuery,
|
||||
result = tryInsertID(db, query, args)
|
||||
if result < 0: dbError(db)
|
||||
|
||||
proc tryInsert*(db: DbConn, query: SqlQuery,pkName: string,
|
||||
args: varargs[string, `$`]): int64
|
||||
{.tags: [WriteDbEffect], since: (1, 3).}=
|
||||
## executes the query (typically "INSERT") and returns the
|
||||
## generated ID for the row or -1 in case of an error.
|
||||
var x = pqgetvalue(setupQuery(db, SqlQuery(string(query) & " RETURNING " & pkName),
|
||||
args), 0, 0)
|
||||
if not isNil(x):
|
||||
result = parseBiggestInt($x)
|
||||
else:
|
||||
result = -1
|
||||
|
||||
proc insert*(db: DbConn, query: SqlQuery, pkName: string,
|
||||
args: varargs[string, `$`]): int64
|
||||
{.tags: [WriteDbEffect], since: (1, 3).} =
|
||||
## executes the query (typically "INSERT") and returns the
|
||||
## generated ID
|
||||
result = tryInsertID(db, query, args)
|
||||
if result < 0: dbError(db)
|
||||
|
||||
proc execAffectedRows*(db: DbConn, query: SqlQuery,
|
||||
args: varargs[string, `$`]): int64 {.tags: [
|
||||
ReadDbEffect, WriteDbEffect].} =
|
||||
|
||||
Reference in New Issue
Block a user