2
0

postgres.pp 9.2 KB

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