db_odbc.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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, dbutils]
  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. dbFormatImpl(formatstr, dbQuote, args)
  189. proc prepareFetch(db: var DbConn, query: SqlQuery,
  190. args: varargs[string, `$`]): TSqlSmallInt {.
  191. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  192. # Prepare a statement, execute it and fetch the data to the driver
  193. # ready for retrieval of the data
  194. # Used internally by iterators and retrieval procs
  195. # requires calling
  196. # properFreeResult(SQL_HANDLE_STMT, db.stmt)
  197. # when finished
  198. db.sqlCheck(SQLAllocHandle(SQL_HANDLE_STMT, db.hDb, db.stmt))
  199. var q = dbFormat(query, args)
  200. db.sqlCheck(SQLPrepare(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
  201. db.sqlCheck(SQLExecute(db.stmt))
  202. result = SQLFetch(db.stmt)
  203. db.sqlCheck(result)
  204. proc prepareFetchDirect(db: var DbConn, query: SqlQuery,
  205. args: varargs[string, `$`]) {.
  206. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  207. # Prepare a statement, execute it and fetch the data to the driver
  208. # ready for retrieval of the data
  209. # Used internally by iterators and retrieval procs
  210. # requires calling
  211. # properFreeResult(SQL_HANDLE_STMT, db.stmt)
  212. # when finished
  213. db.sqlCheck(SQLAllocHandle(SQL_HANDLE_STMT, db.hDb, db.stmt))
  214. var q = dbFormat(query, args)
  215. db.sqlCheck(SQLExecDirect(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
  216. db.sqlCheck(SQLFetch(db.stmt))
  217. proc tryExec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
  218. tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
  219. ## Tries to execute the query and returns true if successful, false otherwise.
  220. var
  221. res:TSqlSmallInt = -1
  222. try:
  223. db.prepareFetchDirect(query, args)
  224. var
  225. rCnt:TSqlLen = -1
  226. res = SQLRowCount(db.stmt, rCnt)
  227. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  228. if res != SQL_SUCCESS: dbError(db)
  229. except: discard
  230. return res == SQL_SUCCESS
  231. proc rawExec(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
  232. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  233. db.prepareFetchDirect(query, args)
  234. proc exec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
  235. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  236. ## Executes the query and raises EDB if not successful.
  237. db.prepareFetchDirect(query, args)
  238. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  239. proc newRow(L: int): Row {.noSideEFfect.} =
  240. newSeq(result, L)
  241. for i in 0..L-1: result[i] = ""
  242. iterator fastRows*(db: var DbConn, query: SqlQuery,
  243. args: varargs[string, `$`]): Row {.
  244. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  245. ## Executes the query and iterates over the result dataset.
  246. ##
  247. ## This is very fast, but potentially dangerous. Use this iterator only
  248. ## if you require **ALL** the rows.
  249. ##
  250. ## Breaking the fastRows() iterator during a loop may cause a driver error
  251. ## for subsequent queries
  252. ##
  253. ## Rows are retrieved from the server at each iteration.
  254. var
  255. rowRes: Row
  256. sz: TSqlLen = 0
  257. cCnt: TSqlSmallInt = 0
  258. res: TSqlSmallInt = 0
  259. res = db.prepareFetch(query, args)
  260. if res == SQL_NO_DATA:
  261. discard
  262. elif res == SQL_SUCCESS:
  263. res = SQLNumResultCols(db.stmt, cCnt)
  264. rowRes = newRow(cCnt)
  265. rowRes.setLen(max(cCnt,0))
  266. while res == SQL_SUCCESS:
  267. for colId in 1..cCnt:
  268. buf[0] = '\0'
  269. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  270. cast[cstring](buf.addr), 4095, sz.addr))
  271. rowRes[colId-1] = $(addr buf)
  272. yield rowRes
  273. res = SQLFetch(db.stmt)
  274. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  275. db.sqlCheck(res)
  276. iterator instantRows*(db: var DbConn, query: SqlQuery,
  277. args: varargs[string, `$`]): InstantRow
  278. {.tags: [ReadDbEffect, WriteDbEffect].} =
  279. ## Same as fastRows but returns a handle that can be used to get column text
  280. ## on demand using `[]`. Returned handle is valid only within the iterator body.
  281. var
  282. rowRes: Row = @[]
  283. sz: TSqlLen = 0
  284. cCnt: TSqlSmallInt = 0
  285. res: TSqlSmallInt = 0
  286. res = db.prepareFetch(query, args)
  287. if res == SQL_NO_DATA:
  288. discard
  289. elif res == SQL_SUCCESS:
  290. res = SQLNumResultCols(db.stmt, cCnt)
  291. rowRes = newRow(cCnt)
  292. rowRes.setLen(max(cCnt,0))
  293. while res == SQL_SUCCESS:
  294. for colId in 1..cCnt:
  295. buf[0] = '\0'
  296. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  297. cast[cstring](buf.addr), 4095, sz.addr))
  298. rowRes[colId-1] = $(addr buf)
  299. yield (row: rowRes, len: cCnt.int)
  300. res = SQLFetch(db.stmt)
  301. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  302. db.sqlCheck(res)
  303. proc `[]`*(row: InstantRow, col: int): string {.inline.} =
  304. ## Returns text for given column of the row
  305. $row.row[col]
  306. proc unsafeColumnAt*(row: InstantRow, index: int): cstring {.inline.} =
  307. ## Return cstring of given column of the row
  308. row.row[index].cstring
  309. proc len*(row: InstantRow): int {.inline.} =
  310. ## Returns number of columns in the row
  311. row.len
  312. proc getRow*(db: var DbConn, query: SqlQuery,
  313. args: varargs[string, `$`]): Row {.
  314. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  315. ## Retrieves a single row. If the query doesn't return any rows, this proc
  316. ## will return a Row with empty strings for each column.
  317. var
  318. rowRes: Row
  319. sz: TSqlLen = 0
  320. cCnt: TSqlSmallInt = 0
  321. res: TSqlSmallInt = 0
  322. res = db.prepareFetch(query, args)
  323. if res == SQL_NO_DATA:
  324. result = @[]
  325. elif res == SQL_SUCCESS:
  326. res = SQLNumResultCols(db.stmt, cCnt)
  327. rowRes = newRow(cCnt)
  328. rowRes.setLen(max(cCnt,0))
  329. for colId in 1..cCnt:
  330. buf[0] = '\0'
  331. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  332. cast[cstring](buf.addr), 4095, sz.addr))
  333. rowRes[colId-1] = $(addr buf)
  334. res = SQLFetch(db.stmt)
  335. result = rowRes
  336. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  337. db.sqlCheck(res)
  338. proc getAllRows*(db: var DbConn, query: SqlQuery,
  339. args: varargs[string, `$`]): seq[Row] {.
  340. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError] .} =
  341. ## Executes the query and returns the whole result dataset.
  342. var
  343. rows: seq[Row] = @[]
  344. rowRes: Row
  345. sz: TSqlLen = 0
  346. cCnt: TSqlSmallInt = 0
  347. res: TSqlSmallInt = 0
  348. res = db.prepareFetch(query, args)
  349. if res == SQL_NO_DATA:
  350. result = @[]
  351. elif res == SQL_SUCCESS:
  352. res = SQLNumResultCols(db.stmt, cCnt)
  353. rowRes = newRow(cCnt)
  354. rowRes.setLen(max(cCnt,0))
  355. while res == SQL_SUCCESS:
  356. for colId in 1..cCnt:
  357. buf[0] = '\0'
  358. db.sqlCheck(SQLGetData(db.stmt, colId.SqlUSmallInt, SQL_C_CHAR,
  359. cast[cstring](buf.addr), 4095, sz.addr))
  360. rowRes[colId-1] = $(addr buf)
  361. rows.add(rowRes)
  362. res = SQLFetch(db.stmt)
  363. result = rows
  364. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  365. db.sqlCheck(res)
  366. iterator rows*(db: var DbConn, query: SqlQuery,
  367. args: varargs[string, `$`]): Row {.
  368. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  369. ## Same as `fastRows`, but slower and safe.
  370. ##
  371. ## This retrieves ALL rows into memory before
  372. ## iterating through the rows.
  373. ## Large dataset queries will impact on memory usage.
  374. for r in items(getAllRows(db, query, args)): yield r
  375. proc getValue*(db: var DbConn, query: SqlQuery,
  376. args: varargs[string, `$`]): string {.
  377. tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
  378. ## Executes the query and returns the first column of the first row of the
  379. ## result dataset. Returns "" if the dataset contains no rows or the database
  380. ## value is NULL.
  381. result = ""
  382. try:
  383. result = getRow(db, query, args)[0]
  384. except: discard
  385. proc tryInsertId*(db: var DbConn, query: SqlQuery,
  386. args: varargs[string, `$`]): int64 {.
  387. tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
  388. ## Executes the query (typically "INSERT") and returns the
  389. ## generated ID for the row or -1 in case of an error.
  390. if not tryExec(db, query, args):
  391. result = -1'i64
  392. else:
  393. result = -1'i64
  394. try:
  395. case sqlGetDBMS(db).toLower():
  396. of "postgresql":
  397. result = getValue(db, sql"SELECT LASTVAL();", []).parseInt
  398. of "mysql":
  399. result = getValue(db, sql"SELECT LAST_INSERT_ID();", []).parseInt
  400. of "sqlite":
  401. result = getValue(db, sql"SELECT LAST_INSERT_ROWID();", []).parseInt
  402. of "microsoft sql server":
  403. result = getValue(db, sql"SELECT SCOPE_IDENTITY();", []).parseInt
  404. of "oracle":
  405. result = getValue(db, sql"SELECT id.currval FROM DUAL;", []).parseInt
  406. else: result = -1'i64
  407. except: discard
  408. proc insertId*(db: var DbConn, query: SqlQuery,
  409. args: varargs[string, `$`]): int64 {.
  410. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  411. ## Executes the query (typically "INSERT") and returns the
  412. ## generated ID for the row.
  413. result = tryInsertID(db, query, args)
  414. if result < 0: dbError(db)
  415. proc tryInsert*(db: var DbConn, query: SqlQuery,pkName: string,
  416. args: varargs[string, `$`]): int64
  417. {.tags: [ReadDbEffect, WriteDbEffect], raises: [], since: (1, 3).} =
  418. ## same as tryInsertID
  419. tryInsertID(db, query, args)
  420. proc insert*(db: var DbConn, query: SqlQuery, pkName: string,
  421. args: varargs[string, `$`]): int64
  422. {.tags: [ReadDbEffect, WriteDbEffect], since: (1, 3).} =
  423. ## same as insertId
  424. result = tryInsert(db, query,pkName, args)
  425. if result < 0: dbError(db)
  426. proc execAffectedRows*(db: var DbConn, query: SqlQuery,
  427. args: varargs[string, `$`]): int64 {.
  428. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  429. ## Runs the query (typically "UPDATE") and returns the
  430. ## number of affected rows
  431. result = -1
  432. db.sqlCheck(SQLAllocHandle(SQL_HANDLE_STMT, db.hDb, db.stmt.SqlHandle))
  433. var q = dbFormat(query, args)
  434. db.sqlCheck(SQLPrepare(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
  435. rawExec(db, query, args)
  436. var rCnt:TSqlLen = -1
  437. db.sqlCheck(SQLRowCount(db.hDb, rCnt))
  438. properFreeResult(SQL_HANDLE_STMT, db.stmt)
  439. result = rCnt.int64
  440. proc close*(db: var DbConn) {.
  441. tags: [WriteDbEffect], raises: [].} =
  442. ## Closes the database connection.
  443. if db.hDb != nil:
  444. try:
  445. var res = SQLDisconnect(db.hDb)
  446. if db.stmt != nil:
  447. res = SQLFreeHandle(SQL_HANDLE_STMT, db.stmt)
  448. res = SQLFreeHandle(SQL_HANDLE_DBC, db.hDb)
  449. res = SQLFreeHandle(SQL_HANDLE_ENV, db.env)
  450. db = (hDb: nil, env: nil, stmt: nil)
  451. except:
  452. discard
  453. proc open*(connection, user, password, database: string): DbConn {.
  454. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  455. ## Opens a database connection.
  456. ##
  457. ## Raises `EDb` if the connection could not be established.
  458. ##
  459. ## Currently the database parameter is ignored,
  460. ## but included to match `open()` in the other db_xxxxx library modules.
  461. var
  462. val = SQL_OV_ODBC3
  463. resLen = 0
  464. result = (hDb: nil, env: nil, stmt: nil)
  465. # allocate environment handle
  466. var res = SQLAllocHandle(SQL_HANDLE_ENV, result.env, result.env)
  467. if res != SQL_SUCCESS: dbError("Error: unable to initialise ODBC environment.")
  468. res = SQLSetEnvAttr(result.env,
  469. SQL_ATTR_ODBC_VERSION.TSqlInteger,
  470. cast[SqlPointer](val), resLen.TSqlInteger)
  471. if res != SQL_SUCCESS: dbError("Error: unable to set ODBC driver version.")
  472. # allocate hDb handle
  473. res = SQLAllocHandle(SQL_HANDLE_DBC, result.env, result.hDb)
  474. if res != SQL_SUCCESS: dbError("Error: unable to allocate connection handle.")
  475. # Connect: connection = dsn str,
  476. res = SQLConnect(result.hDb,
  477. connection.PSQLCHAR , connection.len.TSqlSmallInt,
  478. user.PSQLCHAR, user.len.TSqlSmallInt,
  479. password.PSQLCHAR, password.len.TSqlSmallInt)
  480. if res != SQL_SUCCESS:
  481. result.dbError()
  482. proc setEncoding*(connection: DbConn, encoding: string): bool {.
  483. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
  484. ## Currently not implemented for ODBC.
  485. ##
  486. ## Sets the encoding of a database connection, returns true for
  487. ## success, false for failure.
  488. ##result = set_character_set(connection, encoding) == 0
  489. dbError("setEncoding() is currently not implemented by the db_odbc module")