5 static class PgSQLDataSource : DataSourceDriver
7 class_property(name) = "PostgreSQL";
12 String BuildLocator(DataSource ds)
35 sprintf(t, "host=%s ", ds.host), strcat(r, t);
37 sprintf(t, "port=%s ", ds.port), strcat(r, t);
39 sprintf(t, "user=%s ", ds.user), strcat(r, t);
41 sprintf(t, "password=%s ", ds.pass), strcat(r, t);
45 uint GetDatabasesCount()
56 bool Connect(const String locator)
60 strcat(loc, " dbname=postgres ");*/
61 this.locator = CopyString(locator);
62 /*conn = PQconnectdb(locator);
63 if(PQstatus(conn) != CONNECTION_OK)
65 Logf("Connection to database failed: %s", PQerrorMessage(conn));
73 Log("Status: Feeling groovy!\n");
76 bool RenameDatabase(const String name, const String rename)
80 bool DeleteDatabase(const String name)
84 Database OpenDatabase(const String name, CreateOptions createOptions)
86 char dbLocator[MAX_F_STRING];
90 //PGconn *PQconnectStart(const char *conninfo);
91 //PostgresPollingStatusType PQconnectPoll(PGconn *conn);
92 //void PQreset(PGconn *conn);
93 //int PQresetStart(PGconn *conn);
94 //PostgresPollingStatusType PQresetPoll(PGconn *conn);
96 /*switch(PQstatus(conn))
98 case CONNECTION_STARTED:
99 feedback = "Connecting...";
101 case CONNECTION_MADE:
102 feedback = "Connected to server...";
104 case CONNECTION_AWAITING_RESPONSE:
105 case CONNECTION_AUTH_OK:
106 case CONNECTION_SSL_STARTUP:
107 case CONNECTION_SETENV:
109 feedback = "Connecting...";
112 sprintf(dbLocator, "%s dbname=%s ", locator, name);
113 conn = PQconnectdb(dbLocator);
114 if(PQstatus(conn) != CONNECTION_OK)
116 Logf("Connection to database failed: %s", PQerrorMessage(conn));
119 db = PgSQLDatabase { conn = conn };
124 class PgSQLDatabase : Database
129 char *PQuser(const PGconn *conn);
130 char *PQpass(const PGconn *conn);
131 char *PQhost(const PGconn *conn);
132 char *PQport(const PGconn *conn);
133 char *PQtty(const PGconn *conn);
134 char *PQoptions(const PGconn *conn);
135 ConnStatusType PQstatus(const PGconn *conn);
136 PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
137 const char *PQparameterStatus(const PGconn *conn, const char *paramName);
138 int PQprotocolVersion(const PGconn *conn);
139 int PQserverVersion(const PGconn *conn);
140 char *PQerrorMessage(const PGconn *conn);
141 int PQsocket(const PGconn *conn);
142 int PQbackendPID(const PGconn *conn);
143 SSL *PQgetssl(const PGconn *conn);
154 return conn ? PQdb(conn) : null;
157 uint ObjectsCount(ObjectType type)
163 bool RenameObject(ObjectType type, const String name, const String rename)
169 bool DeleteObject(ObjectType type, const String name)
175 Table OpenTable(const String name, OpenOptions options)
177 char t[MAX_F_STRING];
184 sprintf(t, "SELECT * FROM %s;", name);
197 ExecStatusType status;
199 tbl.res = PQexec(conn, sql);
200 //PGresult *PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes,
201 // const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
202 //PGresult *PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes);
203 //PGresult *PQexecPrepared(PGconn *conn, const char *stmtName, int nParams, const char * const *paramValues,
204 // const int *paramLengths, const int *paramFormats, int resultFormat);
205 //PGresult *PQdescribePrepared(PGconn *conn, const char *stmtName);
206 //PGresult *PQdescribePortal(PGconn *conn, const char *portalName);
207 //char *PQresultErrorMessage(const PGresult *res);
208 //char *PQresultErrorField(const PGresult *res, int fieldcode);
212 PG_DIAG_MESSAGE_PRIMARY
213 PG_DIAG_MESSAGE_DETAIL
215 PG_DIAG_STATEMENT_POSITION
216 PG_DIAG_INTERNAL_POSITION
217 PG_DIAG_INTERNAL_QUERY
221 PG_DIAG_SOURCE_FUNCTION
223 //PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
226 //int PQsendQuery(PGconn *conn, const char *command);
227 //int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes,
228 // const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
229 //int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes);
230 //int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char * const *paramValues,
231 // const int *paramLengths, const int *paramFormats, int resultFormat);
232 //int PQsendDescribePrepared(PGconn *conn, const char *stmtName);
233 //int PQsendDescribePortal(PGconn *conn, const char *portalName);
234 //PGresult *PQgetResult(PGconn *conn);
235 //int PQconsumeInput(PGconn *conn);
236 //int PQisBusy(PGconn *conn);
237 //int PQsetnonblocking(PGconn *conn, int arg);
238 //int PQisnonblocking(const PGconn *conn);
239 //int PQflush(PGconn *conn);
240 //PGcancel *PQgetCancel(PGconn *conn);
241 //void PQfreeCancel(PGcancel *cancel);
242 //int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
243 //int PQrequestCancel(PGconn *conn);
246 status = PQresultStatus(tbl.res);
248 status = PGRES_FATAL_ERROR;
251 case PGRES_EMPTY_QUERY: //The string sent to the server was empty.
252 case PGRES_COPY_OUT: //Copy Out (from server) data transfer started.
253 case PGRES_COPY_IN: //Copy In (to server) data transfer started.
254 case PGRES_BAD_RESPONSE: //The server's response was not understood.
255 case PGRES_NONFATAL_ERROR: //A nonfatal error (a notice or warning) occurred.
256 case PGRES_FATAL_ERROR: //A fatal error occurred.
257 case PGRES_COMMAND_OK: //Successful completion of a command returning no data.
258 Logf("Open table failed: %s", PQresStatus(status));
260 case PGRES_TUPLES_OK: //Successful completion of a command returning data (such as a SELECT or SHOW).
263 //int PQbinaryTuples(const PGresult *res); // returns 0 for textual and 1 for binary
270 class PgSQLTable : Table
285 Field GetFirstField()
290 uint GetFieldsCount()
292 return res ? PQnfields(res) : 0;
297 return res ? PQntuples(res) : 0;
300 DriverRow CreateRow()
305 Field AddField(const String name, Class type, int length)
310 //char *PQfname(const PGresult *res, int column_number);
311 //Oid PQftable(const PGresult *res, int column_number);
312 //int PQftablecol(const PGresult *res, int column_number);
313 //int PQfformat(const PGresult *res, int column_number);
314 //Oid PQftype(const PGresult *res, int column_number);
315 //int PQfmod(const PGresult *res, int column_number);
316 //int PQfsize(const PGresult *res, int column_number);
317 //int PQbinaryTuples(const PGresult *res);
318 //char *PQgetvalue(const PGresult *res, int row_number, int column_number);
319 //int PQgetisnull(const PGresult *res, int row_number, int column_number);
320 //int PQgetlength(const PGresult *res, int row_number, int column_number);
321 //int PQnparams(const PGresult *res);
322 //Oid PQparamtype(const PGresult *res, int param_number);
323 //void PQprint(FILE *fout, /* output stream */ const PGresult *res, const PQprintOpt *po);
325 //char *PQcmdStatus(PGresult *res);
326 //char *PQcmdTuples(PGresult *res);
327 //Oid PQoidValue(const PGresult *res);
328 //char *PQoidStatus(const PGresult *res);
330 //size_t PQescapeStringConn (PGconn *conn, char *to, const char *from, size_t length, int *error);
331 //size_t PQescapeString (char *to, const char *from, size_t length);
333 //unsigned char *PQescapeByteaConn(PGconn *conn, const unsigned char *from, size_t from_length, size_t *to_length);
334 //unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length, size_t *to_length);
335 //unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length);
336 //void PQfreemem(void *ptr);
338 Field FindField(const String name)
342 int num = PQfnumber(res, name);
345 PgSQLField fld { num = num };
352 bool Index(int count, FieldIndex * fieldIndexes)
358 static class PgSQLField : Field
368 //int PQfformat(const PGresult *res, int column_number); // returns 0 for textual and 1 for binary
369 //Oid PQftype(const PGresult *res, int column_number);
370 //query the system table pg_type to obtain the names and properties of the various data types
371 //int PQfmod(const PGresult *res, int column_number);
372 //int PQfsize(const PGresult *res, int column_number);
374 String GetName() { return (tbl && tbl.res) ? PQfname(tbl.res, num) : null; }
375 Class GetType() { return type; }
376 int GetLength() { return length; }
377 Field GetNext() { return next; }
378 Field GetPrev() { return prev; }
384 void Free() { delete this; }
388 //char *PQgetvalue(const PGresult *res, int row_number, int column_number);
389 //int PQgetisnull(const PGresult *res, int row_number, int column_number);
390 //int PQgetlength(const PGresult *res, int row_number, int column_number);
392 //void PQprint(FILE *fout, /* output stream */
393 // const PGresult *res,
394 // const PQprintOpt *po);
397 // pqbool header; /* print output field headings and row count */
398 // pqbool align; /* fill align the fields */
399 // pqbool standard; /* old brain dead format */
400 // pqbool html3; /* output HTML tables */
401 // pqbool expanded; /* expand tables */
402 // pqbool pager; /* use pager for output if needed */
403 // char *fieldSep; /* field separator */
404 // char *tableOpt; /* attributes for HTML table element */
405 // char *caption; /* HTML table caption */
406 // char **fieldName; /* null-terminated array of replacement field names */
409 static class PgSQLRow : DriverRow
426 bool GetData(PgSQLField field, typed_object & data)
430 bool SetData(PgSQLField field, typed_object data)
439 bool Select(MoveOptions move)
441 int rowsCount = tbl.GetRowsCount();
450 if(_num != rowsCount)
470 int CompareRowPath(char * apath, PgSQLField field, typed_object data)
474 int CompareRowNum(int num, PgSQLField field, typed_object data)
478 int CompareRow(PgSQLField field, typed_object data)
482 bool Find(PgSQLField field, MoveOptions move, MatchOptions match, typed_object data)