postgres3.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. { This unit 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. It is for postgreSQL version 7.4 and higher with support for the v3.0
  5. connection-protocol
  6. }
  7. {$IFNDEF FPC_DOTTEDUNITS}
  8. unit postgres3;
  9. {$ENDIF FPC_DOTTEDUNITS}
  10. interface
  11. {$IFDEF FPC_DOTTEDUNITS}
  12. uses Api.Dllist;
  13. {$ELSE FPC_DOTTEDUNITS}
  14. uses dllist;
  15. {$ENDIF FPC_DOTTEDUNITS}
  16. {$PACKRECORDS C}
  17. const
  18. External_library='pq';
  19. {$i postgres3types.inc}
  20. { ----------------
  21. * Exported functions of libpq
  22. * ----------------
  23. }
  24. { === in fe-connect.c === }
  25. { make a new client connection to the backend }
  26. { Asynchronous (non-blocking) }
  27. (* Const before type ignored *)
  28. function PQconnectStart(conninfo:PAnsiChar):PPGconn;cdecl;external External_library name 'PQconnectStart';
  29. function PQconnectPoll(conn:PPGconn):PostgresPollingStatusType;cdecl;external External_library name 'PQconnectPoll';
  30. { Synchronous (blocking) }
  31. (* Const before type ignored *)
  32. function PQconnectdb(conninfo:PAnsiChar):PPGconn;cdecl;external External_library name 'PQconnectdb';
  33. function PQsetdbLogin(pghost:PAnsiChar; pgport:PAnsiChar; pgoptions:PAnsiChar; pgtty:PAnsiChar; dbName:PAnsiChar;
  34. login:PAnsiChar; pwd:PAnsiChar):PPGconn;cdecl;external External_library name 'PQsetdbLogin';
  35. { was #define dname(params) para_def_expr }
  36. { argument types are unknown }
  37. { return type might be wrong }
  38. function PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME : PAnsiChar) : ppgconn;
  39. { close the current connection and free the PGconn data structure }
  40. procedure PQfinish(conn:PPGconn);cdecl;external External_library name 'PQfinish';
  41. { get info about connection options known to PQconnectdb }
  42. function PQconndefaults:PPQconninfoOption;cdecl;external External_library name 'PQconndefaults';
  43. { free the data structure returned by PQconndefaults() }
  44. procedure PQconninfoFree(connOptions:PPQconninfoOption);cdecl;external External_library name 'PQconninfoFree';
  45. {
  46. * close the current connection and restablish a new one with the same
  47. * parameters
  48. }
  49. { Asynchronous (non-blocking) }
  50. function PQresetStart(conn:PPGconn):longint;cdecl;external External_library name 'PQresetStart';
  51. function PQresetPoll(conn:PPGconn):PostgresPollingStatusType;cdecl;external External_library name 'PQresetPoll';
  52. { Synchronous (blocking) }
  53. procedure PQreset(conn:PPGconn);cdecl;external External_library name 'PQreset';
  54. { issue a cancel request }
  55. function PQrequestCancel(conn:PPGconn):longint;cdecl;external External_library name 'PQrequestCancel';
  56. { Accessor functions for PGconn objects }
  57. function PQdb(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQdb';
  58. function PQuser(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQuser';
  59. function PQpass(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQpass';
  60. function PQhost(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQhost';
  61. function PQport(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQport';
  62. function PQtty(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQtty';
  63. function PQoptions(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQoptions';
  64. function PQstatus(conn:PPGconn):TConnStatusType;cdecl;external External_library name 'PQstatus';
  65. function PQtransactionStatus(conn:PPGconn):PGTransactionStatusType;cdecl;external External_library name 'PQtransactionStatus';
  66. function PQparameterStatus(conn:PPGconn; paramName:PAnsiChar):PAnsiChar;cdecl;external External_library name 'PQparameterStatus';
  67. function PQprotocolVersion(conn:PPGconn):longint;cdecl;external External_library name 'PQprotocolVersion';
  68. function PQserverVersion(conn:PPGconn):longint;cdecl;external External_library name 'PQserverVersion';
  69. function PQerrorMessage(conn:PPGconn):PAnsiChar;cdecl;external External_library name 'PQerrorMessage';
  70. function PQsocket(conn:PPGconn):longint;cdecl;external External_library name 'PQsocket';
  71. function PQbackendPID(conn:PPGconn):longint;cdecl;external External_library name 'PQbackendPID';
  72. function PQclientEncoding(conn:PPGconn):longint;cdecl;external External_library name 'PQclientEncoding';
  73. function PQsetClientEncoding(conn:PPGconn; encoding:PAnsiChar):longint;cdecl;external External_library name 'PQsetClientEncoding';
  74. {$ifdef USE_SSL}
  75. { Get the SSL structure associated with a connection }
  76. function PQgetssl(conn:PPGconn):PSSL;cdecl;external External_library name 'PQgetssl';
  77. {$endif}
  78. { Set verbosity for PQerrorMessage and PQresultErrorMessage }
  79. function PQsetErrorVerbosity(conn:PPGconn; verbosity:PGVerbosity):PGVerbosity;cdecl;external External_library name 'PQsetErrorVerbosity';
  80. { Enable/disable tracing }
  81. procedure PQtrace(conn:PPGconn; debug_port:PFILE);cdecl;external External_library name 'PQtrace';
  82. procedure PQuntrace(conn:PPGconn);cdecl;external External_library name 'PQuntrace';
  83. { Override default notice handling routines }
  84. function PQsetNoticeReceiver(conn:PPGconn; proc:PQnoticeReceiver; arg:pointer):PQnoticeReceiver;cdecl;external External_library name 'PQsetNoticeReceiver';
  85. function PQsetNoticeProcessor(conn:PPGconn; proc:PQnoticeProcessor; arg:pointer):PQnoticeProcessor;cdecl;external External_library name 'PQsetNoticeProcessor';
  86. { === in fe-exec.c === }
  87. { Simple synchronous query }
  88. function PQexec(conn:PPGconn; query:PAnsiChar):PPGresult;cdecl;external External_library name 'PQexec';
  89. function PQexecParams(conn:PPGconn; command:PAnsiChar; nParams:longint; paramTypes:POid; paramValues:PPAnsiChar;
  90. paramLengths:Plongint; paramFormats:Plongint; resultFormat:longint):PPGresult;cdecl;external External_library name 'PQexecParams';
  91. function PQexecPrepared(conn:PPGconn; stmtName:PAnsiChar; nParams:longint; paramValues:PPAnsiChar; paramLengths:Plongint;
  92. paramFormats:Plongint; resultFormat:longint):PPGresult;cdecl;external External_library name 'PQexecPrepared';
  93. function PQdescribePrepared(conn:PPGconn; stmtName:PAnsiChar):PPGresult;cdecl;external External_library name 'PQdescribePrepared';
  94. { Interface for multiple-result or asynchronous queries }
  95. function PQsendQuery(conn:PPGconn; query:PAnsiChar):longint;cdecl;external External_library name 'PQsendQuery';
  96. function PQsendQueryParams(conn:PPGconn; command:PAnsiChar; nParams:longint; paramTypes:POid; paramValues:PPAnsiChar;
  97. paramLengths:Plongint; paramFormats:Plongint; resultFormat:longint):longint;cdecl;external External_library name 'PQsendQueryParams';
  98. function PQsendQueryPrepared(conn:PPGconn; stmtName:PAnsiChar; nParams:longint; paramValues:PPAnsiChar; paramLengths:Plongint;
  99. paramFormats:Plongint; resultFormat:longint):longint;cdecl;external External_library name 'PQsendQueryPrepared';
  100. function PQgetResult(conn:PPGconn):PPGresult;cdecl;external External_library name 'PQgetResult';
  101. { Routines for managing an asynchronous query }
  102. function PQisBusy(conn:PPGconn):longint;cdecl;external External_library name 'PQisBusy';
  103. function PQconsumeInput(conn:PPGconn):longint;cdecl;external External_library name 'PQconsumeInput';
  104. { LISTEN/NOTIFY support }
  105. function PQnotifies(conn:PPGconn):PPGnotify;cdecl;external External_library name 'PQnotifies';
  106. { Routines for copy in/out }
  107. function PQputCopyData(conn:PPGconn; buffer:PAnsiChar; nbytes:longint):longint;cdecl;external External_library name 'PQputCopyData';
  108. function PQputCopyEnd(conn:PPGconn; errormsg:PAnsiChar):longint;cdecl;external External_library name 'PQputCopyEnd';
  109. function PQgetCopyData(conn:PPGconn; buffer:PPAnsiChar; async:longint):longint;cdecl;external External_library name 'PQgetCopyData';
  110. { Deprecated routines for copy in/out }
  111. function PQgetline(conn:PPGconn; _string:PAnsiChar; length:longint):longint;cdecl;external External_library name 'PQgetline';
  112. function PQputline(conn:PPGconn; _string:PAnsiChar):longint;cdecl;external External_library name 'PQputline';
  113. function PQgetlineAsync(conn:PPGconn; buffer:PAnsiChar; bufsize:longint):longint;cdecl;external External_library name 'PQgetlineAsync';
  114. function PQputnbytes(conn:PPGconn; buffer:PAnsiChar; nbytes:longint):longint;cdecl;external External_library name 'PQputnbytes';
  115. function PQendcopy(conn:PPGconn):longint;cdecl;external External_library name 'PQendcopy';
  116. { Set blocking/nonblocking connection to the backend }
  117. function PQsetnonblocking(conn:PPGconn; arg:longint):longint;cdecl;external External_library name 'PQsetnonblocking';
  118. function PQisnonblocking(conn:PPGconn):longint;cdecl;external External_library name 'PQisnonblocking';
  119. { Force the write buffer to be written (or at least try) }
  120. function PQflush(conn:PPGconn):longint;cdecl;external External_library name 'PQflush';
  121. {
  122. * "Fast path" interface --- not really recommended for application
  123. * use
  124. }
  125. function PQfn(conn:PPGconn; fnid:longint; result_buf:Plongint; result_len:Plongint; result_is_int:longint;
  126. args:PPQArgBlock; nargs:longint):PPGresult;cdecl;external External_library name 'PQfn';
  127. { Accessor functions for PGresult objects }
  128. function PQresultStatus(res:PPGresult):TExecStatusType;cdecl;external External_library name 'PQresultStatus';
  129. function PQresStatus(status:TExecStatusType):PAnsiChar;cdecl;external External_library name 'PQresStatus';
  130. function PQresultErrorMessage(res:PPGresult):PAnsiChar;cdecl;external External_library name 'PQresultErrorMessage';
  131. function PQresultErrorField(res:PPGresult; fieldcode:longint):PAnsiChar;cdecl;external External_library name 'PQresultErrorField';
  132. function PQntuples(res:PPGresult):longint;cdecl;external External_library name 'PQntuples';
  133. function PQnfields(res:PPGresult):longint;cdecl;external External_library name 'PQnfields';
  134. function PQbinaryTuples(res:PPGresult):longint;cdecl;external External_library name 'PQbinaryTuples';
  135. function PQfname(res:PPGresult; field_num:longint):PAnsiChar;cdecl;external External_library name 'PQfname';
  136. function PQfnumber(res:PPGresult; field_name:PAnsiChar):longint;cdecl;external External_library name 'PQfnumber';
  137. function PQftable(res:PPGresult; field_num:longint):Oid;cdecl;external External_library name 'PQftable';
  138. function PQftablecol(res:PPGresult; field_num:longint):longint;cdecl;external External_library name 'PQftablecol';
  139. function PQfformat(res:PPGresult; field_num:longint):longint;cdecl;external External_library name 'PQfformat';
  140. function PQftype(res:PPGresult; field_num:longint):Oid;cdecl;external External_library name 'PQftype';
  141. function PQfsize(res:PPGresult; field_num:longint):longint;cdecl;external External_library name 'PQfsize';
  142. function PQfmod(res:PPGresult; field_num:longint):longint;cdecl;external External_library name 'PQfmod';
  143. function PQcmdStatus(res:PPGresult):PAnsiChar;cdecl;external External_library name 'PQcmdStatus';
  144. function PQoidStatus(res:PPGresult):PAnsiChar;cdecl;external External_library name 'PQoidStatus';
  145. { old and ugly }
  146. function PQoidValue(res:PPGresult):Oid;cdecl;external External_library name 'PQoidValue';
  147. { new and improved }
  148. function PQcmdTuples(res:PPGresult):PAnsiChar;cdecl;external External_library name 'PQcmdTuples';
  149. function PQgetvalue(res:PPGresult; tup_num:longint; field_num:longint):PAnsiChar;cdecl;external External_library name 'PQgetvalue';
  150. function PQgetlength(res:PPGresult; tup_num:longint; field_num:longint):longint;cdecl;external External_library name 'PQgetlength';
  151. function PQgetisnull(res:PPGresult; tup_num:longint; field_num:longint):longint;cdecl;external External_library name 'PQgetisnull';
  152. { Delete a PGresult }
  153. procedure PQclear(res:PPGresult);cdecl;external External_library name 'PQclear';
  154. { For freeing other alloc'd results, such as PGnotify structs }
  155. procedure PQfreemem(ptr:pointer);cdecl;external External_library name 'PQfreemem';
  156. { Exists for backward compatibility. bjm 2003-03-24 }
  157. { was #define dname(params) para_def_expr }
  158. { argument types are unknown }
  159. { return type might be wrong }
  160. // function PQfreeNotify(ptr : longint) : longint;
  161. {
  162. * Make an empty PGresult with given status (some apps find this
  163. * useful). If conn is not NULL and status indicates an error, the
  164. * conn's errorMessage is copied.
  165. }
  166. function PQmakeEmptyPGresult(conn:PPGconn; status:TExecStatusType):PPGresult;cdecl;external External_library name 'PQmakeEmptyPGresult';
  167. { Quoting strings before inclusion in queries. }
  168. function PQescapeString(till:PAnsiChar; from:PAnsiChar; length:size_t):size_t;cdecl;external External_library name 'PQescapeString';
  169. function PQescapeBytea(bintext:Pbyte; binlen:size_t; bytealen:Psize_t):Pbyte;cdecl;external External_library name 'PQescapeBytea';
  170. function PQunescapeBytea(strtext:Pbyte; retbuflen:Psize_t):Pbyte;cdecl;external External_library name 'PQunescapeBytea';
  171. { === in fe-print.c === }
  172. { output stream }
  173. procedure PQprint(fout:PFILE; res:PPGresult; ps:PPQprintOpt);cdecl;external External_library name 'PQprint';
  174. { option structure }
  175. {
  176. * really old printing routines
  177. }
  178. { where to send the output }
  179. { pad the fields with spaces }
  180. { field separator }
  181. { display headers? }
  182. procedure PQdisplayTuples(res:PPGresult; fp:PFILE; fillAlign:longint; fieldSep:PAnsiChar; printHeader:longint;
  183. quiet:longint);cdecl;external External_library name 'PQdisplayTuples';
  184. (* Const before type ignored *)
  185. { output stream }
  186. { print attribute names }
  187. { delimiter bars }
  188. procedure PQprintTuples(res:PPGresult; fout:PFILE; printAttName:longint; terseOutput:longint; width:longint);cdecl;external External_library name 'PQprintTuples';
  189. { width of column, if 0, use variable
  190. * width }
  191. { === in fe-lobj.c === }
  192. { Large-object access routines }
  193. function lo_open(conn:PPGconn; lobjId:Oid; mode:longint):longint;cdecl;external External_library name 'lo_open';
  194. function lo_close(conn:PPGconn; fd:longint):longint;cdecl;external External_library name 'lo_close';
  195. function lo_read(conn:PPGconn; fd:longint; buf:PAnsiChar; len:size_t):longint;cdecl;external External_library name 'lo_read';
  196. function lo_write(conn:PPGconn; fd:longint; buf:PAnsiChar; len:size_t):longint;cdecl;external External_library name 'lo_write';
  197. function lo_lseek(conn:PPGconn; fd:longint; offset:longint; whence:longint):longint;cdecl;external External_library name 'lo_lseek';
  198. function lo_creat(conn:PPGconn; mode:longint):Oid;cdecl;external External_library name 'lo_creat';
  199. function lo_tell(conn:PPGconn; fd:longint):longint;cdecl;external External_library name 'lo_tell';
  200. function lo_unlink(conn:PPGconn; lobjId:Oid):longint;cdecl;external External_library name 'lo_unlink';
  201. function lo_import(conn:PPGconn; filename:PAnsiChar):Oid;cdecl;external External_library name 'lo_import';
  202. function lo_export(conn:PPGconn; lobjId:Oid; filename:PAnsiChar):longint;cdecl;external External_library name 'lo_export';
  203. { === in fe-misc.c === }
  204. { Determine length of multibyte encoded AnsiChar at *s }
  205. function PQmblen(s:Pbyte; encoding:longint):longint;cdecl;external External_library name 'PQmblen';
  206. { Get encoding id from environment variable PGCLIENTENCODING }
  207. function PQenv2encoding:longint;cdecl;external External_library name 'PQenv2encoding';
  208. implementation
  209. { was #define dname(params) para_def_expr }
  210. { argument types are unknown }
  211. { return type might be wrong }
  212. // This function is also defined in postgres3dyn!
  213. function PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME : PAnsiChar) : ppgconn;
  214. begin
  215. PQsetdb:=PQsetdbLogin(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME,'','');
  216. end;
  217. { was #define dname(params) para_def_expr }
  218. { argument types are unknown }
  219. { return type might be wrong }
  220. { function PQfreeNotify(ptr : longint) : longint;
  221. begin
  222. PQfreeNotify:=PQfreemem(ptr);
  223. end;}
  224. end.