1 {*********************************************************}
3 { Zeos Database Objects }
4 { Native Plain Drivers for PostgreSQL }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
52 unit ZPlainPostgreSqlDriver;
58 uses ZClasses, ZCompatibility, ZPlainDriver;
61 WINDOWS_DLL_LOCATION = 'libpq.dll';
62 WINDOWS_DLL7_LOCATION = 'libpq74.dll';
63 WINDOWS_DLL8_LOCATION = 'libpq81.dll';
64 LINUX_DLL_LOCATION = 'libpq'+SharedSuffix;
65 LINUX_DLL8_LOCATION = 'libpq'+SharedSuffix+'.4';
66 LINUX_DLL82_LOCATION = 'libpq'+SharedSuffix+'.5';
71 { OIDNAMELEN should be set to NAMEDATALEN + sizeof(Oid) }
75 INV_WRITE = $00020000;
84 { Application-visible enum types }
85 TZPostgreSQLConnectStatusType = (
90 TZPostgreSQLFieldCode=( // FirmOS
91 PG_DIAG_SEVERITY=ord('S'),
92 PG_DIAG_SQLSTATE=ord('C'),
93 PG_DIAG_MESSAGE_PRIMARY=ord('M'),
94 PG_DIAG_MESSAGE_DETAIL=ord('D'),
95 PG_DIAG_MESSAGE_HINT=ord('H'),
96 PG_DIAG_STATEMENT_POSITION=ord('P'),
97 PG_DIAG_INTERNAL_POSITION=ord('p'),
98 PG_DIAG_INTERNAL_QUERY=ord('q'),
99 PG_DIAG_CONTEXT=ord('W'),
100 PG_DIAG_SOURCE_FILE=ord('F'),
101 PG_DIAG_SOURCE_LINE=ord('L'),
102 PG_DIAG_SOURCE_FUNCTION=ord('R')
105 TZPostgreSQLExecStatusType = (
107 PGRES_COMMAND_OK, { a query command that doesn't return anything
108 was executed properly by the backend }
109 PGRES_TUPLES_OK, { a query command that returns tuples
110 was executed properly by the backend,
111 PGresult contains the result tuples }
112 PGRES_COPY_OUT, { Copy Out data transfer in progress }
113 PGRES_COPY_IN, { Copy In data transfer in progress }
114 PGRES_BAD_RESPONSE, { an unexpected response was recv'd from
116 PGRES_NONFATAL_ERROR,
120 { PGnotify represents the occurrence of a NOTIFY message.
121 Ideally this would be an opaque typedef, but it's so simple that it's
123 NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
124 whereas in earlier versions it was always your own backend's PID.
126 TZPostgreSQLNotify = packed record
127 relname: PAnsiChar; { name of relation containing data }
128 be_pid: Integer; { process id of backend }
129 payload: PAnsiChar; {additional data in notify}
132 PZPostgreSQLNotify = ^TZPostgreSQLNotify;
134 { PQnoticeProcessor is the function type for the notice-message callback. }
136 TZPostgreSQLNoticeProcessor = procedure(arg: Pointer; message: PAnsiChar); cdecl;
138 { Structure for the conninfo parameter definitions returned by PQconndefaults }
140 TZPostgreSQLConnectInfoOption = packed record
141 keyword: PAnsiChar; { The keyword of the option }
142 envvar: PAnsiChar; { Fallback environment variable name }
143 compiled: PAnsiChar; { Fallback compiled in default value }
144 val: PAnsiChar; { Options value }
145 lab: PAnsiChar; { Label for field in connect dialog }
146 disPAnsiChar: PAnsiChar; { Character to display for this field
147 in a connect dialog. Values are:
148 "" Display entered value as is
149 "*" Password field - hide value
150 "D" Debug options - don't
151 create a field by default }
152 dispsize: Integer; { Field size in characters for dialog }
155 PZPostgreSQLConnectInfoOption = ^TZPostgreSQLConnectInfoOption;
157 { PQArgBlock -- structure for PQfn() arguments }
159 TZPostgreSQLArgBlock = packed record
163 True: (ptr: PInteger); { can't use void (dec compiler barfs) }
164 False: (_int: Integer);
167 PZPostgreSQLArgBlock = ^TZPostgreSQLArgBlock;
169 PZPostgreSQLConnect = Pointer;
170 PZPostgreSQLResult = Pointer;
171 PZPostgreSQLCancel = Pointer;
175 TZPgCharactersetType = (
176 csSQL_ASCII = 0, { SQL/ASCII }
177 csEUC_JP, { EUC for Japanese }
178 csEUC_CN, { EUC for Chinese }
179 csEUC_KR, { EUC for Korean }
180 csEUC_TW, { EUC for Taiwan }
181 csEUC_JIS_2004, {Extended UNIX Code-JP, JIS X 0213 Japanese}
182 csUTF8, { Unicode UTF-8 }
183 csMULE_INTERNAL, { Mule internal code }
184 csLATIN1, { ISO-8859 Latin 1 }
185 csLATIN2, { ISO-8859 Latin 2 }
186 csLATIN3, { ISO-8859 Latin 3 }
187 csLATIN4, { ISO-8859 Latin 4 }
188 csLATIN5, { ISO-8859 Latin 5 }
189 csLATIN6, { ISO-8859 Latin 6 }
190 csLATIN7, { ISO-8859 Latin 7 }
191 csLATIN8, { ISO-8859 Latin 8 }
192 csLATIN9, { ISO-8859 Latin 9 }
193 csLATIN10, { ISO-8859 Latin 10 }
194 csWIN1256, { Arabic Windows }
195 csWIN1258, { Vietnamese Windows }
196 csWIN866, { Alternativny Variant (MS-DOS CP866) }
197 csWIN874, { Thai Windows }
198 csKOI8, {KOI8-R(U) Cyrillic}
199 csKOI8R, { KOI8-R Cyrillic (Russian) }
200 csWIN1251, { windows-1251 }
201 csWIN1252, {Windows CP1252 Western European}
202 csISO_8859_5, { ISO-8859-5 }
203 csISO_8859_6, { ISO-8859-6 }
204 csISO_8859_7, { ISO-8859-7 }
205 csISO_8859_8, { ISO-8859-8 }
206 csWIN1250, { Windows CP1250 Central European }
207 csWIN1253, { Windows CP1253 Greek }
208 csWIN1254, { Windows CP1254 Turkish }
209 csWIN1255, { Windows CP1255 Hebrew }
210 csWIN1257, { Windows CP1257 Baltic }
211 csKOI8U, { KOI8-R Cyrillic (Ukrainian) }
212 csSJIS, { Shift JIS Japanese }
213 csBIG5, { Big Five Traditional Chinese }
214 csGBK, { Extended National Standard Simplified Chinese }
215 csUHC, { Unified Hangul Code Korean }
216 csGB18030, { National Standard Chinese }
217 csJOHAB, {JOHAB Korean (Hangul)}
218 csSHIFT_JIS_2004, {Shift JIS, JIS X 0213 Japanese}
219 csUNICODE_PODBC,{ UNICODE ( < Ver8.1). Can't call it UNICODE as that's already used }
220 csTCVN, { TCVN ( < Ver8.1) }
221 csALT, { ALT ( < Var8.1) }
222 csWIN, { WIN ( < Ver8.1) }
226 { ****************** Plain API Types definition ***************** }
229 { String descriptions of the ExecStatusTypes }
230 pgresStatus = array[$00..$ff] of PAnsiChar;
232 { PGconn encapsulates a connection to the backend.
233 The contents of this struct are not supposed to be known to applications.
238 { PGresult encapsulates the result of a query (or more precisely, of a single
239 SQL command --- a query string given to PQsendQuery can contain multiple
240 commands and thus return multiple PGresult objects).
241 The contents of this struct are not supposed to be known to applications.
247 { PGnotify represents the occurrence of a NOTIFY message.
248 Ideally this would be an opaque typedef, but it's so simple that it's
250 NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
251 whereas in earlier versions it was always your own backend's PID.
253 PGnotify = packed record
254 relname: array [0..NAMEDATALEN-1] of AnsiChar; { name of relation containing data }
255 be_pid: Integer; { process id of backend }
258 PPGnotify = ^PGnotify;
260 { PQnoticeProcessor is the function type for the notice-message callback. }
262 PQnoticeProcessor = procedure(arg: Pointer; message: PAnsiChar); cdecl;
264 { Print options for PQprint() }
267 We can't use the conventional "bool", because we are designed to be
268 included in a user's program, and user may already have that type
269 defined. Pqbool, on the other hand, is unlikely to be used.
272 PPAnsiChar = array[00..$ff] of PAnsiChar;
274 PQprintOpt = packed record
275 header: Byte; { print output field headings and row count }
276 align: Byte; { fill align the fields }
277 standard: Byte; { old brain dead format }
278 html3: Byte; { output html tables }
279 expanded: Byte; { expand tables }
280 pager: Byte; { use pager for output if needed }
281 fieldSep: PAnsiChar; { field separator }
282 tableOpt: PAnsiChar; { insert to HTML <table ...> }
283 caption: PAnsiChar; { HTML <caption> }
284 fieldName: PPAnsiChar; { null terminated array of repalcement field names }
287 PPQprintOpt = ^PQprintOpt;
290 Structure for the conninfo parameter definitions returned by PQconndefaults
293 PQconninfoOption = packed record
294 keyword: PAnsiChar; { The keyword of the option }
295 envvar: PAnsiChar; { Fallback environment variable name }
296 compiled: PAnsiChar; { Fallback compiled in default value }
297 val: PAnsiChar; { Options value }
298 lab: PAnsiChar; { Label for field in connect dialog }
299 disPAnsiChar: PAnsiChar; { Character to display for this field
300 in a connect dialog. Values are:
301 "" Display entered value as is
302 "*" Password field - hide value
303 "D" Debug options - don't
304 create a field by default }
305 dispsize: Integer; { Field size in characters for dialog }
308 PPQConninfoOption = ^PQconninfoOption;
311 PQArgBlock -- structure for PQfn() arguments
314 PQArgBlock = packed record
318 True: (ptr: PInteger); { can't use void (dec compiler barfs) }
319 False: (_int: Integer);
322 PPQArgBlock = ^PQArgBlock;
324 {Prepared statement types}
325 TPQparamTypes = {array of }POid;
326 TPQparamValues = array of PAnsichar;
327 TPQparamLengths = array of Integer;
328 TPQparamFormats = array of Integer;
331 { ************** Plain API Function types definition ************* }
332 { === in fe-connect.c === }
333 TPQconnectdb = function(ConnInfo: PAnsiChar): PPGconn; cdecl; // FirmOS 8.1 OK
334 TPQsetdbLogin = function(Host, Port, Options, Tty, Db, User, Passwd: PAnsiChar): PPGconn; cdecl; // FirmOS 8.1 OK
335 //15022006 FirmOS: omitting PQconnectStart
336 //15022006 FirmOS: omitting PQconnectPoll
337 TPQconndefaults = function: PPQconninfoOption; cdecl;
338 TPQfinish = procedure(Handle: PPGconn); cdecl;
339 TPQreset = procedure(Handle: PPGconn); cdecl;
340 //15022006 FirmOS: omitting PQresetStart
341 //15022006 FirmOS: omitting PQresetPoll
342 TPQrequestCancel = function(Handle: PPGconn): Integer; cdecl;
343 TPQdb = function(Handle: PPGconn): PAnsiChar; cdecl;
344 TPQuser = function(Handle: PPGconn): PAnsiChar; cdecl;
345 TPQpass = function(Handle: PPGconn): PAnsiChar; cdecl;
346 TPQhost = function(Handle: PPGconn): PAnsiChar; cdecl;
347 TPQport = function(Handle: PPGconn): PAnsiChar; cdecl;
348 TPQtty = function(Handle: PPGconn): PAnsiChar; cdecl;
349 TPQoptions = function(Handle: PPGconn): PAnsiChar; cdecl;
350 TPQstatus = function(Handle: PPGconn): TZPostgreSQLConnectStatusType; cdecl;
351 //TBD PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
352 //15022006 FirmOS: omitting const char *PQparameterStatus(const PGconn *conn, const char *paramName);
353 //15022006 FirmOS: omitting PQprotocolVersion
354 TPQserverVersion = function(Handle: PPGconn): Integer; cdecl;
355 TPQerrorMessage = function(Handle: PPGconn): PAnsiChar; cdecl;
356 TPQsocket = function(Handle: PPGconn): Integer; cdecl;
357 TPQbackendPID = function(Handle: PPGconn): Integer; cdecl;
358 //15022006 FirmOS: omitting SSL *PQgetssl(const PGconn *conn);
359 TPQtrace = procedure(Handle: PPGconn; DebugPort: Pointer); cdecl;
360 TPQuntrace = procedure(Handle: PPGconn); cdecl;
361 TPQsetNoticeProcessor = procedure(Handle: PPGconn; Proc: PQnoticeProcessor; Arg: Pointer); cdecl;
363 TPQclientEncoding = function(Handle: PPGconn): Integer; cdecl; //EgonHugeist
364 { === in fe-exec.c === }
365 //* Simple synchronous query */
366 TPQexec = function(Handle: PPGconn; Query: PAnsiChar): PPGresult; cdecl;
367 TPQexecParams = function(Handle: PPGconn; command: PAnsichar;
368 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
369 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
370 resultFormat: Integer): PPGresult; cdecl;
371 TPQprepare = function(Handle: PPGconn; stmtName: PAnsichar;
372 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): PPGresult; cdecl;
373 TPQexecPrepared = function(Handle: PPGconn; stmtName: PAnsichar;
374 nParams: Integer; paramValues: TPQparamValues; paramLengths: TPQparamLengths;
375 paramFormats: TPQparamFormats; resultFormat: Integer): PPGresult; cdecl;
376 //* Interface for multiple-result or asynchronous queries */
377 TPQsendQuery = function(Handle: PPGconn; query: PAnsiChar): Integer; cdecl;
378 TPQsendQueryParams= function(Handle: PPGconn; command: PAnsichar;
379 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
380 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
381 resultFormat: Integer): Integer; cdecl;
382 TPQsendPrepare = function(Handle: PPGconn; stmtName: PAnsichar;
383 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): Integer; cdecl;
384 TPQsendQueryPrepared = function(Handle: PPGconn; stmtName: PAnsichar;
385 nParams: Integer; paramValues: TPQparamValues;
386 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
387 resultFormat: Integer): Integer; cdecl;
388 TPQgetResult = function(Handle: PPGconn): PPGresult; cdecl;
389 //* Describe prepared statements and portals */
390 TPQdescribePrepared = function(Handle: PPGconn; const stmt: PAnsiChar): PPGresult; cdecl;
391 TPQdescribePortal = function(Handle: PPGconn; const portal: PAnsiChar): PPGresult; cdecl;
392 TPQsendDescribePrepared = function(Handle: PPGconn; const stmt: PAnsiChar): Integer; cdecl;
393 TPQsendDescribePortal = function(Handle: PPGconn; const portal: PAnsiChar): Integer; cdecl;
395 TPQnotifies = function(Handle: PPGconn): PPGnotify; cdecl;
396 TPQfreeNotify = procedure(Handle: PPGnotify);cdecl;
397 TPQisBusy = function(Handle: PPGconn): Integer; cdecl;
398 TPQconsumeInput = function(Handle: PPGconn): Integer; cdecl;
399 TPQgetCancel = function(Handle: PPGconn): PGcancel; cdecl;
400 TPQfreeCancel = procedure(Canc: PGcancel); cdecl;
401 TPQcancel = function(Canc: PGcancel; Buffer: PChar; BufSize: Integer): Integer;
402 TPQgetline = function(Handle: PPGconn; Str: PAnsiChar; length: Integer): Integer; cdecl;
403 TPQputline = function(Handle: PPGconn; Str: PAnsiChar): Integer; cdecl;
404 TPQgetlineAsync = function(Handle: PPGconn; Buffer: PAnsiChar; BufSize: Integer): Integer; cdecl;
405 TPQputnbytes = function(Handle: PPGconn; Buffer: PAnsiChar; NBytes: Integer): Integer; cdecl;
406 TPQendcopy = function(Handle: PPGconn): Integer; cdecl;
407 TPQfn = function(Handle: PPGconn; fnid: Integer; result_buf, result_len: PInteger; result_is_int: Integer; args: PPQArgBlock; nargs: Integer): PPGresult; cdecl;
408 TPQresultStatus = function(Result: PPGresult): TZPostgreSQLExecStatusType; cdecl;
409 TPQresultErrorMessage = function(Result: PPGresult): PAnsiChar; cdecl;
410 TPQresultErrorField=function(result: PPGResult; fieldcode:integer):PAnsiChar;cdecl; // postgresql 8
411 TPQntuples = function(Result: PPGresult): Integer; cdecl;
412 TPQnfields = function(Result: PPGresult): Integer; cdecl;
413 TPQbinaryTuples = function(Result: PPGresult): Integer; cdecl;
414 TPQfname = function(Result: PPGresult; field_num: Integer): PAnsiChar; cdecl;
415 TPQfnumber = function(Result: PPGresult; field_name: PAnsiChar): Integer; cdecl;
416 TPQftable = function(Result: PPGresult; field_num: Integer): Oid; cdecl;
417 TPQftablecol = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
418 TPQftype = function(Result: PPGresult; field_num: Integer): Oid; cdecl;
419 TPQfsize = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
420 TPQfmod = function(Result: PPGresult; field_num: Integer): Integer; cdecl;
421 TPQcmdStatus = function(Result: PPGresult): PAnsiChar; cdecl;
422 TPQoidValue = function(Result: PPGresult): Oid; cdecl;
423 TPQoidStatus = function(Result: PPGresult): PAnsiChar; cdecl;
424 TPQcmdTuples = function(Result: PPGresult): PAnsiChar; cdecl;
425 TPQgetvalue = function(Result: PPGresult; tup_num, field_num: Integer): PAnsiChar; cdecl;
426 TPQgetlength = function(Result: PPGresult; tup_num, field_num: Integer): Integer; cdecl;
427 TPQgetisnull = function(Result: PPGresult; tup_num, field_num: Integer): Integer; cdecl;
428 TPQclear = procedure(Result: PPGresult); cdecl;
429 TPQmakeEmptyPGresult = function(Handle: PPGconn; status: TZPostgreSQLExecStatusType): PPGresult; cdecl;
430 //* Quoting strings before inclusion in queries. */
432 TPQescapeStringConn = function(Handle: PGconn; ToChar: PAnsiChar;
433 const FromChar: PAnsiChar; length: NativeUInt; error: PInteger): NativeUInt;cdecl; //7.3
434 TPQescapeLiteral = function(Handle: PGconn; const str: PAnsiChar; len: NativeUInt): PAnsiChar;cdecl;
435 TPQescapeIdentifier = function(Handle: PGconn; const str: PAnsiChar; len: NativeUInt): PAnsiChar;cdecl; //7.3
436 TPQescapeByteaConn = function(Handle: PPGconn;const from:PAnsiChar;from_length:longword;to_lenght:PLongword):PAnsiChar;cdecl;
437 TPQunescapeBytea = function(const from:PAnsiChar;to_lenght:PLongword):PAnsiChar;cdecl;
438 TPQFreemem = procedure(ptr:Pointer);cdecl;
440 //* These forms are deprecated! */
441 TPQescapeString = function(ToChar: PAnsiChar; const FormChar: PAnsiChar; length: NativeUInt): NativeUInt;cdecl; //7.2
442 TPQescapeBytea = function(const from:PAnsiChar;from_length:longword;to_lenght:PLongword):PAnsiChar;cdecl; //7.2
444 { === in fe-lobj.c === }
445 Tlo_open = function(Handle: PPGconn; lobjId: Oid; mode: Integer): Integer; cdecl;
446 Tlo_close = function(Handle: PPGconn; fd: Integer): Integer; cdecl;
447 Tlo_read = function(Handle: PPGconn; fd: Integer; buf: PAnsiChar; len: NativeUInt): Integer; cdecl;
448 Tlo_write = function(Handle: PPGconn; fd: Integer; buf: PAnsiChar; len: NativeUInt): Integer; cdecl;
449 Tlo_lseek = function(Handle: PPGconn; fd, offset, whence: Integer): Integer; cdecl;
450 Tlo_creat = function(Handle: PPGconn; mode: Integer): Oid; cdecl;
451 Tlo_tell = function(Handle: PPGconn; fd: Integer): Integer; cdecl;
452 Tlo_unlink = function(Handle: PPGconn; lobjId: Oid): Integer; cdecl;
453 Tlo_import = function(Handle: PPGconn; filename: PAnsiChar): Oid; cdecl;
454 Tlo_export = function(Handle: PPGconn; lobjId: Oid; filename: PAnsiChar): Integer; cdecl;
456 { ************** Collection of Plain API Function types definition ************* }
457 TZPOSTGRESQL_API = record
458 { === in fe-connect.c === }
459 PQconnectdb: TPQconnectdb;
460 PQsetdbLogin: TPQsetdbLogin;
461 PQconndefaults: TPQconndefaults;
464 PQrequestCancel: TPQrequestCancel;
471 PQoptions: TPQoptions;
473 PQserverVersion: TPQserverVersion;
474 PQerrorMessage: TPQerrorMessage;
476 PQbackendPID: TPQbackendPID;
478 PQuntrace: TPQuntrace;
479 PQsetNoticeProcessor: TPQsetNoticeProcessor;
480 PQclientEncoding: TPQclientEncoding;
481 { === in fe-exec.c === }
483 PQexecParams: TPQexecParams;
484 PQprepare: TPQprepare;
485 PQexecPrepared: TPQexecPrepared;
486 PQsendQuery: TPQsendQuery;
487 PQsendQueryParams: TPQsendQueryParams;
488 PQsendPrepare: TPQsendPrepare;
489 PQsendQueryPrepared: TPQsendQueryPrepared;
490 PQgetResult: TPQgetResult;
491 //* Describe prepared statements and portals */
492 PQdescribePrepared: TPQdescribePrepared;
493 PQdescribePortal: TPQdescribePortal;
494 PQsendDescribePrepared: TPQsendDescribePrepared;
495 PQsendDescribePortal: TPQsendDescribePortal;
496 PQnotifies: TPQnotifies;
497 PQfreeNotify: TPQfreeNotify;
499 PQconsumeInput: TPQconsumeInput;
500 PQgetCancel: TPQgetCancel;
501 PQfreeCancel: TPQfreeCancel;
503 PQgetline: TPQgetline;
504 PQputline: TPQputline;
505 PQgetlineAsync: TPQgetlineAsync;
506 PQputnbytes: TPQputnbytes;
507 PQendcopy: TPQendcopy;
509 PQresultStatus: TPQresultStatus;
510 PQresultErrorMessage: TPQresultErrorMessage;
511 PQresultErrorField: TPQresultErrorField; // postgresql 8
512 PQntuples: TPQntuples;
513 PQnfields: TPQnfields;
514 PQbinaryTuples: TPQbinaryTuples;
516 PQfnumber: TPQfnumber;
518 PQftablecol: TPQftablecol;
522 PQcmdStatus: TPQcmdStatus;
523 PQoidValue: TPQoidValue;
524 PQoidStatus: TPQoidStatus;
525 PQcmdTuples: TPQcmdTuples;
526 PQgetvalue: TPQgetvalue;
527 PQgetlength: TPQgetlength;
528 PQgetisnull: TPQgetisnull;
530 PQmakeEmptyPGresult: TPQmakeEmptyPGresult;
532 PQescapeStringConn: TPQescapeStringConn; //since 7.3
533 PQescapeByteaConn: TPQescapeByteaConn; // postgresql since 7.3
534 PQFreemem: TPQFreemem; // since postgresql 7.4
535 PQescapeString: TPQescapeString; // since postgresql 7.4
536 PQescapeBytea: TPQescapeBytea; // since postgresql 7.4
537 PQunescapeBytea: TPQunescapeBytea; // since postgresql 8.3
538 PQescapeLiteral: TPQescapeLiteral; // since postgresql 9.0
539 PQescapeIdentifier: TPQescapeIdentifier; // since postgresql 9.0
541 { === in fe-lobj.c === }
549 lo_unlink: Tlo_unlink;
550 lo_import: Tlo_import;
551 lo_export: Tlo_export;
554 PAPI = ^TZPOSTGRESQL_API;
558 {** Represents a generic interface to PostgreSQL native API. }
559 IZPostgreSQLPlainDriver = interface (IZPlainDriver)
560 ['{03CD6345-2D7A-4FE2-B03D-3C5656789FEB}']
562 function GetStandardConformingStrings: Boolean;
564 function EncodeBYTEA(const Value: RawByteString; Handle: PZPostgreSQLConnect;
565 Quoted: Boolean = True): RawByteString;
566 function DecodeBYTEA(const value: RawByteString; const Is_bytea_output_hex: Boolean;
567 Handle: PZPostgreSQLConnect): RawByteString;
568 function SupportsEncodeBYTEA: Boolean;
569 function SupportsDecodeBYTEA(const Handle: PZPostgreSQLConnect): Boolean;
570 function SupportsStringEscaping(const ClientDependend: Boolean): Boolean;
572 function ConnectDatabase(ConnInfo: PAnsiChar): PZPostgreSQLConnect;
573 function SetDatabaseLogin(Host, Port, Options, TTY, Db, User,Passwd: PAnsiChar): PZPostgreSQLConnect;
574 function GetConnectDefaults: PZPostgreSQLConnectInfoOption;
576 procedure Finish(Handle: PZPostgreSQLConnect);
577 procedure Reset(Handle: PZPostgreSQLConnect);
578 function RequestCancel(Handle: PZPostgreSQLConnect): Integer;
579 function GetDatabase(Handle: PZPostgreSQLConnect): PAnsiChar;
580 function GetUser(Handle: PZPostgreSQLConnect): PAnsiChar;
581 function GetPassword(Handle: PZPostgreSQLConnect): PAnsiChar;
582 function GetHost(Handle: PZPostgreSQLConnect): PAnsiChar;
583 function GetPort(Handle: PZPostgreSQLConnect): PAnsiChar;
584 function GetTTY(Handle: PZPostgreSQLConnect): PAnsiChar;
585 function GetOptions(Handle: PZPostgreSQLConnect): PAnsiChar;
586 function GetStatus(Handle: PZPostgreSQLConnect):TZPostgreSQLConnectStatusType;
587 function GetClientEncoding(Handle: PPGconn): Integer; //EgonHugeist
589 function GetErrorMessage(Handle: PZPostgreSQLConnect): PAnsiChar;
590 function GetSocket(Handle: PZPostgreSQLConnect): Integer;
591 function GetBackendPID(Handle: PZPostgreSQLConnect): Integer;
592 procedure Trace(Handle: PZPostgreSQLConnect; DebugPort: Pointer);
593 procedure Untrace(Handle: PZPostgreSQLConnect);
594 procedure SetNoticeProcessor(Handle: PZPostgreSQLConnect;Proc: TZPostgreSQLNoticeProcessor; Arg: Pointer);
596 function ExecuteQuery(Handle: PZPostgreSQLConnect;Query: PAnsiChar): PZPostgreSQLResult;
597 function ExecParams(Handle: PPGconn; command: PAnsichar;
598 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
599 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
600 resultFormat: Integer): PPGresult;
601 function Prepare(Handle: PPGconn; stmtName: PAnsichar;
602 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): PPGresult;
603 function ExecPrepared(Handle: PPGconn; stmtName: PAnsichar;
604 nParams: Integer; paramValues: TPQparamValues; paramLengths: TPQparamLengths;
605 paramFormats: TPQparamFormats; resultFormat: Integer): PPGresult;
606 function SendQuery(Handle: PZPostgreSQLConnect; Query: PAnsiChar): Integer;
607 function SendQueryParams(Handle: PPGconn; command: PAnsichar;
608 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
609 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
610 resultFormat: Integer): Integer;
611 function SendPrepare(Handle: PPGconn; stmtName: PAnsichar;
612 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): Integer;
613 function SendQueryPrepared(Handle: PPGconn; stmtName: PAnsichar;
614 nParams: Integer; paramValues: TPQparamValues;
615 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
616 resultFormat: Integer): Integer;
617 function GetResult(Handle: PZPostgreSQLConnect): PZPostgreSQLResult;
618 //* Describe prepared statements and portals */
619 function DescribePrepared(Handle: PPGconn; const stmt: PAnsiChar): PPGresult;
620 function DescribePortal(Handle: PPGconn; const portal: PAnsiChar): PPGresult;
621 function SendDescribePrepared(Handle: PPGconn; const stmt: PAnsiChar): Integer;
622 function SendDescribePortal(Handle: PPGconn; const portal: PAnsiChar): Integer;
623 function Notifies(Handle: PZPostgreSQLConnect): PZPostgreSQLNotify;
624 procedure FreeNotify(Handle: PZPostgreSQLNotify);
626 function IsBusy(Handle: PZPostgreSQLConnect): Integer;
627 function ConsumeInput(Handle: PZPostgreSQLConnect): Integer;
628 function GetCancel(Handle: PZPostgreSQLConnect): PZPostgreSQLCancel;
629 procedure FreeCancel( Canc: PZPostgreSQLCancel);
630 function Cancel( Canc: PZPostgreSQLCancel; Buffer: PChar; Length: Integer): Integer;
631 function GetLine(Handle: PZPostgreSQLConnect; Str: PAnsiChar;
632 Length: Integer): Integer;
633 function PutLine(Handle: PZPostgreSQLConnect; Str: PAnsiChar): Integer;
634 function GetLineAsync(Handle: PZPostgreSQLConnect; Buffer: PAnsiChar;
635 Length: Integer): Integer;
637 function PutBytes(Handle: PZPostgreSQLConnect; Buffer: PAnsiChar;
638 Length: Integer): Integer;
639 function EndCopy(Handle: PZPostgreSQLConnect): Integer;
640 function ExecuteFunction(Handle: PZPostgreSQLConnect; fnid: Integer;
641 result_buf, result_len: PInteger; result_is_int: Integer;
642 args: PZPostgreSQLArgBlock; nargs: Integer): PZPostgreSQLResult;
643 function GetResultStatus(Res: PZPostgreSQLResult):
644 TZPostgreSQLExecStatusType;
646 function GetResultErrorMessage(Res: PZPostgreSQLResult): PAnsiChar;
647 function GetResultErrorField(Res: PZPostgreSQLResult; FieldCode: TZPostgreSQLFieldCode): PAnsiChar;
649 function GetRowCount(Res: PZPostgreSQLResult): Integer;
650 function GetFieldCount(Res: PZPostgreSQLResult): Integer;
652 function GetBinaryTuples(Res: PZPostgreSQLResult): Integer;
653 function GetFieldName(Res: PZPostgreSQLResult; FieldNum: Integer): PAnsiChar;
654 function GetFieldNumber(Res: PZPostgreSQLResult; FieldName: PAnsiChar): Integer;
655 function GetFieldTableOID(Res: PZPostgreSQLResult; FieldNum: Integer) : Oid;
656 function GetFieldTableColIdx(Res: PZPostgreSQLResult; FieldNum: Integer) : Integer;
657 function GetFieldType(Res: PZPostgreSQLResult; FieldNum: Integer): Oid;
658 function GetFieldSize(Res: PZPostgreSQLResult; FieldNum: Integer): Integer;
659 function GetFieldMode(Res: PZPostgreSQLResult; FieldNum: Integer): Integer;
660 function GetCommandStatus(Res: PZPostgreSQLResult): PAnsiChar;
661 function GetOidValue(Res: PZPostgreSQLResult): Oid;
662 function GetOidStatus(Res: PZPostgreSQLResult): PAnsiChar;
663 function GetCommandTuples(Res: PZPostgreSQLResult): PAnsiChar;
665 function GetValue(Res: PZPostgreSQLResult; TupNum, FieldNum: Integer): PAnsiChar;
666 function GetLength(Res: PZPostgreSQLResult; TupNum, FieldNum: Integer): Integer;
667 function GetIsNull(Res: PZPostgreSQLResult; TupNum, FieldNum: Integer): Integer;
668 procedure Clear(Res: PZPostgreSQLResult);
670 function MakeEmptyResult(Handle: PZPostgreSQLConnect;
671 Status: TZPostgreSQLExecStatusType): PZPostgreSQLResult;
673 function OpenLargeObject(Handle: PZPostgreSQLConnect; ObjId: Oid;
674 Mode: Integer): Integer;
675 function CloseLargeObject(Handle: PZPostgreSQLConnect;
676 Fd: Integer): Integer;
677 function ReadLargeObject(Handle: PZPostgreSQLConnect; Fd: Integer;
678 Buffer: PAnsiChar; Length: Integer): Integer;
679 function WriteLargeObject(Handle: PZPostgreSQLConnect; Fd: Integer;
680 Buffer: PAnsiChar; Length: Integer): Integer;
681 function SeekLargeObject(Handle: PZPostgreSQLConnect;
682 Fd, Offset, Whence: Integer): Integer;
683 function CreateLargeObject(Handle: PZPostgreSQLConnect;
685 function TellLargeObject(Handle: PZPostgreSQLConnect;
686 Fd: Integer): Integer;
687 function UnlinkLargeObject(Handle: PZPostgreSQLConnect;
688 ObjId: Oid): Integer;
689 function ImportLargeObject(Handle: PZPostgreSQLConnect;
690 FileName: PAnsiChar): Oid;
691 function ExportLargeObject(Handle: PZPostgreSQLConnect; ObjId: Oid;
692 FileName: PAnsiChar): Integer;
693 function GetPlainFunc:PAPI;
696 {** Implements a base driver for PostgreSQL}
697 TZPostgreSQLBaseDriver = class(TZAbstractPlainDriver, IZPostgreSQLPlainDriver)
699 POSTGRESQL_API: TZPOSTGRESQL_API;
700 function GetStandardConformingStrings: Boolean; virtual;
701 function GetUnicodeCodePageName: String; override;
702 procedure LoadCodePages; override;
703 procedure LoadApi; override;
707 function EncodeBYTEA(const Value: RawByteString; Handle: PZPostgreSQLConnect;
708 Quoted: Boolean = True): RawByteString;
709 function DecodeBYTEA(const value: RawByteString; const Is_bytea_output_hex: Boolean;
710 Handle: PZPostgreSQLConnect): RawByteString;
712 function SupportsEncodeBYTEA: Boolean;
713 function SupportsDecodeBYTEA(const Handle: PZPostgreSQLConnect): Boolean;
714 function SupportsStringEscaping(const ClientDependend: Boolean): Boolean;
716 function ConnectDatabase(ConnInfo: PAnsiChar): PZPostgreSQLConnect;
717 function SetDatabaseLogin(Host, Port, Options, TTY, Db, User,
718 Passwd: PAnsiChar): PZPostgreSQLConnect;
719 function GetConnectDefaults: PZPostgreSQLConnectInfoOption;
721 procedure Finish(Handle: PZPostgreSQLConnect);
722 procedure Reset(Handle: PZPostgreSQLConnect);
723 function RequestCancel(Handle: PZPostgreSQLConnect): Integer;
724 function GetDatabase(Handle: PZPostgreSQLConnect): PAnsiChar;
725 function GetUser(Handle: PZPostgreSQLConnect): PAnsiChar;
726 function GetPassword(Handle: PZPostgreSQLConnect): PAnsiChar;
727 function GetHost(Handle: PZPostgreSQLConnect): PAnsiChar;
728 function GetPort(Handle: PZPostgreSQLConnect): PAnsiChar;
729 function GetTTY(Handle: PZPostgreSQLConnect): PAnsiChar;
730 function GetOptions(Handle: PZPostgreSQLConnect): PAnsiChar;
731 function GetStatus(Handle: PZPostgreSQLConnect):
732 TZPostgreSQLConnectStatusType;
733 function GetClientEncoding(Handle: PPGconn): Integer; //EgonHugeist
735 function GetErrorMessage(Handle: PZPostgreSQLConnect): PAnsiChar;
736 function GetSocket(Handle: PZPostgreSQLConnect): Integer;
737 function GetBackendPID(Handle: PZPostgreSQLConnect): Integer;
738 procedure Trace(Handle: PZPostgreSQLConnect; DebugPort: Pointer);
739 procedure Untrace(Handle: PZPostgreSQLConnect);
740 procedure SetNoticeProcessor(Handle: PZPostgreSQLConnect;
741 Proc: TZPostgreSQLNoticeProcessor; Arg: Pointer);
743 function ExecuteQuery(Handle: PZPostgreSQLConnect;
744 Query: PAnsiChar): PZPostgreSQLResult;
745 function ExecParams(Handle: PPGconn; command: PAnsichar;
746 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
747 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
748 resultFormat: Integer): PPGresult;
749 function Prepare(Handle: PPGconn; stmtName: PAnsichar;
750 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): PPGresult;
751 function ExecPrepared(Handle: PPGconn; stmtName: PAnsichar;
752 nParams: Integer; paramValues: TPQparamValues; paramLengths: TPQparamLengths;
753 paramFormats: TPQparamFormats; resultFormat: Integer): PPGresult;
754 function SendQuery(Handle: PZPostgreSQLConnect; Query: PAnsiChar): Integer;
755 function SendQueryParams(Handle: PPGconn; command: PAnsichar;
756 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
757 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
758 resultFormat: Integer): Integer;
759 function SendPrepare(Handle: PPGconn; stmtName: PAnsichar;
760 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): Integer;
761 function SendQueryPrepared(Handle: PPGconn; stmtName: PAnsichar;
762 nParams: Integer; paramValues: TPQparamValues;
763 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
764 resultFormat: Integer): Integer;
765 function GetResult(Handle: PZPostgreSQLConnect): PZPostgreSQLResult;
766 function DescribePrepared(Handle: PPGconn; const stmt: PAnsiChar): PPGresult;
767 function DescribePortal(Handle: PPGconn; const portal: PAnsiChar): PPGresult;
768 function SendDescribePrepared(Handle: PPGconn; const stmt: PAnsiChar): Integer;
769 function SendDescribePortal(Handle: PPGconn; const portal: PAnsiChar): Integer;
771 function Notifies(Handle: PZPostgreSQLConnect): PZPostgreSQLNotify;
772 procedure FreeNotify(Handle: PZPostgreSQLNotify);
774 function IsBusy(Handle: PZPostgreSQLConnect): Integer;
775 function ConsumeInput(Handle: PZPostgreSQLConnect): Integer;
776 function GetCancel(Handle: PZPostgreSQLConnect): PZPostgreSQLCancel;
777 procedure FreeCancel( Canc: PZPostgreSQLCancel);
778 function Cancel( Canc: PZPostgreSQLCancel; Buffer: PChar; Length: Integer): Integer;
779 function GetLine(Handle: PZPostgreSQLConnect; Buffer: PAnsiChar;
780 Length: Integer): Integer;
781 function PutLine(Handle: PZPostgreSQLConnect; Buffer: PAnsiChar): Integer;
782 function GetLineAsync(Handle: PZPostgreSQLConnect; Buffer: PAnsiChar;
783 Length: Integer): Integer;
785 function PutBytes(Handle: PZPostgreSQLConnect; Buffer: PAnsiChar;
786 Length: Integer): Integer;
787 function EndCopy(Handle: PZPostgreSQLConnect): Integer;
788 function ExecuteFunction(Handle: PZPostgreSQLConnect; fnid: Integer;
789 result_buf, result_len: PInteger; result_is_int: Integer;
790 args: PZPostgreSQLArgBlock; nargs: Integer): PZPostgreSQLResult;
791 function GetResultStatus(Res: PZPostgreSQLResult): TZPostgreSQLExecStatusType;
792 function GetResultErrorMessage(Res: PZPostgreSQLResult): PAnsiChar;
793 function GetResultErrorField(Res: PZPostgreSQLResult;FieldCode:TZPostgreSQLFieldCode):PAnsiChar;
795 function GetRowCount(Res: PZPostgreSQLResult): Integer;
796 function GetFieldCount(Res: PZPostgreSQLResult): Integer;
798 function GetBinaryTuples(Res: PZPostgreSQLResult): Integer;
799 function GetFieldName(Res: PZPostgreSQLResult;
800 FieldNum: Integer): PAnsiChar;
801 function GetFieldNumber(Res: PZPostgreSQLResult;
802 FieldName: PAnsiChar): Integer;
803 function GetFieldTableOID(Res: PZPostgreSQLResult; FieldNum: Integer) : Oid;
804 function GetFieldTableColIdx(Res: PZPostgreSQLResult; FieldNum: Integer) : Integer;
805 function GetFieldType(Res: PZPostgreSQLResult;
806 FieldNum: Integer): Oid;
807 function GetFieldSize(Res: PZPostgreSQLResult;
808 FieldNum: Integer): Integer;
809 function GetFieldMode(Res: PZPostgreSQLResult;
810 FieldNum: Integer): Integer;
811 function GetCommandStatus(Res: PZPostgreSQLResult): PAnsiChar;
812 function GetOidValue(Res: PZPostgreSQLResult): Oid;
813 function GetOidStatus(Res: PZPostgreSQLResult): PAnsiChar;
814 function GetCommandTuples(Res: PZPostgreSQLResult): PAnsiChar;
816 function GetValue(Res: PZPostgreSQLResult;
817 TupNum, FieldNum: Integer): PAnsiChar;
818 function GetLength(Res: PZPostgreSQLResult;
819 TupNum, FieldNum: Integer): Integer;
820 function GetIsNull(Res: PZPostgreSQLResult;
821 TupNum, FieldNum: Integer): Integer;
822 procedure Clear(Res: PZPostgreSQLResult);
824 function MakeEmptyResult(Handle: PZPostgreSQLConnect;
825 Status: TZPostgreSQLExecStatusType): PZPostgreSQLResult;
827 function OpenLargeObject(Handle: PZPostgreSQLConnect; ObjId: Oid;
828 Mode: Integer): Integer;
829 function CloseLargeObject(Handle: PZPostgreSQLConnect;
830 Fd: Integer): Integer;
831 function ReadLargeObject(Handle: PZPostgreSQLConnect; Fd: Integer;
832 Buffer: PAnsiChar; Length: Integer): Integer;
833 function WriteLargeObject(Handle: PZPostgreSQLConnect; Fd: Integer;
834 Buffer: PAnsiChar; Length: Integer): Integer;
835 function SeekLargeObject(Handle: PZPostgreSQLConnect;
836 Fd, Offset, Whence: Integer): Integer;
837 function CreateLargeObject(Handle: PZPostgreSQLConnect;
839 function TellLargeObject(Handle: PZPostgreSQLConnect;
840 Fd: Integer): Integer;
841 function UnlinkLargeObject(Handle: PZPostgreSQLConnect;
842 ObjId: Oid): Integer;
843 function ImportLargeObject(Handle: PZPostgreSQLConnect;
844 FileName: PAnsiChar): Oid;
845 function ExportLargeObject(Handle: PZPostgreSQLConnect; ObjId: Oid;
846 FileName: PAnsiChar): Integer;
847 function GetPlainFunc:PAPI;
848 function EscapeString(Handle: Pointer; const Value: RawByteString;
849 ConSettings: PZConSettings; WasEncoded: Boolean = False): RawByteString; override;
852 {** Implements a driver for PostgreSQL 7.4 }
853 TZPostgreSQL7PlainDriver = class(TZPostgreSQLBaseDriver, IZPlainDriver,
854 IZPostgreSQLPlainDriver)
856 function Clone: IZPlainDriver; override;
860 function GetProtocol: string; override;
861 function GetDescription: string; override;
865 {** Implements a driver for PostgreSQL 8.1 }
867 { TZPostgreSQL8PlainDriver }
869 TZPostgreSQL8PlainDriver = class(TZPostgreSQLBaseDriver, IZPlainDriver,IZPostgreSQLPlainDriver)
871 function Clone: IZPlainDriver; override;
872 function GetUnicodeCodePageName: String; override;
873 procedure LoadCodePages; override;
877 function GetProtocol: string; override;
878 function GetDescription: string; override;
881 { TZPostgreSQL8PlainDriver }
883 TZPostgreSQL9PlainDriver = class(TZPostgreSQL8PlainDriver)
885 function Clone: IZPlainDriver; override;
886 function GetStandardConformingStrings: Boolean; override;
887 procedure LoadCodePages; override;
891 function GetProtocol: string; override;
892 function GetDescription: string; override;
897 uses SysUtils, ZPlainLoader, Classes, ZEncoding
898 {$IFDEF WITH_UNITANSISTRINGS}, AnsiStrings{$ENDIF};
900 { TZPostgreSQLBaseDriver }
902 function TZPostgreSQLBaseDriver.GetUnicodeCodePageName: String;
907 procedure TZPostgreSQLBaseDriver.LoadCodePages;
910 AddCodePage('EUC_JP', Ord(csEUC_JP), ceAnsi, zCP_EUC_JP, '', 3); { EUC_JP Japanese EUC }
911 AddCodePage('EUC_CN', Ord(csEUC_CN), ceAnsi, zCP_EUC_CN, '', 3); {EUC_CN Chinese EUC}
912 AddCodePage('EUC_KR', Ord(csEUC_KR), ceAnsi, zCP_euc_kr, '', 3); {Extended UNIX Code-KR Korean}
913 AddCodePage('JOHAB', Ord(csJOHAB), ceAnsi, ZCP_JOHAB, '', 3); {JOHAB Korean (Hangul)}
914 AddCodePage('EUC_TW', Ord(csEUC_TW), ceAnsi, $ffff, '', 3); {Extended UNIX Code-TW Traditional Chinese, Taiwanese}
915 AddCodePage('UNICODE', Ord(csUNICODE_PODBC), ceUTF8, zCP_UTF8, '', 4); {UNICODE Unicode (UTF-8)}
916 AddCodePage('MULE_INTERNAL', Ord(csMULE_INTERNAL), ceAnsi, $ffff, '', 4); { Mule internal code Multilingual Emacs }
918 AddCodePage('SQL_ASCII', Ord(csSQL_ASCII), ceAnsi, zCP_us_ascii); {unspecified (see text) any}
919 AddCodePage('LATIN1', Ord(csLATIN1), ceAnsi, zCP_WIN1252); { ISO 8859-1, ECMA 94 Western European }
920 AddCodePage('LATIN2', Ord(csLATIN2), ceAnsi, zCP_L2_ISO_8859_2); { ISO 8859-2, ECMA 94 Central European }
921 AddCodePage('LATIN3', Ord(csLATIN3), ceAnsi, zCP_L3_ISO_8859_3); { ISO 8859-3, ECMA 94 South European }
922 AddCodePage('LATIN4', Ord(csLATIN4), ceAnsi, zCP_L4_ISO_8859_4); { ISO 8859-4, ECMA 94 North European }
923 AddCodePage('LATIN5', Ord(csLATIN5), ceAnsi, zCP_L5_ISO_8859_9); { ISO 8859-9, ECMA 128 Turkish }
924 AddCodePage('LATIN6', Ord(csLATIN6), ceAnsi, zCP_L6_ISO_8859_10); { ISO 8859-10, ECMA 144 Nordic }
925 AddCodePage('LATIN7', Ord(csLATIN7), ceAnsi, zCP_L7_ISO_8859_13); { ISO 8859-13 Baltic }
926 AddCodePage('LATIN8', Ord(csLATIN8), ceAnsi, zCP_L8_ISO_8859_14); { ISO 8859-14 Celtic }
927 AddCodePage('LATIN9', Ord(csLATIN9), ceAnsi, zCP_L9_ISO_8859_15); { ISO 8859-15 LATIN1 with Euro and accents }
928 AddCodePage('LATIN10', Ord(csLATIN10), ceAnsi, zCP_L10_ISO_8859_16); { ISO 8859-16, ASRO SR 14111 Romanian }
929 AddCodePage('ISO_8859_5', Ord(csISO_8859_5), ceAnsi, zCP_L5_ISO_8859_5); { ISO 8859-5, ECMA 113 Latin/Cyrillic}
930 AddCodePage('ISO_8859_6', Ord(csISO_8859_6), ceAnsi, zCP_L6_ISO_8859_6); { ISO 8859-6, ECMA 114 Latin/Arabic }
931 AddCodePage('ISO_8859_7', Ord(csISO_8859_7), ceAnsi, zCP_L7_ISO_8859_7); { ISO 8859-7, ECMA 118 Latin/Greek }
932 AddCodePage('ISO_8859_8', Ord(csISO_8859_8), ceAnsi, zCP_L8_ISO_8859_8); { ISO 8859-8, ECMA 121 Latin/Hebrew }
933 AddCodePage('KOI8', Ord(csKOI8), ceAnsi, zCP_KOI8R); { KOI8-R(U) Cyrillic }
934 AddCodePage('WIN', Ord(csWIN), ceAnsi, zCP_WIN1251); { Windows CP1251 }
935 AddCodePage('ALT', Ord(csALT), ceAnsi, zCP_DOS866); { Windows CP866 }
936 AddCodePage('WIN1256', Ord(csWIN1256), ceAnsi, cCP_WIN1256); { Windows CP1256 Arabic }
937 AddCodePage('TCVN', Ord(csTCVN), ceAnsi, zCP_WIN1258); { TCVN-5712/Windows CP1258 (Vietnamese) }
938 AddCodePage('WIN874', Ord(csWIN874), ceAnsi, zCP_DOS874); { Windows CP874 (Thai) }
941 procedure TZPostgreSQLBaseDriver.LoadApi;
943 { ************** Load adresses of API Functions ************* }
946 { === in fe-connect.c === }
947 @POSTGRESQL_API.PQconnectdb := GetAddress('PQconnectdb');
948 @POSTGRESQL_API.PQsetdbLogin := GetAddress('PQsetdbLogin');
949 @POSTGRESQL_API.PQconndefaults := GetAddress('PQconndefaults');
950 @POSTGRESQL_API.PQfinish := GetAddress('PQfinish');
951 @POSTGRESQL_API.PQreset := GetAddress('PQreset');
952 @POSTGRESQL_API.PQrequestCancel := GetAddress('PQrequestCancel');
953 @POSTGRESQL_API.PQdb := GetAddress('PQdb');
954 @POSTGRESQL_API.PQuser := GetAddress('PQuser');
955 @POSTGRESQL_API.PQpass := GetAddress('PQpass');
956 @POSTGRESQL_API.PQhost := GetAddress('PQhost');
957 @POSTGRESQL_API.PQport := GetAddress('PQport');
958 @POSTGRESQL_API.PQtty := GetAddress('PQtty');
959 @POSTGRESQL_API.PQoptions := GetAddress('PQoptions');
960 @POSTGRESQL_API.PQstatus := GetAddress('PQstatus');
961 @POSTGRESQL_API.PQserverVersion:= GetAddress('PQserverVersion');
962 @POSTGRESQL_API.PQerrorMessage := GetAddress('PQerrorMessage');
963 @POSTGRESQL_API.PQsocket := GetAddress('PQsocket');
964 @POSTGRESQL_API.PQbackendPID := GetAddress('PQbackendPID');
965 @POSTGRESQL_API.PQtrace := GetAddress('PQtrace');
966 @POSTGRESQL_API.PQuntrace := GetAddress('PQuntrace');
967 @POSTGRESQL_API.PQsetNoticeProcessor := GetAddress('PQsetNoticeProcessor');
968 @POSTGRESQL_API.PQclientEncoding := GetAddress('PQclientEncoding');
969 { === in fe-exec.c === }
970 @POSTGRESQL_API.PQexec := GetAddress('PQexec');
971 @POSTGRESQL_API.PQexecParams := GetAddress('PQexecParams');
972 @POSTGRESQL_API.PQprepare := GetAddress('PQprepare');
973 @POSTGRESQL_API.PQexecPrepared := GetAddress('PQexecPrepared');
974 @POSTGRESQL_API.PQsendQuery := GetAddress('PQsendQuery');
975 @POSTGRESQL_API.PQsendQueryParams:= GetAddress('PQsendQueryParams');
976 @POSTGRESQL_API.PQsendPrepare := GetAddress('PQsendPrepare');
977 @POSTGRESQL_API.PQsendQueryPrepared := GetAddress('PQsendQueryPrepared');
978 @POSTGRESQL_API.PQgetResult := GetAddress('PQgetResult');
980 @POSTGRESQL_API.PQnotifies := GetAddress('PQnotifies');
981 @POSTGRESQL_API.PQfreeNotify := GetAddress('PQfreeNotify');
982 @POSTGRESQL_API.PQisBusy := GetAddress('PQisBusy');
983 @POSTGRESQL_API.PQconsumeInput := GetAddress('PQconsumeInput');
984 @POSTGRESQL_API.PQgetline := GetAddress('PQgetline');
985 @POSTGRESQL_API.PQputline := GetAddress('PQputline');
986 @POSTGRESQL_API.PQgetlineAsync := GetAddress('PQgetlineAsync');
987 @POSTGRESQL_API.PQputnbytes := GetAddress('PQputnbytes');
988 @POSTGRESQL_API.PQendcopy := GetAddress('PQendcopy');
989 @POSTGRESQL_API.PQfn := GetAddress('PQfn');
990 @POSTGRESQL_API.PQresultStatus := GetAddress('PQresultStatus');
991 @POSTGRESQL_API.PQresultErrorMessage := GetAddress('PQresultErrorMessage');
992 @POSTGRESQL_API.PQntuples := GetAddress('PQntuples');
993 @POSTGRESQL_API.PQnfields := GetAddress('PQnfields');
994 @POSTGRESQL_API.PQbinaryTuples := GetAddress('PQbinaryTuples');
995 @POSTGRESQL_API.PQfname := GetAddress('PQfname');
996 @POSTGRESQL_API.PQfnumber := GetAddress('PQfnumber');
997 @POSTGRESQL_API.PQftable := GetAddress('PQftable');
998 @POSTGRESQL_API.PQftablecol := GetAddress('PQftablecol');
999 @POSTGRESQL_API.PQftype := GetAddress('PQftype');
1000 @POSTGRESQL_API.PQfsize := GetAddress('PQfsize');
1001 @POSTGRESQL_API.PQfmod := GetAddress('PQfmod');
1002 @POSTGRESQL_API.PQcmdStatus := GetAddress('PQcmdStatus');
1003 @POSTGRESQL_API.PQoidValue := GetAddress('PQoidValue');
1004 @POSTGRESQL_API.PQoidStatus := GetAddress('PQoidStatus');
1005 @POSTGRESQL_API.PQcmdTuples := GetAddress('PQcmdTuples');
1006 @POSTGRESQL_API.PQgetvalue := GetAddress('PQgetvalue');
1007 @POSTGRESQL_API.PQgetlength := GetAddress('PQgetlength');
1008 @POSTGRESQL_API.PQgetisnull := GetAddress('PQgetisnull');
1009 @POSTGRESQL_API.PQclear := GetAddress('PQclear');
1010 @POSTGRESQL_API.PQmakeEmptyPGresult := GetAddress('PQmakeEmptyPGresult');
1012 { === in fe-lobj.c === }
1013 @POSTGRESQL_API.lo_open := GetAddress('lo_open');
1014 @POSTGRESQL_API.lo_close := GetAddress('lo_close');
1015 @POSTGRESQL_API.lo_read := GetAddress('lo_read');
1016 @POSTGRESQL_API.lo_write := GetAddress('lo_write');
1017 @POSTGRESQL_API.lo_lseek := GetAddress('lo_lseek');
1018 @POSTGRESQL_API.lo_creat := GetAddress('lo_creat');
1019 @POSTGRESQL_API.lo_tell := GetAddress('lo_tell');
1020 @POSTGRESQL_API.lo_unlink := GetAddress('lo_unlink');
1021 @POSTGRESQL_API.lo_import := GetAddress('lo_import');
1022 @POSTGRESQL_API.lo_export := GetAddress('lo_export');
1023 @POSTGRESQL_API.PQescapeStringConn := GetAddress('PQescapeStringConn'); //since 7.3
1024 @POSTGRESQL_API.PQescapeByteaConn := GetAddress('PQescapeByteaConn'); // postgresql since 7.3
1025 @POSTGRESQL_API.PQFreemem := GetAddress('PQfreemem'); // since postgresql 7.4
1026 @POSTGRESQL_API.PQescapeString := GetAddress('PQescapeString'); // since postgresql 7.4
1027 @POSTGRESQL_API.PQescapeBytea := GetAddress('PQescapeBytea'); // since postgresql 7.4
1028 @POSTGRESQL_API.PQunescapeBytea := GetAddress('PQunescapeBytea'); // since postgresql 8.3
1029 @POSTGRESQL_API.PQescapeLiteral := GetAddress('PQescapeLiteral'); // since postgresql 9.0
1030 @POSTGRESQL_API.PQescapeIdentifier := GetAddress('PQescapeIdentifier'); // since postgresql 9.0
1032 @POSTGRESQL_API.PQresultErrorField := GetAddress('PQresultErrorField');
1033 @POSTGRESQL_API.PQgetCancel := GetAddress('PQgetCancel');
1034 @POSTGRESQL_API.PQfreeCancel := GetAddress('PQfreeCancel');
1035 @POSTGRESQL_API.PQcancel := GetAddress('PQcancel');
1039 constructor TZPostgreSQLBaseDriver.Create;
1042 FLoader := TZNativeLibraryLoader.Create([]);
1043 {$IFNDEF STRICT_DLL_LOADING}
1045 FLoader.AddLocation(WINDOWS_DLL_LOCATION);
1047 FLoader.AddLocation(LINUX_DLL_LOCATION);
1053 procedure TZPostgreSQLBaseDriver.Clear(Res: PZPostgreSQLResult);
1055 POSTGRESQL_API.PQclear(Res);
1058 function TZPostgreSQLBaseDriver.CloseLargeObject(
1059 Handle: PZPostgreSQLConnect; Fd: Integer): Integer;
1061 Result := POSTGRESQL_API.lo_close(Handle, Fd);
1064 function TZPostgreSQLBaseDriver.ConnectDatabase(
1065 ConnInfo: PAnsiChar): PZPostgreSQLConnect;
1067 Result := POSTGRESQL_API.PQconnectdb(ConnInfo);
1070 function TZPostgreSQLBaseDriver.ConsumeInput(
1071 Handle: PZPostgreSQLConnect): Integer;
1073 Result := POSTGRESQL_API.PQconsumeInput(Handle);
1076 function TZPostgreSQLBaseDriver.GetCancel(Handle: PZPostgreSQLConnect): PZPostgreSQLCancel;
1078 if Assigned(POSTGRESQL_API.PQgetCancel) then
1079 Result := POSTGRESQL_API.PQgetCancel(Handle)
1084 procedure TZPostgreSQLBaseDriver.FreeCancel(Canc: PZPostgreSQLCancel);
1086 if Assigned(POSTGRESQL_API.PQfreeCancel) then
1087 POSTGRESQL_API.PQfreeCancel( Canc);
1090 function TZPostgreSQLBaseDriver.Cancel(Canc: PZPostgreSQLCancel; Buffer: PChar; Length: Integer): Integer;
1092 if Assigned(POSTGRESQL_API.PQcancel) then
1093 Result := POSTGRESQL_API.PQcancel( Canc, Buffer, Length)
1098 function TZPostgreSQLBaseDriver.CreateLargeObject(
1099 Handle: PZPostgreSQLConnect; Mode: Integer): Oid;
1101 Result := POSTGRESQL_API.lo_creat(Handle, Mode);
1104 function TZPostgreSQLBaseDriver.DecodeBYTEA(const value: RawByteString;
1105 const Is_bytea_output_hex: Boolean; Handle: PZPostgreSQLConnect): RawByteString;
1111 if ( Is_bytea_output_hex ) then
1113 Len := (Length(value)-{$IFDEF WITH_UNITANSISTRINGS}AnsiStrings.AnsiPos{$ELSE}Pos{$ENDIF}('x', value)) div 2; //GetLength of binary result
1114 Ansi := AnsiString(Copy(value, {$IFDEF WITH_UNITANSISTRINGS}AnsiStrings.AnsiPos{$ELSE}Pos{$ENDIF}('x', value)+1, Length(value))); //remove the first 'x'sign-byte
1115 SetLength(Result, Len); //Set length of binary-result
1116 HexToBin(PAnsiChar(Ansi), PAnsichar(Result), Len); //convert hex to binary
1119 if Assigned(POSTGRESQL_API.PQUnescapeBytea) then
1121 decoded := POSTGRESQL_API.PQUnescapeBytea(PAnsiChar(value), @len);
1122 SetLength(result, len);
1124 Move(decoded^, result[1], len);
1125 if Assigned(POSTGRESQL_API.PQFreemem) then
1126 POSTGRESQL_API.PQFreemem(decoded);
1132 function TZPostgreSQLBaseDriver.SupportsEncodeBYTEA: Boolean;
1134 Result := Assigned(POSTGRESQL_API.PQescapeByteaConn) or
1135 Assigned(POSTGRESQL_API.PQescapeBytea);
1138 function TZPostgreSQLBaseDriver.SupportsDecodeBYTEA(const Handle: PZPostgreSQLConnect): Boolean;
1140 Result := ( POSTGRESQL_API.PQserverVersion(Handle) div 10000 >= 9 ) or
1141 Assigned(POSTGRESQL_API.PQUnescapeBytea);
1144 function TZPostgreSQLBaseDriver.SupportsStringEscaping(const ClientDependend: Boolean): Boolean;
1146 if ClientDependend then
1147 Result := Assigned(POSTGRESQL_API.PQescapeStringConn)
1149 Result := Assigned(POSTGRESQL_API.PQescapeStringConn) or
1150 Assigned(POSTGRESQL_API.PQescapeString);
1153 function TZPostgreSQLBaseDriver.EncodeBYTEA(const Value: RawByteString;
1154 Handle: PZPostgreSQLConnect; Quoted: Boolean = True): RawByteString;
1160 if assigned(POSTGRESQL_API.PQescapeByteaConn) or
1161 Assigned(POSTGRESQL_API.PQescapeBytea) then
1163 leng := Length(Value);
1164 if assigned(POSTGRESQL_API.PQescapeByteaConn) then
1165 encoded := POSTGRESQL_API.PQescapeByteaConn(Handle, PAnsiChar(value), leng, @len)
1167 encoded := POSTGRESQL_API.PQescapeBytea(PAnsiChar(value),leng,@len);
1168 SetLength(result, len -1); //removes the #0 byte
1170 {$IFDEF WITH_STRLCOPY_DEPRECATED}AnsiStrings.{$ENDIF}StrLCopy(PAnsiChar(result), encoded, len - 1);
1172 POSTGRESQL_API.PQFreemem(encoded);
1174 result := ''''+result+'''';
1180 function TZPostgreSQLBaseDriver.EndCopy( Handle: PZPostgreSQLConnect): Integer;
1182 Result := POSTGRESQL_API.PQendcopy(Handle);
1185 function TZPostgreSQLBaseDriver.ExecuteFunction(
1186 Handle: PZPostgreSQLConnect; fnid: Integer; result_buf,
1187 result_len: PInteger; result_is_int: Integer; args: PZPostgreSQLArgBlock;
1188 nargs: Integer): PZPostgreSQLResult;
1190 Result := POSTGRESQL_API.PQfn(Handle, fnid, result_buf,
1191 result_len, result_is_int, PPQArgBlock(args), nargs);
1194 function TZPostgreSQLBaseDriver.ExecuteQuery(
1195 Handle: PZPostgreSQLConnect; Query: PAnsiChar): PZPostgreSQLResult;
1197 Result := POSTGRESQL_API.PQexec(Handle, Query);
1200 function TZPostgreSQLBaseDriver.ExecParams(Handle: PPGconn; command: PAnsichar;
1201 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
1202 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
1203 resultFormat: Integer): PPGresult;
1205 if Assigned(POSTGRESQL_API.PQexecParams) then
1206 Result := POSTGRESQL_API.PQexecParams(Handle, command, nParams, paramtypes,
1207 paramValues, paramLengths, paramFormats, resultFormat)
1212 function TZPostgreSQLBaseDriver.Prepare(Handle: PPGconn; stmtName: PAnsichar;
1213 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): PPGresult;
1215 if Assigned(POSTGRESQL_API.PQprepare) then
1216 Result := POSTGRESQL_API.PQprepare(Handle, stmtName, query, nParams, paramTypes)
1221 function TZPostgreSQLBaseDriver.ExecPrepared(Handle: PPGconn; stmtName: PAnsichar;
1222 nParams: Integer; paramValues: TPQparamValues; paramLengths: TPQparamLengths;
1223 paramFormats: TPQparamFormats; resultFormat: Integer): PPGresult;
1225 if Assigned(POSTGRESQL_API.PQexecPrepared) then
1226 Result := POSTGRESQL_API.PQexecPrepared(Handle, stmtName, nParams,
1227 paramValues, paramLengths, paramFormats, resultFormat)
1232 function TZPostgreSQLBaseDriver.SendQuery(Handle: PZPostgreSQLConnect; Query: PAnsiChar): Integer;
1234 Result := POSTGRESQL_API.PQsendQuery(Handle, Query);
1237 function TZPostgreSQLBaseDriver.SendQueryParams(Handle: PPGconn; command: PAnsichar;
1238 nParams: Integer; paramTypes: TPQparamTypes; paramValues: TPQparamValues;
1239 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
1240 resultFormat: Integer): Integer;
1242 if Assigned(POSTGRESQL_API.PQsendQueryParams) then
1243 Result := POSTGRESQL_API.PQsendQueryParams(Handle, command, nParams,
1244 paramTypes, paramValues, paramLengths, paramFormats, resultFormat)
1249 function TZPostgreSQLBaseDriver.SendPrepare(Handle: PPGconn; stmtName: PAnsichar;
1250 query: PAnsiChar; nParams: Integer; paramTypes: TPQparamTypes): Integer;
1252 if Assigned(POSTGRESQL_API.PQsendPrepare) then
1253 Result := POSTGRESQL_API.PQsendPrepare(Handle, stmtName, query, nParams,
1259 function TZPostgreSQLBaseDriver.SendQueryPrepared(Handle: PPGconn; stmtName: PAnsichar;
1260 nParams: Integer; paramValues: TPQparamValues;
1261 paramLengths: TPQparamLengths; paramFormats: TPQparamFormats;
1262 resultFormat: Integer): Integer;
1264 if Assigned(POSTGRESQL_API.PQsendQueryPrepared) then
1265 Result := POSTGRESQL_API.PQsendQueryPrepared(Handle, stmtName, nParams,
1266 paramValues, paramLengths, paramFormats, resultFormat)
1271 function TZPostgreSQLBaseDriver.GetResult(Handle: PZPostgreSQLConnect): PZPostgreSQLResult;
1273 Result := POSTGRESQL_API.PQgetResult(Handle);
1276 function TZPostgreSQLBaseDriver.DescribePrepared(Handle: PPGconn;
1277 const stmt: PAnsiChar): PPGresult;
1279 if Assigned(POSTGRESQL_API.PQdescribePrepared) then
1280 Result := POSTGRESQL_API.PQdescribePrepared(Handle, stmt)
1285 function TZPostgreSQLBaseDriver.DescribePortal(Handle: PPGconn;
1286 const portal: PAnsiChar): PPGresult;
1288 if Assigned(POSTGRESQL_API.PQdescribePortal) then
1289 Result := POSTGRESQL_API.PQdescribePortal(Handle, portal)
1294 function TZPostgreSQLBaseDriver.SendDescribePrepared(Handle: PPGconn;
1295 const stmt: PAnsiChar): Integer;
1297 if Assigned(POSTGRESQL_API.PQsendDescribePrepared) then
1298 Result := POSTGRESQL_API.PQsendDescribePrepared(Handle, stmt)
1303 function TZPostgreSQLBaseDriver.SendDescribePortal(Handle: PPGconn;
1304 const portal: PAnsiChar): Integer;
1306 if Assigned(POSTGRESQL_API.PQsendDescribePortal) then
1307 Result := POSTGRESQL_API.PQsendDescribePortal(Handle, portal)
1312 function TZPostgreSQLBaseDriver.ExportLargeObject(
1313 Handle: PZPostgreSQLConnect; ObjId: Oid; FileName: PAnsiChar): Integer;
1315 Result := POSTGRESQL_API.lo_export(Handle, ObjId, FileName);
1318 procedure TZPostgreSQLBaseDriver.Finish(Handle: PZPostgreSQLConnect);
1320 POSTGRESQL_API.PQfinish(Handle);
1323 procedure TZPostgreSQLBaseDriver.FreeNotify(Handle: PZPostgreSQLNotify);
1325 POSTGRESQL_API.PQfreeNotify(PPGnotify(Handle));
1328 function TZPostgreSQLBaseDriver.GetBackendPID(
1329 Handle: PZPostgreSQLConnect): Integer;
1331 Result := POSTGRESQL_API.PQbackendPID(Handle);
1334 function TZPostgreSQLBaseDriver.GetBinaryTuples(
1335 Res: PZPostgreSQLResult): Integer;
1337 Result := POSTGRESQL_API.PQbinaryTuples(Res);
1340 function TZPostgreSQLBaseDriver.GetCommandStatus(
1341 Res: PZPostgreSQLResult): PAnsiChar;
1343 Result := POSTGRESQL_API.PQcmdStatus(Res);
1346 function TZPostgreSQLBaseDriver.GetCommandTuples(
1347 Res: PZPostgreSQLResult): PAnsiChar;
1349 Result := POSTGRESQL_API.PQcmdTuples(Res);
1352 function TZPostgreSQLBaseDriver.GetConnectDefaults:
1353 PZPostgreSQLConnectInfoOption;
1355 Result := PZPostgreSQLConnectInfoOption(POSTGRESQL_API.PQconndefaults);
1358 function TZPostgreSQLBaseDriver.GetDatabase(
1359 Handle: PZPostgreSQLConnect): PAnsiChar;
1361 Result := POSTGRESQL_API.PQdb(Handle);
1364 function TZPostgreSQLBaseDriver.GetErrorMessage(
1365 Handle: PZPostgreSQLConnect): PAnsiChar;
1367 Result := POSTGRESQL_API.PQerrorMessage(Handle);
1370 function TZPostgreSQLBaseDriver.GetFieldCount(
1371 Res: PZPostgreSQLResult): Integer;
1373 Result := POSTGRESQL_API.PQnfields(Res);
1376 function TZPostgreSQLBaseDriver.GetFieldMode(Res: PZPostgreSQLResult;
1377 FieldNum: Integer): Integer;
1379 Result := POSTGRESQL_API.PQfmod(Res, FieldNum);
1382 function TZPostgreSQLBaseDriver.GetFieldName(Res: PZPostgreSQLResult;
1383 FieldNum: Integer): PAnsiChar;
1385 Result := POSTGRESQL_API.PQfname(Res, FieldNum);
1388 function TZPostgreSQLBaseDriver.GetFieldNumber(
1389 Res: PZPostgreSQLResult; FieldName: PAnsiChar): Integer;
1391 Result := POSTGRESQL_API.PQfnumber(Res, FieldName);
1394 function TZPostgreSQLBaseDriver.GetFieldTableOID(Res: PZPostgreSQLResult; FieldNum: Integer) : Oid;
1396 Result := POSTGRESQL_API.PQftable(Res, FieldNum);
1399 function TZPostgreSQLBaseDriver.GetFieldTableColIdx(Res: PZPostgreSQLResult; FieldNum: Integer) : Integer;
1401 Result := POSTGRESQL_API.PQftablecol(Res, FieldNum);
1404 function TZPostgreSQLBaseDriver.GetFieldSize(Res: PZPostgreSQLResult;
1405 FieldNum: Integer): Integer;
1407 Result := POSTGRESQL_API.PQfsize(Res, FieldNum);
1410 function TZPostgreSQLBaseDriver.GetFieldType(Res: PZPostgreSQLResult;
1411 FieldNum: Integer): Oid;
1413 Result := POSTGRESQL_API.PQftype(Res, FieldNum);
1416 function TZPostgreSQLBaseDriver.GetHost(
1417 Handle: PZPostgreSQLConnect): PAnsiChar;
1419 Result := POSTGRESQL_API.PQhost(Handle);
1422 function TZPostgreSQLBaseDriver.GetIsNull(Res: PZPostgreSQLResult;
1423 TupNum, FieldNum: Integer): Integer;
1425 Result := POSTGRESQL_API.PQgetisnull(Res, TupNum, FieldNum);
1428 function TZPostgreSQLBaseDriver.GetLength(Res: PZPostgreSQLResult;
1429 TupNum, FieldNum: Integer): Integer;
1431 Result := POSTGRESQL_API.PQgetlength(Res, TupNum, FieldNum);
1434 function TZPostgreSQLBaseDriver.GetLine(Handle: PZPostgreSQLConnect;
1435 Buffer: PAnsiChar; Length: Integer): Integer;
1437 Result := POSTGRESQL_API.PQgetline(Handle, Buffer, Length);
1440 function TZPostgreSQLBaseDriver.GetLineAsync(
1441 Handle: PZPostgreSQLConnect; Buffer: PAnsiChar; Length: Integer): Integer;
1443 Result := POSTGRESQL_API.PQgetlineAsync(Handle, Buffer, Length);
1446 function TZPostgreSQLBaseDriver.GetOidStatus(
1447 Res: PZPostgreSQLResult): PAnsiChar;
1449 Result := POSTGRESQL_API.PQoidStatus(Res);
1452 function TZPostgreSQLBaseDriver.GetOidValue(
1453 Res: PZPostgreSQLResult): Oid;
1455 Result := POSTGRESQL_API.PQoidValue(Res);
1458 function TZPostgreSQLBaseDriver.GetOptions(
1459 Handle: PZPostgreSQLConnect): PAnsiChar;
1461 Result := POSTGRESQL_API.PQoptions(Handle);
1464 function TZPostgreSQLBaseDriver.GetPassword(
1465 Handle: PZPostgreSQLConnect): PAnsiChar;
1467 Result := POSTGRESQL_API.PQpass(Handle);
1470 function TZPostgreSQLBaseDriver.GetPort(
1471 Handle: PZPostgreSQLConnect): PAnsiChar;
1473 Result := POSTGRESQL_API.PQport(Handle);
1476 function TZPostgreSQLBaseDriver.GetResultErrorField(Res: PZPostgreSQLResult; FieldCode: TZPostgreSQLFieldCode): PAnsiChar;
1478 if Assigned(POSTGRESQL_API.PQresultErrorField) then
1479 Result := POSTGRESQL_API.PQresultErrorField(Res, ord(FieldCode))
1485 function TZPostgreSQLBaseDriver.GetResultErrorMessage(
1486 Res: PZPostgreSQLResult): PAnsiChar;
1488 Result := POSTGRESQL_API.PQresultErrorMessage(Res);
1491 function TZPostgreSQLBaseDriver.GetResultStatus(
1492 Res: PZPostgreSQLResult): TZPostgreSQLExecStatusType;
1494 Result := TZPostgreSQLExecStatusType(POSTGRESQL_API.PQresultStatus(Res));
1497 function TZPostgreSQLBaseDriver.GetRowCount(
1498 Res: PZPostgreSQLResult): Integer;
1500 Result := POSTGRESQL_API.PQntuples(Res);
1503 function TZPostgreSQLBaseDriver.GetSocket(
1504 Handle: PZPostgreSQLConnect): Integer;
1506 Result := POSTGRESQL_API.PQsocket(Handle);
1509 function TZPostgreSQLBaseDriver.GetStandardConformingStrings: Boolean;
1514 function TZPostgreSQLBaseDriver.GetStatus(
1515 Handle: PZPostgreSQLConnect): TZPostgreSQLConnectStatusType;
1517 Result := TZPostgreSQLConnectStatusType(POSTGRESQL_API.PQstatus(Handle));
1520 function TZPostgreSQLBaseDriver.GetClientEncoding(Handle: PPGconn): Integer; //EgonHugeist
1522 Result := POSTGRESQL_API.PQclientEncoding(Handle);
1525 function TZPostgreSQLBaseDriver.GetTTY(
1526 Handle: PZPostgreSQLConnect): PAnsiChar;
1528 Result := POSTGRESQL_API.PQtty(Handle);
1531 function TZPostgreSQLBaseDriver.GetUser(
1532 Handle: PZPostgreSQLConnect): PAnsiChar;
1534 Result := POSTGRESQL_API.PQuser(Handle);
1537 function TZPostgreSQLBaseDriver.GetValue(Res: PZPostgreSQLResult;
1538 TupNum, FieldNum: Integer): PAnsiChar;
1540 Result := POSTGRESQL_API.PQgetvalue(Res, TupNum, FieldNum);
1543 function TZPostgreSQLBaseDriver.ImportLargeObject(
1544 Handle: PZPostgreSQLConnect; FileName: PAnsiChar): Oid;
1546 Result := POSTGRESQL_API.lo_import(Handle, FileName);
1549 function TZPostgreSQLBaseDriver.IsBusy(
1550 Handle: PZPostgreSQLConnect): Integer;
1552 Result := POSTGRESQL_API.PQisBusy(Handle);
1555 function TZPostgreSQLBaseDriver.MakeEmptyResult(
1556 Handle: PZPostgreSQLConnect;
1557 Status: TZPostgreSQLExecStatusType): PZPostgreSQLResult;
1559 Result := POSTGRESQL_API.PQmakeEmptyPGresult(Handle,
1560 TZPostgreSQLExecStatusType(Status));
1563 function TZPostgreSQLBaseDriver.Notifies(
1564 Handle: PZPostgreSQLConnect): PZPostgreSQLNotify;
1566 Result := PZPostgreSQLNotify(POSTGRESQL_API.PQnotifies(Handle));
1569 function TZPostgreSQLBaseDriver.OpenLargeObject(
1570 Handle: PZPostgreSQLConnect; ObjId: Oid; Mode: Integer): Integer;
1572 Result := POSTGRESQL_API.lo_open(Handle, ObjId, Mode);
1575 function TZPostgreSQLBaseDriver.PutBytes(Handle: PZPostgreSQLConnect;
1576 Buffer: PAnsiChar; Length: Integer): Integer;
1578 Result := POSTGRESQL_API.PQputnbytes(Handle, Buffer, Length);
1581 function TZPostgreSQLBaseDriver.PutLine(Handle: PZPostgreSQLConnect;
1582 Buffer: PAnsiChar): Integer;
1584 Result := POSTGRESQL_API.PQputline(Handle, Buffer);
1587 function TZPostgreSQLBaseDriver.ReadLargeObject(
1588 Handle: PZPostgreSQLConnect; Fd: Integer; Buffer: PAnsiChar;
1589 Length: Integer): Integer;
1591 Result := POSTGRESQL_API.lo_read(Handle, Fd, Buffer, Length);
1594 function TZPostgreSQLBaseDriver.RequestCancel(
1595 Handle: PZPostgreSQLConnect): Integer;
1597 Result := POSTGRESQL_API.PQrequestCancel(Handle);
1600 procedure TZPostgreSQLBaseDriver.Reset(Handle: PZPostgreSQLConnect);
1602 POSTGRESQL_API.PQreset(Handle);
1605 function TZPostgreSQLBaseDriver.SeekLargeObject(
1606 Handle: PZPostgreSQLConnect; Fd, Offset, Whence: Integer): Integer;
1608 Result := POSTGRESQL_API.lo_lseek(Handle, Fd, Offset, Whence);
1611 function TZPostgreSQLBaseDriver.SetDatabaseLogin(Host, Port, Options,
1612 TTY, Db, User, Passwd: PAnsiChar): PZPostgreSQLConnect;
1614 Result := POSTGRESQL_API.PQsetdbLogin(Host, Port, Options, TTY, Db,
1618 procedure TZPostgreSQLBaseDriver.SetNoticeProcessor(
1619 Handle: PZPostgreSQLConnect; Proc: TZPostgreSQLNoticeProcessor;
1622 POSTGRESQL_API.PQsetNoticeProcessor(Handle, Proc, Arg);
1625 function TZPostgreSQLBaseDriver.TellLargeObject(
1626 Handle: PZPostgreSQLConnect; Fd: Integer): Integer;
1628 Result := POSTGRESQL_API.lo_tell(Handle, Fd);
1631 procedure TZPostgreSQLBaseDriver.Trace(Handle: PZPostgreSQLConnect;
1632 DebugPort: Pointer);
1634 POSTGRESQL_API.PQtrace(Handle, DebugPort);
1637 function TZPostgreSQLBaseDriver.UnlinkLargeObject(
1638 Handle: PZPostgreSQLConnect; ObjId: Oid): Integer;
1640 Result := POSTGRESQL_API.lo_unlink(Handle, ObjId);
1643 procedure TZPostgreSQLBaseDriver.Untrace(Handle: PZPostgreSQLConnect);
1645 POSTGRESQL_API.PQuntrace(Handle);
1648 function TZPostgreSQLBaseDriver.WriteLargeObject(
1649 Handle: PZPostgreSQLConnect; Fd: Integer; Buffer: PAnsiChar;
1650 Length: Integer): Integer;
1652 Result := POSTGRESQL_API.lo_write(Handle, Fd, Buffer, Length);
1655 function TZPostgreSQLBaseDriver.GetPlainFunc():PAPI;
1657 result:= @POSTGRESQL_API;
1660 function TZPostgreSQLBaseDriver.EscapeString(Handle: Pointer; const Value: RawByteString;
1661 ConSettings: PZConSettings; WasEncoded: Boolean = False): RawByteString;
1665 SourceTemp: RawByteString;
1668 if ( Assigned(POSTGRESQL_API.PQescapeStringConn) or
1669 Assigned(POSTGRESQL_API.PQescapeString) ) and ( Value <> '' )then
1673 SourceTemp := Value;
1678 SourceTemp := ZPlainString(Value, ConSettings); //check encoding too
1680 GetMem(Temp, Length(SourceTemp)*2);
1681 if Assigned(POSTGRESQL_API.PQescapeStringConn) then
1682 ResLen := POSTGRESQL_API.PQescapeStringConn(Handle, Temp,
1685 PAnsiChar(SourceTemp), {$IFDEF WITH_STRLEN_DEPRECATED}AnsiStrings.{$ENDIF}StrLen(PAnsiChar(SourceTemp)), @IError)
1687 ResLen := POSTGRESQL_API.PQescapeString(Temp, PAnsiChar(SourceTemp),
1688 {$IFDEF WITH_STRLEN_DEPRECATED}AnsiStrings.{$ENDIF}StrLen(PAnsiChar(SourceTemp)));
1689 if not (IError = 0) then
1690 raise Exception.Create('Wrong escape behavior!');
1691 SetLength(Result, ResLen);
1692 Move(Temp^, PAnsiChar(Result)^, ResLen);
1697 Result := #39+Result+#39;
1700 { TZPostgreSQL7PlainDriver }
1702 function TZPostgreSQL7PlainDriver.Clone: IZPlainDriver;
1704 Result := TZPostgreSQL7PlainDriver.Create;
1707 constructor TZPostgreSQL7PlainDriver.Create;
1711 FLoader.AddLocation(WINDOWS_DLL7_LOCATION);
1715 function TZPostgreSQL7PlainDriver.GetProtocol: string;
1717 Result := 'postgresql-7';
1720 function TZPostgreSQL7PlainDriver.GetDescription: string;
1722 Result := 'Native Plain Driver for PostgreSQL 7.x';
1725 { TZPostgreSQL8PlainDriver }
1726 function TZPostgreSQL8PlainDriver.Clone: IZPlainDriver;
1728 Result := TZPostgreSQL8PlainDriver.Create;
1731 function TZPostgreSQL8PlainDriver.GetUnicodeCodePageName: String;
1736 procedure TZPostgreSQL8PlainDriver.LoadCodePages;
1738 inherited LoadCodePages;
1741 ResetCodePage(Ord(csUNICODE_PODBC), 'UTF8', Ord(csUTF8), ceUTF8, zCP_UTF8, '', 4); { Unicode, 8-bit all }
1742 AddCodePage('BIG5', Ord(csBIG5), ceAnsi, zCP_Big5, '', 2); { Big Five Traditional Chinese }
1743 AddCodePage('GB18030', Ord(csGB18030), ceAnsi, zCP_GB18030, '', 2); { National Standard Chinese }
1744 AddCodePage('GBK', Ord(csGBK), ceAnsi, zCP_GB2312, '', 2); { Extended National Standard Simplified Chinese }
1745 AddCodePage('SJIS', Ord(csSJIS), ceAnsi, zCP_SHIFTJS, '', 2); { Shift JIS Japanese }
1746 AddCodePage('UHC', Ord(csUHC), ceAnsi, zCP_EUCKR, '', 2); { Unified Hangul Code Korean }
1748 ResetCodePage(Ord(csALT), 'WIN866', Ord(csWIN866), ceAnsi, zCP_DOS866); { Windows CP866 Cyrillic } //No longer in use
1749 AddCodePage('WIN874', Ord(csWIN874), ceAnsi, zCP_DOS874); { Windows CP874 Thai }
1750 AddCodePage('WIN1250', Ord(csWIN1250), ceAnsi, zCP_WIN1250); { Windows CP1250 Central European }
1751 ResetCodePage(Ord(csWIN), 'WIN1251', Ord(csWIN1251), ceAnsi, zCP_WIN1251); { Windows CP1251 Cyrillic } //No longer in use
1752 AddCodePage('WIN1252', Ord(csWIN1252), ceAnsi, zCP_WIN1252); { Windows CP1252 Western European }
1753 ResetCodePage(Ord(csTCVN), 'WIN1258', Ord(csWIN1258),ceAnsi, zCP_WIN1258); { Windows CP1258 Vietnamese } //No longer in use
1757 AddCodePage('EUC_JIS_2004', Ord(csEUC_JIS_2004), ceAnsi, $ffff, '', 3); { Extended UNIX Code-JP, JIS X 0213 Japanese }
1758 AddCodePage('SHIFT_JIS_2004', Ord(csSHIFT_JIS_2004), ceAnsi, zCP_SHIFTJS, '', 3); { Shift JIS, JIS X 0213 Japanese }
1760 AddCodePage('WIN1253', Ord(csWIN1253), ceAnsi, zCP_WIN1253); { Windows CP1253 Greek }
1761 AddCodePage('WIN1254', Ord(csWIN1254), ceAnsi, zCP_WIN1254); { Windows CP1254 Turkish }
1762 AddCodePage('WIN1255', Ord(csWIN1255), ceAnsi, zCP_WIN1255); { Windows CP1255 Hebrew }
1763 AddCodePage('WIN1257', Ord(csWIN1257), ceAnsi, zCP_WIN1257); { Windows CP1257 Baltic }
1767 AddCodePage('KOI8U', Ord(csKOI8U), ceAnsi, zCP_KOI8U); { KOI8-U Cyrillic (Ukrainian) }
1770 constructor TZPostgreSQL8PlainDriver.Create;
1774 FLoader.AddLocation(WINDOWS_DLL8_LOCATION);
1776 FLoader.AddLocation(LINUX_DLL82_LOCATION);
1777 FLoader.AddLocation(LINUX_DLL8_LOCATION);
1781 function TZPostgreSQL8PlainDriver.GetProtocol: string;
1783 Result := 'postgresql-8';
1786 function TZPostgreSQL8PlainDriver.GetDescription: string;
1788 Result := 'Native Plain Driver for PostgreSQL 8.x';
1791 { TZPostgreSQL9PlainDriver }
1792 function TZPostgreSQL9PlainDriver.Clone: IZPlainDriver;
1794 Result := TZPostgreSQL9PlainDriver.Create;
1797 procedure TZPostgreSQL9PlainDriver.LoadCodePages;
1799 inherited LoadCodePages;
1800 ResetCodePage(Ord(csKOI8), 'KOI8R', Ord(csKOI8R)); { KOI8-R Cyrillic (Russian) } //No longer in use
1803 constructor TZPostgreSQL9PlainDriver.Create;
1806 Self.FLoader.ClearLocations;
1807 {$IFNDEF STRICT_DLL_LOADING}
1809 FLoader.AddLocation(WINDOWS_DLL_LOCATION);
1811 FLoader.AddLocation(LINUX_DLL_LOCATION);
1816 function TZPostgreSQL9PlainDriver.GetProtocol: string;
1818 Result := 'postgresql-9';
1821 function TZPostgreSQL9PlainDriver.GetDescription: string;
1823 Result := 'Native Plain Driver for PostgreSQL 9.x';
1826 function TZPostgreSQL9PlainDriver.GetStandardConformingStrings: Boolean;