00001 {*********************************************************}
00002 { }
00003 { Zeos Database Objects }
00004 { Plain interface to libpq.dll }
00005 { Version 7.4 }
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 ZPlainPostgreSql7;
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 = 'libpq74.dll';
00069 {$IFNDEF STRICT_DLL_LOADING}
00070 WINDOWS2_DLL_LOCATION = 'libpq.dll';
00071 {$ENDIF}
00072 LINUX_DLL_LOCATION = 'libpq.so';
00073
00074 { Type Lengths }
00075 NAMEDATALEN = 32;
00076 { OIDNAMELEN should be set to NAMEDATALEN + sizeof(Oid) }
00077 OIDNAMELEN = 36;
00078
00079 INV_WRITE = $00020000;
00080 INV_READ = $00040000;
00081
00082 BLOB_SEEK_SET = 0;
00083 BLOB_SEEK_CUR = 1;
00084 BLOB_SEEK_END = 2;
00085
00086
00087 { ****************** Plain API Types definition ***************** }
00088
00089 type
00090 Oid = Integer;
00091
00092 { Application-visible enum types }
00093 ConnStatusType = (
00094 CONNECTION_OK,
00095 CONNECTION_BAD
00096 );
00097
00098 ExecStatusType = (
00099 PGRES_EMPTY_QUERY,
00100 PGRES_COMMAND_OK, { a query command that doesn't return anything
00101 was executed properly by the backend }
00102 PGRES_TUPLES_OK, { a query command that returns tuples
00103 was executed properly by the backend,
00104 PGresult contains the result tuples }
00105 PGRES_COPY_OUT, { Copy Out data transfer in progress }
00106 PGRES_COPY_IN, { Copy In data transfer in progress }
00107 PGRES_BAD_RESPONSE, { an unexpected response was recv'd from
00108 the backend }
00109 PGRES_NONFATAL_ERROR,
00110 PGRES_FATAL_ERROR
00111 );
00112
00113 { String descriptions of the ExecStatusTypes }
00114 pgresStatus = array[$00..$ff] of PChar;
00115
00116 { PGconn encapsulates a connection to the backend.
00117 The contents of this struct are not supposed to be known to applications.
00118 }
00119 PGconn = Pointer;
00120 PPGconn = Pointer;
00121
00122 { PGresult encapsulates the result of a query (or more precisely, of a single
00123 SQL command --- a query string given to PQsendQuery can contain multiple
00124 commands and thus return multiple PGresult objects).
00125 The contents of this struct are not supposed to be known to applications.
00126 }
00127 PGresult = Pointer;
00128 PPGresult = Pointer;
00129
00130 { PGnotify represents the occurrence of a NOTIFY message.
00131 Ideally this would be an opaque typedef, but it's so simple that it's
00132 unlikely to change.
00133 NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
00134 whereas in earlier versions it was always your own backend's PID.
00135 }
00136 PGnotify = packed record
00137 relname: array [0..NAMEDATALEN-1] of Char; { name of relation containing data }
00138 be_pid: Integer; { process id of backend }
00139 end;
00140
00141 PPGnotify = ^PGnotify;
00142
00143 { PQnoticeProcessor is the function type for the notice-message callback. }
00144
00145 PQnoticeProcessor = procedure(arg: Pointer; message: PChar); cdecl;
00146
00147 { Print options for PQprint() }
00148
00149 {
00150 We can't use the conventional "bool", because we are designed to be
00151 included in a user's program, and user may already have that type
00152 defined. Pqbool, on the other hand, is unlikely to be used.
00153 }
00154
00155 PPChar = array[00..$ff] of PChar;
00156
00157 PQprintOpt = packed record
00158 header: Byte; { print output field headings and row count }
00159 align: Byte; { fill align the fields }
00160 standard: Byte; { old brain dead format }
00161 html3: Byte; { output html tables }
00162 expanded: Byte; { expand tables }
00163 pager: Byte; { use pager for output if needed }
00164 fieldSep: PChar; { field separator }
00165 tableOpt: PChar; { insert to HTML <table ...> }
00166 caption: PChar; { HTML <caption> }
00167 fieldName: PPChar; { null terminated array of repalcement field names }
00168 end;
00169
00170 PPQprintOpt = ^PQprintOpt;
00171
00172 { ----------------
00173 Structure for the conninfo parameter definitions returned by PQconndefaults
00174 ----------------
00175 }
00176 PQconninfoOption = packed record
00177 keyword: PChar; { The keyword of the option }
00178 envvar: PChar; { Fallback environment variable name }
00179 compiled: PChar; { Fallback compiled in default value }
00180 val: PChar; { Options value }
00181 lab: PChar; { Label for field in connect dialog }
00182 dispchar: PChar; { Character to display for this field
00183 in a connect dialog. Values are:
00184 "" Display entered value as is
00185 "*" Password field - hide value
00186 "D" Debug options - don't
00187 create a field by default }
00188 dispsize: Integer; { Field size in characters for dialog }
00189 end;
00190
00191 PPQConninfoOption = ^PQconninfoOption;
00192
00193 { ----------------
00194 PQArgBlock -- structure for PQfn() arguments
00195 ----------------
00196 }
00197 PQArgBlock = packed record
00198 len: Integer;
00199 isint: Integer;
00200 case u: Boolean of
00201 True: (ptr: PInteger); { can't use void (dec compiler barfs) }
00202 False: (_int: Integer);
00203 end;
00204
00205 PPQArgBlock = ^PQArgBlock;
00206
00207
00208 { ************** Plain API Function types definition ************* }
00209
00210 { === in fe-connect.c === }
00211 TPQconnectdb = function(ConnInfo: PChar): PPGconn; cdecl;
00212 TPQsetdbLogin = function(Host, Port, Options, Tty, Db, User, Passwd: PChar): PPGconn; cdecl;
00213 TPQconndefaults = function: PPQconninfoOption; cdecl;
00214 TPQfinish = procedure(Handle: PPGconn); cdecl;
00215 TPQreset = procedure(Handle: PPGconn); cdecl;
00216 TPQrequestCancel = function(Handle: PPGconn): Integer; cdecl;
00217 TPQdb = function(Handle: PPGconn): PChar; cdecl;
00218 TPQuser = function(Handle: PPGconn): PChar; cdecl;
00219 TPQpass = function(Handle: PPGconn): PChar; cdecl;
00220 TPQhost = function(Handle: PPGconn): PChar; cdecl;
00221 TPQport = function(Handle: PPGconn): PChar; cdecl;
00222 TPQtty = function(Handle: PPGconn): PChar; cdecl;
00223 TPQoptions = function(Handle: PPGconn): PChar; cdecl;
00224 TPQstatus = function(Handle: PPGconn): ConnStatusType; cdecl;
00225 TPQerrorMessage = function(Handle: PPGconn): PChar; cdecl;
00226 TPQsocket = function(Handle: PPGconn): Integer; cdecl;
00227 TPQbackendPID = function(Handle: PPGconn): Integer; cdecl;
00228 TPQtrace = procedure(Handle: PPGconn; DebugPort: Pointer); cdecl;
00229 TPQuntrace = procedure(Handle: PPGconn); cdecl;
00230 TPQsetNoticeProcessor = procedure(Handle: PPGconn; Proc: PQnoticeProcessor; Arg: Pointer); cdecl;
00231
00232 { === in fe-exec.c === }
00233 TPQexec = function(Handle: PPGconn; Query: PChar): PPGresult; cdecl;
00234 TPQnotifies = function(Handle: PPGconn): PPGnotify; cdecl;
00235 TPQfreeNotify = procedure(Handle: PPGnotify);cdecl;
00236 TPQsendQuery = function(Handle: PPGconn; Query: PChar): Integer; cdecl;
00237 TPQgetResult = function(Handle: PPGconn): PPGresult; cdecl;
00238 TPQisBusy = function(Handle: PPGconn): Integer; cdecl;
00239 TPQconsumeInput = function(Handle: PPGconn): Integer; cdecl;
00240 TPQgetline = function(Handle: PPGconn; Str: PChar; length: Integer): Integer; cdecl;
00241 TPQputline = function(Handle: PPGconn; Str: PChar): Integer; cdecl;
00242 TPQgetlineAsync = function(Handle: PPGconn; Buffer: PChar; BufSize: Integer): Integer; cdecl;
00243 TPQputnbytes = function(Handle: PPGconn; Buffer: PChar; NBytes: Integer): Integer; cdecl;
00244 TPQendcopy = function(Handle: PPGconn): Integer; cdecl;
00245 TPQfn = function(Handle: PPGconn; fnid: Integer; result_buf, result_len: PInteger; result_is_int: Integer; args: PPQArgBlock; nargs: Integer): PPGresult; cdecl;
00246 TPQresultStatus = function(Result: PPGresult): ExecStatusType; cdecl;
00247 TPQresultErrorMessage = function(Result: PPGresult): PChar; cdecl;
00248 TPQntuples = function(Result: PPGresult): Integer; cdecl;
00249 TPQnfields = function(Result: PPGresult): Integer; cdecl;
00250 TPQbinaryTuples = function(Result: PPGresult): Integer; cdecl;
00251 TPQfname = function(Result: PPGresult; field_num: Integer): PChar; cdecl;
00252 TPQfnumber = function(Result: PPGresult; field_name: PChar): Integer; cdecl;
00253 TPQftype = function(Result: PPGresult; field_num: Integer): Oid; cdecl;
00254 TPQfsize = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
00255 TPQfmod = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
00256 TPQcmdStatus = function(Result: PPGresult): PChar; cdecl;
00257 TPQoidValue = function(Result: PPGresult): Oid; cdecl;
00258 TPQoidStatus = function(Result: PPGresult): PChar; cdecl;
00259 TPQcmdTuples = function(Result: PPGresult): PChar; cdecl;
00260 TPQgetvalue = function(Result: PPGresult; tup_num, field_num: Integer): PChar; cdecl;
00261 TPQgetlength = function(Result: PPGresult; tup_num, field_num: Integer): Integer; cdecl;
00262 TPQgetisnull = function(Result: PPGresult; tup_num, field_num: Integer): Integer; cdecl;
00263 TPQclear = procedure(Result: PPGresult); cdecl;
00264 TPQmakeEmptyPGresult = function(Handle: PPGconn; status: ExecStatusType): PPGresult; cdecl;
00265
00266 { === in fe-lobj.c === }
00267 Tlo_open = function(Handle: PPGconn; lobjId: Oid; mode: Integer): Integer; cdecl;
00268 Tlo_close = function(Handle: PPGconn; fd: Integer): Integer; cdecl;
00269 Tlo_read = function(Handle: PPGconn; fd: Integer; buf: PChar; len: Integer): Integer; cdecl;
00270 Tlo_write = function(Handle: PPGconn; fd: Integer; buf: PChar; len: Integer): Integer; cdecl;
00271 Tlo_lseek = function(Handle: PPGconn; fd, offset, whence: Integer): Integer; cdecl;
00272 Tlo_creat = function(Handle: PPGconn; mode: Integer): Oid; cdecl;
00273 Tlo_tell = function(Handle: PPGconn; fd: Integer): Integer; cdecl;
00274 Tlo_unlink = function(Handle: PPGconn; lobjId: Oid): Integer; cdecl;
00275 Tlo_import = function(Handle: PPGconn; filename: PChar): Oid; cdecl;
00276 Tlo_export = function(Handle: PPGconn; lobjId: Oid; filename: PChar): Integer; cdecl;
00277
00278
00279 { ************* Plain API Function variables definition ************ }
00280
00281 var
00282 { === in fe-connect.c === }
00283 PQconnectdb: TPQconnectdb;
00284 PQsetdbLogin: TPQsetdbLogin;
00285 PQconndefaults: TPQconndefaults;
00286 PQfinish: TPQfinish;
00287 PQreset: TPQreset;
00288 PQrequestCancel: TPQrequestCancel;
00289 PQdb: TPQdb;
00290 PQuser: TPQuser;
00291 PQpass: TPQpass;
00292 PQhost: TPQhost;
00293 PQport: TPQport;
00294 PQtty: TPQtty;
00295 PQoptions: TPQoptions;
00296 PQstatus: TPQstatus;
00297 PQerrorMessage: TPQerrorMessage;
00298 PQsocket: TPQsocket;
00299 PQbackendPID: TPQbackendPID;
00300 PQtrace: TPQtrace;
00301 PQuntrace: TPQuntrace;
00302 PQsetNoticeProcessor: TPQsetNoticeProcessor;
00303
00304 { === in fe-exec.c === }
00305 PQexec: TPQexec;
00306 PQnotifies: TPQnotifies;
00307 PQfreeNotify: TPQfreeNotify;
00308 PQsendQuery: TPQsendQuery;
00309 PQgetResult: TPQgetResult;
00310 PQisBusy: TPQisBusy;
00311 PQconsumeInput: TPQconsumeInput;
00312 PQgetline: TPQgetline;
00313 PQputline: TPQputline;
00314 PQgetlineAsync: TPQgetlineAsync;
00315 PQputnbytes: TPQputnbytes;
00316 PQendcopy: TPQendcopy;
00317 PQfn: TPQfn;
00318 PQresultStatus: TPQresultStatus;
00319 PQresultErrorMessage: TPQresultErrorMessage;
00320 PQntuples: TPQntuples;
00321 PQnfields: TPQnfields;
00322 PQbinaryTuples: TPQbinaryTuples;
00323 PQfname: TPQfname;
00324 PQfnumber: TPQfnumber;
00325 PQftype: TPQftype;
00326 PQfsize: TPQfsize;
00327 PQfmod: TPQfmod;
00328 PQcmdStatus: TPQcmdStatus;
00329 PQoidValue: TPQoidValue;
00330 PQoidStatus: TPQoidStatus;
00331 PQcmdTuples: TPQcmdTuples;
00332 PQgetvalue: TPQgetvalue;
00333 PQgetlength: TPQgetlength;
00334 PQgetisnull: TPQgetisnull;
00335 PQclear: TPQclear;
00336 PQmakeEmptyPGresult: TPQmakeEmptyPGresult;
00337
00338 { === in fe-lobj.c === }
00339 lo_open: Tlo_open;
00340 lo_close: Tlo_close;
00341 lo_read: Tlo_read;
00342 lo_write: Tlo_write;
00343 lo_lseek: Tlo_lseek;
00344 lo_creat: Tlo_creat;
00345 lo_tell: Tlo_tell;
00346 lo_unlink: Tlo_unlink;
00347 lo_import: Tlo_import;
00348 lo_export: Tlo_export;
00349
00350 var
00351 LibraryLoader: TZNativeLibraryLoader;
00352
00353 implementation
00354
00355 type
00356 {** Implements a loader for PostgreSQL native library. }
00357 TZPostgreSQLNativeLibraryLoader = class (TZNativeLibraryLoader)
00358 public
00359 function Load: Boolean; override;
00360 end;
00361
00362 { TZPostgreSQLNativeLibraryLoader }
00363
00364 {**
00365 Loads a library module.
00366 @return <code>True</code> if library was successfully loaded.
00367 }
00368 function TZPostgreSQLNativeLibraryLoader.Load: Boolean;
00369 begin
00370 Result := inherited Load;
00371
00372 { === in fe-connect.c === }
00373 @PQconnectdb := GetAddress('PQconnectdb');
00374 @PQsetdbLogin := GetAddress('PQsetdbLogin');
00375 @PQconndefaults := GetAddress('PQconndefaults');
00376 @PQfinish := GetAddress('PQfinish');
00377 @PQreset := GetAddress('PQreset');
00378 @PQrequestCancel := GetAddress('PQrequestCancel');
00379 @PQdb := GetAddress('PQdb');
00380 @PQuser := GetAddress('PQuser');
00381 @PQpass := GetAddress('PQpass');
00382 @PQhost := GetAddress('PQhost');
00383 @PQport := GetAddress('PQport');
00384 @PQtty := GetAddress('PQtty');
00385 @PQoptions := GetAddress('PQoptions');
00386 @PQstatus := GetAddress('PQstatus');
00387 @PQerrorMessage := GetAddress('PQerrorMessage');
00388 @PQsocket := GetAddress('PQsocket');
00389 @PQbackendPID := GetAddress('PQbackendPID');
00390 @PQtrace := GetAddress('PQtrace');
00391 @PQuntrace := GetAddress('PQuntrace');
00392 @PQsetNoticeProcessor := GetAddress('PQsetNoticeProcessor');
00393
00394 { === in fe-exec.c === }
00395 @PQexec := GetAddress('PQexec');
00396 @PQnotifies := GetAddress('PQnotifies');
00397 @PQfreeNotify := GetAddress('PQfreeNotify');
00398 @PQsendQuery := GetAddress('PQsendQuery');
00399 @PQgetResult := GetAddress('PQgetResult');
00400 @PQisBusy := GetAddress('PQisBusy');
00401 @PQconsumeInput := GetAddress('PQconsumeInput');
00402 @PQgetline := GetAddress('PQgetline');
00403 @PQputline := GetAddress('PQputline');
00404 @PQgetlineAsync := GetAddress('PQgetlineAsync');
00405 @PQputnbytes := GetAddress('PQputnbytes');
00406 @PQendcopy := GetAddress('PQendcopy');
00407 @PQfn := GetAddress('PQfn');
00408 @PQresultStatus := GetAddress('PQresultStatus');
00409 @PQresultErrorMessage := GetAddress('PQresultErrorMessage');
00410 @PQntuples := GetAddress('PQntuples');
00411 @PQnfields := GetAddress('PQnfields');
00412 @PQbinaryTuples := GetAddress('PQbinaryTuples');
00413 @PQfname := GetAddress('PQfname');
00414 @PQfnumber := GetAddress('PQfnumber');
00415 @PQftype := GetAddress('PQftype');
00416 @PQfsize := GetAddress('PQfsize');
00417 @PQfmod := GetAddress('PQfmod');
00418 @PQcmdStatus := GetAddress('PQcmdStatus');
00419 @PQoidValue := GetAddress('PQoidValue');
00420 @PQoidStatus := GetAddress('PQoidStatus');
00421 @PQcmdTuples := GetAddress('PQcmdTuples');
00422 @PQgetvalue := GetAddress('PQgetvalue');
00423 @PQgetlength := GetAddress('PQgetlength');
00424 @PQgetisnull := GetAddress('PQgetisnull');
00425 @PQclear := GetAddress('PQclear');
00426 @PQmakeEmptyPGresult := GetAddress('PQmakeEmptyPGresult');
00427
00428 { === in fe-lobj.c === }
00429 @lo_open := GetAddress('lo_open');
00430 @lo_close := GetAddress('lo_close');
00431 @lo_read := GetAddress('lo_read');
00432 @lo_write := GetAddress('lo_write');
00433 @lo_lseek := GetAddress('lo_lseek');
00434 @lo_creat := GetAddress('lo_creat');
00435 @lo_tell := GetAddress('lo_tell');
00436 @lo_unlink := GetAddress('lo_unlink');
00437 @lo_import := GetAddress('lo_import');
00438 @lo_export := GetAddress('lo_export');
00439 end;
00440
00441 initialization
00442 {$IFNDEF UNIX}
00443 LibraryLoader := TZPostgreSQLNativeLibraryLoader.Create(
00444 [WINDOWS1_DLL_LOCATION
00445 {$IFNDEF STRICT_DLL_LOADING}
00446 , WINDOWS2_DLL_LOCATION
00447 {$ENDIF}
00448 ]);
00449 {$ELSE}
00450 LibraryLoader := TZPostgreSQLNativeLibraryLoader.Create(
00451 [LINUX_DLL_LOCATION]);
00452 {$ENDIF}
00453 finalization
00454 if Assigned(LibraryLoader) then
00455 LibraryLoader.Free;
00456 end.