postgres.nim 17 KB

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