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 // FIXME: should be sealed internal class
59 sealed internal class PostgresLibrary
61 #region PInvoke Functions
63 // pinvoke prototypes to PostgreSQL client library
64 // pq.dll on windows and libpq.so on linux
67 public static extern IntPtr PQconnectStart (string conninfo);
68 // PGconn *PQconnectStart(const char *conninfo);
71 public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
72 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);
75 public static extern IntPtr PQconnectdb (string conninfo);
76 // PGconn *PQconnectdb(const char *conninfo);
79 public static extern IntPtr PQsetdbLogin (string pghost,
80 string pgport, string pgoptions,
81 string pgtty, string dbName,
82 string login, string pwd);
83 // PGconn *PQsetdbLogin(const char *pghost,
84 // const char *pgport, const char *pgoptions,
85 // const char *pgtty, const char *dbName,
86 // const char *login, const char *pwd);
89 public static extern void PQfinish (IntPtr conn);
90 // void PQfinish(PGconn *conn);
93 public static extern IntPtr PQconndefaults ();
94 // PQconninfoOption *PQconndefaults(void);
97 public static extern void PQconninfoFree (IntPtr connOptions);
98 // void PQconninfoFree(PQconninfoOption *connOptions);
101 public static extern int PQresetStart (IntPtr conn);
102 // int PQresetStart(PGconn *conn);
105 public static extern IntPtr PQresetPoll (IntPtr conn);
106 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
109 public static extern void PQreset (IntPtr conn);
110 // void PQreset(PGconn *conn);
113 public static extern int PQrequestCancel (IntPtr conn);
114 // int PQrequestCancel(PGconn *conn);
117 public static extern string PQdb (IntPtr conn);
118 // char *PQdb(const PGconn *conn);
121 public static extern string PQuser (IntPtr conn);
122 // char *PQuser(const PGconn *conn);
125 public static extern string PQpass (IntPtr conn);
126 // char *PQpass(const PGconn *conn);
129 public static extern string PQhost (IntPtr conn);
130 // char *PQhost(const PGconn *conn);
133 public static extern string PQport (IntPtr conn);
134 // char *PQport(const PGconn *conn);
137 public static extern string PQtty (IntPtr conn);
138 // char *PQtty(const PGconn *conn);
141 public static extern string PQoptions (IntPtr conn);
142 // char *PQoptions(const PGconn *conn);
145 public static extern ConnStatusType PQstatus (IntPtr conn);
146 // ConnStatusType PQstatus(const PGconn *conn);
149 public static extern string PQerrorMessage (IntPtr conn);
150 // char *PQerrorMessage(const PGconn *conn);
153 public static extern int PQsocket (IntPtr conn);
154 // int PQsocket(const PGconn *conn);
157 public static extern int PQbackendPID (IntPtr conn);
158 // int PQbackendPID(const PGconn *conn);
161 public static extern int PQclientEncoding (IntPtr conn);
162 // int PQclientEncoding(const PGconn *conn);
165 public static extern int PQsetClientEncoding (IntPtr conn,
167 // int PQsetClientEncoding(PGconn *conn,
168 // const char *encoding);
170 //FIXME: when loading, causes runtime exception
172 //public static extern IntPtr PQgetssl (IntPtr conn);
173 // SSL *PQgetssl(PGconn *conn);
176 public static extern void PQtrace (IntPtr conn,
178 // void PQtrace(PGconn *conn,
179 // FILE *debug_port);
182 public static extern void PQuntrace (IntPtr conn);
183 // void PQuntrace(PGconn *conn);
186 public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
187 IntPtr proc, IntPtr arg);
188 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
189 // PQnoticeProcessor proc, void *arg);
192 public static extern int PQescapeString (string to,
193 string from, int length);
194 // size_t PQescapeString(char *to,
195 // const char *from, size_t length);
198 public static extern string PQescapeBytea (string bintext,
199 int binlen, IntPtr bytealen);
200 // unsigned char *PQescapeBytea(unsigned char *bintext,
201 // size_t binlen, size_t *bytealen);
204 public static extern IntPtr PQexec (IntPtr conn,
206 // PGresult *PQexec(PGconn *conn,
207 // const char *query);
210 public static extern IntPtr PQnotifies (IntPtr conn);
211 // PGnotify *PQnotifies(PGconn *conn);
214 public static extern void PQfreeNotify (IntPtr notify);
215 // void PQfreeNotify(PGnotify *notify);
218 public static extern int PQsendQuery (IntPtr conn,
220 // int PQsendQuery(PGconn *conn,
221 // const char *query);
224 public static extern IntPtr PQgetResult (IntPtr conn);
225 // PGresult *PQgetResult(PGconn *conn);
228 public static extern int PQisBusy (IntPtr conn);
229 // int PQisBusy(PGconn *conn);
232 public static extern int PQconsumeInput (IntPtr conn);
233 // int PQconsumeInput(PGconn *conn);
236 public static extern int PQgetline (IntPtr conn,
237 string str, int length);
238 // int PQgetline(PGconn *conn,
239 // char *string, int length);
242 public static extern int PQputline (IntPtr conn,
244 // int PQputline(PGconn *conn,
245 // const char *string);
248 public static extern int PQgetlineAsync (IntPtr conn,
249 string buffer, int bufsize);
250 // int PQgetlineAsync(PGconn *conn, char *buffer,
254 public static extern int PQputnbytes (IntPtr conn,
255 string buffer, int nbytes);
256 // int PQputnbytes(PGconn *conn,
257 //const char *buffer, int nbytes);
260 public static extern int PQendcopy (IntPtr conn);
261 // int PQendcopy(PGconn *conn);
264 public static extern int PQsetnonblocking (IntPtr conn,
266 // int PQsetnonblocking(PGconn *conn, int arg);
269 public static extern int PQisnonblocking (IntPtr conn);
270 // int PQisnonblocking(const PGconn *conn);
273 public static extern int PQflush (IntPtr conn);
274 // int PQflush(PGconn *conn);
277 public static extern IntPtr PQfn (IntPtr conn, int fnid,
278 IntPtr result_buf, IntPtr result_len,
279 int result_is_int, IntPtr args,
281 // PGresult *PQfn(PGconn *conn, int fnid,
282 // int *result_buf, int *result_len,
283 // int result_is_int, const PQArgBlock *args,
287 public static extern ExecStatusType PQresultStatus (IntPtr res);
288 // ExecStatusType PQresultStatus(const PGresult *res);
291 public static extern string PQresStatus (IntPtr status);
292 // char *PQresStatus(ExecStatusType status);
295 public static extern string PQresultErrorMessage (IntPtr res);
296 // char *PQresultErrorMessage(const PGresult *res);
299 public static extern int PQntuples (IntPtr res);
300 // int PQntuples(const PGresult *res);
303 public static extern int PQnfields (IntPtr res);
304 // int PQnfields(const PGresult *res);
307 public static extern int PQbinaryTuples (IntPtr res);
308 // int PQbinaryTuples(const PGresult *res);
311 public static extern string PQfname (IntPtr res,
313 // char *PQfname(const PGresult *res,
317 public static extern int PQfnumber (IntPtr res,
319 // int PQfnumber(const PGresult *res,
320 // const char *field_name);
323 public static extern int PQftype (IntPtr res,
325 // Oid PQftype(const PGresult *res,
329 public static extern int PQfsize (IntPtr res,
331 // int PQfsize(const PGresult *res,
335 public static extern int PQfmod (IntPtr res, int field_num);
336 // int PQfmod(const PGresult *res, int field_num);
339 public static extern string PQcmdStatus (IntPtr res);
340 // char *PQcmdStatus(PGresult *res);
343 public static extern string PQoidStatus (IntPtr res);
344 // char *PQoidStatus(const PGresult *res);
347 public static extern int PQoidValue (IntPtr res);
348 // Oid PQoidValue(const PGresult *res);
351 public static extern string PQcmdTuples (IntPtr res);
352 // char *PQcmdTuples(PGresult *res);
355 public static extern string PQgetvalue (IntPtr res,
356 int tup_num, int field_num);
357 // char *PQgetvalue(const PGresult *res,
358 // int tup_num, int field_num);
361 public static extern int PQgetlength (IntPtr res,
362 int tup_num, int field_num);
363 // int PQgetlength(const PGresult *res,
364 // int tup_num, int field_num);
367 public static extern int PQgetisnull (IntPtr res,
368 int tup_num, int field_num);
369 // int PQgetisnull(const PGresult *res,
370 // int tup_num, int field_num);
373 public static extern void PQclear (IntPtr res);
374 // void PQclear(PGresult *res);
377 public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
379 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
380 // ExecStatusType status);
383 public static extern void PQprint (IntPtr fout,
384 IntPtr res, IntPtr ps);
385 // void PQprint(FILE *fout,
386 // const PGresult *res, const PQprintOpt *ps);
389 public static extern void PQdisplayTuples (IntPtr res,
390 IntPtr fp, int fillAlign, string fieldSep,
391 int printHeader, int quiet);
392 // void PQdisplayTuples(const PGresult *res,
393 // FILE *fp, int fillAlign, const char *fieldSep,
394 // int printHeader, int quiet);
397 public static extern void PQprintTuples (IntPtr res,
398 IntPtr fout, int printAttName, int terseOutput,
400 // void PQprintTuples(const PGresult *res,
401 // FILE *fout, int printAttName, int terseOutput,
405 public static extern int lo_open (IntPtr conn,
406 int lobjId, int mode);
407 // int lo_open(PGconn *conn,
408 // Oid lobjId, int mode);
411 public static extern int lo_close (IntPtr conn, int fd);
412 // int lo_close(PGconn *conn, int fd);
415 public static extern int lo_read (IntPtr conn,
416 int fd, string buf, int len);
417 // int lo_read(PGconn *conn,
418 // int fd, char *buf, size_t len);
421 public static extern int lo_write (IntPtr conn,
422 int fd, string buf, int len);
423 // int lo_write(PGconn *conn,
424 // int fd, char *buf, size_t len);
427 public static extern int lo_lseek (IntPtr conn,
428 int fd, int offset, int whence);
429 // int lo_lseek(PGconn *conn,
430 // int fd, int offset, int whence);
433 public static extern int lo_creat (IntPtr conn,
435 // Oid lo_creat(PGconn *conn,
439 public static extern int lo_tell (IntPtr conn, int fd);
440 // int lo_tell(PGconn *conn, int fd);
443 public static extern int lo_unlink (IntPtr conn,
445 // int lo_unlink(PGconn *conn,
449 public static extern int lo_import (IntPtr conn,
451 // Oid lo_import(PGconn *conn,
452 // const char *filename);
455 public static extern int lo_export (IntPtr conn,
456 int lobjId, string filename);
457 // int lo_export(PGconn *conn,
458 // Oid lobjId, const char *filename);
461 public static extern int PQmblen (string s,
463 // int PQmblen(const unsigned char *s,
467 public static extern int PQenv2encoding ();
468 // int PQenv2encoding(void);