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