2 // Mono.Data.PostgreSqlClient.PostgresLibrary.cs
4 // PInvoke methods to libpq
5 // which is PostgreSQL client library
7 // May also contain enumerations,
8 // data types, or wrapper methods.
11 // Rodrigo Moya (rodrigo@ximian.com)
12 // Daniel Morgan (danmorg@sc.rr.com)
14 // (C) Ximian, Inc 2002
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 // *** uncomment #define to get debug messages, comment for production ***
39 //#define DEBUG_PostgresLibrary
43 using System
.Runtime
.InteropServices
;
44 using System
.Diagnostics
;
45 using System
.Collections
;
47 namespace Mono
.Data
.PostgreSqlClient
{
49 /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS BELOW */
51 internal enum ConnStatusType
57 CONNECTION_AWAITING_RESPONSE
,
62 internal enum PostgresPollingStatusType
64 PGRES_POLLING_FAILED
= 0,
65 PGRES_POLLING_READING
,
66 PGRES_POLLING_WRITING
,
71 internal enum ExecStatusType
73 PGRES_EMPTY_QUERY
= 0,
83 sealed internal class PostgresLibrary
85 #region PInvoke Functions
87 // pinvoke prototypes to PostgreSQL client library
88 // pq.dll on windows and libpq.so on linux
91 public static extern IntPtr
PQconnectStart (string conninfo
);
92 // PGconn *PQconnectStart(const char *conninfo);
95 public static extern PostgresPollingStatusType
PQconnectPoll (IntPtr conn
);
96 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);
99 public static extern IntPtr
PQconnectdb (string conninfo
);
100 // PGconn *PQconnectdb(const char *conninfo);
103 public static extern IntPtr
PQsetdbLogin (string pghost
,
104 string pgport
, string pgoptions
,
105 string pgtty
, string dbName
,
106 string login
, string pwd
);
107 // PGconn *PQsetdbLogin(const char *pghost,
108 // const char *pgport, const char *pgoptions,
109 // const char *pgtty, const char *dbName,
110 // const char *login, const char *pwd);
113 public static extern void PQfinish (IntPtr conn
);
114 // void PQfinish(PGconn *conn);
117 public static extern IntPtr
PQconndefaults ();
118 // PQconninfoOption *PQconndefaults(void);
121 public static extern void PQconninfoFree (IntPtr connOptions
);
122 // void PQconninfoFree(PQconninfoOption *connOptions);
125 public static extern int PQresetStart (IntPtr conn
);
126 // int PQresetStart(PGconn *conn);
129 public static extern IntPtr
PQresetPoll (IntPtr conn
);
130 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
133 public static extern void PQreset (IntPtr conn
);
134 // void PQreset(PGconn *conn);
137 public static extern int PQrequestCancel (IntPtr conn
);
138 // int PQrequestCancel(PGconn *conn);
141 public static extern string PQdb (IntPtr conn
);
142 // char *PQdb(const PGconn *conn);
145 public static extern string PQuser (IntPtr conn
);
146 // char *PQuser(const PGconn *conn);
149 public static extern string PQpass (IntPtr conn
);
150 // char *PQpass(const PGconn *conn);
153 public static extern string PQhost (IntPtr conn
);
154 // char *PQhost(const PGconn *conn);
157 public static extern string PQport (IntPtr conn
);
158 // char *PQport(const PGconn *conn);
161 public static extern string PQtty (IntPtr conn
);
162 // char *PQtty(const PGconn *conn);
165 public static extern string PQoptions (IntPtr conn
);
166 // char *PQoptions(const PGconn *conn);
169 public static extern ConnStatusType
PQstatus (IntPtr conn
);
170 // ConnStatusType PQstatus(const PGconn *conn);
173 public static extern string PQerrorMessage (IntPtr conn
);
174 // char *PQerrorMessage(const PGconn *conn);
177 public static extern int PQsocket (IntPtr conn
);
178 // int PQsocket(const PGconn *conn);
181 public static extern int PQbackendPID (IntPtr conn
);
182 // int PQbackendPID(const PGconn *conn);
185 public static extern int PQclientEncoding (IntPtr conn
);
186 // int PQclientEncoding(const PGconn *conn);
189 public static extern int PQsetClientEncoding (IntPtr conn
,
191 // int PQsetClientEncoding(PGconn *conn,
192 // const char *encoding);
194 //FIXME: when loading, causes runtime exception
196 //public static extern IntPtr PQgetssl (IntPtr conn);
197 // SSL *PQgetssl(PGconn *conn);
200 public static extern void PQtrace (IntPtr conn
,
202 // void PQtrace(PGconn *conn,
203 // FILE *debug_port);
206 public static extern void PQuntrace (IntPtr conn
);
207 // void PQuntrace(PGconn *conn);
210 public static extern IntPtr
PQsetNoticeProcessor (IntPtr conn
,
211 IntPtr proc
, IntPtr arg
);
212 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
213 // PQnoticeProcessor proc, void *arg);
216 public static extern uint PQescapeString (out string to
,
217 string from, uint length
);
218 // size_t PQescapeString(char *to,
219 // const char *from, size_t length);
222 public static extern byte[] PQescapeBytea (byte[] bintext
,
223 uint binlen
, uint bytealen
);
224 // unsigned char *PQescapeBytea(unsigned char *bintext,
225 // size_t binlen, size_t *bytealen);
228 public static extern IntPtr
PQexec (IntPtr conn
,
230 // PGresult *PQexec(PGconn *conn,
231 // const char *query);
234 public static extern IntPtr
PQnotifies (IntPtr conn
);
235 // PGnotify *PQnotifies(PGconn *conn);
238 public static extern void PQfreeNotify (IntPtr notify
);
239 // void PQfreeNotify(PGnotify *notify);
242 public static extern int PQsendQuery (IntPtr conn
,
244 // int PQsendQuery(PGconn *conn,
245 // const char *query);
248 public static extern IntPtr
PQgetResult (IntPtr conn
);
249 // PGresult *PQgetResult(PGconn *conn);
252 public static extern int PQisBusy (IntPtr conn
);
253 // int PQisBusy(PGconn *conn);
256 public static extern int PQconsumeInput (IntPtr conn
);
257 // int PQconsumeInput(PGconn *conn);
260 public static extern int PQgetline (IntPtr conn
,
261 string str
, int length
);
262 // int PQgetline(PGconn *conn,
263 // char *string, int length);
266 public static extern int PQputline (IntPtr conn
,
268 // int PQputline(PGconn *conn,
269 // const char *string);
272 public static extern int PQgetlineAsync (IntPtr conn
,
273 string buffer
, int bufsize
);
274 // int PQgetlineAsync(PGconn *conn, char *buffer,
278 public static extern int PQputnbytes (IntPtr conn
,
279 string buffer
, int nbytes
);
280 // int PQputnbytes(PGconn *conn,
281 //const char *buffer, int nbytes);
284 public static extern int PQendcopy (IntPtr conn
);
285 // int PQendcopy(PGconn *conn);
288 public static extern int PQsetnonblocking (IntPtr conn
,
290 // int PQsetnonblocking(PGconn *conn, int arg);
293 public static extern int PQisnonblocking (IntPtr conn
);
294 // int PQisnonblocking(const PGconn *conn);
297 public static extern int PQflush (IntPtr conn
);
298 // int PQflush(PGconn *conn);
301 public static extern IntPtr
PQfn (IntPtr conn
, int fnid
,
302 IntPtr result_buf
, IntPtr result_len
,
303 int result_is_int
, IntPtr args
,
305 // PGresult *PQfn(PGconn *conn, int fnid,
306 // int *result_buf, int *result_len,
307 // int result_is_int, const PQArgBlock *args,
311 public static extern ExecStatusType
PQresultStatus (IntPtr res
);
312 // ExecStatusType PQresultStatus(const PGresult *res);
315 public static extern string PQresStatus (ExecStatusType status
);
316 // char *PQresStatus(ExecStatusType status);
319 public static extern string PQresultErrorMessage (IntPtr res
);
320 // char *PQresultErrorMessage(const PGresult *res);
323 public static extern int PQntuples (IntPtr res
);
324 // int PQntuples(const PGresult *res);
327 public static extern int PQnfields (IntPtr res
);
328 // int PQnfields(const PGresult *res);
331 public static extern int PQbinaryTuples (IntPtr res
);
332 // int PQbinaryTuples(const PGresult *res);
335 public static extern string PQfname (IntPtr res
,
337 // char *PQfname(const PGresult *res,
341 public static extern int PQfnumber (IntPtr res
,
343 // int PQfnumber(const PGresult *res,
344 // const char *field_name);
347 public static extern int PQftype (IntPtr res
,
349 // Oid PQftype(const PGresult *res,
353 public static extern int PQfsize (IntPtr res
,
355 // int PQfsize(const PGresult *res,
359 public static extern int PQfmod (IntPtr res
, int field_num
);
360 // int PQfmod(const PGresult *res, int field_num);
363 public static extern string PQcmdStatus (IntPtr res
);
364 // char *PQcmdStatus(PGresult *res);
367 public static extern string PQoidStatus (IntPtr res
);
368 // char *PQoidStatus(const PGresult *res);
371 public static extern int PQoidValue (IntPtr res
);
372 // Oid PQoidValue(const PGresult *res);
375 public static extern string PQcmdTuples (IntPtr res
);
376 // char *PQcmdTuples(PGresult *res);
379 public static extern string PQgetvalue (IntPtr res
,
380 int tup_num
, int field_num
);
381 // char *PQgetvalue(const PGresult *res,
382 // int tup_num, int field_num);
385 public static extern int PQgetlength (IntPtr res
,
386 int tup_num
, int field_num
);
387 // int PQgetlength(const PGresult *res,
388 // int tup_num, int field_num);
391 public static extern int PQgetisnull (IntPtr res
,
392 int tup_num
, int field_num
);
393 // int PQgetisnull(const PGresult *res,
394 // int tup_num, int field_num);
397 public static extern void PQclear (IntPtr res
);
398 // void PQclear(PGresult *res);
401 public static extern IntPtr
PQmakeEmptyPGresult (IntPtr conn
,
403 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
404 // ExecStatusType status);
407 public static extern void PQprint (IntPtr fout
,
408 IntPtr res
, IntPtr ps
);
409 // void PQprint(FILE *fout,
410 // const PGresult *res, const PQprintOpt *ps);
413 public static extern void PQdisplayTuples (IntPtr res
,
414 IntPtr fp
, int fillAlign
, string fieldSep
,
415 int printHeader
, int quiet
);
416 // void PQdisplayTuples(const PGresult *res,
417 // FILE *fp, int fillAlign, const char *fieldSep,
418 // int printHeader, int quiet);
421 public static extern void PQprintTuples (IntPtr res
,
422 IntPtr fout
, int printAttName
, int terseOutput
,
424 // void PQprintTuples(const PGresult *res,
425 // FILE *fout, int printAttName, int terseOutput,
429 public static extern int lo_open (IntPtr conn
,
430 int lobjId
, int mode
);
431 // int lo_open(PGconn *conn,
432 // Oid lobjId, int mode);
435 public static extern int lo_close (IntPtr conn
, int fd
);
436 // int lo_close(PGconn *conn, int fd);
439 public static extern int lo_read (IntPtr conn
,
440 int fd
, string buf
, int len
);
441 // int lo_read(PGconn *conn,
442 // int fd, char *buf, size_t len);
445 public static extern int lo_write (IntPtr conn
,
446 int fd
, string buf
, int len
);
447 // int lo_write(PGconn *conn,
448 // int fd, char *buf, size_t len);
451 public static extern int lo_lseek (IntPtr conn
,
452 int fd
, int offset
, int whence
);
453 // int lo_lseek(PGconn *conn,
454 // int fd, int offset, int whence);
457 public static extern int lo_creat (IntPtr conn
,
459 // Oid lo_creat(PGconn *conn,
463 public static extern int lo_tell (IntPtr conn
, int fd
);
464 // int lo_tell(PGconn *conn, int fd);
467 public static extern int lo_unlink (IntPtr conn
,
469 // int lo_unlink(PGconn *conn,
473 public static extern int lo_import (IntPtr conn
,
475 // Oid lo_import(PGconn *conn,
476 // const char *filename);
479 public static extern int lo_export (IntPtr conn
,
480 int lobjId
, string filename
);
481 // int lo_export(PGconn *conn,
482 // Oid lobjId, const char *filename);
485 public static extern int PQmblen (string s
,
487 // int PQmblen(const unsigned char *s,
491 public static extern int PQenv2encoding ();
492 // int PQenv2encoding(void);