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