postgres.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. {.deprecated: [TSockAddr: SockAddr, TPGresAttDesc: PgresAttDesc,
  143. TPGresAttValue: PgresAttValue, TExecStatusType: ExecStatusType,
  144. TPGlobjfuncs: Pglobjfuncs, TConnStatusType: ConnStatusType, TPGconn: Pgconn,
  145. TPGresult: PGresult].}
  146. proc pqconnectStart*(conninfo: cstring): PPGconn{.cdecl, dynlib: dllName,
  147. importc: "PQconnectStart".}
  148. proc pqconnectPoll*(conn: PPGconn): PostgresPollingStatusType{.cdecl,
  149. dynlib: dllName, importc: "PQconnectPoll".}
  150. proc pqconnectdb*(conninfo: cstring): PPGconn{.cdecl, dynlib: dllName,
  151. importc: "PQconnectdb".}
  152. proc pqsetdbLogin*(pghost: cstring, pgport: cstring, pgoptions: cstring,
  153. pgtty: cstring, dbName: cstring, login: cstring, pwd: cstring): PPGconn{.
  154. cdecl, dynlib: dllName, importc: "PQsetdbLogin".}
  155. proc pqsetdb*(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME: cstring): Ppgconn
  156. proc pqfinish*(conn: PPGconn){.cdecl, dynlib: dllName, importc: "PQfinish".}
  157. proc pqconndefaults*(): PPQconninfoOption{.cdecl, dynlib: dllName,
  158. importc: "PQconndefaults".}
  159. proc pqconninfoFree*(connOptions: PPQconninfoOption){.cdecl, dynlib: dllName,
  160. importc: "PQconninfoFree".}
  161. proc pqresetStart*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  162. importc: "PQresetStart".}
  163. proc pqresetPoll*(conn: PPGconn): PostgresPollingStatusType{.cdecl,
  164. dynlib: dllName, importc: "PQresetPoll".}
  165. proc pqreset*(conn: PPGconn){.cdecl, dynlib: dllName, importc: "PQreset".}
  166. proc pqrequestCancel*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  167. importc: "PQrequestCancel".}
  168. proc pqdb*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQdb".}
  169. proc pquser*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQuser".}
  170. proc pqpass*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQpass".}
  171. proc pqhost*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQhost".}
  172. proc pqport*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQport".}
  173. proc pqtty*(conn: PPGconn): cstring{.cdecl, dynlib: dllName, importc: "PQtty".}
  174. proc pqoptions*(conn: PPGconn): cstring{.cdecl, dynlib: dllName,
  175. importc: "PQoptions".}
  176. proc pqstatus*(conn: PPGconn): ConnStatusType{.cdecl, dynlib: dllName,
  177. importc: "PQstatus".}
  178. proc pqtransactionStatus*(conn: PPGconn): PGTransactionStatusType{.cdecl,
  179. dynlib: dllName, importc: "PQtransactionStatus".}
  180. proc pqparameterStatus*(conn: PPGconn, paramName: cstring): cstring{.cdecl,
  181. dynlib: dllName, importc: "PQparameterStatus".}
  182. proc pqprotocolVersion*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  183. importc: "PQprotocolVersion".}
  184. proc pqerrorMessage*(conn: PPGconn): cstring{.cdecl, dynlib: dllName,
  185. importc: "PQerrorMessage".}
  186. proc pqsocket*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  187. importc: "PQsocket".}
  188. proc pqbackendPID*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  189. importc: "PQbackendPID".}
  190. proc pqclientEncoding*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  191. importc: "PQclientEncoding".}
  192. proc pqsetClientEncoding*(conn: PPGconn, encoding: cstring): int32{.cdecl,
  193. dynlib: dllName, importc: "PQsetClientEncoding".}
  194. when defined(USE_SSL):
  195. # Get the SSL structure associated with a connection
  196. proc pqgetssl*(conn: PPGconn): PSSL{.cdecl, dynlib: dllName,
  197. importc: "PQgetssl".}
  198. proc pqsetErrorVerbosity*(conn: PPGconn, verbosity: PGVerbosity): PGVerbosity{.
  199. cdecl, dynlib: dllName, importc: "PQsetErrorVerbosity".}
  200. proc pqtrace*(conn: PPGconn, debug_port: File){.cdecl, dynlib: dllName,
  201. importc: "PQtrace".}
  202. proc pquntrace*(conn: PPGconn){.cdecl, dynlib: dllName, importc: "PQuntrace".}
  203. proc pqsetNoticeReceiver*(conn: PPGconn, theProc: PQnoticeReceiver, arg: pointer): PQnoticeReceiver{.
  204. cdecl, dynlib: dllName, importc: "PQsetNoticeReceiver".}
  205. proc pqsetNoticeProcessor*(conn: PPGconn, theProc: PQnoticeProcessor,
  206. arg: pointer): PQnoticeProcessor{.cdecl,
  207. dynlib: dllName, importc: "PQsetNoticeProcessor".}
  208. proc pqexec*(conn: PPGconn, query: cstring): PPGresult{.cdecl, dynlib: dllName,
  209. importc: "PQexec".}
  210. proc pqexecParams*(conn: PPGconn, command: cstring, nParams: int32,
  211. paramTypes: POid, paramValues: cstringArray,
  212. paramLengths, paramFormats: ptr int32, resultFormat: int32): PPGresult{.
  213. cdecl, dynlib: dllName, importc: "PQexecParams".}
  214. proc pqprepare*(conn: PPGconn, stmtName, query: cstring, nParams: int32,
  215. paramTypes: POid): PPGresult{.cdecl, dynlib: dllName, importc: "PQprepare".}
  216. proc pqexecPrepared*(conn: PPGconn, stmtName: cstring, nParams: int32,
  217. paramValues: cstringArray,
  218. paramLengths, paramFormats: ptr int32, resultFormat: int32): PPGresult{.
  219. cdecl, dynlib: dllName, importc: "PQexecPrepared".}
  220. proc pqsendQuery*(conn: PPGconn, query: cstring): int32{.cdecl, dynlib: dllName,
  221. importc: "PQsendQuery".}
  222. proc pqsendQueryParams*(conn: PPGconn, command: cstring, nParams: int32,
  223. paramTypes: POid, paramValues: cstringArray,
  224. paramLengths, paramFormats: ptr int32,
  225. resultFormat: int32): int32{.cdecl, dynlib: dllName,
  226. importc: "PQsendQueryParams".}
  227. proc pqsendQueryPrepared*(conn: PPGconn, stmtName: cstring, nParams: int32,
  228. paramValues: cstringArray,
  229. paramLengths, paramFormats: ptr int32,
  230. resultFormat: int32): int32{.cdecl, dynlib: dllName,
  231. importc: "PQsendQueryPrepared".}
  232. proc pqgetResult*(conn: PPGconn): PPGresult{.cdecl, dynlib: dllName,
  233. importc: "PQgetResult".}
  234. proc pqisBusy*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  235. importc: "PQisBusy".}
  236. proc pqconsumeInput*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  237. importc: "PQconsumeInput".}
  238. proc pqnotifies*(conn: PPGconn): PPGnotify{.cdecl, dynlib: dllName,
  239. importc: "PQnotifies".}
  240. proc pqputCopyData*(conn: PPGconn, buffer: cstring, nbytes: int32): int32{.
  241. cdecl, dynlib: dllName, importc: "PQputCopyData".}
  242. proc pqputCopyEnd*(conn: PPGconn, errormsg: cstring): int32{.cdecl,
  243. dynlib: dllName, importc: "PQputCopyEnd".}
  244. proc pqgetCopyData*(conn: PPGconn, buffer: cstringArray, async: int32): int32{.
  245. cdecl, dynlib: dllName, importc: "PQgetCopyData".}
  246. proc pqgetline*(conn: PPGconn, str: cstring, len: int32): int32{.cdecl,
  247. dynlib: dllName, importc: "PQgetline".}
  248. proc pqputline*(conn: PPGconn, str: cstring): int32{.cdecl, dynlib: dllName,
  249. importc: "PQputline".}
  250. proc pqgetlineAsync*(conn: PPGconn, buffer: cstring, bufsize: int32): int32{.
  251. cdecl, dynlib: dllName, importc: "PQgetlineAsync".}
  252. proc pqputnbytes*(conn: PPGconn, buffer: cstring, nbytes: int32): int32{.cdecl,
  253. dynlib: dllName, importc: "PQputnbytes".}
  254. proc pqendcopy*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  255. importc: "PQendcopy".}
  256. proc pqsetnonblocking*(conn: PPGconn, arg: int32): int32{.cdecl,
  257. dynlib: dllName, importc: "PQsetnonblocking".}
  258. proc pqisnonblocking*(conn: PPGconn): int32{.cdecl, dynlib: dllName,
  259. importc: "PQisnonblocking".}
  260. proc pqflush*(conn: PPGconn): int32{.cdecl, dynlib: dllName, importc: "PQflush".}
  261. proc pqfn*(conn: PPGconn, fnid: int32, result_buf, result_len: ptr int32,
  262. result_is_int: int32, args: PPQArgBlock, nargs: int32): PPGresult{.
  263. cdecl, dynlib: dllName, importc: "PQfn".}
  264. proc pqresultStatus*(res: PPGresult): ExecStatusType{.cdecl, dynlib: dllName,
  265. importc: "PQresultStatus".}
  266. proc pqresStatus*(status: ExecStatusType): cstring{.cdecl, dynlib: dllName,
  267. importc: "PQresStatus".}
  268. proc pqresultErrorMessage*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  269. importc: "PQresultErrorMessage".}
  270. proc pqresultErrorField*(res: PPGresult, fieldcode: int32): cstring{.cdecl,
  271. dynlib: dllName, importc: "PQresultErrorField".}
  272. proc pqntuples*(res: PPGresult): int32{.cdecl, dynlib: dllName,
  273. importc: "PQntuples".}
  274. proc pqnfields*(res: PPGresult): int32{.cdecl, dynlib: dllName,
  275. importc: "PQnfields".}
  276. proc pqbinaryTuples*(res: PPGresult): int32{.cdecl, dynlib: dllName,
  277. importc: "PQbinaryTuples".}
  278. proc pqfname*(res: PPGresult, field_num: int32): cstring{.cdecl,
  279. dynlib: dllName, importc: "PQfname".}
  280. proc pqfnumber*(res: PPGresult, field_name: cstring): int32{.cdecl,
  281. dynlib: dllName, importc: "PQfnumber".}
  282. proc pqftable*(res: PPGresult, field_num: int32): Oid{.cdecl, dynlib: dllName,
  283. importc: "PQftable".}
  284. proc pqftablecol*(res: PPGresult, field_num: int32): int32{.cdecl,
  285. dynlib: dllName, importc: "PQftablecol".}
  286. proc pqfformat*(res: PPGresult, field_num: int32): int32{.cdecl,
  287. dynlib: dllName, importc: "PQfformat".}
  288. proc pqftype*(res: PPGresult, field_num: int32): Oid{.cdecl, dynlib: dllName,
  289. importc: "PQftype".}
  290. proc pqfsize*(res: PPGresult, field_num: int32): int32{.cdecl, dynlib: dllName,
  291. importc: "PQfsize".}
  292. proc pqfmod*(res: PPGresult, field_num: int32): int32{.cdecl, dynlib: dllName,
  293. importc: "PQfmod".}
  294. proc pqcmdStatus*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  295. importc: "PQcmdStatus".}
  296. proc pqoidStatus*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  297. importc: "PQoidStatus".}
  298. proc pqoidValue*(res: PPGresult): Oid{.cdecl, dynlib: dllName,
  299. importc: "PQoidValue".}
  300. proc pqcmdTuples*(res: PPGresult): cstring{.cdecl, dynlib: dllName,
  301. importc: "PQcmdTuples".}
  302. proc pqgetvalue*(res: PPGresult, tup_num: int32, field_num: int32): cstring{.
  303. cdecl, dynlib: dllName, importc: "PQgetvalue".}
  304. proc pqgetlength*(res: PPGresult, tup_num: int32, field_num: int32): int32{.
  305. cdecl, dynlib: dllName, importc: "PQgetlength".}
  306. proc pqgetisnull*(res: PPGresult, tup_num: int32, field_num: int32): int32{.
  307. cdecl, dynlib: dllName, importc: "PQgetisnull".}
  308. proc pqclear*(res: PPGresult){.cdecl, dynlib: dllName, importc: "PQclear".}
  309. proc pqfreemem*(p: pointer){.cdecl, dynlib: dllName, importc: "PQfreemem".}
  310. proc pqmakeEmptyPGresult*(conn: PPGconn, status: ExecStatusType): PPGresult{.
  311. cdecl, dynlib: dllName, importc: "PQmakeEmptyPGresult".}
  312. proc pqescapeString*(till, `from`: cstring, len: int): int{.cdecl,
  313. dynlib: dllName, importc: "PQescapeString".}
  314. proc pqescapeBytea*(bintext: cstring, binlen: int, bytealen: var int): cstring{.
  315. cdecl, dynlib: dllName, importc: "PQescapeBytea".}
  316. proc pqunescapeBytea*(strtext: cstring, retbuflen: var int): cstring{.cdecl,
  317. dynlib: dllName, importc: "PQunescapeBytea".}
  318. proc pqprint*(fout: File, res: PPGresult, ps: PPQprintOpt){.cdecl,
  319. dynlib: dllName, importc: "PQprint".}
  320. proc pqdisplayTuples*(res: PPGresult, fp: File, fillAlign: int32,
  321. fieldSep: cstring, printHeader: int32, quiet: int32){.
  322. cdecl, dynlib: dllName, importc: "PQdisplayTuples".}
  323. proc pqprintTuples*(res: PPGresult, fout: File, printAttName: int32,
  324. terseOutput: int32, width: int32){.cdecl, dynlib: dllName,
  325. importc: "PQprintTuples".}
  326. proc lo_open*(conn: PPGconn, lobjId: Oid, mode: int32): int32{.cdecl,
  327. dynlib: dllName, importc: "lo_open".}
  328. proc lo_close*(conn: PPGconn, fd: int32): int32{.cdecl, dynlib: dllName,
  329. importc: "lo_close".}
  330. proc lo_read*(conn: PPGconn, fd: int32, buf: cstring, length: int): int32{.
  331. cdecl, dynlib: dllName, importc: "lo_read".}
  332. proc lo_write*(conn: PPGconn, fd: int32, buf: cstring, length: int): int32{.
  333. cdecl, dynlib: dllName, importc: "lo_write".}
  334. proc lo_lseek*(conn: PPGconn, fd: int32, offset: int32, whence: int32): int32{.
  335. cdecl, dynlib: dllName, importc: "lo_lseek".}
  336. proc lo_creat*(conn: PPGconn, mode: int32): Oid{.cdecl, dynlib: dllName,
  337. importc: "lo_creat".}
  338. proc lo_tell*(conn: PPGconn, fd: int32): int32{.cdecl, dynlib: dllName,
  339. importc: "lo_tell".}
  340. proc lo_unlink*(conn: PPGconn, lobjId: Oid): int32{.cdecl, dynlib: dllName,
  341. importc: "lo_unlink".}
  342. proc lo_import*(conn: PPGconn, filename: cstring): Oid{.cdecl, dynlib: dllName,
  343. importc: "lo_import".}
  344. proc lo_export*(conn: PPGconn, lobjId: Oid, filename: cstring): int32{.cdecl,
  345. dynlib: dllName, importc: "lo_export".}
  346. proc pqmblen*(s: cstring, encoding: int32): int32{.cdecl, dynlib: dllName,
  347. importc: "PQmblen".}
  348. proc pqenv2encoding*(): int32{.cdecl, dynlib: dllName, importc: "PQenv2encoding".}
  349. proc pqsetdb(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME: cstring): PPgConn =
  350. result = pqSetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, "", "")