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 lib/impure/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:
parent
7b3dcfde20
commit
6668b43409
5 changed files with 68 additions and 0 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
|
|
||||||
## Standard library additions and changes
|
## Standard library additions and changes
|
||||||
|
- Add `tryInsert`,`insert` procs to db_* libs accept primary key column name.
|
||||||
- Added `xmltree.newVerbatimText` support create `style`'s,`script`'s text.
|
- Added `xmltree.newVerbatimText` support create `style`'s,`script`'s text.
|
||||||
- `uri` adds Data URI Base64, implements RFC-2397.
|
- `uri` adds Data URI Base64, implements RFC-2397.
|
||||||
- Add [DOM Parser](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser)
|
- Add [DOM Parser](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser)
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,8 @@ import strutils, mysql
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
|
import std/private/since
|
||||||
|
|
||||||
type
|
type
|
||||||
DbConn* = distinct PMySQL ## encapsulates a database connection
|
DbConn* = distinct PMySQL ## encapsulates a database connection
|
||||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
||||||
|
|
@ -359,6 +361,19 @@ proc insertId*(db: DbConn, query: SqlQuery,
|
||||||
result = tryInsertID(db, query, args)
|
result = tryInsertID(db, query, args)
|
||||||
if result < 0: dbError(db)
|
if result < 0: dbError(db)
|
||||||
|
|
||||||
|
proc tryInsert*(db: DbConn, query: SqlQuery, pkName: string,
|
||||||
|
args: varargs[string, `$`]): int64
|
||||||
|
{.tags: [WriteDbEffect], raises: [], since: (1, 3).} =
|
||||||
|
## same as tryInsertID
|
||||||
|
tryInsertID(db, query, args)
|
||||||
|
|
||||||
|
proc insert*(db: DbConn, query: SqlQuery, pkName: string,
|
||||||
|
args: varargs[string, `$`]): int64
|
||||||
|
{.tags: [WriteDbEffect], since: (1, 3).} =
|
||||||
|
## same as insertId
|
||||||
|
result = tryInsert(db, query,pkName, args)
|
||||||
|
if result < 0: dbError(db)
|
||||||
|
|
||||||
proc execAffectedRows*(db: DbConn, query: SqlQuery,
|
proc execAffectedRows*(db: DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.
|
args: varargs[string, `$`]): int64 {.
|
||||||
tags: [ReadDbEffect, WriteDbEffect].} =
|
tags: [ReadDbEffect, WriteDbEffect].} =
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,8 @@ import strutils, odbcsql
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
|
import std/private/since
|
||||||
|
|
||||||
type
|
type
|
||||||
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
||||||
DbConn* = OdbcConnTyp ## encapsulates a database connection
|
DbConn* = OdbcConnTyp ## encapsulates a database connection
|
||||||
|
|
@ -451,6 +453,19 @@ proc insertId*(db: var DbConn, query: SqlQuery,
|
||||||
result = tryInsertID(db, query, args)
|
result = tryInsertID(db, query, args)
|
||||||
if result < 0: dbError(db)
|
if result < 0: dbError(db)
|
||||||
|
|
||||||
|
proc tryInsert*(db: var DbConn, query: SqlQuery,pkName: string,
|
||||||
|
args: varargs[string, `$`]): int64
|
||||||
|
{.tags: [ReadDbEffect, WriteDbEffect], raises: [], since: (1, 3).} =
|
||||||
|
## same as tryInsertID
|
||||||
|
tryInsertID(db, query, args)
|
||||||
|
|
||||||
|
proc insert*(db: var DbConn, query: SqlQuery, pkName: string,
|
||||||
|
args: varargs[string, `$`]): int64
|
||||||
|
{.tags: [ReadDbEffect, WriteDbEffect], since: (1, 3).} =
|
||||||
|
## same as insertId
|
||||||
|
result = tryInsert(db, query,pkName, args)
|
||||||
|
if result < 0: dbError(db)
|
||||||
|
|
||||||
proc execAffectedRows*(db: var DbConn, query: SqlQuery,
|
proc execAffectedRows*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.
|
args: varargs[string, `$`]): int64 {.
|
||||||
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,8 @@ import strutils, postgres
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
|
import std/private/since
|
||||||
|
|
||||||
type
|
type
|
||||||
DbConn* = PPGconn ## encapsulates a database connection
|
DbConn* = PPGconn ## encapsulates a database connection
|
||||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
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)
|
result = tryInsertID(db, query, args)
|
||||||
if result < 0: dbError(db)
|
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,
|
proc execAffectedRows*(db: DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.tags: [
|
args: varargs[string, `$`]): int64 {.tags: [
|
||||||
ReadDbEffect, WriteDbEffect].} =
|
ReadDbEffect, WriteDbEffect].} =
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,8 @@ import sqlite3
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
|
import std/private/since
|
||||||
|
|
||||||
type
|
type
|
||||||
DbConn* = PSqlite3 ## Encapsulates a database connection.
|
DbConn* = PSqlite3 ## Encapsulates a database connection.
|
||||||
Row* = seq[string] ## A row of a dataset. `NULL` database values will be
|
Row* = seq[string] ## A row of a dataset. `NULL` database values will be
|
||||||
|
|
@ -636,6 +638,19 @@ proc insertID*(db: DbConn, query: SqlQuery,
|
||||||
result = tryInsertID(db, query, args)
|
result = tryInsertID(db, query, args)
|
||||||
if result < 0: dbError(db)
|
if result < 0: dbError(db)
|
||||||
|
|
||||||
|
proc tryInsert*(db: DbConn, query: SqlQuery, pkName: string,
|
||||||
|
args: varargs[string, `$`]): int64
|
||||||
|
{.tags: [WriteDbEffect], raises: [], since: (1, 3).} =
|
||||||
|
## same as tryInsertID
|
||||||
|
tryInsertID(db, query, args)
|
||||||
|
|
||||||
|
proc insert*(db: DbConn, query: SqlQuery, pkName: string,
|
||||||
|
args: varargs[string, `$`]): int64
|
||||||
|
{.tags: [WriteDbEffect], since: (1, 3).} =
|
||||||
|
## same as insertId
|
||||||
|
result = tryInsert(db, query,pkName, args)
|
||||||
|
if result < 0: dbError(db)
|
||||||
|
|
||||||
proc execAffectedRows*(db: DbConn, query: SqlQuery,
|
proc execAffectedRows*(db: DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.
|
args: varargs[string, `$`]): int64 {.
|
||||||
tags: [ReadDbEffect, WriteDbEffect].} =
|
tags: [ReadDbEffect, WriteDbEffect].} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue