2002-05-03 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / PostgresLibrary.cs
1 //
2 // System.Data.SqlClient.PostgresLibrary.cs  
3 //
4 // PInvoke methods to libpq
5 // which is PostgreSQL client library
6 //
7 // May also contain enumerations,
8 // data types, or wrapper methods.
9 //
10 // Author:
11 //   Rodrigo Moya (rodrigo@ximian.com)
12 //   Daniel Morgan (danmorg@sc.rr.com)
13 //
14 // (C) Ximian, Inc 2002
15 //
16
17 using System;
18 using System.Data;
19 using System.Runtime.InteropServices;
20 using System.Diagnostics;
21
22 namespace System.Data.SqlClient
23 {
24         /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS */
25         
26         internal enum ConnStatusType
27         {
28                 CONNECTION_OK,
29                 CONNECTION_BAD,
30                 CONNECTION_STARTED,
31                 CONNECTION_MADE,
32                 CONNECTION_AWAITING_RESPONSE,
33                 CONNECTION_AUTH_OK,                      
34                 CONNECTION_SETENV               
35         } 
36
37         internal enum PostgresPollingStatusType
38         {
39                 PGRES_POLLING_FAILED = 0,
40                 PGRES_POLLING_READING,
41                 PGRES_POLLING_WRITING,
42                 PGRES_POLLING_OK,
43                 PGRES_POLLING_ACTIVE
44         }
45
46         internal enum ExecStatusType
47         {
48                 PGRES_EMPTY_QUERY = 0,
49                 PGRES_COMMAND_OK,                       
50                 PGRES_TUPLES_OK,                        
51                 PGRES_COPY_OUT,                         
52                 PGRES_COPY_IN,                          
53                 PGRES_BAD_RESPONSE,                     
54                 PGRES_NONFATAL_ERROR,
55                 PGRES_FATAL_ERROR
56         }
57
58         sealed internal class PostgresHelper {
59
60                 public static object OidTypeToSystem (int oid, String value) {
61                         object obj = null;
62
63                         // FIXME: more types need 
64                         //        to be converted 
65                         //        from PostgreSQL oid type
66                         //        to .NET System.<type>
67
68                         switch(oid) {
69                         case 1023: // varchar
70                         case 25: // text
71                         case 18: // char
72                                 obj = (object) value; // String
73                                 break;
74                         case 16: // bool
75                                 obj = (object) Boolean.Parse(value);
76                                 break;
77                         case 21: // int2\r
78                                 obj = (object) Int16.Parse(value);\r
79                                 break;\r
80                         case 23: // int4\r
81                                 obj = (object) Int32.Parse(value);\r
82                                 break;\r
83                         case 20: // int8\r
84                                 obj = (object) Int64.Parse(value);\r
85                                 break;\r
86                         }
87
88                         return obj;
89                 }
90
91         }
92
93         sealed internal class PostgresLibrary
94         {
95                 #region PInvoke Functions
96
97                 // pinvoke prototypes to PostgreSQL client library
98                 // pq.dll on windows and libpq.so on linux
99
100                 [DllImport("pq")]
101                 public static extern IntPtr PQconnectStart (string conninfo);
102                 // PGconn *PQconnectStart(const char *conninfo);
103
104                 [DllImport("pq")]
105                 public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
106                 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);       
107
108                 [DllImport("pq")]
109                 public static extern IntPtr PQconnectdb (string conninfo);
110                 // PGconn *PQconnectdb(const char *conninfo);
111
112                 [DllImport("pq")]
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);
121
122                 [DllImport("pq")]
123                 public static extern void PQfinish (IntPtr conn);
124                 // void PQfinish(PGconn *conn);
125
126                 [DllImport("pq")]
127                 public static extern IntPtr PQconndefaults ();
128                 // PQconninfoOption *PQconndefaults(void);
129
130                 [DllImport("pq")]
131                 public static extern void PQconninfoFree (IntPtr connOptions);
132                 // void PQconninfoFree(PQconninfoOption *connOptions);
133
134                 [DllImport("pq")]
135                 public static extern int PQresetStart (IntPtr conn);
136                 // int PQresetStart(PGconn *conn);
137
138                 [DllImport("pq")]
139                 public static extern IntPtr PQresetPoll (IntPtr conn);
140                 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
141
142                 [DllImport("pq")]
143                 public static extern void PQreset (IntPtr conn);
144                 // void PQreset(PGconn *conn);
145
146                 [DllImport("pq")]
147                 public static extern int PQrequestCancel (IntPtr conn);
148                 // int PQrequestCancel(PGconn *conn);
149
150                 [DllImport("pq")]
151                 public static extern string PQdb (IntPtr conn);
152                 // char *PQdb(const PGconn *conn);
153
154                 [DllImport("pq")]
155                 public static extern string PQuser (IntPtr conn);
156                 // char *PQuser(const PGconn *conn);
157
158                 [DllImport("pq")]
159                 public static extern string PQpass (IntPtr conn);
160                 // char *PQpass(const PGconn *conn);
161
162                 [DllImport("pq")]
163                 public static extern string PQhost (IntPtr conn);
164                 // char *PQhost(const PGconn *conn);
165
166                 [DllImport("pq")]
167                 public static extern string PQport (IntPtr conn);
168                 // char *PQport(const PGconn *conn);
169
170                 [DllImport("pq")]
171                 public static extern string PQtty (IntPtr conn);
172                 // char *PQtty(const PGconn *conn);
173
174                 [DllImport("pq")]
175                 public static extern string PQoptions (IntPtr conn);
176                 // char *PQoptions(const PGconn *conn);
177
178                 [DllImport("pq")]
179                 public static extern ConnStatusType PQstatus (IntPtr conn);
180                 // ConnStatusType PQstatus(const PGconn *conn);
181
182                 [DllImport("pq")]
183                 public static extern string PQerrorMessage (IntPtr conn);
184                 // char *PQerrorMessage(const PGconn *conn);
185
186                 [DllImport("pq")]
187                 public static extern int PQsocket (IntPtr conn);
188                 // int PQsocket(const PGconn *conn);
189
190                 [DllImport("pq")]
191                 public static extern int PQbackendPID (IntPtr conn);
192                 // int PQbackendPID(const PGconn *conn);
193
194                 [DllImport("pq")]
195                 public static extern int PQclientEncoding (IntPtr conn);
196                 // int PQclientEncoding(const PGconn *conn);
197
198                 [DllImport("pq")]
199                 public static extern int PQsetClientEncoding (IntPtr conn,
200                         string encoding);
201                 // int PQsetClientEncoding(PGconn *conn, 
202                 //              const char *encoding);
203
204                 //FIXME: when loading, causes runtime exception
205                 //[DllImport("pq")]
206                 //public static extern IntPtr PQgetssl (IntPtr conn);
207                 // SSL *PQgetssl(PGconn *conn);
208
209                 [DllImport("pq")]
210                 public static extern void PQtrace (IntPtr conn, 
211                         IntPtr debug_port);
212                 // void PQtrace(PGconn *conn, 
213                 //              FILE *debug_port);
214
215                 [DllImport("pq")]
216                 public static extern void PQuntrace (IntPtr conn);
217                 // void PQuntrace(PGconn *conn);
218
219                 [DllImport("pq")]
220                 public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
221                         IntPtr proc, IntPtr arg);
222                 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, 
223                 //              PQnoticeProcessor proc, void *arg);
224
225                 [DllImport("pq")]
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);
230
231                 [DllImport("pq")]
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);
236
237                 [DllImport("pq")]
238                 public static extern IntPtr PQexec (IntPtr conn,
239                         string query);
240                 // PGresult *PQexec(PGconn *conn, 
241                 //      const char *query);
242
243                 [DllImport("pq")]
244                 public static extern IntPtr PQnotifies (IntPtr conn);
245                 // PGnotify *PQnotifies(PGconn *conn);
246
247                 [DllImport("pq")]
248                 public static extern void PQfreeNotify (IntPtr notify);
249                 // void PQfreeNotify(PGnotify *notify);
250
251                 [DllImport("pq")]
252                 public static extern int PQsendQuery (IntPtr conn,
253                         string query);
254                 // int PQsendQuery(PGconn *conn, 
255                 //      const char *query);
256
257                 [DllImport("pq")]
258                 public static extern IntPtr PQgetResult (IntPtr conn);
259                 // PGresult *PQgetResult(PGconn *conn);
260
261                 [DllImport("pq")]
262                 public static extern int PQisBusy (IntPtr conn);
263                 // int PQisBusy(PGconn *conn);
264
265                 [DllImport("pq")]
266                 public static extern int PQconsumeInput (IntPtr conn);
267                 // int PQconsumeInput(PGconn *conn);
268
269                 [DllImport("pq")]
270                 public static extern int PQgetline (IntPtr conn,
271                         string str, int length);
272                 // int PQgetline(PGconn *conn,
273                 //      char *string, int length);
274
275                 [DllImport("pq")]
276                 public static extern int PQputline (IntPtr conn,
277                         string str);
278                 // int PQputline(PGconn *conn, 
279                 //      const char *string);
280
281                 [DllImport("pq")]
282                 public static extern int PQgetlineAsync (IntPtr conn,
283                         string buffer, int bufsize);
284                 // int PQgetlineAsync(PGconn *conn, char *buffer,
285                 //      int bufsize);
286
287                 [DllImport("pq")]
288                 public static extern int PQputnbytes (IntPtr conn,
289                         string buffer, int nbytes);
290                 // int PQputnbytes(PGconn *conn, 
291                 //const char *buffer, int nbytes);
292
293                 [DllImport("pq")]
294                 public static extern int PQendcopy (IntPtr conn);
295                 // int PQendcopy(PGconn *conn);
296
297                 [DllImport("pq")]
298                 public static extern int PQsetnonblocking (IntPtr conn,
299                         int arg);
300                 // int PQsetnonblocking(PGconn *conn, int arg);
301
302                 [DllImport("pq")]
303                 public static extern int PQisnonblocking (IntPtr conn);
304                 // int PQisnonblocking(const PGconn *conn);
305
306                 [DllImport("pq")]
307                 public static extern int PQflush (IntPtr conn);
308                 // int PQflush(PGconn *conn);
309
310                 [DllImport("pq")]
311                 public static extern IntPtr PQfn (IntPtr conn, int fnid, 
312                         IntPtr result_buf, IntPtr result_len, 
313                         int result_is_int, IntPtr args,
314                         int nargs);
315                 // PGresult *PQfn(PGconn *conn, int fnid, 
316                 //      int *result_buf, int *result_len, 
317                 //      int result_is_int, const PQArgBlock *args,
318                 //      int nargs);
319
320                 [DllImport("pq")]
321                 public static extern ExecStatusType PQresultStatus (IntPtr res);
322                 // ExecStatusType PQresultStatus(const PGresult *res);
323
324                 [DllImport("pq")]
325                 public static extern string PQresStatus (ExecStatusType status);
326                 // char *PQresStatus(ExecStatusType status);
327
328                 [DllImport("pq")]
329                 public static extern string PQresultErrorMessage (IntPtr res);
330                 // char *PQresultErrorMessage(const PGresult *res);
331
332                 [DllImport("pq")]
333                 public static extern int PQntuples (IntPtr res);
334                 // int PQntuples(const PGresult *res);
335
336                 [DllImport("pq")]
337                 public static extern int PQnfields (IntPtr res);
338                 // int PQnfields(const PGresult *res);
339
340                 [DllImport("pq")]
341                 public static extern int PQbinaryTuples (IntPtr res);
342                 // int PQbinaryTuples(const PGresult *res);
343
344                 [DllImport("pq")]
345                 public static extern string PQfname (IntPtr res,
346                         int field_num);
347                 // char *PQfname(const PGresult *res,
348                 //      int field_num);
349
350                 [DllImport("pq")]
351                 public static extern int PQfnumber (IntPtr res,
352                         string field_name);
353                 // int PQfnumber(const PGresult *res, 
354                 //      const char *field_name);
355
356                 [DllImport("pq")]
357                 public static extern int PQftype (IntPtr res,
358                         int field_num);
359                 // Oid PQftype(const PGresult *res,
360                 //      int field_num);
361
362                 [DllImport("pq")]
363                 public static extern int PQfsize (IntPtr res,
364                         int field_num);
365                 // int PQfsize(const PGresult *res,
366                 //      int field_num);
367
368                 [DllImport("pq")]
369                 public static extern int PQfmod (IntPtr res, int field_num);
370                 // int PQfmod(const PGresult *res, int field_num);
371
372                 [DllImport("pq")]
373                 public static extern string PQcmdStatus (IntPtr res);
374                 // char *PQcmdStatus(PGresult *res);
375
376                 [DllImport("pq")]
377                 public static extern string PQoidStatus (IntPtr res);
378                 // char *PQoidStatus(const PGresult *res);
379
380                 [DllImport("pq")]
381                 public static extern int PQoidValue (IntPtr res);
382                 // Oid PQoidValue(const PGresult *res);
383
384                 [DllImport("pq")]
385                 public static extern string PQcmdTuples (IntPtr res);
386                 // char *PQcmdTuples(PGresult *res);
387
388                 [DllImport("pq")]
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);
393
394                 [DllImport("pq")]
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);
399
400                 [DllImport("pq")]
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);
405
406                 [DllImport("pq")]
407                 public static extern void PQclear (IntPtr res);
408                 // void PQclear(PGresult *res);
409
410                 [DllImport("pq")]
411                 public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
412                         IntPtr status);
413                 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
414                 //      ExecStatusType status);
415
416                 [DllImport("pq")]
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);
421
422                 [DllImport("pq")]
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);
429
430                 [DllImport("pq")]
431                 public static extern void PQprintTuples (IntPtr res,
432                         IntPtr fout, int printAttName, int terseOutput, 
433                         int width);
434                 // void PQprintTuples(const PGresult *res,
435                 //      FILE *fout, int printAttName, int terseOutput, 
436                 //      int width);                                             
437
438                 [DllImport("pq")]
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);
443
444                 [DllImport("pq")]
445                 public static extern int lo_close (IntPtr conn, int fd);
446                 // int lo_close(PGconn *conn, int fd);
447
448                 [DllImport("pq")]
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);
453
454                 [DllImport("pq")]
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);
459
460                 [DllImport("pq")]
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);
465
466                 [DllImport("pq")]
467                 public static extern int lo_creat (IntPtr conn,
468                         int mode);
469                 // Oid lo_creat(PGconn *conn,
470                 //      int mode);
471
472                 [DllImport("pq")]
473                 public static extern int lo_tell (IntPtr conn, int fd);
474                 // int lo_tell(PGconn *conn, int fd);
475
476                 [DllImport("pq")]
477                 public static extern int lo_unlink (IntPtr conn,
478                         int lobjId);
479                 // int lo_unlink(PGconn *conn,
480                 //      Oid lobjId);
481
482                 [DllImport("pq")]
483                 public static extern int lo_import (IntPtr conn,
484                         string filename);
485                 // Oid lo_import(PGconn *conn,
486                 //      const char *filename);
487
488                 [DllImport("pq")]
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);
493
494                 [DllImport("pq")]
495                 public static extern int PQmblen (string s,
496                         int encoding);
497                 // int PQmblen(const unsigned char *s,
498                 //      int encoding);
499
500                 [DllImport("pq")]
501                 public static extern int PQenv2encoding ();
502                 // int PQenv2encoding(void);
503
504                 #endregion
505         }
506 }