00001 {*********************************************************}
00002 { }
00003 { Zeos Database Objects }
00004 { Plain interface to libpq.dll }
00005 { Version 8.1 }
00006 { }
00007 { Originally written by Sergey Seroukhov }
00008 { }
00009 {*********************************************************}
00010
00011 {@********************************************************}
00012 { Copyright (c) 1999-2006 Zeos Development Group }
00013 { }
00014 { License Agreement: }
00015 { }
00016 { This library is distributed in the hope that it will be }
00017 { useful, but WITHOUT ANY WARRANTY; without even the }
00018 { implied warranty of MERCHANTABILITY or FITNESS FOR }
00019 { A PARTICULAR PURPOSE. See the GNU Lesser General }
00020 { Public License for more details. }
00021 { }
00022 { The source code of the ZEOS Libraries and packages are }
00023 { distributed under the Library GNU General Public }
00024 { License (see the file COPYING / COPYING.ZEOS) }
00025 { with the following modification: }
00026 { As a special exception, the copyright holders of this }
00027 { library give you permission to link this library with }
00028 { independent modules to produce an executable, }
00029 { regardless of the license terms of these independent }
00030 { modules, and to copy and distribute the resulting }
00031 { executable under terms of your choice, provided that }
00032 { you also meet, for each linked independent module, }
00033 { the terms and conditions of the license of that module. }
00034 { An independent module is a module which is not derived }
00035 { from or based on this library. If you modify this }
00036 { library, you may extend this exception to your version }
00037 { of the library, but you are not obligated to do so. }
00038 { If you do not wish to do so, delete this exception }
00039 { statement from your version. }
00040 { }
00041 { }
00042 { The project web site is located on: }
00043 { http:
00044 { http:
00045 { svn:
00046 { }
00047 { http:
00048 { http:
00049 { }
00050 { }
00051 { }
00052 { Zeos Development Group. }
00053 {********************************************************@}
00054
00055 unit ZPlainPostgreSql8;
00056
00057 interface
00058
00059 {$I ZPlain.inc}
00060
00061 {$J+}
00062
00063 uses ZCompatibility, ZPlainLoader;
00064
00065 { ***************** Plain API Constants definition **************** }
00066
00067 const
00068 WINDOWS1_DLL_LOCATION = 'libpq81.dll';
00069 {$IFNDEF STRICT_DLL_LOADING}
00070 WINDOWS2_DLL_LOCATION = 'libpq.dll';
00071 {$ENDIF}
00072 LINUX1_DLL_LOCATION = 'libpq.so.4';
00073 LINUX2_DLL_LOCATION = 'libpq.so';
00074
00075 { Type Lengths }
00076 NAMEDATALEN = 32;
00077 { OIDNAMELEN should be set to NAMEDATALEN + sizeof(Oid) }
00078 OIDNAMELEN = 36;
00079
00080 INV_WRITE = $00020000;
00081 INV_READ = $00040000;
00082
00083 BLOB_SEEK_SET = 0;
00084 BLOB_SEEK_CUR = 1;
00085 BLOB_SEEK_END = 2;
00086
00087
00088 { ****************** Plain API Types definition ***************** }
00089
00090 type
00091 Oid = Integer;
00092
00093 { Application-visible enum types }
00094 ConnStatusType = (
00095 CONNECTION_OK,
00096 CONNECTION_BAD
00097 );
00098
00099 ExecStatusType = (
00100 PGRES_EMPTY_QUERY,
00101 PGRES_COMMAND_OK, { a query command that doesn't return anything
00102 was executed properly by the backend }
00103 PGRES_TUPLES_OK, { a query command that returns tuples
00104 was executed properly by the backend,
00105 PGresult contains the result tuples }
00106 PGRES_COPY_OUT, { Copy Out data transfer in progress }
00107 PGRES_COPY_IN, { Copy In data transfer in progress }
00108 PGRES_BAD_RESPONSE, { an unexpected response was recv'd from
00109 the backend }
00110 PGRES_NONFATAL_ERROR,
00111 PGRES_FATAL_ERROR
00112 );
00113
00114 { String descriptions of the ExecStatusTypes }
00115 pgresStatus = array[$00..$ff] of PChar;
00116
00117 { PGconn encapsulates a connection to the backend.
00118 The contents of this struct are not supposed to be known to applications.
00119 }
00120 PGconn = Pointer;
00121 PPGconn = Pointer;
00122
00123 { PGresult encapsulates the result of a query (or more precisely, of a single
00124 SQL command --- a query string given to PQsendQuery can contain multiple
00125 commands and thus return multiple PGresult objects).
00126 The contents of this struct are not supposed to be known to applications.
00127 }
00128 PGresult = Pointer;
00129 PPGresult = Pointer;
00130
00131 { PGnotify represents the occurrence of a NOTIFY message.
00132 Ideally this would be an opaque typedef, but it's so simple that it's
00133 unlikely to change.
00134 NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
00135 whereas in earlier versions it was always your own backend's PID.
00136 }
00137 PGnotify = packed record
00138 relname: array [0..NAMEDATALEN-1] of Char; { name of relation containing data }
00139 be_pid: Integer; { process id of backend }
00140 end;
00141
00142 PPGnotify = ^PGnotify;
00143
00144 { PQnoticeProcessor is the function type for the notice-message callback. }
00145
00146 PQnoticeProcessor = procedure(arg: Pointer; message: PChar); cdecl;
00147
00148 { Print options for PQprint() }
00149
00150 {
00151 We can't use the conventional "bool", because we are designed to be
00152 included in a user's program, and user may already have that type
00153 defined. Pqbool, on the other hand, is unlikely to be used.
00154 }
00155
00156 PPChar = array[00..$ff] of PChar;
00157
00158 PQprintOpt = packed record
00159 header: Byte; { print output field headings and row count }
00160 align: Byte; { fill align the fields }
00161 standard: Byte; { old brain dead format }
00162 html3: Byte; { output html tables }
00163 expanded: Byte; { expand tables }
00164 pager: Byte; { use pager for output if needed }
00165 fieldSep: PChar; { field separator }
00166 tableOpt: PChar; { insert to HTML <table ...> }
00167 caption: PChar; { HTML <caption> }
00168 fieldName: PPChar; { null terminated array of repalcement field names }
00169 end;
00170
00171 PPQprintOpt = ^PQprintOpt;
00172
00173 { ----------------
00174 Structure for the conninfo parameter definitions returned by PQconndefaults
00175 ----------------
00176 }
00177 PQconninfoOption = packed record
00178 keyword: PChar; { The keyword of the option }
00179 envvar: PChar; { Fallback environment variable name }
00180 compiled: PChar; { Fallback compiled in default value }
00181 val: PChar; { Options value }
00182 lab: PChar; { Label for field in connect dialog }
00183 dispchar: PChar; { Character to display for this field
00184 in a connect dialog. Values are:
00185 "" Display entered value as is
00186 "*" Password field - hide value
00187 "D" Debug options - don't
00188 create a field by default }
00189 dispsize: Integer; { Field size in characters for dialog }
00190 end;
00191
00192 PPQConninfoOption = ^PQconninfoOption;
00193
00194 { ----------------
00195 PQArgBlock -- structure for PQfn() arguments
00196 ----------------
00197 }
00198 PQArgBlock = packed record
00199 len: Integer;
00200 isint: Integer;
00201 case u: Boolean of
00202 True: (ptr: PInteger); { can't use void (dec compiler barfs) }
00203 False: (_int: Integer);
00204 end;
00205
00206 PPQArgBlock = ^PQArgBlock;
00207
00208
00209 { ************** Plain API Function types definition ************* }
00210
00211 { === in fe-connect.c === }
00212 TPQconnectdb = function(ConnInfo: PChar): PPGconn; cdecl;
00213 TPQsetdbLogin = function(Host, Port, Options, Tty, Db, User, Passwd: PChar): PPGconn; cdecl;
00214
00215
00216 TPQconndefaults = function: PPQconninfoOption; cdecl;
00217 TPQfinish = procedure(Handle: PPGconn); cdecl;
00218 TPQreset = procedure(Handle: PPGconn); cdecl;
00219
00220
00221
00222
00223 TPQrequestCancel = function(Handle: PPGconn): Integer; cdecl;
00224
00225 TPQdb = function(Handle: PPGconn): PChar; cdecl;
00226 TPQuser = function(Handle: PPGconn): PChar; cdecl;
00227 TPQpass = function(Handle: PPGconn): PChar; cdecl;
00228 TPQhost = function(Handle: PPGconn): PChar; cdecl;
00229 TPQport = function(Handle: PPGconn): PChar; cdecl;
00230 TPQtty = function(Handle: PPGconn): PChar; cdecl;
00231 TPQoptions = function(Handle: PPGconn): PChar; cdecl;
00232 TPQstatus = function(Handle: PPGconn): ConnStatusType; cdecl;
00233
00234
00235
00236
00237
00238
00239
00240
00241 TPQerrorMessage = function(Handle: PPGconn): PChar; cdecl;
00242 TPQsocket = function(Handle: PPGconn): Integer; cdecl;
00243 TPQbackendPID = function(Handle: PPGconn): Integer; cdecl;
00244
00245
00246
00247
00248 TPQtrace = procedure(Handle: PPGconn; DebugPort: Pointer); cdecl;
00249 TPQuntrace = procedure(Handle: PPGconn); cdecl;
00250 TPQsetNoticeProcessor = procedure(Handle: PPGconn; Proc: PQnoticeProcessor; Arg: Pointer); cdecl;
00251
00252 { === in fe-exec.c === }
00253 TPQexec = function(Handle: PPGconn; Query: PChar): PPGresult; cdecl;
00254 TPQnotifies = function(Handle: PPGconn): PPGnotify; cdecl;
00255 TPQfreeNotify = procedure(Handle: PPGnotify);cdecl;
00256 TPQsendQuery = function(Handle: PPGconn; Query: PChar): Integer; cdecl;
00257 TPQgetResult = function(Handle: PPGconn): PPGresult; cdecl;
00258 TPQisBusy = function(Handle: PPGconn): Integer; cdecl;
00259 TPQconsumeInput = function(Handle: PPGconn): Integer; cdecl;
00260 TPQgetline = function(Handle: PPGconn; Str: PChar; length: Integer): Integer; cdecl;
00261 TPQputline = function(Handle: PPGconn; Str: PChar): Integer; cdecl;
00262 TPQgetlineAsync = function(Handle: PPGconn; Buffer: PChar; BufSize: Integer): Integer; cdecl;
00263 TPQputnbytes = function(Handle: PPGconn; Buffer: PChar; NBytes: Integer): Integer; cdecl;
00264 TPQendcopy = function(Handle: PPGconn): Integer; cdecl;
00265 TPQfn = function(Handle: PPGconn; fnid: Integer; result_buf, result_len: PInteger; result_is_int: Integer; args: PPQArgBlock; nargs: Integer): PPGresult; cdecl;
00266 TPQresultStatus = function(Result: PPGresult): ExecStatusType; cdecl;
00267 TPQresultErrorMessage = function(Result: PPGresult): PChar; cdecl;
00268
00269 TPQresultErrorField=function(result: PPGResult; fieldcode:integer):PChar;cdecl;
00270
00271 TPQntuples = function(Result: PPGresult): Integer; cdecl;
00272 TPQnfields = function(Result: PPGresult): Integer; cdecl;
00273 TPQbinaryTuples = function(Result: PPGresult): Integer; cdecl;
00274 TPQfname = function(Result: PPGresult; field_num: Integer): PChar; cdecl;
00275 TPQfnumber = function(Result: PPGresult; field_name: PChar): Integer; cdecl;
00276 TPQftype = function(Result: PPGresult; field_num: Integer): Oid; cdecl;
00277 TPQfsize = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
00278 TPQfmod = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
00279 TPQcmdStatus = function(Result: PPGresult): PChar; cdecl;
00280 TPQoidValue = function(Result: PPGresult): Oid; cdecl;
00281 TPQoidStatus = function(Result: PPGresult): PChar; cdecl;
00282 TPQcmdTuples = function(Result: PPGresult): PChar; cdecl;
00283 TPQgetvalue = function(Result: PPGresult; tup_num, field_num: Integer): PChar; cdecl;
00284 TPQgetlength = function(Result: PPGresult; tup_num, field_num: Integer): Integer; cdecl;
00285 TPQgetisnull = function(Result: PPGresult; tup_num, field_num: Integer): Integer; cdecl;
00286 TPQclear = procedure(Result: PPGresult); cdecl;
00287 TPQmakeEmptyPGresult = function(Handle: PPGconn; status: ExecStatusType): PPGresult; cdecl;
00288
00289
00290
00291 TPQescapeByteaConn =function(Handle: PPGconn;const from:pchar;from_length:longword;to_lenght:PLongword):PChar;cdecl;
00292 TPQescapeBytea =function(const from:pchar;from_length:longword;to_lenght:PLongword):PChar;cdecl;
00293
00294
00295
00296
00297
00298
00299
00300 TPQunescapeBytea =function(const from:pchar;to_lenght:PLongword):PChar;cdecl;
00301
00302
00303 TPQFreemem = procedure(ptr:Pointer);cdecl;
00304
00305
00306 { === in fe-lobj.c === }
00307 Tlo_open = function(Handle: PPGconn; lobjId: Oid; mode: Integer): Integer; cdecl;
00308 Tlo_close = function(Handle: PPGconn; fd: Integer): Integer; cdecl;
00309 Tlo_read = function(Handle: PPGconn; fd: Integer; buf: PChar; len: Integer): Integer; cdecl;
00310 Tlo_write = function(Handle: PPGconn; fd: Integer; buf: PChar; len: Integer): Integer; cdecl;
00311 Tlo_lseek = function(Handle: PPGconn; fd, offset, whence: Integer): Integer; cdecl;
00312 Tlo_creat = function(Handle: PPGconn; mode: Integer): Oid; cdecl;
00313 Tlo_tell = function(Handle: PPGconn; fd: Integer): Integer; cdecl;
00314 Tlo_unlink = function(Handle: PPGconn; lobjId: Oid): Integer; cdecl;
00315 Tlo_import = function(Handle: PPGconn; filename: PChar): Oid; cdecl;
00316 Tlo_export = function(Handle: PPGconn; lobjId: Oid; filename: PChar): Integer; cdecl;
00317
00318
00319 { ************* Plain API Function variables definition ************ }
00320
00321 var
00322 { === in fe-connect.c === }
00323 PQconnectdb: TPQconnectdb;
00324 PQsetdbLogin: TPQsetdbLogin;
00325 PQconndefaults: TPQconndefaults;
00326 PQfinish: TPQfinish;
00327 PQreset: TPQreset;
00328 PQrequestCancel: TPQrequestCancel;
00329 PQdb: TPQdb;
00330 PQuser: TPQuser;
00331 PQpass: TPQpass;
00332 PQhost: TPQhost;
00333 PQport: TPQport;
00334 PQtty: TPQtty;
00335 PQoptions: TPQoptions;
00336 PQstatus: TPQstatus;
00337 PQerrorMessage: TPQerrorMessage;
00338 PQsocket: TPQsocket;
00339 PQbackendPID: TPQbackendPID;
00340 PQtrace: TPQtrace;
00341 PQuntrace: TPQuntrace;
00342 PQsetNoticeProcessor: TPQsetNoticeProcessor;
00343
00344 { === in fe-exec.c === }
00345 PQexec: TPQexec;
00346 PQnotifies: TPQnotifies;
00347 PQfreeNotify: TPQfreeNotify;
00348 PQsendQuery: TPQsendQuery;
00349 PQgetResult: TPQgetResult;
00350 PQisBusy: TPQisBusy;
00351 PQconsumeInput: TPQconsumeInput;
00352 PQgetline: TPQgetline;
00353 PQputline: TPQputline;
00354 PQgetlineAsync: TPQgetlineAsync;
00355 PQputnbytes: TPQputnbytes;
00356 PQendcopy: TPQendcopy;
00357 PQfn: TPQfn;
00358 PQresultStatus: TPQresultStatus;
00359 PQresultErrorMessage: TPQresultErrorMessage;
00360 PQresultErrorField: TPQresultErrorField;
00361 PQntuples: TPQntuples;
00362 PQnfields: TPQnfields;
00363 PQbinaryTuples: TPQbinaryTuples;
00364 PQfname: TPQfname;
00365 PQfnumber: TPQfnumber;
00366 PQftype: TPQftype;
00367 PQfsize: TPQfsize;
00368 PQfmod: TPQfmod;
00369 PQcmdStatus: TPQcmdStatus;
00370 PQoidValue: TPQoidValue;
00371 PQoidStatus: TPQoidStatus;
00372 PQcmdTuples: TPQcmdTuples;
00373 PQgetvalue: TPQgetvalue;
00374 PQgetlength: TPQgetlength;
00375 PQgetisnull: TPQgetisnull;
00376 PQclear: TPQclear;
00377 PQmakeEmptyPGresult: TPQmakeEmptyPGresult;
00378
00379
00380 PQescapeByteaConn:TPQescapeByteaConn;
00381 PQescapeBytea:TPQescapeBytea;
00382 PQunescapeBytea:TPQunescapeBytea;
00383 PQFreemem:TPQFreemem;
00384
00385 { === in fe-lobj.c === }
00386 lo_open: Tlo_open;
00387 lo_close: Tlo_close;
00388 lo_read: Tlo_read;
00389 lo_write: Tlo_write;
00390 lo_lseek: Tlo_lseek;
00391 lo_creat: Tlo_creat;
00392 lo_tell: Tlo_tell;
00393 lo_unlink: Tlo_unlink;
00394 lo_import: Tlo_import;
00395 lo_export: Tlo_export;
00396
00397 var
00398 LibraryLoader: TZNativeLibraryLoader;
00399
00400 implementation
00401
00402 type
00403 {** Implements a loader for PostgreSQL native library. }
00404 TZPostgreSQLNativeLibraryLoader = class (TZNativeLibraryLoader)
00405 public
00406 function Load: Boolean; override;
00407 end;
00408
00409 { TZPostgreSQLNativeLibraryLoader }
00410
00411 {**
00412 Loads a library module.
00413 @return <code>True</code> if library was successfully loaded.
00414 }
00415 function TZPostgreSQLNativeLibraryLoader.Load: Boolean;
00416 begin
00417 Result := inherited Load;
00418
00419 { === in fe-connect.c === }
00420 @PQfreemem := GetAddress('PQfreemem');
00421 @PQescapeByteaConn := GetAddress('PQescapeByteaConn');
00422 @PQescapeBytea := GetAddress('PQescapeBytea');
00423 @PQunescapeBytea := GetAddress('PQunescapeBytea');
00424
00425 @PQconnectdb := GetAddress('PQconnectdb');
00426 @PQsetdbLogin := GetAddress('PQsetdbLogin');
00427 @PQconndefaults := GetAddress('PQconndefaults');
00428 @PQfinish := GetAddress('PQfinish');
00429 @PQreset := GetAddress('PQreset');
00430 @PQrequestCancel := GetAddress('PQrequestCancel');
00431 @PQdb := GetAddress('PQdb');
00432 @PQuser := GetAddress('PQuser');
00433 @PQpass := GetAddress('PQpass');
00434 @PQhost := GetAddress('PQhost');
00435 @PQport := GetAddress('PQport');
00436 @PQtty := GetAddress('PQtty');
00437 @PQoptions := GetAddress('PQoptions');
00438 @PQstatus := GetAddress('PQstatus');
00439 @PQerrorMessage := GetAddress('PQerrorMessage');
00440 @PQsocket := GetAddress('PQsocket');
00441 @PQbackendPID := GetAddress('PQbackendPID');
00442 @PQtrace := GetAddress('PQtrace');
00443 @PQuntrace := GetAddress('PQuntrace');
00444 @PQsetNoticeProcessor := GetAddress('PQsetNoticeProcessor');
00445
00446 { === in fe-exec.c === }
00447 @PQexec := GetAddress('PQexec');
00448 @PQnotifies := GetAddress('PQnotifies');
00449 @PQfreeNotify := GetAddress('PQfreeNotify');
00450 @PQsendQuery := GetAddress('PQsendQuery');
00451 @PQgetResult := GetAddress('PQgetResult');
00452 @PQisBusy := GetAddress('PQisBusy');
00453 @PQconsumeInput := GetAddress('PQconsumeInput');
00454 @PQgetline := GetAddress('PQgetline');
00455 @PQputline := GetAddress('PQputline');
00456 @PQgetlineAsync := GetAddress('PQgetlineAsync');
00457 @PQputnbytes := GetAddress('PQputnbytes');
00458 @PQendcopy := GetAddress('PQendcopy');
00459 @PQfn := GetAddress('PQfn');
00460 @PQresultStatus := GetAddress('PQresultStatus');
00461 @PQresultErrorMessage := GetAddress('PQresultErrorMessage');
00462 @PQresultErrorField := GetAddress('PQresultErrorField');
00463 @PQntuples := GetAddress('PQntuples');
00464 @PQnfields := GetAddress('PQnfields');
00465 @PQbinaryTuples := GetAddress('PQbinaryTuples');
00466 @PQfname := GetAddress('PQfname');
00467 @PQfnumber := GetAddress('PQfnumber');
00468 @PQftype := GetAddress('PQftype');
00469 @PQfsize := GetAddress('PQfsize');
00470 @PQfmod := GetAddress('PQfmod');
00471 @PQcmdStatus := GetAddress('PQcmdStatus');
00472 @PQoidValue := GetAddress('PQoidValue');
00473 @PQoidStatus := GetAddress('PQoidStatus');
00474 @PQcmdTuples := GetAddress('PQcmdTuples');
00475 @PQgetvalue := GetAddress('PQgetvalue');
00476 @PQgetlength := GetAddress('PQgetlength');
00477 @PQgetisnull := GetAddress('PQgetisnull');
00478 @PQclear := GetAddress('PQclear');
00479 @PQmakeEmptyPGresult := GetAddress('PQmakeEmptyPGresult');
00480
00481 { === in fe-lobj.c === }
00482 @lo_open := GetAddress('lo_open');
00483 @lo_close := GetAddress('lo_close');
00484 @lo_read := GetAddress('lo_read');
00485 @lo_write := GetAddress('lo_write');
00486 @lo_lseek := GetAddress('lo_lseek');
00487 @lo_creat := GetAddress('lo_creat');
00488 @lo_tell := GetAddress('lo_tell');
00489 @lo_unlink := GetAddress('lo_unlink');
00490 @lo_import := GetAddress('lo_import');
00491 @lo_export := GetAddress('lo_export');
00492 end;
00493
00494 initialization
00495 {$IFNDEF UNIX}
00496 LibraryLoader := TZPostgreSQLNativeLibraryLoader.Create(
00497 [WINDOWS1_DLL_LOCATION
00498 {$IFNDEF STRICT_DLL_LOADING}
00499 , WINDOWS2_DLL_LOCATION
00500 {$ENDIF}
00501 ]);
00502 {$ELSE}
00503 LibraryLoader := TZPostgreSQLNativeLibraryLoader.Create([LINUX1_DLL_LOCATION,LINUX2_DLL_LOCATION]);
00504 {$ENDIF}
00505 finalization
00506 if Assigned(LibraryLoader) then
00507 LibraryLoader.Free;
00508 end.