2002-09-16 Gaurav Vaish <gvaish_mono@lycos.com>
[mono.git] / mcs / class / Mono.Data.PostgreSqlClient / 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 // *** uncomment #define to get debug messages, comment for production ***
18 //#define DEBUG_PostgresLibrary
19
20 using System;
21 using System.Data;
22 using System.Runtime.InteropServices;
23 using System.Diagnostics;
24 using System.Collections;
25
26 namespace System.Data.SqlClient {
27
28         /* IMPORTANT: DO NOT CHANGE ANY OF THESE ENUMS BELOW */
29         
30         internal enum ConnStatusType
31         {
32                 CONNECTION_OK,
33                 CONNECTION_BAD,
34                 CONNECTION_STARTED,
35                 CONNECTION_MADE,
36                 CONNECTION_AWAITING_RESPONSE,
37                 CONNECTION_AUTH_OK,                      
38                 CONNECTION_SETENV               
39         } 
40
41         internal enum PostgresPollingStatusType
42         {
43                 PGRES_POLLING_FAILED = 0,
44                 PGRES_POLLING_READING,
45                 PGRES_POLLING_WRITING,
46                 PGRES_POLLING_OK,
47                 PGRES_POLLING_ACTIVE
48         }
49
50         internal enum ExecStatusType
51         {
52                 PGRES_EMPTY_QUERY = 0,
53                 PGRES_COMMAND_OK,                       
54                 PGRES_TUPLES_OK,                        
55                 PGRES_COPY_OUT,                         
56                 PGRES_COPY_IN,                          
57                 PGRES_BAD_RESPONSE,                     
58                 PGRES_NONFATAL_ERROR,
59                 PGRES_FATAL_ERROR
60         }
61
62         sealed internal class PostgresLibrary
63         {
64                 #region PInvoke Functions
65
66                 // pinvoke prototypes to PostgreSQL client library
67                 // pq.dll on windows and libpq.so on linux
68
69                 [DllImport("pq")]
70                 public static extern IntPtr PQconnectStart (string conninfo);
71                 // PGconn *PQconnectStart(const char *conninfo);
72
73                 [DllImport("pq")]
74                 public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
75                 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);       
76
77                 [DllImport("pq")]
78                 public static extern IntPtr PQconnectdb (string conninfo);
79                 // PGconn *PQconnectdb(const char *conninfo);
80
81                 [DllImport("pq")]
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);
90
91                 [DllImport("pq")]
92                 public static extern void PQfinish (IntPtr conn);
93                 // void PQfinish(PGconn *conn);
94
95                 [DllImport("pq")]
96                 public static extern IntPtr PQconndefaults ();
97                 // PQconninfoOption *PQconndefaults(void);
98
99                 [DllImport("pq")]
100                 public static extern void PQconninfoFree (IntPtr connOptions);
101                 // void PQconninfoFree(PQconninfoOption *connOptions);
102
103                 [DllImport("pq")]
104                 public static extern int PQresetStart (IntPtr conn);
105                 // int PQresetStart(PGconn *conn);
106
107                 [DllImport("pq")]
108                 public static extern IntPtr PQresetPoll (IntPtr conn);
109                 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
110
111                 [DllImport("pq")]
112                 public static extern void PQreset (IntPtr conn);
113                 // void PQreset(PGconn *conn);
114
115                 [DllImport("pq")]
116                 public static extern int PQrequestCancel (IntPtr conn);
117                 // int PQrequestCancel(PGconn *conn);
118
119                 [DllImport("pq")]
120                 public static extern string PQdb (IntPtr conn);
121                 // char *PQdb(const PGconn *conn);
122
123                 [DllImport("pq")]
124                 public static extern string PQuser (IntPtr conn);
125                 // char *PQuser(const PGconn *conn);
126
127                 [DllImport("pq")]
128                 public static extern string PQpass (IntPtr conn);
129                 // char *PQpass(const PGconn *conn);
130
131                 [DllImport("pq")]
132                 public static extern string PQhost (IntPtr conn);
133                 // char *PQhost(const PGconn *conn);
134
135                 [DllImport("pq")]
136                 public static extern string PQport (IntPtr conn);
137                 // char *PQport(const PGconn *conn);
138
139                 [DllImport("pq")]
140                 public static extern string PQtty (IntPtr conn);
141                 // char *PQtty(const PGconn *conn);
142
143                 [DllImport("pq")]
144                 public static extern string PQoptions (IntPtr conn);
145                 // char *PQoptions(const PGconn *conn);
146
147                 [DllImport("pq")]
148                 public static extern ConnStatusType PQstatus (IntPtr conn);
149                 // ConnStatusType PQstatus(const PGconn *conn);
150
151                 [DllImport("pq")]
152                 public static extern string PQerrorMessage (IntPtr conn);
153                 // char *PQerrorMessage(const PGconn *conn);
154
155                 [DllImport("pq")]
156                 public static extern int PQsocket (IntPtr conn);
157                 // int PQsocket(const PGconn *conn);
158
159                 [DllImport("pq")]
160                 public static extern int PQbackendPID (IntPtr conn);
161                 // int PQbackendPID(const PGconn *conn);
162
163                 [DllImport("pq")]
164                 public static extern int PQclientEncoding (IntPtr conn);
165                 // int PQclientEncoding(const PGconn *conn);
166
167                 [DllImport("pq")]
168                 public static extern int PQsetClientEncoding (IntPtr conn,
169                         string encoding);
170                 // int PQsetClientEncoding(PGconn *conn, 
171                 //              const char *encoding);
172
173                 //FIXME: when loading, causes runtime exception
174                 //[DllImport("pq")]
175                 //public static extern IntPtr PQgetssl (IntPtr conn);
176                 // SSL *PQgetssl(PGconn *conn);
177
178                 [DllImport("pq")]
179                 public static extern void PQtrace (IntPtr conn, 
180                         IntPtr debug_port);
181                 // void PQtrace(PGconn *conn, 
182                 //              FILE *debug_port);
183
184                 [DllImport("pq")]
185                 public static extern void PQuntrace (IntPtr conn);
186                 // void PQuntrace(PGconn *conn);
187
188                 [DllImport("pq")]
189                 public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
190                         IntPtr proc, IntPtr arg);
191                 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, 
192                 //              PQnoticeProcessor proc, void *arg);
193
194                 [DllImport("pq")]
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);
199
200                 [DllImport("pq")]
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);
205
206                 [DllImport("pq")]
207                 public static extern IntPtr PQexec (IntPtr conn,
208                         string query);
209                 // PGresult *PQexec(PGconn *conn, 
210                 //      const char *query);
211
212                 [DllImport("pq")]
213                 public static extern IntPtr PQnotifies (IntPtr conn);
214                 // PGnotify *PQnotifies(PGconn *conn);
215
216                 [DllImport("pq")]
217                 public static extern void PQfreeNotify (IntPtr notify);
218                 // void PQfreeNotify(PGnotify *notify);
219
220                 [DllImport("pq")]
221                 public static extern int PQsendQuery (IntPtr conn,
222                         string query);
223                 // int PQsendQuery(PGconn *conn, 
224                 //      const char *query);
225
226                 [DllImport("pq")]
227                 public static extern IntPtr PQgetResult (IntPtr conn);
228                 // PGresult *PQgetResult(PGconn *conn);
229
230                 [DllImport("pq")]
231                 public static extern int PQisBusy (IntPtr conn);
232                 // int PQisBusy(PGconn *conn);
233
234                 [DllImport("pq")]
235                 public static extern int PQconsumeInput (IntPtr conn);
236                 // int PQconsumeInput(PGconn *conn);
237
238                 [DllImport("pq")]
239                 public static extern int PQgetline (IntPtr conn,
240                         string str, int length);
241                 // int PQgetline(PGconn *conn,
242                 //      char *string, int length);
243
244                 [DllImport("pq")]
245                 public static extern int PQputline (IntPtr conn,
246                         string str);
247                 // int PQputline(PGconn *conn, 
248                 //      const char *string);
249
250                 [DllImport("pq")]
251                 public static extern int PQgetlineAsync (IntPtr conn,
252                         string buffer, int bufsize);
253                 // int PQgetlineAsync(PGconn *conn, char *buffer,
254                 //      int bufsize);
255
256                 [DllImport("pq")]
257                 public static extern int PQputnbytes (IntPtr conn,
258                         string buffer, int nbytes);
259                 // int PQputnbytes(PGconn *conn, 
260                 //const char *buffer, int nbytes);
261
262                 [DllImport("pq")]
263                 public static extern int PQendcopy (IntPtr conn);
264                 // int PQendcopy(PGconn *conn);
265
266                 [DllImport("pq")]
267                 public static extern int PQsetnonblocking (IntPtr conn,
268                         int arg);
269                 // int PQsetnonblocking(PGconn *conn, int arg);
270
271                 [DllImport("pq")]
272                 public static extern int PQisnonblocking (IntPtr conn);
273                 // int PQisnonblocking(const PGconn *conn);
274
275                 [DllImport("pq")]
276                 public static extern int PQflush (IntPtr conn);
277                 // int PQflush(PGconn *conn);
278
279                 [DllImport("pq")]
280                 public static extern IntPtr PQfn (IntPtr conn, int fnid, 
281                         IntPtr result_buf, IntPtr result_len, 
282                         int result_is_int, IntPtr args,
283                         int nargs);
284                 // PGresult *PQfn(PGconn *conn, int fnid, 
285                 //      int *result_buf, int *result_len, 
286                 //      int result_is_int, const PQArgBlock *args,
287                 //      int nargs);
288
289                 [DllImport("pq")]
290                 public static extern ExecStatusType PQresultStatus (IntPtr res);
291                 // ExecStatusType PQresultStatus(const PGresult *res);
292
293                 [DllImport("pq")]
294                 public static extern string PQresStatus (ExecStatusType status);
295                 // char *PQresStatus(ExecStatusType status);
296
297                 [DllImport("pq")]
298                 public static extern string PQresultErrorMessage (IntPtr res);
299                 // char *PQresultErrorMessage(const PGresult *res);
300
301                 [DllImport("pq")]
302                 public static extern int PQntuples (IntPtr res);
303                 // int PQntuples(const PGresult *res);
304
305                 [DllImport("pq")]
306                 public static extern int PQnfields (IntPtr res);
307                 // int PQnfields(const PGresult *res);
308
309                 [DllImport("pq")]
310                 public static extern int PQbinaryTuples (IntPtr res);
311                 // int PQbinaryTuples(const PGresult *res);
312
313                 [DllImport("pq")]
314                 public static extern string PQfname (IntPtr res,
315                         int field_num);
316                 // char *PQfname(const PGresult *res,
317                 //      int field_num);
318
319                 [DllImport("pq")]
320                 public static extern int PQfnumber (IntPtr res,
321                         string field_name);
322                 // int PQfnumber(const PGresult *res, 
323                 //      const char *field_name);
324
325                 [DllImport("pq")]
326                 public static extern int PQftype (IntPtr res,
327                         int field_num);
328                 // Oid PQftype(const PGresult *res,
329                 //      int field_num);
330
331                 [DllImport("pq")]
332                 public static extern int PQfsize (IntPtr res,
333                         int field_num);
334                 // int PQfsize(const PGresult *res,
335                 //      int field_num);
336
337                 [DllImport("pq")]
338                 public static extern int PQfmod (IntPtr res, int field_num);
339                 // int PQfmod(const PGresult *res, int field_num);
340
341                 [DllImport("pq")]
342                 public static extern string PQcmdStatus (IntPtr res);
343                 // char *PQcmdStatus(PGresult *res);
344
345                 [DllImport("pq")]
346                 public static extern string PQoidStatus (IntPtr res);
347                 // char *PQoidStatus(const PGresult *res);
348
349                 [DllImport("pq")]
350                 public static extern int PQoidValue (IntPtr res);
351                 // Oid PQoidValue(const PGresult *res);
352
353                 [DllImport("pq")]
354                 public static extern string PQcmdTuples (IntPtr res);
355                 // char *PQcmdTuples(PGresult *res);
356
357                 [DllImport("pq")]
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);
362
363                 [DllImport("pq")]
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);
368
369                 [DllImport("pq")]
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);
374
375                 [DllImport("pq")]
376                 public static extern void PQclear (IntPtr res);
377                 // void PQclear(PGresult *res);
378
379                 [DllImport("pq")]
380                 public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
381                         IntPtr status);
382                 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
383                 //      ExecStatusType status);
384
385                 [DllImport("pq")]
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);
390
391                 [DllImport("pq")]
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);
398
399                 [DllImport("pq")]
400                 public static extern void PQprintTuples (IntPtr res,
401                         IntPtr fout, int printAttName, int terseOutput, 
402                         int width);
403                 // void PQprintTuples(const PGresult *res,
404                 //      FILE *fout, int printAttName, int terseOutput, 
405                 //      int width);                                             
406
407                 [DllImport("pq")]
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);
412
413                 [DllImport("pq")]
414                 public static extern int lo_close (IntPtr conn, int fd);
415                 // int lo_close(PGconn *conn, int fd);
416
417                 [DllImport("pq")]
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);
422
423                 [DllImport("pq")]
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);
428
429                 [DllImport("pq")]
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);
434
435                 [DllImport("pq")]
436                 public static extern int lo_creat (IntPtr conn,
437                         int mode);
438                 // Oid lo_creat(PGconn *conn,
439                 //      int mode);
440
441                 [DllImport("pq")]
442                 public static extern int lo_tell (IntPtr conn, int fd);
443                 // int lo_tell(PGconn *conn, int fd);
444
445                 [DllImport("pq")]
446                 public static extern int lo_unlink (IntPtr conn,
447                         int lobjId);
448                 // int lo_unlink(PGconn *conn,
449                 //      Oid lobjId);
450
451                 [DllImport("pq")]
452                 public static extern int lo_import (IntPtr conn,
453                         string filename);
454                 // Oid lo_import(PGconn *conn,
455                 //      const char *filename);
456
457                 [DllImport("pq")]
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);
462
463                 [DllImport("pq")]
464                 public static extern int PQmblen (string s,
465                         int encoding);
466                 // int PQmblen(const unsigned char *s,
467                 //      int encoding);
468
469                 [DllImport("pq")]
470                 public static extern int PQenv2encoding ();
471                 // int PQenv2encoding(void);
472
473                 #endregion
474         }
475 }