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
19 using System.Runtime.InteropServices;
20 using System.Diagnostics;
22 namespace System.Data.SqlClient
24 /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS */
26 internal enum ConnStatusType
32 CONNECTION_AWAITING_RESPONSE,
37 internal enum PostgresPollingStatusType
39 PGRES_POLLING_FAILED = 0,
40 PGRES_POLLING_READING,
41 PGRES_POLLING_WRITING,
46 internal enum ExecStatusType
48 PGRES_EMPTY_QUERY = 0,
58 sealed internal class PostgresHelper {
60 public static object OidTypeToSystem (int oid, String value) {
63 // FIXME: more types need
65 // from PostgreSQL oid type
66 // to .NET System.<type>
72 obj = (object) value; // String
75 obj = (object) Boolean.Parse(value);
78 obj = (object) Int16.Parse(value);
\r
81 obj = (object) Int32.Parse(value);
\r
84 obj = (object) Int64.Parse(value);
\r
93 sealed internal class PostgresLibrary
95 #region PInvoke Functions
97 // pinvoke prototypes to PostgreSQL client library
98 // pq.dll on windows and libpq.so on linux
101 public static extern IntPtr PQconnectStart (string conninfo);
102 // PGconn *PQconnectStart(const char *conninfo);
105 public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
106 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);
109 public static extern IntPtr PQconnectdb (string conninfo);
110 // PGconn *PQconnectdb(const char *conninfo);
113 public static extern IntPtr PQsetdbLogin (string pghost,
114 string pgport, string pgoptions,
115 string pgtty, string dbName,
116 string login, string pwd);
117 // PGconn *PQsetdbLogin(const char *pghost,
118 // const char *pgport, const char *pgoptions,
119 // const char *pgtty, const char *dbName,
120 // const char *login, const char *pwd);
123 public static extern void PQfinish (IntPtr conn);
124 // void PQfinish(PGconn *conn);
127 public static extern IntPtr PQconndefaults ();
128 // PQconninfoOption *PQconndefaults(void);
131 public static extern void PQconninfoFree (IntPtr connOptions);
132 // void PQconninfoFree(PQconninfoOption *connOptions);
135 public static extern int PQresetStart (IntPtr conn);
136 // int PQresetStart(PGconn *conn);
139 public static extern IntPtr PQresetPoll (IntPtr conn);
140 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
143 public static extern void PQreset (IntPtr conn);
144 // void PQreset(PGconn *conn);
147 public static extern int PQrequestCancel (IntPtr conn);
148 // int PQrequestCancel(PGconn *conn);
151 public static extern string PQdb (IntPtr conn);
152 // char *PQdb(const PGconn *conn);
155 public static extern string PQuser (IntPtr conn);
156 // char *PQuser(const PGconn *conn);
159 public static extern string PQpass (IntPtr conn);
160 // char *PQpass(const PGconn *conn);
163 public static extern string PQhost (IntPtr conn);
164 // char *PQhost(const PGconn *conn);
167 public static extern string PQport (IntPtr conn);
168 // char *PQport(const PGconn *conn);
171 public static extern string PQtty (IntPtr conn);
172 // char *PQtty(const PGconn *conn);
175 public static extern string PQoptions (IntPtr conn);
176 // char *PQoptions(const PGconn *conn);
179 public static extern ConnStatusType PQstatus (IntPtr conn);
180 // ConnStatusType PQstatus(const PGconn *conn);
183 public static extern string PQerrorMessage (IntPtr conn);
184 // char *PQerrorMessage(const PGconn *conn);
187 public static extern int PQsocket (IntPtr conn);
188 // int PQsocket(const PGconn *conn);
191 public static extern int PQbackendPID (IntPtr conn);
192 // int PQbackendPID(const PGconn *conn);
195 public static extern int PQclientEncoding (IntPtr conn);
196 // int PQclientEncoding(const PGconn *conn);
199 public static extern int PQsetClientEncoding (IntPtr conn,
201 // int PQsetClientEncoding(PGconn *conn,
202 // const char *encoding);
204 //FIXME: when loading, causes runtime exception
206 //public static extern IntPtr PQgetssl (IntPtr conn);
207 // SSL *PQgetssl(PGconn *conn);
210 public static extern void PQtrace (IntPtr conn,
212 // void PQtrace(PGconn *conn,
213 // FILE *debug_port);
216 public static extern void PQuntrace (IntPtr conn);
217 // void PQuntrace(PGconn *conn);
220 public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
221 IntPtr proc, IntPtr arg);
222 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
223 // PQnoticeProcessor proc, void *arg);
226 public static extern int PQescapeString (string to,
227 string from, int length);
228 // size_t PQescapeString(char *to,
229 // const char *from, size_t length);
232 public static extern string PQescapeBytea (string bintext,
233 int binlen, IntPtr bytealen);
234 // unsigned char *PQescapeBytea(unsigned char *bintext,
235 // size_t binlen, size_t *bytealen);
238 public static extern IntPtr PQexec (IntPtr conn,
240 // PGresult *PQexec(PGconn *conn,
241 // const char *query);
244 public static extern IntPtr PQnotifies (IntPtr conn);
245 // PGnotify *PQnotifies(PGconn *conn);
248 public static extern void PQfreeNotify (IntPtr notify);
249 // void PQfreeNotify(PGnotify *notify);
252 public static extern int PQsendQuery (IntPtr conn,
254 // int PQsendQuery(PGconn *conn,
255 // const char *query);
258 public static extern IntPtr PQgetResult (IntPtr conn);
259 // PGresult *PQgetResult(PGconn *conn);
262 public static extern int PQisBusy (IntPtr conn);
263 // int PQisBusy(PGconn *conn);
266 public static extern int PQconsumeInput (IntPtr conn);
267 // int PQconsumeInput(PGconn *conn);
270 public static extern int PQgetline (IntPtr conn,
271 string str, int length);
272 // int PQgetline(PGconn *conn,
273 // char *string, int length);
276 public static extern int PQputline (IntPtr conn,
278 // int PQputline(PGconn *conn,
279 // const char *string);
282 public static extern int PQgetlineAsync (IntPtr conn,
283 string buffer, int bufsize);
284 // int PQgetlineAsync(PGconn *conn, char *buffer,
288 public static extern int PQputnbytes (IntPtr conn,
289 string buffer, int nbytes);
290 // int PQputnbytes(PGconn *conn,
291 //const char *buffer, int nbytes);
294 public static extern int PQendcopy (IntPtr conn);
295 // int PQendcopy(PGconn *conn);
298 public static extern int PQsetnonblocking (IntPtr conn,
300 // int PQsetnonblocking(PGconn *conn, int arg);
303 public static extern int PQisnonblocking (IntPtr conn);
304 // int PQisnonblocking(const PGconn *conn);
307 public static extern int PQflush (IntPtr conn);
308 // int PQflush(PGconn *conn);
311 public static extern IntPtr PQfn (IntPtr conn, int fnid,
312 IntPtr result_buf, IntPtr result_len,
313 int result_is_int, IntPtr args,
315 // PGresult *PQfn(PGconn *conn, int fnid,
316 // int *result_buf, int *result_len,
317 // int result_is_int, const PQArgBlock *args,
321 public static extern ExecStatusType PQresultStatus (IntPtr res);
322 // ExecStatusType PQresultStatus(const PGresult *res);
325 public static extern string PQresStatus (ExecStatusType status);
326 // char *PQresStatus(ExecStatusType status);
329 public static extern string PQresultErrorMessage (IntPtr res);
330 // char *PQresultErrorMessage(const PGresult *res);
333 public static extern int PQntuples (IntPtr res);
334 // int PQntuples(const PGresult *res);
337 public static extern int PQnfields (IntPtr res);
338 // int PQnfields(const PGresult *res);
341 public static extern int PQbinaryTuples (IntPtr res);
342 // int PQbinaryTuples(const PGresult *res);
345 public static extern string PQfname (IntPtr res,
347 // char *PQfname(const PGresult *res,
351 public static extern int PQfnumber (IntPtr res,
353 // int PQfnumber(const PGresult *res,
354 // const char *field_name);
357 public static extern int PQftype (IntPtr res,
359 // Oid PQftype(const PGresult *res,
363 public static extern int PQfsize (IntPtr res,
365 // int PQfsize(const PGresult *res,
369 public static extern int PQfmod (IntPtr res, int field_num);
370 // int PQfmod(const PGresult *res, int field_num);
373 public static extern string PQcmdStatus (IntPtr res);
374 // char *PQcmdStatus(PGresult *res);
377 public static extern string PQoidStatus (IntPtr res);
378 // char *PQoidStatus(const PGresult *res);
381 public static extern int PQoidValue (IntPtr res);
382 // Oid PQoidValue(const PGresult *res);
385 public static extern string PQcmdTuples (IntPtr res);
386 // char *PQcmdTuples(PGresult *res);
389 public static extern string PQgetvalue (IntPtr res,
390 int tup_num, int field_num);
391 // char *PQgetvalue(const PGresult *res,
392 // int tup_num, int field_num);
395 public static extern int PQgetlength (IntPtr res,
396 int tup_num, int field_num);
397 // int PQgetlength(const PGresult *res,
398 // int tup_num, int field_num);
401 public static extern int PQgetisnull (IntPtr res,
402 int tup_num, int field_num);
403 // int PQgetisnull(const PGresult *res,
404 // int tup_num, int field_num);
407 public static extern void PQclear (IntPtr res);
408 // void PQclear(PGresult *res);
411 public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
413 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
414 // ExecStatusType status);
417 public static extern void PQprint (IntPtr fout,
418 IntPtr res, IntPtr ps);
419 // void PQprint(FILE *fout,
420 // const PGresult *res, const PQprintOpt *ps);
423 public static extern void PQdisplayTuples (IntPtr res,
424 IntPtr fp, int fillAlign, string fieldSep,
425 int printHeader, int quiet);
426 // void PQdisplayTuples(const PGresult *res,
427 // FILE *fp, int fillAlign, const char *fieldSep,
428 // int printHeader, int quiet);
431 public static extern void PQprintTuples (IntPtr res,
432 IntPtr fout, int printAttName, int terseOutput,
434 // void PQprintTuples(const PGresult *res,
435 // FILE *fout, int printAttName, int terseOutput,
439 public static extern int lo_open (IntPtr conn,
440 int lobjId, int mode);
441 // int lo_open(PGconn *conn,
442 // Oid lobjId, int mode);
445 public static extern int lo_close (IntPtr conn, int fd);
446 // int lo_close(PGconn *conn, int fd);
449 public static extern int lo_read (IntPtr conn,
450 int fd, string buf, int len);
451 // int lo_read(PGconn *conn,
452 // int fd, char *buf, size_t len);
455 public static extern int lo_write (IntPtr conn,
456 int fd, string buf, int len);
457 // int lo_write(PGconn *conn,
458 // int fd, char *buf, size_t len);
461 public static extern int lo_lseek (IntPtr conn,
462 int fd, int offset, int whence);
463 // int lo_lseek(PGconn *conn,
464 // int fd, int offset, int whence);
467 public static extern int lo_creat (IntPtr conn,
469 // Oid lo_creat(PGconn *conn,
473 public static extern int lo_tell (IntPtr conn, int fd);
474 // int lo_tell(PGconn *conn, int fd);
477 public static extern int lo_unlink (IntPtr conn,
479 // int lo_unlink(PGconn *conn,
483 public static extern int lo_import (IntPtr conn,
485 // Oid lo_import(PGconn *conn,
486 // const char *filename);
489 public static extern int lo_export (IntPtr conn,
490 int lobjId, string filename);
491 // int lo_export(PGconn *conn,
492 // Oid lobjId, const char *filename);
495 public static extern int PQmblen (string s,
497 // int PQmblen(const unsigned char *s,
501 public static extern int PQenv2encoding ();
502 // int PQenv2encoding(void);