db_odbc.nim 18 KB

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