db_odbc.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Nim Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## A higher level `ODBC` database wrapper.
  10. ##
  11. ## This is the same interface that is implemented for other databases.
  12. ##
  13. ## This has NOT yet been (extensively) tested against ODBC drivers for
  14. ## Teradata, Oracle, Sybase, MSSqlvSvr, et. al. databases.
  15. ##
  16. ## Currently all queries are ANSI calls, not Unicode.
  17. ##
  18. ## See also: `db_postgres <db_postgres.html>`_, `db_sqlite <db_sqlite.html>`_,
  19. ## `db_mysql <db_mysql.html>`_.
  20. ##
  21. ## Parameter substitution
  22. ## ======================
  23. ##
  24. ## All `db_*` modules support the same form of parameter substitution.
  25. ## That is, using the `?` (question mark) to signify the place where a
  26. ## value should be placed. For example:
  27. ##
  28. ## .. code-block:: Nim
  29. ## sql"INSERT INTO myTable (colA, colB, colC) VALUES (?, ?, ?)"
  30. ##
  31. ##
  32. ## Examples
  33. ## ========
  34. ##
  35. ## Opening a connection to a database
  36. ## ----------------------------------
  37. ##
  38. ## .. code-block:: Nim
  39. ## import std/db_odbc
  40. ## var db = open("localhost", "user", "password", "dbname")
  41. ## db.close()
  42. ##
  43. ## Creating a table
  44. ## ----------------
  45. ##
  46. ## .. code-block:: Nim
  47. ## db.exec(sql"DROP TABLE IF EXISTS myTable")
  48. ## db.exec(sql("""CREATE TABLE myTable (
  49. ## id integer,
  50. ## name varchar(50) not null)"""))
  51. ##
  52. ## Inserting data
  53. ## --------------
  54. ##
  55. ## .. code-block:: Nim
  56. ## db.exec(sql"INSERT INTO myTable (id, name) VALUES (0, ?)",
  57. ## "Andreas")
  58. ##
  59. ## Large example
  60. ## -------------
  61. ##
  62. ## .. code-block:: Nim
  63. ##
  64. ## import std/[db_odbc, math]
  65. ##
  66. ## var theDb = open("localhost", "nim", "nim", "test")
  67. ##
  68. ## theDb.exec(sql"Drop table if exists myTestTbl")
  69. ## theDb.exec(sql("create table myTestTbl (" &
  70. ## " Id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, " &
  71. ## " Name VARCHAR(50) NOT NULL, " &
  72. ## " i INT(11), " &
  73. ## " f DECIMAL(18,10))"))
  74. ##
  75. ## theDb.exec(sql"START TRANSACTION")
  76. ## for i in 1..1000:
  77. ## theDb.exec(sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)",
  78. ## "Item#" & $i, i, sqrt(i.float))
  79. ## theDb.exec(sql"COMMIT")
  80. ##
  81. ## for x in theDb.fastRows(sql"select * from myTestTbl"):
  82. ## echo x
  83. ##
  84. ## let id = theDb.tryInsertId(sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)",
  85. ## "Item#1001", 1001, sqrt(1001.0))
  86. ## echo "Inserted item: ", theDb.getValue(sql"SELECT name FROM myTestTbl WHERE id=?", id)
  87. ##
  88. ## theDb.close()
  89. import strutils, odbcsql
  90. import db_common
  91. export db_common
  92. import std/private/since
  93. type
  94. OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
  95. DbConn* = OdbcConnTyp ## encapsulates a database connection
  96. Row* = seq[string] ## a row of a dataset. NULL database values will be
  97. ## converted to nil.
  98. InstantRow* = tuple[row: seq[string], len: int] ## a handle that can be
  99. ## used to get a row's
  100. ## column text on demand
  101. var
  102. buf: array[0..4096, char]
  103. proc properFreeResult(hType: int, sqlres: var SqlHandle) {.
  104. tags: [WriteDbEffect], raises: [].} =
  105. try:
  106. discard SQLFreeHandle(hType.TSqlSmallInt, sqlres)
  107. sqlres = nil
  108. except: discard
  109. proc getErrInfo(db: var DbConn): tuple[res: int, ss, ne, msg: string] {.
  110. tags: [ReadDbEffect], raises: [].} =
  111. ## Returns ODBC error information
  112. var
  113. sqlState: array[0..512, char]
  114. nativeErr: array[0..512, char]
  115. errMsg: array[0..512, char]
  116. retSz: TSqlSmallInt = 0
  117. res: TSqlSmallInt = 0
  118. try:
  119. sqlState[0] = '\0'
  120. nativeErr[0] = '\0'
  121. errMsg[0] = '\0'
  122. res = SQLErr(db.env, db.hDb, db.stmt,
  123. cast[PSQLCHAR](sqlState.addr),
  124. cast[PSQLCHAR](nativeErr.addr),
  125. cast[PSQLCHAR](errMsg.addr),
  126. 511.TSqlSmallInt, retSz.addr)
  127. except:
  128. discard
  129. return (res.int, $(addr sqlState), $(addr nativeErr), $(addr errMsg))
  130. proc dbError*(db: var DbConn) {.
  131. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError] .} =
  132. ## Raises an `[DbError]` exception with ODBC error information
  133. var
  134. e: ref DbError
  135. ss, ne, msg: string = ""
  136. isAnError = false
  137. res: int = 0
  138. prevSs = ""
  139. while true:
  140. prevSs = ss
  141. (res, ss, ne, msg) = db.getErrInfo()
  142. if prevSs == ss:
  143. break
  144. # sqlState of 00000 is not an error
  145. elif ss == "00000":
  146. break
  147. elif ss == "01000":
  148. echo "\nWarning: ", ss, " ", msg
  149. continue
  150. else:
  151. isAnError = true
  152. echo "\nError: ", ss, " ", msg
  153. if isAnError:
  154. new(e)
  155. e.msg = "ODBC Error"
  156. if db.stmt != nil:
  157. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  158. properFreeResult(SQL_HANDLE_DBC, db.hDb)
  159. properFreeResult(SQL_HANDLE_ENV, db.env)
  160. raise e
  161. proc sqlCheck(db: var DbConn, resVal: TSqlSmallInt) {.raises: [DbError]} =
  162. ## Wrapper that raises [EDb] if `resVal` is neither SQL_SUCCESS or SQL_NO_DATA
  163. if resVal notIn [SQL_SUCCESS, SQL_NO_DATA]: dbError(db)
  164. proc sqlGetDBMS(db: var DbConn): string {.
  165. tags: [ReadDbEffect, WriteDbEffect], raises: [] .} =
  166. ## Returns the ODBC SQL_DBMS_NAME string
  167. const
  168. SQL_DBMS_NAME = 17.SqlUSmallInt
  169. var
  170. sz: TSqlSmallInt = 0
  171. buf[0] = '\0'
  172. try:
  173. db.sqlCheck(SQLGetInfo(db.hDb, SQL_DBMS_NAME, cast[SqlPointer](buf.addr),
  174. 4095.TSqlSmallInt, sz.addr))
  175. except: discard
  176. return $(addr buf)
  177. proc dbQuote*(s: string): string {.noSideEffect.} =
  178. ## DB quotes the string.
  179. result = "'"
  180. for c in items(s):
  181. if c == '\'': add(result, "''")
  182. else: add(result, c)
  183. add(result, '\'')
  184. proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string {.
  185. noSideEffect.} =
  186. ## Replace any `?` placeholders with `args`,
  187. ## and quotes the arguments
  188. result = ""
  189. var a = 0
  190. for c in items(string(formatstr)):
  191. if c == '?':
  192. add(result, dbQuote(args[a]))
  193. inc(a)
  194. else:
  195. add(result, c)
  196. proc prepareFetch(db: var DbConn, query: SqlQuery,
  197. args: varargs[string, `$`]): TSqlSmallInt {.
  198. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  199. # Prepare a statement, execute it and fetch the data to the driver
  200. # ready for retrieval of the data
  201. # Used internally by iterators and retrieval procs
  202. # requires calling
  203. # properFreeResult(SQL_HANDLE_STMT, db.stmt)
  204. # when finished
  205. db.sqlCheck(SQLAllocHandle(SQL_HANDLE_STMT, db.hDb, db.stmt))
  206. var q = dbFormat(query, args)
  207. db.sqlCheck(SQLPrepare(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
  208. db.sqlCheck(SQLExecute(db.stmt))
  209. result = SQLFetch(db.stmt)
  210. db.sqlCheck(result)
  211. proc prepareFetchDirect(db: var DbConn, query: SqlQuery,
  212. args: varargs[string, `$`]) {.
  213. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  214. # Prepare a statement, execute it and fetch the data to the driver
  215. # ready for retrieval of the data
  216. # Used internally by iterators and retrieval procs
  217. # requires calling
  218. # properFreeResult(SQL_HANDLE_STMT, db.stmt)
  219. # when finished
  220. db.sqlCheck(SQLAllocHandle(SQL_HANDLE_STMT, db.hDb, db.stmt))
  221. var q = dbFormat(query, args)
  222. db.sqlCheck(SQLExecDirect(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
  223. db.sqlCheck(SQLFetch(db.stmt))
  224. proc tryExec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
  225. tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
  226. ## Tries to execute the query and returns true if successful, false otherwise.
  227. var
  228. res:TSqlSmallInt = -1
  229. try:
  230. db.prepareFetchDirect(query, args)
  231. var
  232. rCnt:TSqlLen = -1
  233. res = SQLRowCount(db.stmt, rCnt)
  234. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  235. if res != SQL_SUCCESS: dbError(db)
  236. except: discard
  237. return res == SQL_SUCCESS
  238. proc rawExec(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
  239. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  240. db.prepareFetchDirect(query, args)
  241. proc exec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
  242. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  243. ## Executes the query and raises EDB if not successful.
  244. db.prepareFetchDirect(query, args)
  245. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  246. proc newRow(L: int): Row {.noSideEFfect.} =
  247. newSeq(result, L)
  248. for i in 0..L-1: result[i] = ""
  249. iterator fastRows*(db: var DbConn, query: SqlQuery,
  250. args: varargs[string, `$`]): Row {.
  251. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  252. ## Executes the query and iterates over the result dataset.
  253. ##
  254. ## This is very fast, but potentially dangerous. Use this iterator only
  255. ## if you require **ALL** the rows.
  256. ##
  257. ## Breaking the fastRows() iterator during a loop may cause a driver error
  258. ## for subsequent queries
  259. ##
  260. ## Rows are retrieved from the server at each iteration.
  261. var
  262. rowRes: Row
  263. sz: TSqlLen = 0
  264. cCnt: TSqlSmallInt = 0
  265. res: TSqlSmallInt = 0
  266. res = db.prepareFetch(query, args)
  267. if res == SQL_NO_DATA:
  268. discard
  269. elif res == SQL_SUCCESS:
  270. res = SQLNumResultCols(db.stmt, cCnt)
  271. rowRes = newRow(cCnt)
  272. rowRes.setLen(max(cCnt,0))
  273. while res == SQL_SUCCESS:
  274. for colId in 1..cCnt:
  275. buf[0] = '\0'
  276. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  277. cast[cstring](buf.addr), 4095, sz.addr))
  278. rowRes[colId-1] = $(addr buf)
  279. yield rowRes
  280. res = SQLFetch(db.stmt)
  281. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  282. db.sqlCheck(res)
  283. iterator instantRows*(db: var DbConn, query: SqlQuery,
  284. args: varargs[string, `$`]): InstantRow
  285. {.tags: [ReadDbEffect, WriteDbEffect].} =
  286. ## Same as fastRows but returns a handle that can be used to get column text
  287. ## on demand using []. Returned handle is valid only within the iterator body.
  288. var
  289. rowRes: Row = @[]
  290. sz: TSqlLen = 0
  291. cCnt: TSqlSmallInt = 0
  292. res: TSqlSmallInt = 0
  293. res = db.prepareFetch(query, args)
  294. if res == SQL_NO_DATA:
  295. discard
  296. elif res == SQL_SUCCESS:
  297. res = SQLNumResultCols(db.stmt, cCnt)
  298. rowRes = newRow(cCnt)
  299. rowRes.setLen(max(cCnt,0))
  300. while res == SQL_SUCCESS:
  301. for colId in 1..cCnt:
  302. buf[0] = '\0'
  303. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  304. cast[cstring](buf.addr), 4095, sz.addr))
  305. rowRes[colId-1] = $(addr buf)
  306. yield (row: rowRes, len: cCnt.int)
  307. res = SQLFetch(db.stmt)
  308. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  309. db.sqlCheck(res)
  310. proc `[]`*(row: InstantRow, col: int): string {.inline.} =
  311. ## Returns text for given column of the row
  312. $row.row[col]
  313. proc unsafeColumnAt*(row: InstantRow, index: int): cstring {.inline.} =
  314. ## Return cstring of given column of the row
  315. row.row[index]
  316. proc len*(row: InstantRow): int {.inline.} =
  317. ## Returns number of columns in the row
  318. row.len
  319. proc getRow*(db: var DbConn, query: SqlQuery,
  320. args: varargs[string, `$`]): Row {.
  321. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  322. ## Retrieves a single row. If the query doesn't return any rows, this proc
  323. ## will return a Row with empty strings for each column.
  324. var
  325. rowRes: Row
  326. sz: TSqlLen = 0
  327. cCnt: TSqlSmallInt = 0
  328. res: TSqlSmallInt = 0
  329. res = db.prepareFetch(query, args)
  330. if res == SQL_NO_DATA:
  331. result = @[]
  332. elif res == SQL_SUCCESS:
  333. res = SQLNumResultCols(db.stmt, cCnt)
  334. rowRes = newRow(cCnt)
  335. rowRes.setLen(max(cCnt,0))
  336. for colId in 1..cCnt:
  337. buf[0] = '\0'
  338. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  339. cast[cstring](buf.addr), 4095, sz.addr))
  340. rowRes[colId-1] = $(addr buf)
  341. res = SQLFetch(db.stmt)
  342. result = rowRes
  343. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  344. db.sqlCheck(res)
  345. proc getAllRows*(db: var DbConn, query: SqlQuery,
  346. args: varargs[string, `$`]): seq[Row] {.
  347. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError] .} =
  348. ## Executes the query and returns the whole result dataset.
  349. var
  350. rows: seq[Row] = @[]
  351. rowRes: Row
  352. sz: TSqlLen = 0
  353. cCnt: TSqlSmallInt = 0
  354. res: TSqlSmallInt = 0
  355. res = db.prepareFetch(query, args)
  356. if res == SQL_NO_DATA:
  357. result = @[]
  358. elif res == SQL_SUCCESS:
  359. res = SQLNumResultCols(db.stmt, cCnt)
  360. rowRes = newRow(cCnt)
  361. rowRes.setLen(max(cCnt,0))
  362. while res == SQL_SUCCESS:
  363. for colId in 1..cCnt:
  364. buf[0] = '\0'
  365. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  366. cast[cstring](buf.addr), 4095, sz.addr))
  367. rowRes[colId-1] = $(addr buf)
  368. rows.add(rowRes)
  369. res = SQLFetch(db.stmt)
  370. result = rows
  371. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  372. db.sqlCheck(res)
  373. iterator rows*(db: var DbConn, query: SqlQuery,
  374. args: varargs[string, `$`]): Row {.
  375. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  376. ## Same as `fastRows`, but slower and safe.
  377. ##
  378. ## This retrieves ALL rows into memory before
  379. ## iterating through the rows.
  380. ## Large dataset queries will impact on memory usage.
  381. for r in items(getAllRows(db, query, args)): yield r
  382. proc getValue*(db: var DbConn, query: SqlQuery,
  383. args: varargs[string, `$`]): string {.
  384. tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
  385. ## Executes the query and returns the first column of the first row of the
  386. ## result dataset. Returns "" if the dataset contains no rows or the database
  387. ## value is NULL.
  388. result = ""
  389. try:
  390. result = getRow(db, query, args)[0]
  391. except: discard
  392. proc tryInsertId*(db: var DbConn, query: SqlQuery,
  393. args: varargs[string, `$`]): int64 {.
  394. tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
  395. ## Executes the query (typically "INSERT") and returns the
  396. ## generated ID for the row or -1 in case of an error.
  397. if not tryExec(db, query, args):
  398. result = -1'i64
  399. else:
  400. result = -1'i64
  401. try:
  402. case sqlGetDBMS(db).toLower():
  403. of "postgresql":
  404. result = getValue(db, sql"SELECT LASTVAL();", []).parseInt
  405. of "mysql":
  406. result = getValue(db, sql"SELECT LAST_INSERT_ID();", []).parseInt
  407. of "sqlite":
  408. result = getValue(db, sql"SELECT LAST_INSERT_ROWID();", []).parseInt
  409. of "microsoft sql server":
  410. result = getValue(db, sql"SELECT SCOPE_IDENTITY();", []).parseInt
  411. of "oracle":
  412. result = getValue(db, sql"SELECT id.currval FROM DUAL;", []).parseInt
  413. else: result = -1'i64
  414. except: discard
  415. proc insertId*(db: var DbConn, query: SqlQuery,
  416. args: varargs[string, `$`]): int64 {.
  417. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  418. ## Executes the query (typically "INSERT") and returns the
  419. ## generated ID for the row.
  420. result = tryInsertID(db, query, args)
  421. if result < 0: dbError(db)
  422. proc tryInsert*(db: var DbConn, query: SqlQuery,pkName: string,
  423. args: varargs[string, `$`]): int64
  424. {.tags: [ReadDbEffect, WriteDbEffect], raises: [], since: (1, 3).} =
  425. ## same as tryInsertID
  426. tryInsertID(db, query, args)
  427. proc insert*(db: var DbConn, query: SqlQuery, pkName: string,
  428. args: varargs[string, `$`]): int64
  429. {.tags: [ReadDbEffect, WriteDbEffect], since: (1, 3).} =
  430. ## same as insertId
  431. result = tryInsert(db, query,pkName, args)
  432. if result < 0: dbError(db)
  433. proc execAffectedRows*(db: var DbConn, query: SqlQuery,
  434. args: varargs[string, `$`]): int64 {.
  435. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  436. ## Runs the query (typically "UPDATE") and returns the
  437. ## number of affected rows
  438. result = -1
  439. db.sqlCheck(SQLAllocHandle(SQL_HANDLE_STMT, db.hDb, db.stmt.SqlHandle))
  440. var q = dbFormat(query, args)
  441. db.sqlCheck(SQLPrepare(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
  442. rawExec(db, query, args)
  443. var rCnt:TSqlLen = -1
  444. db.sqlCheck(SQLRowCount(db.hDb, rCnt))
  445. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  446. result = rCnt.int64
  447. proc close*(db: var DbConn) {.
  448. tags: [WriteDbEffect], raises: [].} =
  449. ## Closes the database connection.
  450. if db.hDb != nil:
  451. try:
  452. var res = SQLDisconnect(db.hDb)
  453. if db.stmt != nil:
  454. res = SQLFreeHandle(SQL_HANDLE_STMT, db.stmt)
  455. res = SQLFreeHandle(SQL_HANDLE_DBC, db.hDb)
  456. res = SQLFreeHandle(SQL_HANDLE_ENV, db.env)
  457. db = (hDb: nil, env: nil, stmt: nil)
  458. except:
  459. discard
  460. proc open*(connection, user, password, database: string): DbConn {.
  461. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  462. ## Opens a database connection.
  463. ##
  464. ## Raises `EDb` if the connection could not be established.
  465. ##
  466. ## Currently the database parameter is ignored,
  467. ## but included to match `open()` in the other db_xxxxx library modules.
  468. var
  469. val = SQL_OV_ODBC3
  470. resLen = 0
  471. result = (hDb: nil, env: nil, stmt: nil)
  472. # allocate environment handle
  473. var res = SQLAllocHandle(SQL_HANDLE_ENV, result.env, result.env)
  474. if res != SQL_SUCCESS: dbError("Error: unable to initialise ODBC environment.")
  475. res = SQLSetEnvAttr(result.env,
  476. SQL_ATTR_ODBC_VERSION.TSqlInteger,
  477. cast[SqlPointer](val), resLen.TSqlInteger)
  478. if res != SQL_SUCCESS: dbError("Error: unable to set ODBC driver version.")
  479. # allocate hDb handle
  480. res = SQLAllocHandle(SQL_HANDLE_DBC, result.env, result.hDb)
  481. if res != SQL_SUCCESS: dbError("Error: unable to allocate connection handle.")
  482. # Connect: connection = dsn str,
  483. res = SQLConnect(result.hDb,
  484. connection.PSQLCHAR , connection.len.TSqlSmallInt,
  485. user.PSQLCHAR, user.len.TSqlSmallInt,
  486. password.PSQLCHAR, password.len.TSqlSmallInt)
  487. if res != SQL_SUCCESS:
  488. result.dbError()
  489. proc setEncoding*(connection: DbConn, encoding: string): bool {.
  490. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  491. ## Currently not implemented for ODBC.
  492. ##
  493. ## Sets the encoding of a database connection, returns true for
  494. ## success, false for failure.
  495. ##result = set_character_set(connection, encoding) == 0
  496. dbError("setEncoding() is currently not implemented by the db_odbc module")