2002-04-16 Daniel Morgan <danmorg@sc.rr.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 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         // FIXME: should be sealed internal class
59         sealed internal class PostgresLibrary
60         {
61                 #region PInvoke Functions
62
63                 // pinvoke prototypes to PostgreSQL client library
64                 // pq.dll on windows and libpq.so on linux
65
66                 [DllImport("pq")]
67                 public static extern IntPtr PQconnectStart (string conninfo);
68                 // PGconn *PQconnectStart(const char *conninfo);
69
70                 [DllImport("pq")]
71                 public static extern PostgresPollingStatusType PQconnectPoll (IntPtr conn);
72                 // PostgresPollingStatusType PQconnectPoll(PGconn *conn);       
73
74                 [DllImport("pq")]
75                 public static extern IntPtr PQconnectdb (string conninfo);
76                 // PGconn *PQconnectdb(const char *conninfo);
77
78                 [DllImport("pq")]
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);
87
88                 [DllImport("pq")]
89                 public static extern void PQfinish (IntPtr conn);
90                 // void PQfinish(PGconn *conn);
91
92                 [DllImport("pq")]
93                 public static extern IntPtr PQconndefaults ();
94                 // PQconninfoOption *PQconndefaults(void);
95
96                 [DllImport("pq")]
97                 public static extern void PQconninfoFree (IntPtr connOptions);
98                 // void PQconninfoFree(PQconninfoOption *connOptions);
99
100                 [DllImport("pq")]
101                 public static extern int PQresetStart (IntPtr conn);
102                 // int PQresetStart(PGconn *conn);
103
104                 [DllImport("pq")]
105                 public static extern IntPtr PQresetPoll (IntPtr conn);
106                 // PostgresPollingStatusType PQresetPoll(PGconn *conn);
107
108                 [DllImport("pq")]
109                 public static extern void PQreset (IntPtr conn);
110                 // void PQreset(PGconn *conn);
111
112                 [DllImport("pq")]
113                 public static extern int PQrequestCancel (IntPtr conn);
114                 // int PQrequestCancel(PGconn *conn);
115
116                 [DllImport("pq")]
117                 public static extern string PQdb (IntPtr conn);
118                 // char *PQdb(const PGconn *conn);
119
120                 [DllImport("pq")]
121                 public static extern string PQuser (IntPtr conn);
122                 // char *PQuser(const PGconn *conn);
123
124                 [DllImport("pq")]
125                 public static extern string PQpass (IntPtr conn);
126                 // char *PQpass(const PGconn *conn);
127
128                 [DllImport("pq")]
129                 public static extern string PQhost (IntPtr conn);
130                 // char *PQhost(const PGconn *conn);
131
132                 [DllImport("pq")]
133                 public static extern string PQport (IntPtr conn);
134                 // char *PQport(const PGconn *conn);
135
136                 [DllImport("pq")]
137                 public static extern string PQtty (IntPtr conn);
138                 // char *PQtty(const PGconn *conn);
139
140                 [DllImport("pq")]
141                 public static extern string PQoptions (IntPtr conn);
142                 // char *PQoptions(const PGconn *conn);
143
144                 [DllImport("pq")]
145                 public static extern ConnStatusType PQstatus (IntPtr conn);
146                 // ConnStatusType PQstatus(const PGconn *conn);
147
148                 [DllImport("pq")]
149                 public static extern string PQerrorMessage (IntPtr conn);
150                 // char *PQerrorMessage(const PGconn *conn);
151
152                 [DllImport("pq")]
153                 public static extern int PQsocket (IntPtr conn);
154                 // int PQsocket(const PGconn *conn);
155
156                 [DllImport("pq")]
157                 public static extern int PQbackendPID (IntPtr conn);
158                 // int PQbackendPID(const PGconn *conn);
159
160                 [DllImport("pq")]
161                 public static extern int PQclientEncoding (IntPtr conn);
162                 // int PQclientEncoding(const PGconn *conn);
163
164                 [DllImport("pq")]
165                 public static extern int PQsetClientEncoding (IntPtr conn,
166                         string encoding);
167                 // int PQsetClientEncoding(PGconn *conn, 
168                 //              const char *encoding);
169
170                 //FIXME: when loading, causes runtime exception
171                 //[DllImport("pq")]
172                 //public static extern IntPtr PQgetssl (IntPtr conn);
173                 // SSL *PQgetssl(PGconn *conn);
174
175                 [DllImport("pq")]
176                 public static extern void PQtrace (IntPtr conn, 
177                         IntPtr debug_port);
178                 // void PQtrace(PGconn *conn, 
179                 //              FILE *debug_port);
180
181                 [DllImport("pq")]
182                 public static extern void PQuntrace (IntPtr conn);
183                 // void PQuntrace(PGconn *conn);
184
185                 [DllImport("pq")]
186                 public static extern IntPtr PQsetNoticeProcessor (IntPtr conn,
187                         IntPtr proc, IntPtr arg);
188                 // PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, 
189                 //              PQnoticeProcessor proc, void *arg);
190
191                 [DllImport("pq")]
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);
196
197                 [DllImport("pq")]
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);
202
203                 [DllImport("pq")]
204                 public static extern IntPtr PQexec (IntPtr conn,
205                         string query);
206                 // PGresult *PQexec(PGconn *conn, 
207                 //      const char *query);
208
209                 [DllImport("pq")]
210                 public static extern IntPtr PQnotifies (IntPtr conn);
211                 // PGnotify *PQnotifies(PGconn *conn);
212
213                 [DllImport("pq")]
214                 public static extern void PQfreeNotify (IntPtr notify);
215                 // void PQfreeNotify(PGnotify *notify);
216
217                 [DllImport("pq")]
218                 public static extern int PQsendQuery (IntPtr conn,
219                         string query);
220                 // int PQsendQuery(PGconn *conn, 
221                 //      const char *query);
222
223                 [DllImport("pq")]
224                 public static extern IntPtr PQgetResult (IntPtr conn);
225                 // PGresult *PQgetResult(PGconn *conn);
226
227                 [DllImport("pq")]
228                 public static extern int PQisBusy (IntPtr conn);
229                 // int PQisBusy(PGconn *conn);
230
231                 [DllImport("pq")]
232                 public static extern int PQconsumeInput (IntPtr conn);
233                 // int PQconsumeInput(PGconn *conn);
234
235                 [DllImport("pq")]
236                 public static extern int PQgetline (IntPtr conn,
237                         string str, int length);
238                 // int PQgetline(PGconn *conn,
239                 //      char *string, int length);
240
241                 [DllImport("pq")]
242                 public static extern int PQputline (IntPtr conn,
243                         string str);
244                 // int PQputline(PGconn *conn, 
245                 //      const char *string);
246
247                 [DllImport("pq")]
248                 public static extern int PQgetlineAsync (IntPtr conn,
249                         string buffer, int bufsize);
250                 // int PQgetlineAsync(PGconn *conn, char *buffer,
251                 //      int bufsize);
252
253                 [DllImport("pq")]
254                 public static extern int PQputnbytes (IntPtr conn,
255                         string buffer, int nbytes);
256                 // int PQputnbytes(PGconn *conn, 
257                 //const char *buffer, int nbytes);
258
259                 [DllImport("pq")]
260                 public static extern int PQendcopy (IntPtr conn);
261                 // int PQendcopy(PGconn *conn);
262
263                 [DllImport("pq")]
264                 public static extern int PQsetnonblocking (IntPtr conn,
265                         int arg);
266                 // int PQsetnonblocking(PGconn *conn, int arg);
267
268                 [DllImport("pq")]
269                 public static extern int PQisnonblocking (IntPtr conn);
270                 // int PQisnonblocking(const PGconn *conn);
271
272                 [DllImport("pq")]
273                 public static extern int PQflush (IntPtr conn);
274                 // int PQflush(PGconn *conn);
275
276                 [DllImport("pq")]
277                 public static extern IntPtr PQfn (IntPtr conn, int fnid, 
278                         IntPtr result_buf, IntPtr result_len, 
279                         int result_is_int, IntPtr args,
280                         int nargs);
281                 // PGresult *PQfn(PGconn *conn, int fnid, 
282                 //      int *result_buf, int *result_len, 
283                 //      int result_is_int, const PQArgBlock *args,
284                 //      int nargs);
285
286                 [DllImport("pq")]
287                 public static extern ExecStatusType PQresultStatus (IntPtr res);
288                 // ExecStatusType PQresultStatus(const PGresult *res);
289
290                 [DllImport("pq")]
291                 public static extern string PQresStatus (IntPtr status);
292                 // char *PQresStatus(ExecStatusType status);
293
294                 [DllImport("pq")]
295                 public static extern string PQresultErrorMessage (IntPtr res);
296                 // char *PQresultErrorMessage(const PGresult *res);
297
298                 [DllImport("pq")]
299                 public static extern int PQntuples (IntPtr res);
300                 // int PQntuples(const PGresult *res);
301
302                 [DllImport("pq")]
303                 public static extern int PQnfields (IntPtr res);
304                 // int PQnfields(const PGresult *res);
305
306                 [DllImport("pq")]
307                 public static extern int PQbinaryTuples (IntPtr res);
308                 // int PQbinaryTuples(const PGresult *res);
309
310                 [DllImport("pq")]
311                 public static extern string PQfname (IntPtr res,
312                         int field_num);
313                 // char *PQfname(const PGresult *res,
314                 //      int field_num);
315
316                 [DllImport("pq")]
317                 public static extern int PQfnumber (IntPtr res,
318                         string field_name);
319                 // int PQfnumber(const PGresult *res, 
320                 //      const char *field_name);
321
322                 [DllImport("pq")]
323                 public static extern int PQftype (IntPtr res,
324                         int field_num);
325                 // Oid PQftype(const PGresult *res,
326                 //      int field_num);
327
328                 [DllImport("pq")]
329                 public static extern int PQfsize (IntPtr res,
330                         int field_num);
331                 // int PQfsize(const PGresult *res,
332                 //      int field_num);
333
334                 [DllImport("pq")]
335                 public static extern int PQfmod (IntPtr res, int field_num);
336                 // int PQfmod(const PGresult *res, int field_num);
337
338                 [DllImport("pq")]
339                 public static extern string PQcmdStatus (IntPtr res);
340                 // char *PQcmdStatus(PGresult *res);
341
342                 [DllImport("pq")]
343                 public static extern string PQoidStatus (IntPtr res);
344                 // char *PQoidStatus(const PGresult *res);
345
346                 [DllImport("pq")]
347                 public static extern int PQoidValue (IntPtr res);
348                 // Oid PQoidValue(const PGresult *res);
349
350                 [DllImport("pq")]
351                 public static extern string PQcmdTuples (IntPtr res);
352                 // char *PQcmdTuples(PGresult *res);
353
354                 [DllImport("pq")]
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);
359
360                 [DllImport("pq")]
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);
365
366                 [DllImport("pq")]
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);
371
372                 [DllImport("pq")]
373                 public static extern void PQclear (IntPtr res);
374                 // void PQclear(PGresult *res);
375
376                 [DllImport("pq")]
377                 public static extern IntPtr PQmakeEmptyPGresult (IntPtr conn,
378                         IntPtr status);
379                 // PGresult *PQmakeEmptyPGresult(PGconn *conn,
380                 //      ExecStatusType status);
381
382                 [DllImport("pq")]
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);
387
388                 [DllImport("pq")]
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);
395
396                 [DllImport("pq")]
397                 public static extern void PQprintTuples (IntPtr res,
398                         IntPtr fout, int printAttName, int terseOutput, 
399                         int width);
400                 // void PQprintTuples(const PGresult *res,
401                 //      FILE *fout, int printAttName, int terseOutput, 
402                 //      int width);                                             
403
404                 [DllImport("pq")]
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);
409
410                 [DllImport("pq")]
411                 public static extern int lo_close (IntPtr conn, int fd);
412                 // int lo_close(PGconn *conn, int fd);
413
414                 [DllImport("pq")]
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);
419
420                 [DllImport("pq")]
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);
425
426                 [DllImport("pq")]
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);
431
432                 [DllImport("pq")]
433                 public static extern int lo_creat (IntPtr conn,
434                         int mode);
435                 // Oid lo_creat(PGconn *conn,
436                 //      int mode);
437
438                 [DllImport("pq")]
439                 public static extern int lo_tell (IntPtr conn, int fd);
440                 // int lo_tell(PGconn *conn, int fd);
441
442                 [DllImport("pq")]
443                 public static extern int lo_unlink (IntPtr conn,
444                         int lobjId);
445                 // int lo_unlink(PGconn *conn,
446                 //      Oid lobjId);
447
448                 [DllImport("pq")]
449                 public static extern int lo_import (IntPtr conn,
450                         string filename);
451                 // Oid lo_import(PGconn *conn,
452                 //      const char *filename);
453
454                 [DllImport("pq")]
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);
459
460                 [DllImport("pq")]
461                 public static extern int PQmblen (string s,
462                         int encoding);
463                 // int PQmblen(const unsigned char *s,
464                 //      int encoding);
465
466                 [DllImport("pq")]
467                 public static extern int PQenv2encoding ();
468                 // int PQenv2encoding(void);
469
470                 #endregion
471         }
472 }