mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-06 13:07:48 +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 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:
@@ -3,6 +3,7 @@
|
||||
|
||||
|
||||
## 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.
|
||||
- `uri` adds Data URI Base64, implements RFC-2397.
|
||||
- Add [DOM Parser](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser)
|
||||
|
||||
@@ -88,6 +88,8 @@ import strutils, mysql
|
||||
import db_common
|
||||
export db_common
|
||||
|
||||
import std/private/since
|
||||
|
||||
type
|
||||
DbConn* = distinct PMySQL ## encapsulates a database connection
|
||||
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)
|
||||
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,
|
||||
args: varargs[string, `$`]): int64 {.
|
||||
tags: [ReadDbEffect, WriteDbEffect].} =
|
||||
|
||||
@@ -92,6 +92,8 @@ import strutils, odbcsql
|
||||
import db_common
|
||||
export db_common
|
||||
|
||||
import std/private/since
|
||||
|
||||
type
|
||||
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
||||
DbConn* = OdbcConnTyp ## encapsulates a database connection
|
||||
@@ -451,6 +453,19 @@ proc insertId*(db: var DbConn, query: SqlQuery,
|
||||
result = tryInsertID(db, query, args)
|
||||
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,
|
||||
args: varargs[string, `$`]): int64 {.
|
||||
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||
|
||||
@@ -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].} =
|
||||
|
||||
@@ -118,6 +118,8 @@ import sqlite3
|
||||
import db_common
|
||||
export db_common
|
||||
|
||||
import std/private/since
|
||||
|
||||
type
|
||||
DbConn* = PSqlite3 ## Encapsulates a database connection.
|
||||
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)
|
||||
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,
|
||||
args: varargs[string, `$`]): int64 {.
|
||||
tags: [ReadDbEffect, WriteDbEffect].} =
|
||||
|
||||
Reference in New Issue
Block a user