postgres.pp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. unit postgres;
  2. interface
  3. uses dllist;
  4. {$linklib pq}
  5. {$linklib c}
  6. { Not always needed. If you have problems linking, try to add this }
  7. { $linklib crypt}
  8. { $include "libpq/pqcomm.h"}
  9. Type
  10. Oid = cardinal;
  11. MsgType = Cardinal;
  12. PLongint = ^Longint;
  13. TSockAddr = Array [1..112] of byte; { Testded using C version sizeof() }
  14. Const
  15. NAMEDATALEN = 32;
  16. OIDNAMELEN = 36;
  17. Type
  18. TFILE = Longint;
  19. PFIle = ^TFILE;
  20. type
  21. TConnStatusType = (CONNECTION_OK,CONNECTION_BAD);
  22. PConnStatusType= ^TConnStatusType;
  23. TExecStatusType = (PGRES_EMPTY_QUERY,PGRES_COMMAND_OK,PGRES_TUPLES_OK,
  24. PGRES_COPY_OUT,
  25. PGRES_COPY_IN,
  26. PGRES_BAD_RESPONSE,
  27. PGRES_NONFATAL_ERROR,
  28. PGRES_FATAL_ERROR
  29. );
  30. PExecStatusType= ^TExecStatusType;
  31. {
  32. extern const char pgresStatus[];
  33. }
  34. const
  35. ERROR_MSG_LENGTH = 4096;
  36. COMMAND_LENGTH = 20;
  37. REMARK_LENGTH = 80;
  38. PORTAL_NAME_LENGTH = 16;
  39. type
  40. TPQArgBlock = record
  41. len : longint;
  42. isint : longint;
  43. u : record
  44. case longint of
  45. 0 : ( ptr:Plongint );
  46. 1 : ( integer:longint );
  47. end;
  48. end;
  49. PPQArgBlock= ^TPQArgBlock;
  50. TPGresAttDesc = record
  51. name : Pchar;
  52. adtid : Oid;
  53. adtsize : integer;
  54. end;
  55. PPGresAttDesc= ^TPGresAttDesc;
  56. PPPGresAttDesc= ^PPGresAttDesc;
  57. const
  58. NULL_LEN = -1;
  59. type
  60. TPGresAttValue = record
  61. len : longint;
  62. value : Pchar;
  63. end;
  64. PPGresAttValue= ^TPGresAttValue;
  65. PPPGresAttValue= ^PPGresAttValue;
  66. TPGnotify = record
  67. relname : array[0..(NAMEDATALEN)-1] of char;
  68. be_pid : longint;
  69. end;
  70. PPGnotify= ^TPGnotify;
  71. TPGlobjfuncs = record
  72. fn_lo_open : Oid;
  73. fn_lo_close : Oid;
  74. fn_lo_creat : Oid;
  75. fn_lo_unlink : Oid;
  76. fn_lo_lseek : Oid;
  77. fn_lo_tell : Oid;
  78. fn_lo_read : Oid;
  79. fn_lo_write : Oid;
  80. end;
  81. PPGlobjfuncs= ^TPGlobjfuncs;
  82. TPGconn = record
  83. pghost : Pchar;
  84. pgtty : Pchar;
  85. pgport : Pchar;
  86. pgoptions : Pchar;
  87. dbName : Pchar;
  88. status : TConnStatusType;
  89. errorMessage : array[0..(ERROR_MSG_LENGTH)-1] of char;
  90. Pfin : PFILE;
  91. Pfout : PFILE;
  92. Pfdebug : PFILE;
  93. sock : longint;
  94. laddr : TSockAddr;
  95. raddr : TSockAddr;
  96. salt : array[0..(2)-1] of char;
  97. asyncNotifyWaiting : longint;
  98. notifyList : PDllist;
  99. pguser : Pchar;
  100. pgpass : Pchar;
  101. lobjfuncs : PPGlobjfuncs;
  102. end;
  103. PPGconn= ^TPGconn;
  104. const
  105. CMDSTATUS_LEN = 40;
  106. type
  107. TPGresult = record
  108. ntups : longint;
  109. numAttributes : longint;
  110. attDescs : PPGresAttDesc;
  111. tuples : PPPGresAttValue;
  112. tupArrSize : longint;
  113. resultStatus : TExecStatusType;
  114. cmdStatus : array[0..(CMDSTATUS_LEN)-1] of char;
  115. binary : longint;
  116. conn : PPGconn;
  117. end;
  118. PPGresult= ^TPGresult;
  119. pqbool = char;
  120. TPQprintopt = record
  121. header : pqbool;
  122. align : pqbool;
  123. standard : pqbool;
  124. html3 : pqbool;
  125. expanded : pqbool;
  126. pager : pqbool;
  127. fieldSep : Pchar;
  128. tableOpt : Pchar;
  129. caption : Pchar;
  130. end;
  131. PPQprintopt= ^TPQprintopt;
  132. TPQconninfoOption = Record
  133. keyword : pchar;
  134. environ : pchar;
  135. compiled : pchar;
  136. val : pchar;
  137. Thelabel : pchar;
  138. dispchar : pchar;
  139. dispsize : longint;
  140. end;
  141. PPQconninfoOption = ^TPQconninfoOption;
  142. const
  143. MAX_MESSAGE_LEN = 8193;
  144. BYTELEN = 8;
  145. MAX_FIELDS = 512;
  146. DefaultHost : pchar = 'localhost';
  147. DefaultTty : pchar = '';
  148. DefaultOption : pchar = '';
  149. DefaultAuthtype : pchar = '';
  150. DefaultPassword : pchar = '';
  151. type
  152. TTUPLE = pointer;
  153. PTUPLE = ^TTUPLE;
  154. function PQconnectdb(conninfo:Pchar):PPGconn;cdecl; external;
  155. function PQconndefaults:PPQconninfoOption;cdecl; external;
  156. function PQsetdbLogin(pghost,pgport,pgoptions,pgtty,dbName,login,pwd : pchar):PPGConn;cdecl;external;
  157. procedure PQfinish(conn:PPGconn);cdecl; external;
  158. procedure PQreset(conn:PPGconn);cdecl; external;
  159. function PQdb(conn:PPGconn):Pchar;cdecl; external;
  160. function PQuser(conn:PPGconn):Pchar;cdecl; external;
  161. function PQhost(conn:PPGconn):Pchar;cdecl; external;
  162. function PQoptions(conn:PPGconn):Pchar;cdecl; external;
  163. function PQport(conn:PPGconn):Pchar;cdecl; external;
  164. function PQtty(conn:PPGconn):Pchar;cdecl; external;
  165. function PQstatus(conn:PPGconn):TConnStatusType;cdecl; external;
  166. function PQerrorMessage(conn:PPGconn):Pchar;cdecl; external;
  167. procedure PQtrace(conn:PPGconn; debug_port:PFILE);cdecl; external;
  168. procedure PQuntrace(conn:PPGconn);cdecl; external;
  169. function PQexec(conn:PPGconn; query:Pchar):PPGresult;cdecl; external;
  170. function PQgetline(conn:PPGconn; str:Pchar; len:longint):longint;cdecl; external;
  171. function PQendcopy(conn:PPGconn):longint;cdecl; external;
  172. function PQputline(conn:PPGconn; str:Pchar) : longint;cdecl; external;
  173. function PQresultStatus(res:PPGresult):TExecStatusType;cdecl; external;
  174. function PQntuples(res:PPGresult):longint;cdecl; external;
  175. function PQnfields(res:PPGresult):longint;cdecl; external;
  176. function PQfname(res:PPGresult; field_num:longint):Pchar;cdecl; external;
  177. function PQfnumber(res:PPGresult; field_name:Pchar):longint;cdecl; external;
  178. function PQftype(res:PPGresult; field_num:longint):Oid;cdecl; external;
  179. function PQfsize(res:PPGresult; field_num:longint):integer;cdecl; external;
  180. function PQcmdStatus(res:PPGresult):Pchar;cdecl; external;
  181. function PQgetvalue(res:PPGresult; tup_num:longint; field_num:longint):Pchar;cdecl; external;
  182. function PQgetlength(res:PPGresult; tup_num:longint; field_num:longint):longint;cdecl; external;
  183. function PQgetisnull(res:PPGresult; tup_num:longint; field_num:longint):longint;cdecl; external;
  184. procedure PQclear(res:PPGresult);cdecl; external;
  185. procedure PQdisplayTuples(res:PPGresult; fp:PFILE; fillAlign:longint; fieldSep:Pchar; printHeader:longint; quiet:longint);cdecl; external;
  186. procedure PQprintTuples(res:PPGresult; fout:PFILE; printAttName:longint; terseOutput:longint; width:longint);cdecl; external;
  187. procedure PQprint(fout:PFILE; res:PPGresult; ps:PPQprintOpt);cdecl; external;
  188. function PQnotifies(conn:PPGconn):PPGnotify;cdecl; external;
  189. function PQfn(conn:PPGconn; fnid:longint; result_buf:Plongint; result_len:Plongint; result_is_int:longint; args:PPQArgBlock; nargs:longint):PPGresult;cdecl; external;
  190. function fe_getauthsvc(PQerrormsg:Pchar):MsgType;cdecl; external;
  191. procedure fe_setauthsvc(name:Pchar; PQerrormsg:Pchar);cdecl; external;
  192. function fe_getauthname(PQerrormsg:Pchar):Pchar;cdecl; external;
  193. function pqGets(s:Pchar; maxlen:longint; stream:PFILE; debug:PFILE):longint;cdecl; external;
  194. function pqGetnchar(s:Pchar; maxlen:longint; stream:PFILE; debug:PFILE):longint;cdecl; external;
  195. function pqPutnchar(s:Pchar; maxlen:longint; stream:PFILE; debug:PFILE):longint;cdecl; external;
  196. function pqPuts(s:Pchar; stream:PFILE; debug:PFILE):longint;cdecl; external;
  197. function pqGetc(stream:PFILE; debug:PFILE):longint;cdecl; external;
  198. function pqGetInt(result:Plongint; bytes:longint; stream:PFILE; debug:PFILE):longint;cdecl; external;
  199. function pqPutInt(n:longint; bytes:longint; stream:PFILE; debug:PFILE):longint;cdecl; external;
  200. procedure pqFlush(stream:PFILE; debug:PFILE);cdecl; external;
  201. function PQoidStatus(res : PPGresult) : pchar;cdecl;external;
  202. function PQcmdTuples(res : PPGresult) : pchar;cdecl;external;
  203. function lo_open(conn:PPGconn; lobjId:Oid; mode:longint):longint; cdecl; external;
  204. function lo_close(conn:PPGconn; fd:longint):longint; cdecl; external;
  205. function lo_read(conn:PPGconn; fd:longint; buf:Pchar; len:longint):longint; cdecl; external;
  206. function lo_write(conn:PPGconn; fd:longint; buf:Pchar; len:longint):longint; cdecl; external;
  207. function lo_lseek(conn:PPGconn; fd:longint; offset:longint; whence:longint):longint; cdecl; external;
  208. function lo_creat(conn:PPGconn; mode:longint):Oid;cdecl;external;
  209. function lo_tell(conn:PPGconn; fd:longint):longint; cdecl; external;
  210. function lo_unlink(conn:PPGconn; lobjId:Oid):longint; cdecl; external;
  211. function lo_import(conn:PPGconn; filename:Pchar):Oid;cdecl;external;
  212. function lo_export(conn:PPGconn; lobjId:Oid; filename:Pchar):longint; cdecl; external;
  213. {$ifdef PGSQL6_2_1}
  214. Function PQsetdb(pghost,pgport,pgoptions,pgtty,dbName : pchar):PPGConn; cdecl;external;
  215. {$else}
  216. function PQsetdb(pghost,pgport,pgoptions,pgtty,dbName : pchar):PPGConn;
  217. {$endif}
  218. implementation
  219. { Define helper functions }
  220. {
  221. In version 6.2.xxx, PGsetdb is a function in libpq.
  222. in version 6.3.xxx, PGsetdb is a macro, pointing to setdblogin !!
  223. }
  224. {$ifndef PGSQL6_2_1}
  225. function PQsetdb(pghost,pgport,pgoptions,pgtty,dbName : pchar):PPGConn;
  226. begin
  227. PQsetdb:=PQsetdbLogin(pghost,pgport,pgoptions,pgtty,dbName,nil,nil);
  228. end;
  229. {$endif}
  230. end.