2 // System.Data.SqlClient.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
17 // *** uncomment #define to get debug messages, comment for production ***
18 //#define DEBUG_PostgresLibrary
22 using System.Runtime.InteropServices;
23 using System.Diagnostics;
24 using System.Collections;
26 namespace System.Data.SqlClient {
28 /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS BELOW */
30 internal enum ConnStatusType
36 CONNECTION_AWAITING_RESPONSE,
41 internal enum PostgresPollingStatusType
43 PGRES_POLLING_FAILED = 0,
44 PGRES_POLLING_READING,
45 PGRES_POLLING_WRITING,
50 internal enum ExecStatusType
52 PGRES_EMPTY_QUERY = 0,
62 sealed internal class PostgresLibrary
64 #region PInvoke Functions
66 // pinvoke prototypes to PostgreSQL client library
67 // pq.dll on windows and libpq.so on linux
70 public static extern IntPtr PQconnectStart (string conninfo);
71 // PGconn *PQconnectStart(const char *conninfo);
74 public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
75 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);
78 public static extern IntPtr PQconnectdb (string conninfo);
79 // PGconn *PQconnectdb(const char *conninfo);
82 public static extern IntPtr PQsetdbLogin (string pghost,
83 string pgport, string pgoptions,
84 string pgtty, string dbName,
85 string login, string pwd);
86 // PGconn *PQsetdbLogin(const char *pghost,
87 // const char *pgport, const char *pgoptions,
88 // const char *pgtty, const char *dbName,
89 // const char *login, const char *pwd);
92 public static extern void PQfinish (IntPtr conn);
93 // void PQfinish(PGconn *conn);
96 public static extern IntPtr PQconndefaults ();
97 // PQconninfoOption *PQconndefaults(void);
100 public static extern void PQconninfoFree (IntPtr connOptions);
101 // void PQconninfoFree(PQconninfoOption *connOptions);
104 public static extern int PQresetStart (IntPtr conn);
105 // int PQresetStart(PGconn *conn);
108 public static extern IntPtr PQresetPoll (IntPtr conn);
109 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
112 public static extern void PQreset (IntPtr conn);
113 // void PQreset(PGconn *conn);
116 public static extern int PQrequestCancel (IntPtr conn);
117 // int PQrequestCancel(PGconn *conn);
120 public static extern string PQdb (IntPtr conn);
121 // char *PQdb(const PGconn *conn);
124 public static extern string PQuser (IntPtr conn);
125 // char *PQuser(const PGconn *conn);
128 public static extern string PQpass (IntPtr conn);
129 // char *PQpass(const PGconn *conn);
132 public static extern string PQhost (IntPtr conn);
133 // char *PQhost(const PGconn *conn);
136 public static extern string PQport (IntPtr conn);
137 // char *PQport(const PGconn *conn);
140 public static extern string PQtty (IntPtr conn);
141 // char *PQtty(const PGconn *conn);
144 public static extern string PQoptions (IntPtr conn);
145 // char *PQoptions(const PGconn *conn);
148 public static extern ConnStatusType PQstatus (IntPtr conn);
149 // ConnStatusType PQstatus(const PGconn *conn);
152 public static extern string PQerrorMessage (IntPtr conn);
153 // char *PQerrorMessage(const PGconn *conn);
156 public static extern int PQsocket (IntPtr conn);
157 // int PQsocket(const PGconn *conn);
160 public static extern int PQbackendPID (IntPtr conn);
161 // int PQbackendPID(const PGconn *conn);
164 public static extern int PQclientEncoding (IntPtr conn);
165 // int PQclientEncoding(const PGconn *conn);
168 public static extern int PQsetClientEncoding (IntPtr conn,
170 // int PQsetClientEncoding(PGconn *conn,
171 // const char *encoding);
173 //FIXME: when loading, causes runtime exception
175 //public static extern IntPtr PQgetssl (IntPtr conn);
176 // SSL *PQgetssl(PGconn *conn);
179 public static extern void PQtrace (IntPtr conn,
181 // void PQtrace(PGconn *conn,
182 // FILE *debug_port);
185 public static extern void PQuntrace (IntPtr conn);
186 // void PQuntrace(PGconn *conn);
189 public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
190 IntPtr proc, IntPtr arg);
191 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
192 // PQnoticeProcessor proc, void *arg);
195 public static extern int PQescapeString (string to,
196 string from, int length);
197 // size_t PQescapeString(char *to,
198 // const char *from, size_t length);
201 public static extern string PQescapeBytea (string bintext,
202 int binlen, IntPtr bytealen);
203 // unsigned char *PQescapeBytea(unsigned char *bintext,
204 // size_t binlen, size_t *bytealen);
207 public static extern IntPtr PQexec (IntPtr conn,
209 // PGresult *PQexec(PGconn *conn,
210 // const char *query);
213 public static extern IntPtr PQnotifies (IntPtr conn);
214 // PGnotify *PQnotifies(PGconn *conn);
217 public static extern void PQfreeNotify (IntPtr notify);
218 // void PQfreeNotify(PGnotify *notify);
221 public static extern int PQsendQuery (IntPtr conn,
223 // int PQsendQuery(PGconn *conn,
224 // const char *query);
227 public static extern IntPtr PQgetResult (IntPtr conn);
228 // PGresult *PQgetResult(PGconn *conn);
231 public static extern int PQisBusy (IntPtr conn);
232 // int PQisBusy(PGconn *conn);
235 public static extern int PQconsumeInput (IntPtr conn);
236 // int PQconsumeInput(PGconn *conn);
239 public static extern int PQgetline (IntPtr conn,
240 string str, int length);
241 // int PQgetline(PGconn *conn,
242 // char *string, int length);
245 public static extern int PQputline (IntPtr conn,
247 // int PQputline(PGconn *conn,
248 // const char *string);
251 public static extern int PQgetlineAsync (IntPtr conn,
252 string buffer, int bufsize);
253 // int PQgetlineAsync(PGconn *conn, char *buffer,
257 public static extern int PQputnbytes (IntPtr conn,
258 string buffer, int nbytes);
259 // int PQputnbytes(PGconn *conn,
260 //const char *buffer, int nbytes);
263 public static extern int PQendcopy (IntPtr conn);
264 // int PQendcopy(PGconn *conn);
267 public static extern int PQsetnonblocking (IntPtr conn,
269 // int PQsetnonblocking(PGconn *conn, int arg);
272 public static extern int PQisnonblocking (IntPtr conn);
273 // int PQisnonblocking(const PGconn *conn);
276 public static extern int PQflush (IntPtr conn);
277 // int PQflush(PGconn *conn);
280 public static extern IntPtr PQfn (IntPtr conn, int fnid,
281 IntPtr result_buf, IntPtr result_len,
282 int result_is_int, IntPtr args,
284 // PGresult *PQfn(PGconn *conn, int fnid,
285 // int *result_buf, int *result_len,
286 // int result_is_int, const PQArgBlock *args,
290 public static extern ExecStatusType PQresultStatus (IntPtr res);
291 // ExecStatusType PQresultStatus(const PGresult *res);
294 public static extern string PQresStatus (ExecStatusType status);
295 // char *PQresStatus(ExecStatusType status);
298 public static extern string PQresultErrorMessage (IntPtr res);
299 // char *PQresultErrorMessage(const PGresult *res);
302 public static extern int PQntuples (IntPtr res);
303 // int PQntuples(const PGresult *res);
306 public static extern int PQnfields (IntPtr res);
307 // int PQnfields(const PGresult *res);
310 public static extern int PQbinaryTuples (IntPtr res);
311 // int PQbinaryTuples(const PGresult *res);
314 public static extern string PQfname (IntPtr res,
316 // char *PQfname(const PGresult *res,
320 public static extern int PQfnumber (IntPtr res,
322 // int PQfnumber(const PGresult *res,
323 // const char *field_name);
326 public static extern int PQftype (IntPtr res,
328 // Oid PQftype(const PGresult *res,
332 public static extern int PQfsize (IntPtr res,
334 // int PQfsize(const PGresult *res,
338 public static extern int PQfmod (IntPtr res, int field_num);
339 // int PQfmod(const PGresult *res, int field_num);
342 public static extern string PQcmdStatus (IntPtr res);
343 // char *PQcmdStatus(PGresult *res);
346 public static extern string PQoidStatus (IntPtr res);
347 // char *PQoidStatus(const PGresult *res);
350 public static extern int PQoidValue (IntPtr res);
351 // Oid PQoidValue(const PGresult *res);
354 public static extern string PQcmdTuples (IntPtr res);
355 // char *PQcmdTuples(PGresult *res);
358 public static extern string PQgetvalue (IntPtr res,
359 int tup_num, int field_num);
360 // char *PQgetvalue(const PGresult *res,
361 // int tup_num, int field_num);
364 public static extern int PQgetlength (IntPtr res,
365 int tup_num, int field_num);
366 // int PQgetlength(const PGresult *res,
367 // int tup_num, int field_num);
370 public static extern int PQgetisnull (IntPtr res,
371 int tup_num, int field_num);
372 // int PQgetisnull(const PGresult *res,
373 // int tup_num, int field_num);
376 public static extern void PQclear (IntPtr res);
377 // void PQclear(PGresult *res);
380 public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
382 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
383 // ExecStatusType status);
386 public static extern void PQprint (IntPtr fout,
387 IntPtr res, IntPtr ps);
388 // void PQprint(FILE *fout,
389 // const PGresult *res, const PQprintOpt *ps);
392 public static extern void PQdisplayTuples (IntPtr res,
393 IntPtr fp, int fillAlign, string fieldSep,
394 int printHeader, int quiet);
395 // void PQdisplayTuples(const PGresult *res,
396 // FILE *fp, int fillAlign, const char *fieldSep,
397 // int printHeader, int quiet);
400 public static extern void PQprintTuples (IntPtr res,
401 IntPtr fout, int printAttName, int terseOutput,
403 // void PQprintTuples(const PGresult *res,
404 // FILE *fout, int printAttName, int terseOutput,
408 public static extern int lo_open (IntPtr conn,
409 int lobjId, int mode);
410 // int lo_open(PGconn *conn,
411 // Oid lobjId, int mode);
414 public static extern int lo_close (IntPtr conn, int fd);
415 // int lo_close(PGconn *conn, int fd);
418 public static extern int lo_read (IntPtr conn,
419 int fd, string buf, int len);
420 // int lo_read(PGconn *conn,
421 // int fd, char *buf, size_t len);
424 public static extern int lo_write (IntPtr conn,
425 int fd, string buf, int len);
426 // int lo_write(PGconn *conn,
427 // int fd, char *buf, size_t len);
430 public static extern int lo_lseek (IntPtr conn,
431 int fd, int offset, int whence);
432 // int lo_lseek(PGconn *conn,
433 // int fd, int offset, int whence);
436 public static extern int lo_creat (IntPtr conn,
438 // Oid lo_creat(PGconn *conn,
442 public static extern int lo_tell (IntPtr conn, int fd);
443 // int lo_tell(PGconn *conn, int fd);
446 public static extern int lo_unlink (IntPtr conn,
448 // int lo_unlink(PGconn *conn,
452 public static extern int lo_import (IntPtr conn,
454 // Oid lo_import(PGconn *conn,
455 // const char *filename);
458 public static extern int lo_export (IntPtr conn,
459 int lobjId, string filename);
460 // int lo_export(PGconn *conn,
461 // Oid lobjId, const char *filename);
464 public static extern int PQmblen (string s,
466 // int PQmblen(const unsigned char *s,
470 public static extern int PQenv2encoding ();
471 // int PQenv2encoding(void);