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:
Bung
2020-05-23 01:24:52 +08:00
committed by ringabout
parent 988a4ef900
commit 6f7c749ea5

View File

@@ -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].} =