1 module libpq.libpq;
2 
3 import core.stdc.stdio;
4 
5 extern (C):
6 nothrow:
7 @nogc:
8 
9 //pg_config_ext.h
10 alias PG_INT64_TYPE = long;
11 
12 //postgres_ext.h
13 alias uint Oid;
14 alias long pg_int64;
15 
16 enum OID_MAX = uint.max;
17 enum PG_DIAG_SEVERITY = 'S';
18 enum PG_DIAG_SQLSTATE = 'C';
19 enum PG_DIAG_MESSAGE_PRIMARY = 'M';
20 enum PG_DIAG_MESSAGE_DETAIL = 'D';
21 enum PG_DIAG_MESSAGE_HINT = 'H';
22 enum PG_DIAG_STATEMENT_POSITION = 'P';
23 enum PG_DIAG_INTERNAL_POSITION = 'p';
24 enum PG_DIAG_INTERNAL_QUERY = 'q';
25 enum PG_DIAG_CONTEXT = 'W';
26 enum PG_DIAG_SCHEMA_NAME = 's';
27 enum PG_DIAG_TABLE_NAME = 't';
28 enum PG_DIAG_COLUMN_NAME = 'c';
29 enum PG_DIAG_DATATYPE_NAME = 'd';
30 enum PG_DIAG_CONSTRAINT_NAME = 'n';
31 enum PG_DIAG_SOURCE_FILE = 'F';
32 enum PG_DIAG_SOURCE_LINE = 'L';
33 enum PG_DIAG_SOURCE_FUNCTION = 'R';
34 //libpq-fe.h
35 enum PG_COPYRES_ATTRS = 0x01;
36 enum PG_COPYRES_TUPLES = 0x02;
37 enum PG_COPYRES_EVENTS = 0x04;
38 enum PG_COPYRES_NOTICEHOOKS = 0x08;
39 enum PQnoPasswordSupplied = "fe_sendauth: no password supplied\n";
40 
41 enum 
42 {
43     CONNECTION_OK = 0,
44     CONNECTION_BAD = 1,
45     CONNECTION_STARTED = 2,
46     CONNECTION_MADE = 3,
47     CONNECTION_AWAITING_RESPONSE = 4,
48     CONNECTION_AUTH_OK = 5,
49     CONNECTION_SETENV = 6,
50     CONNECTION_SSL_STARTUP = 7,
51     CONNECTION_NEEDED = 8
52 }
53 
54 // TODO: fix aliases to alias X = Y style
55 alias int ConnStatusType;
56 
57 enum 
58 {
59     PGRES_POLLING_FAILED = 0,
60     PGRES_POLLING_READING = 1,
61     PGRES_POLLING_WRITING = 2,
62     PGRES_POLLING_OK = 3,
63     PGRES_POLLING_ACTIVE = 4
64 }
65 
66 alias int PostgresPollingStatusType;
67 
68 enum 
69 {
70     PGRES_EMPTY_QUERY = 0,
71     PGRES_COMMAND_OK = 1,
72     PGRES_TUPLES_OK = 2,
73     PGRES_COPY_OUT = 3,
74     PGRES_COPY_IN = 4,
75     PGRES_BAD_RESPONSE = 5,
76     PGRES_NONFATAL_ERROR = 6,
77     PGRES_FATAL_ERROR = 7,
78     PGRES_COPY_BOTH = 8,
79     PGRES_SINGLE_TUPLE = 9
80 }
81 
82 alias int ExecStatusType;
83 
84 enum 
85 {
86     PQTRANS_IDLE = 0,
87     PQTRANS_ACTIVE = 1,
88     PQTRANS_INTRANS = 2,
89     PQTRANS_INERROR = 3,
90     PQTRANS_UNKNOWN = 4
91 }
92 
93 alias int PGTransactionStatusType;
94 
95 enum 
96 {
97     PQERRORS_TERSE = 0,
98     PQERRORS_DEFAULT = 1,
99     PQERRORS_VERBOSE = 2
100 }
101 
102 alias int PGVerbosity;
103 
104 enum 
105 {
106     PQPING_OK = 0,
107     PQPING_REJECT = 1,
108     PQPING_NO_RESPONSE = 2,
109     PQPING_NO_ATTEMPT = 3
110 }
111 
112 alias int PGPing;
113 
114 struct pg_conn;
115 struct pg_result;
116 struct pg_cancel;
117 
118 alias pg_conn PGconn;
119 alias pg_result PGresult;
120 alias pg_cancel PGcancel;
121 
122 struct pgNotify
123 {
124     char* relname;
125     int be_pid;
126     char* extra;
127     pgNotify* next;
128 }
129 
130 alias pgNotify PGnotify;
131 
132 alias void function (void*, const(pg_result)*) PQnoticeReceiver;
133 alias void function (void*, const(char)*) PQnoticeProcessor;
134 
135 alias char pqbool;
136 
137 struct _PQprintOpt
138 {
139     pqbool header;
140     pqbool align_;
141     pqbool standard;
142     pqbool html3;
143     pqbool expanded;
144     pqbool pager;
145     char* fieldSep;
146     char* tableOpt;
147     char* caption;
148     char** fieldName;
149 }
150 
151 alias _PQprintOpt PQprintOpt;
152 
153 struct _PQconninfoOption
154 {
155     char* keyword;
156     char* envvar;
157     char* compiled;
158     char* val;
159     char* label;
160     char* dispchar;
161     int dispsize;
162 }
163 
164 alias _PQconninfoOption PQconninfoOption;
165 
166 struct _PQArgBlock
167 {
168     int len;
169     int isint;
170 
171     union
172     {
173         int* ptr;
174         int integer;
175     }
176 }
177 
178 alias _PQArgBlock PQArgBlock;
179 
180 struct pgresAttDesc
181 {
182     char* name;
183     Oid tableid;
184     int columnid;
185     int format;
186     Oid typid;
187     int typlen;
188     int atttypmod;
189 }
190 
191 alias pgresAttDesc PGresAttDesc;
192 /* ----------------
193  * Exported functions of libpq
194  * ----------------
195  */
196 
197 /* ===	in fe-connect.c === */
198 
199 /* make a new client connection to the backend */
200 /* Asynchronous (non-blocking) */
201 PGconn* PQconnectStart (const(char)* conninfo);
202 PGconn* PQconnectStartParams (const(char*)* keywords, const(char*)* values, int expand_dbname);
203 PostgresPollingStatusType PQconnectPoll (PGconn* conn);
204 PGconn* PQconnectdb (const(char)* conninfo);
205 PGconn* PQconnectdbParams (const(char*)* keywords, const(char*)* values, int expand_dbname);
206 
207 PGconn* PQsetdbLogin (const(char)* pghost, const(char)* pgport, const(char)* pgoptions, const(char)* pgtty, const(char)* dbName, const(char)* login, const(char)* pwd);
208 //#define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)  \
209 //	PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
210 PGconn* PQsetdb(const(char)* pghost, const(char)* pgport, const(char)* pgoptions, const(char)* pgtty, const(char)* dbName){
211     return PQsetdbLogin(pghost,pgport,pgoptions, pgtty,dbName,null,null);
212 }
213 
214 void PQfinish (PGconn* conn);
215 PQconninfoOption* PQconndefaults ();
216 PQconninfoOption* PQconninfoParse (const(char)* conninfo, char** errmsg);
217 PQconninfoOption* PQconninfo (PGconn* conn);
218 void PQconninfoFree (PQconninfoOption* connOptions);
219 
220 /*
221  * close the current connection and restablish a new one with the same
222  * parameters
223  */
224 /* Asynchronous (non-blocking) */
225 int PQresetStart (PGconn* conn);
226 PostgresPollingStatusType PQresetPoll (PGconn* conn);
227 void PQreset (PGconn* conn);
228 PGcancel* PQgetCancel (PGconn* conn);
229 void PQfreeCancel (PGcancel* cancel);
230 int PQcancel (PGcancel* cancel, char* errbuf, int errbufsize);
231 int PQrequestCancel (PGconn* conn);
232 char* PQdb (const(PGconn)* conn);
233 char* PQuser (const(PGconn)* conn);
234 char* PQpass (const(PGconn)* conn);
235 char* PQhost (const(PGconn)* conn);
236 char* PQport (const(PGconn)* conn);
237 char* PQtty (const(PGconn)* conn);
238 char* PQoptions (const(PGconn)* conn);
239 ConnStatusType PQstatus (const(PGconn)* conn);
240 PGTransactionStatusType PQtransactionStatus (const(PGconn)* conn);
241 const(char)* PQparameterStatus (const(PGconn)* conn, const(char)* paramName);
242 int PQprotocolVersion (const(PGconn)* conn);
243 int PQserverVersion (const(PGconn)* conn);
244 char* PQerrorMessage (const(PGconn)* conn);
245 int PQsocket (const(PGconn)* conn);
246 int PQbackendPID (const(PGconn)* conn);
247 int PQconnectionNeedsPassword (const(PGconn)* conn);
248 int PQconnectionUsedPassword (const(PGconn)* conn);
249 int PQclientEncoding (const(PGconn)* conn);
250 int PQsetClientEncoding (PGconn* conn, const(char)* encoding);
251 void* PQgetssl (PGconn* conn);
252 void PQinitSSL (int do_init);
253 void PQinitOpenSSL (int do_ssl, int do_crypto);
254 PGVerbosity PQsetErrorVerbosity (PGconn* conn, PGVerbosity verbosity);
255 void PQtrace (PGconn* conn, FILE* debug_port);
256 void PQuntrace (PGconn* conn);
257 PQnoticeReceiver PQsetNoticeReceiver (PGconn* conn, PQnoticeReceiver proc, void* arg);
258 PQnoticeProcessor PQsetNoticeProcessor (PGconn* conn, PQnoticeProcessor proc, void* arg);
259 alias void function (int) pgthreadlock_t;
260 pgthreadlock_t PQregisterThreadLock (pgthreadlock_t newhandler);
261 PGresult* PQexec (PGconn* conn, const(char)* query);
262 PGresult* PQexecParams (PGconn* conn, const(char)* command, int nParams, const(Oid)* paramTypes, const(char*)* paramValues, const(int)* paramLengths, const(int)* paramFormats, int resultFormat);
263 PGresult* PQprepare (PGconn* conn, const(char)* stmtName, const(char)* query, int nParams, const(Oid)* paramTypes);
264 PGresult* PQexecPrepared (PGconn* conn, const(char)* stmtName, int nParams, const(char*)* paramValues, const(int)* paramLengths, const(int)* paramFormats, int resultFormat);
265 int PQsendQuery (PGconn* conn, const(char)* query);
266 int PQsendQueryParams (PGconn* conn, const(char)* command, int nParams, const(Oid)* paramTypes, const(char*)* paramValues, const(int)* paramLengths, const(int)* paramFormats, int resultFormat);
267 int PQsendPrepare (PGconn* conn, const(char)* stmtName, const(char)* query, int nParams, const(Oid)* paramTypes);
268 int PQsendQueryPrepared (PGconn* conn, const(char)* stmtName, int nParams, const(char*)* paramValues, const(int)* paramLengths, const(int)* paramFormats, int resultFormat);
269 int PQsetSingleRowMode (PGconn* conn);
270 PGresult* PQgetResult (PGconn* conn);
271 int PQisBusy (PGconn* conn);
272 int PQconsumeInput (PGconn* conn);
273 PGnotify* PQnotifies (PGconn* conn);
274 int PQputCopyData (PGconn* conn, const(char)* buffer, int nbytes);
275 int PQputCopyEnd (PGconn* conn, const(char)* errormsg);
276 int PQgetCopyData (PGconn* conn, char** buffer, int async);
277 int PQgetline (PGconn* conn, char* string, int length);
278 int PQputline (PGconn* conn, const(char)* string);
279 int PQgetlineAsync (PGconn* conn, char* buffer, int bufsize);
280 int PQputnbytes (PGconn* conn, const(char)* buffer, int nbytes);
281 int PQendcopy (PGconn* conn);
282 int PQsetnonblocking (PGconn* conn, int arg);
283 int PQisnonblocking (const(PGconn)* conn);
284 int PQisthreadsafe ();
285 PGPing PQping (const(char)* conninfo);
286 PGPing PQpingParams (const(char*)* keywords, const(char*)* values, int expand_dbname);
287 int PQflush (PGconn* conn);
288 PGresult* PQfn (PGconn* conn, int fnid, int* result_buf, int* result_len, int result_is_int, const(PQArgBlock)* args, int nargs);
289 ExecStatusType PQresultStatus (const(PGresult)* res);
290 char* PQresStatus (ExecStatusType status);
291 char* PQresultErrorMessage (const(PGresult)* res);
292 char* PQresultErrorField (const(PGresult)* res, int fieldcode);
293 int PQntuples (const(PGresult)* res);
294 int PQnfields (const(PGresult)* res);
295 int PQbinaryTuples (const(PGresult)* res);
296 char* PQfname (const(PGresult)* res, int field_num);
297 int PQfnumber (const(PGresult)* res, const(char)* field_name);
298 Oid PQftable (const(PGresult)* res, int field_num);
299 int PQftablecol (const(PGresult)* res, int field_num);
300 int PQfformat (const(PGresult)* res, int field_num);
301 Oid PQftype (const(PGresult)* res, int field_num);
302 int PQfsize (const(PGresult)* res, int field_num);
303 int PQfmod (const(PGresult)* res, int field_num);
304 char* PQcmdStatus (PGresult* res);
305 char* PQoidStatus (const(PGresult)* res);
306 Oid PQoidValue (const(PGresult)* res);
307 char* PQcmdTuples (PGresult* res);
308 char* PQgetvalue (const(PGresult)* res, int tup_num, int field_num);
309 int PQgetlength (const(PGresult)* res, int tup_num, int field_num);
310 int PQgetisnull (const(PGresult)* res, int tup_num, int field_num);
311 int PQnparams (const(PGresult)* res);
312 Oid PQparamtype (const(PGresult)* res, int param_num);
313 PGresult* PQdescribePrepared (PGconn* conn, const(char)* stmt);
314 PGresult* PQdescribePortal (PGconn* conn, const(char)* portal);
315 int PQsendDescribePrepared (PGconn* conn, const(char)* stmt);
316 int PQsendDescribePortal (PGconn* conn, const(char)* portal);
317 void PQclear (PGresult* res);
318 void PQfreemem (void* ptr);
319 
320 alias PQfreeNotify = PQfreemem;
321 
322 PGresult* PQmakeEmptyPGresult (PGconn* conn, ExecStatusType status);
323 PGresult* PQcopyResult (const(PGresult)* src, int flags);
324 int PQsetResultAttrs (PGresult* res, int numAttributes, PGresAttDesc* attDescs);
325 void* PQresultAlloc (PGresult* res, size_t nBytes);
326 int PQsetvalue (PGresult* res, int tup_num, int field_num, char* value, int len);
327 size_t PQescapeStringConn (PGconn* conn, char* to, const(char)* from, size_t length, int* error);
328 char* PQescapeLiteral (PGconn* conn, const(char)* str, size_t len);
329 char* PQescapeIdentifier (PGconn* conn, const(char)* str, size_t len);
330 ubyte* PQescapeByteaConn (PGconn* conn, const(ubyte)* from, size_t from_length, size_t* to_length);
331 ubyte* PQunescapeBytea (const(ubyte)* strtext, size_t* retbuflen);
332 size_t PQescapeString (char* to, const(char)* from, size_t length);
333 ubyte* PQescapeBytea (const(ubyte)* from, size_t from_length, size_t* to_length);
334 void PQprint (FILE* fout, const(PGresult)* res, const(PQprintOpt)* ps);
335 void PQdisplayTuples (const(PGresult)* res, FILE* fp, int fillAlign, const(char)* fieldSep, int printHeader, int quiet);
336 void PQprintTuples (const(PGresult)* res, FILE* fout, int printAttName, int terseOutput, int width);
337 int lo_open (PGconn* conn, Oid lobjId, int mode);
338 int lo_close (PGconn* conn, int fd);
339 int lo_read (PGconn* conn, int fd, char* buf, size_t len);
340 int lo_write (PGconn* conn, int fd, const(char)* buf, size_t len);
341 int lo_lseek (PGconn* conn, int fd, int offset, int whence);
342 pg_int64 lo_lseek64 (PGconn* conn, int fd, pg_int64 offset, int whence);
343 Oid lo_creat (PGconn* conn, int mode);
344 Oid lo_create (PGconn* conn, Oid lobjId);
345 int lo_tell (PGconn* conn, int fd);
346 pg_int64 lo_tell64 (PGconn* conn, int fd);
347 int lo_truncate (PGconn* conn, int fd, size_t len);
348 int lo_truncate64 (PGconn* conn, int fd, pg_int64 len);
349 int lo_unlink (PGconn* conn, Oid lobjId);
350 Oid lo_import (PGconn* conn, const(char)* filename);
351 Oid lo_import_with_oid (PGconn* conn, const(char)* filename, Oid lobjId);
352 int lo_export (PGconn* conn, Oid lobjId, const(char)* filename);
353 int PQlibVersion ();
354 int PQmblen (const(char)* s, int encoding);
355 int PQdsplen (const(char)* s, int encoding);
356 int PQenv2encoding ();
357 char* PQencryptPassword (const(char)* passwd, const(char)* user);
358 int pg_char_to_encoding (const(char)* name);
359 const(char)* pg_encoding_to_char (int encoding);
360 int pg_valid_server_encoding_id (int encoding);
361 
362 //libpq-events.h
363 
364 enum _Anonymous_0
365 {
366     PGEVT_REGISTER = 0,
367     PGEVT_CONNRESET = 1,
368     PGEVT_CONNDESTROY = 2,
369     PGEVT_RESULTCREATE = 3,
370     PGEVT_RESULTCOPY = 4,
371     PGEVT_RESULTDESTROY = 5
372 }
373 
374 alias _Anonymous_0 PGEventId;
375 
376 struct _Anonymous_1
377 {
378     PGconn* conn;
379 }
380 
381 alias _Anonymous_1 PGEventRegister;
382 
383 struct _Anonymous_2
384 {
385     PGconn* conn;
386 }
387 
388 alias _Anonymous_2 PGEventConnReset;
389 
390 struct _Anonymous_3
391 {
392     PGconn* conn;
393 }
394 
395 alias _Anonymous_3 PGEventConnDestroy;
396 
397 struct _Anonymous_4
398 {
399     PGconn* conn;
400     PGresult* result;
401 }
402 
403 alias _Anonymous_4 PGEventResultCreate;
404 
405 struct _Anonymous_5
406 {
407     const(PGresult)* src;
408     PGresult* dest;
409 }
410 
411 alias _Anonymous_5 PGEventResultCopy;
412 
413 struct _Anonymous_6
414 {
415     PGresult* result;
416 }
417 
418 alias _Anonymous_6 PGEventResultDestroy;
419 alias int function (_Anonymous_0, void*, void*) PGEventProc;
420 
421 int PQregisterEventProc (PGconn* conn, PGEventProc proc, const(char)* name, void* passThrough);
422 int PQsetInstanceData (PGconn* conn, PGEventProc proc, void* data);
423 void* PQinstanceData (const(PGconn)* conn, PGEventProc proc);
424 int PQresultSetInstanceData (PGresult* result, PGEventProc proc, void* data);
425 void* PQresultInstanceData (const(PGresult)* result, PGEventProc proc);
426 int PQfireResultCreateEvents (PGconn* conn, PGresult* res);