postgres_redone.nim 16 KB

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