postgres.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module contains the definitions for structures and externs for
  10. # functions used by frontend postgres applications. It is based on
  11. # Postgresql's libpq-fe.h.
  12. #
  13. # It is for postgreSQL version 7.4 and higher with support for the v3.0
  14. # connection-protocol.
  15. #
  16. {.deadCodeElim: on.} # dce option deprecated
  17. when defined(windows):
  18. const
  19. dllName = "libpq.dll"
  20. elif defined(macosx):
  21. const
  22. dllName = "libpq.dylib"
  23. else:
  24. const
  25. dllName = "libpq.so(.5|)"
  26. type
  27. POid* = ptr Oid
  28. Oid* = int32
  29. const
  30. ERROR_MSG_LENGTH* = 4096
  31. CMDSTATUS_LEN* = 40
  32. type
  33. SockAddr* = array[1..112, int8]
  34. PGresAttDesc*{.pure, final.} = object
  35. name*: cstring
  36. adtid*: Oid
  37. adtsize*: int
  38. PPGresAttDesc* = ptr PGresAttDesc
  39. PPPGresAttDesc* = ptr PPGresAttDesc
  40. PGresAttValue*{.pure, final.} = object
  41. length*: int32
  42. value*: cstring
  43. PPGresAttValue* = ptr PGresAttValue
  44. PPPGresAttValue* = ptr PPGresAttValue
  45. PExecStatusType* = ptr ExecStatusType
  46. ExecStatusType* = enum
  47. PGRES_EMPTY_QUERY = 0, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PGRES_COPY_OUT,
  48. PGRES_COPY_IN, PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR, PGRES_FATAL_ERROR
  49. PGlobjfuncs*{.pure, final.} = object
  50. fn_lo_open*: Oid
  51. fn_lo_close*: Oid
  52. fn_lo_creat*: Oid
  53. fn_lo_unlink*: Oid
  54. fn_lo_lseek*: Oid
  55. fn_lo_tell*: Oid
  56. fn_lo_read*: Oid
  57. fn_lo_write*: Oid
  58. PPGlobjfuncs* = ptr PGlobjfuncs
  59. PConnStatusType* = ptr ConnStatusType
  60. ConnStatusType* = enum
  61. CONNECTION_OK, CONNECTION_BAD, CONNECTION_STARTED, CONNECTION_MADE,
  62. CONNECTION_AWAITING_RESPONSE, CONNECTION_AUTH_OK, CONNECTION_SETENV,
  63. CONNECTION_SSL_STARTUP, CONNECTION_NEEDED
  64. PGconn*{.pure, final.} = object
  65. pghost*: cstring
  66. pgtty*: cstring
  67. pgport*: cstring
  68. pgoptions*: cstring
  69. dbName*: cstring
  70. status*: ConnStatusType
  71. errorMessage*: array[0..(ERROR_MSG_LENGTH) - 1, char]
  72. Pfin*: File
  73. Pfout*: File
  74. Pfdebug*: File
  75. sock*: int32
  76. laddr*: SockAddr
  77. raddr*: SockAddr
  78. salt*: array[0..(2) - 1, char]
  79. asyncNotifyWaiting*: int32
  80. notifyList*: pointer
  81. pguser*: cstring
  82. pgpass*: cstring
  83. lobjfuncs*: PPGlobjfuncs
  84. PPGconn* = ptr PGconn
  85. PGresult*{.pure, final.} = object
  86. ntups*: int32
  87. numAttributes*: int32
  88. attDescs*: PPGresAttDesc
  89. tuples*: PPPGresAttValue
  90. tupArrSize*: int32
  91. resultStatus*: ExecStatusType
  92. cmdStatus*: array[0..(CMDSTATUS_LEN) - 1, char]
  93. binary*: int32
  94. conn*: PPGconn
  95. PPGresult* = ptr PGresult
  96. PPostgresPollingStatusType* = ptr PostgresPollingStatusType
  97. PostgresPollingStatusType* = enum
  98. PGRES_POLLING_FAILED = 0, PGRES_POLLING_READING, PGRES_POLLING_WRITING,
  99. PGRES_POLLING_OK, PGRES_POLLING_ACTIVE
  100. PPGTransactionStatusType* = ptr PGTransactionStatusType
  101. PGTransactionStatusType* = enum
  102. PQTRANS_IDLE, PQTRANS_ACTIVE, PQTRANS_INTRANS, PQTRANS_INERROR,
  103. PQTRANS_UNKNOWN
  104. PPGVerbosity* = ptr PGVerbosity
  105. PGVerbosity* = enum
  106. PQERRORS_TERSE, PQERRORS_DEFAULT, PQERRORS_VERBOSE
  107. PpgNotify* = ptr pgNotify
  108. pgNotify*{.pure, final.} = object
  109. relname*: cstring
  110. be_pid*: int32
  111. extra*: cstring
  112. PQnoticeReceiver* = proc (arg: pointer, res: PPGresult){.cdecl.}
  113. PQnoticeProcessor* = proc (arg: pointer, message: cstring){.cdecl.}
  114. Ppqbool* = ptr pqbool
  115. pqbool* = char
  116. P_PQprintOpt* = ptr PQprintOpt
  117. PQprintOpt*{.pure, final.} = object
  118. header*: pqbool
  119. align*: pqbool
  120. standard*: pqbool
  121. html3*: pqbool
  122. expanded*: pqbool
  123. pager*: pqbool
  124. fieldSep*: cstring
  125. tableOpt*: cstring
  126. caption*: cstring
  127. fieldName*: ptr cstring
  128. P_PQconninfoOption* = ptr PQconninfoOption
  129. PQconninfoOption*{.pure, final.} = object
  130. keyword*: cstring
  131. envvar*: cstring
  132. compiled*: cstring
  133. val*: cstring
  134. label*: cstring
  135. dispchar*: cstring
  136. dispsize*: int32
  137. PPQArgBlock* = ptr PQArgBlock
  138. PQArgBlock*{.pure, final.} = object
  139. length*: int32
  140. isint*: int32
  141. p*: pointer
  142. proc pqinitOpenSSL*(do_ssl: int32, do_crypto: int32) {.cdecl, dynlib: dllName,
  143. importc: "PQinitOpenSSL".}
  144. proc pqconnectStart*(conninfo: cstring): PPGconn{.cdecl, dynlib: dllName,
  145. importc: "PQconnectStart".}
  146. proc pqconnectPoll*(conn: PPGconn): PostgresPollingStatusType{.cdecl,
  147. dynlib: dllName, importc: "PQconnectPoll".}
  148. proc pqconnectdb*(conninfo: cstring): PPGconn{.cdecl, dynlib: dllName,
  149. importc: "PQconnectdb".}
  150. proc pqsetdbLogin*(pghost: cstring, pgport: cstring, pgoptions: cstring,
  151. pgtty: cstring, dbName: cstring, login: cstring, pwd: cstring): PPGconn{.
  152. cdecl, dynlib: dllName, importc: "PQsetdbLogin".}
  153. proc pqsetdb*(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME: cstring): Ppgconn
  154. proc pqfinish*(conn: PPGconn){.cdecl, dynlib: dllName, importc: "PQfinish".}
  155. proc pqconndefaults*(): PPQconninfoOption{.cdecl, dynlib: dllName,
  156. importc: "PQconndefaults".}
  157. proc pqconninfoFree*(connOptions: PPQconninfoOption){.cdecl, dynlib: dllName,
  158. importc: "PQconninfoFree".}
  159. proc pqresetStart*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  160. importc: "PQresetStart".}
  161. proc pqresetPoll*(conn: PPGconn): PostgresPollingStatusType{.cdecl,
  162. dynlib: dllName, importc: "PQresetPoll".}
  163. proc pqreset*(conn: PPGconn){.cdecl, dynlib: dllName, importc: "PQreset".}
  164. proc pqrequestCancel*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  165. importc: "PQrequestCancel".}
  166. proc pqdb*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQdb".}
  167. proc pquser*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQuser".}
  168. proc pqpass*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQpass".}
  169. proc pqhost*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQhost".}
  170. proc pqport*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQport".}
  171. proc pqtty*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQtty".}
  172. proc pqoptions*(conn: PPGconn): cstring{.cdecl, dynlib: dllName,
  173. importc: "PQoptions".}
  174. proc pqstatus*(conn: PPGconn): ConnStatusType{.cdecl, dynlib: dllName,
  175. importc: "PQstatus".}
  176. proc pqtransactionStatus*(conn: PPGconn): PGTransactionStatusType{.cdecl,
  177. dynlib: dllName, importc: "PQtransactionStatus".}
  178. proc pqparameterStatus*(conn: PPGconn, paramName: cstring): cstring{.cdecl,
  179. dynlib: dllName, importc: "PQparameterStatus".}
  180. proc pqprotocolVersion*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  181. importc: "PQprotocolVersion".}
  182. proc pqerrorMessage*(conn: PPGconn): cstring{.cdecl, dynlib: dllName,
  183. importc: "PQerrorMessage".}
  184. proc pqsocket*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  185. importc: "PQsocket".}
  186. proc pqbackendPID*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  187. importc: "PQbackendPID".}
  188. proc pqclientEncoding*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  189. importc: "PQclientEncoding".}
  190. proc pqsetClientEncoding*(conn: PPGconn, encoding: cstring): int32{.cdecl,
  191. dynlib: dllName, importc: "PQsetClientEncoding".}
  192. when defined(USE_SSL):
  193. # Get the SSL structure associated with a connection
  194. proc pqgetssl*(conn: PPGconn): PSSL{.cdecl, dynlib: dllName,
  195. importc: "PQgetssl".}
  196. proc pqsetErrorVerbosity*(conn: PPGconn, verbosity: PGVerbosity): PGVerbosity{.
  197. cdecl, dynlib: dllName, importc: "PQsetErrorVerbosity".}
  198. proc pqtrace*(conn: PPGconn, debug_port: File){.cdecl, dynlib: dllName,
  199. importc: "PQtrace".}
  200. proc pquntrace*(conn: PPGconn){.cdecl, dynlib: dllName, importc: "PQuntrace".}
  201. proc pqsetNoticeReceiver*(conn: PPGconn, theProc: PQnoticeReceiver, arg: pointer): PQnoticeReceiver{.
  202. cdecl, dynlib: dllName, importc: "PQsetNoticeReceiver".}
  203. proc pqsetNoticeProcessor*(conn: PPGconn, theProc: PQnoticeProcessor,
  204. arg: pointer): PQnoticeProcessor{.cdecl,
  205. dynlib: dllName, importc: "PQsetNoticeProcessor".}
  206. proc pqexec*(conn: PPGconn, query: cstring): PPGresult{.cdecl, dynlib: dllName,
  207. importc: "PQexec".}
  208. proc pqexecParams*(conn: PPGconn, command: cstring, nParams: int32,
  209. paramTypes: POid, paramValues: cstringArray,
  210. paramLengths, paramFormats: ptr int32, resultFormat: int32): PPGresult{.
  211. cdecl, dynlib: dllName, importc: "PQexecParams".}
  212. proc pqprepare*(conn: PPGconn, stmtName, query: cstring, nParams: int32,
  213. paramTypes: POid): PPGresult{.cdecl, dynlib: dllName, importc: "PQprepare".}
  214. proc pqexecPrepared*(conn: PPGconn, stmtName: cstring, nParams: int32,
  215. paramValues: cstringArray,
  216. paramLengths, paramFormats: ptr int32, resultFormat: int32): PPGresult{.
  217. cdecl, dynlib: dllName, importc: "PQexecPrepared".}
  218. proc pqsendQuery*(conn: PPGconn, query: cstring): int32{.cdecl, dynlib: dllName,
  219. importc: "PQsendQuery".}
  220. proc pqsendQueryParams*(conn: PPGconn, command: cstring, nParams: int32,
  221. paramTypes: POid, paramValues: cstringArray,
  222. paramLengths, paramFormats: ptr int32,
  223. resultFormat: int32): int32{.cdecl, dynlib: dllName,
  224. importc: "PQsendQueryParams".}
  225. proc pqsendQueryPrepared*(conn: PPGconn, stmtName: cstring, nParams: int32,
  226. paramValues: cstringArray,
  227. paramLengths, paramFormats: ptr int32,
  228. resultFormat: int32): int32{.cdecl, dynlib: dllName,
  229. importc: "PQsendQueryPrepared".}
  230. proc pqgetResult*(conn: PPGconn): PPGresult{.cdecl, dynlib: dllName,
  231. importc: "PQgetResult".}
  232. proc pqisBusy*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  233. importc: "PQisBusy".}
  234. proc pqconsumeInput*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  235. importc: "PQconsumeInput".}
  236. proc pqnotifies*(conn: PPGconn): PPGnotify{.cdecl, dynlib: dllName,
  237. importc: "PQnotifies".}
  238. proc pqputCopyData*(conn: PPGconn, buffer: cstring, nbytes: int32): int32{.
  239. cdecl, dynlib: dllName, importc: "PQputCopyData".}
  240. proc pqputCopyEnd*(conn: PPGconn, errormsg: cstring): int32{.cdecl,
  241. dynlib: dllName, importc: "PQputCopyEnd".}
  242. proc pqgetCopyData*(conn: PPGconn, buffer: cstringArray, async: int32): int32{.
  243. cdecl, dynlib: dllName, importc: "PQgetCopyData".}
  244. proc pqgetline*(conn: PPGconn, str: cstring, len: int32): int32{.cdecl,
  245. dynlib: dllName, importc: "PQgetline".}
  246. proc pqputline*(conn: PPGconn, str: cstring): int32{.cdecl, dynlib: dllName,
  247. importc: "PQputline".}
  248. proc pqgetlineAsync*(conn: PPGconn, buffer: cstring, bufsize: int32): int32{.
  249. cdecl, dynlib: dllName, importc: "PQgetlineAsync".}
  250. proc pqputnbytes*(conn: PPGconn, buffer: cstring, nbytes: int32): int32{.cdecl,
  251. dynlib: dllName, importc: "PQputnbytes".}
  252. proc pqendcopy*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  253. importc: "PQendcopy".}
  254. proc pqsetnonblocking*(conn: PPGconn, arg: int32): int32{.cdecl,
  255. dynlib: dllName, importc: "PQsetnonblocking".}
  256. proc pqisnonblocking*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  257. importc: "PQisnonblocking".}
  258. proc pqflush*(conn: PPGconn): int32{.cdecl, dynlib: dllName, importc: "PQflush".}
  259. proc pqfn*(conn: PPGconn, fnid: int32, result_buf, result_len: ptr int32,
  260. result_is_int: int32, args: PPQArgBlock, nargs: int32): PPGresult{.
  261. cdecl, dynlib: dllName, importc: "PQfn".}
  262. proc pqresultStatus*(res: PPGresult): ExecStatusType{.cdecl, dynlib: dllName,
  263. importc: "PQresultStatus".}
  264. proc pqresStatus*(status: ExecStatusType): cstring{.cdecl, dynlib: dllName,
  265. importc: "PQresStatus".}
  266. proc pqresultErrorMessage*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  267. importc: "PQresultErrorMessage".}
  268. proc pqresultErrorField*(res: PPGresult, fieldcode: int32): cstring{.cdecl,
  269. dynlib: dllName, importc: "PQresultErrorField".}
  270. proc pqntuples*(res: PPGresult): int32{.cdecl, dynlib: dllName,
  271. importc: "PQntuples".}
  272. proc pqnfields*(res: PPGresult): int32{.cdecl, dynlib: dllName,
  273. importc: "PQnfields".}
  274. proc pqbinaryTuples*(res: PPGresult): int32{.cdecl, dynlib: dllName,
  275. importc: "PQbinaryTuples".}
  276. proc pqfname*(res: PPGresult, field_num: int32): cstring{.cdecl,
  277. dynlib: dllName, importc: "PQfname".}
  278. proc pqfnumber*(res: PPGresult, field_name: cstring): int32{.cdecl,
  279. dynlib: dllName, importc: "PQfnumber".}
  280. proc pqftable*(res: PPGresult, field_num: int32): Oid{.cdecl, dynlib: dllName,
  281. importc: "PQftable".}
  282. proc pqftablecol*(res: PPGresult, field_num: int32): int32{.cdecl,
  283. dynlib: dllName, importc: "PQftablecol".}
  284. proc pqfformat*(res: PPGresult, field_num: int32): int32{.cdecl,
  285. dynlib: dllName, importc: "PQfformat".}
  286. proc pqftype*(res: PPGresult, field_num: int32): Oid{.cdecl, dynlib: dllName,
  287. importc: "PQftype".}
  288. proc pqfsize*(res: PPGresult, field_num: int32): int32{.cdecl, dynlib: dllName,
  289. importc: "PQfsize".}
  290. proc pqfmod*(res: PPGresult, field_num: int32): int32{.cdecl, dynlib: dllName,
  291. importc: "PQfmod".}
  292. proc pqcmdStatus*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  293. importc: "PQcmdStatus".}
  294. proc pqoidStatus*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  295. importc: "PQoidStatus".}
  296. proc pqoidValue*(res: PPGresult): Oid{.cdecl, dynlib: dllName,
  297. importc: "PQoidValue".}
  298. proc pqcmdTuples*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  299. importc: "PQcmdTuples".}
  300. proc pqgetvalue*(res: PPGresult, tup_num: int32, field_num: int32): cstring{.
  301. cdecl, dynlib: dllName, importc: "PQgetvalue".}
  302. proc pqgetlength*(res: PPGresult, tup_num: int32, field_num: int32): int32{.
  303. cdecl, dynlib: dllName, importc: "PQgetlength".}
  304. proc pqgetisnull*(res: PPGresult, tup_num: int32, field_num: int32): int32{.
  305. cdecl, dynlib: dllName, importc: "PQgetisnull".}
  306. proc pqclear*(res: PPGresult){.cdecl, dynlib: dllName, importc: "PQclear".}
  307. proc pqfreemem*(p: pointer){.cdecl, dynlib: dllName, importc: "PQfreemem".}
  308. proc pqmakeEmptyPGresult*(conn: PPGconn, status: ExecStatusType): PPGresult{.
  309. cdecl, dynlib: dllName, importc: "PQmakeEmptyPGresult".}
  310. proc pqescapeString*(till, `from`: cstring, len: int): int{.cdecl,
  311. dynlib: dllName, importc: "PQescapeString".}
  312. proc pqescapeBytea*(bintext: cstring, binlen: int, bytealen: var int): cstring{.
  313. cdecl, dynlib: dllName, importc: "PQescapeBytea".}
  314. proc pqunescapeBytea*(strtext: cstring, retbuflen: var int): cstring{.cdecl,
  315. dynlib: dllName, importc: "PQunescapeBytea".}
  316. proc pqprint*(fout: File, res: PPGresult, ps: PPQprintOpt){.cdecl,
  317. dynlib: dllName, importc: "PQprint".}
  318. proc pqdisplayTuples*(res: PPGresult, fp: File, fillAlign: int32,
  319. fieldSep: cstring, printHeader: int32, quiet: int32){.
  320. cdecl, dynlib: dllName, importc: "PQdisplayTuples".}
  321. proc pqprintTuples*(res: PPGresult, fout: File, printAttName: int32,
  322. terseOutput: int32, width: int32){.cdecl, dynlib: dllName,
  323. importc: "PQprintTuples".}
  324. proc lo_open*(conn: PPGconn, lobjId: Oid, mode: int32): int32{.cdecl,
  325. dynlib: dllName, importc: "lo_open".}
  326. proc lo_close*(conn: PPGconn, fd: int32): int32{.cdecl, dynlib: dllName,
  327. importc: "lo_close".}
  328. proc lo_read*(conn: PPGconn, fd: int32, buf: cstring, length: int): int32{.
  329. cdecl, dynlib: dllName, importc: "lo_read".}
  330. proc lo_write*(conn: PPGconn, fd: int32, buf: cstring, length: int): int32{.
  331. cdecl, dynlib: dllName, importc: "lo_write".}
  332. proc lo_lseek*(conn: PPGconn, fd: int32, offset: int32, whence: int32): int32{.
  333. cdecl, dynlib: dllName, importc: "lo_lseek".}
  334. proc lo_creat*(conn: PPGconn, mode: int32): Oid{.cdecl, dynlib: dllName,
  335. importc: "lo_creat".}
  336. proc lo_tell*(conn: PPGconn, fd: int32): int32{.cdecl, dynlib: dllName,
  337. importc: "lo_tell".}
  338. proc lo_unlink*(conn: PPGconn, lobjId: Oid): int32{.cdecl, dynlib: dllName,
  339. importc: "lo_unlink".}
  340. proc lo_import*(conn: PPGconn, filename: cstring): Oid{.cdecl, dynlib: dllName,
  341. importc: "lo_import".}
  342. proc lo_export*(conn: PPGconn, lobjId: Oid, filename: cstring): int32{.cdecl,
  343. dynlib: dllName, importc: "lo_export".}
  344. proc pqmblen*(s: cstring, encoding: int32): int32{.cdecl, dynlib: dllName,
  345. importc: "PQmblen".}
  346. proc pqenv2encoding*(): int32{.cdecl, dynlib: dllName, importc: "PQenv2encoding".}
  347. proc pqsetdb(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME: cstring): PPgConn =
  348. result = pqSetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, "", "")