9 PGconn *PGh[HNDMAX] = {
10 NULL, NULL, NULL, NULL, NULL,
11 NULL, NULL, NULL, NULL, NULL
14 #define E_NOHANDLERS 0
16 char *msqlErrors[] = {
17 "Out of database handlers."
20 char msqlErrMsg[BUFSIZ],
22 PGresult *queryres = NULL;
25 msqlConnect(char *host)
29 for (count = 0; count < HNDMAX; count++)
30 if (PGh[count] == NULL)
35 strncpy(msqlErrMsg, msqlErrors[E_NOHANDLERS], BUFSIZ);
39 PGh[count] = malloc(sizeof(PGconn));
40 PGh[count]->pghost = host ? strdup(host) : NULL;
45 msqlSelectDB(int handle, char *dbname)
47 char *options = calloc(1, BUFSIZ);
48 char *e = getenv("PG_OPTIONS");
53 if (PGh[handle]->pghost)
55 strcat(options, "host=");
56 strncat(options, PGh[handle]->pghost, BUFSIZ);
57 strncat(options, " ", BUFSIZ);
58 free(PGh[handle]->pghost);
59 PGh[handle]->pghost = NULL;
61 strncat(options, "dbname=", BUFSIZ);
62 strncat(options, dbname, BUFSIZ);
63 strncat(options, " ", BUFSIZ);
64 strncat(options, e, BUFSIZ);
66 PGh[handle] = PQconnectdb(options);
68 strncpy(msqlErrMsg, PQerrorMessage(PGh[handle]), BUFSIZ);
69 return (PQstatus(PGh[handle]) == CONNECTION_BAD ? -1 : 0);
73 msqlQuery(int handle, char *query)
75 char *tq = strdup(query);
78 PGconn *conn = PGh[handle];
81 res = PQexec(conn, p);
83 rcode = PQresultStatus(res);
85 if (rcode == PGRES_TUPLES_OK)
88 return PQntuples(res);
90 else if (rcode == PGRES_FATAL_ERROR || rcode == PGRES_NONFATAL_ERROR)
105 msqlCreateDB(int a, char *b)
109 snprintf(tbuf, BUFSIZ, "create database %s", b);
110 return msqlQuery(a, tbuf) >= 0 ? 0 : -1;
114 msqlDropDB(int a, char *b)
118 snprintf(tbuf, BUFSIZ, "drop database %s", b);
119 return msqlQuery(a, tbuf) >= 0 ? 0 : -1;
128 msqlGetProtoInfo(void)
133 msqlReloadAcls(int a)
138 msqlGetServerInfo(void)
143 msqlGetHostInfo(void)
148 msqlUnixTimeToDate(time_t date)
153 msqlUnixTimeToTime(time_t time)
170 msqlDataSeek(m_result * result, int count)
174 result->cursor = result->queryData;
175 for (c = 1; c < count; c++)
176 if (result->cursor->next)
177 result->cursor = result->cursor->next;
181 msqlFieldSeek(m_result * result, int count)
185 result->fieldCursor = result->fieldData;
186 for (c = 1; c < count; c++)
187 if (result->fieldCursor->next)
188 result->fieldCursor = result->fieldCursor->next;
192 msqlFreeResult(m_result * result)
197 free(result->fieldData);
198 result->cursor = result->queryData;
199 while (result->cursor)
202 m_row m = result->cursor->data;
204 for (c = 0; m[c]; c++)
207 result->cursor = result->cursor->next;
209 free(result->queryData);
215 msqlFetchRow(m_result * row)
217 m_data *r = row->cursor;
221 row->cursor = row->cursor->next;
222 return (m_row) r->data;
228 msqlGetSequenceInfo(int a, char *b)
233 msqlFetchField(m_result * mr)
235 m_field *m = (m_field *) mr->fieldCursor;
239 mr->fieldCursor = mr->fieldCursor->next;
250 if (msqlQuery(a, "select datname from pg_database") > 0)
252 m = msqlStoreResult();
260 msqlListTables(int a)
265 snprintf(tbuf, BUFSIZ,
266 "select relname from pg_class where relkind='r' and relowner=%d",
268 if (msqlQuery(a, tbuf) > 0)
270 m = msqlStoreResult();
278 msqlListFields(int a, char *b)
284 msqlListIndex(int a, char *b, char *c)
289 snprintf(tbuf, BUFSIZ,
290 "select relname from pg_class where relkind='i' and relowner=%d",
292 if (msqlQuery(a, tbuf) > 0)
294 m = msqlStoreResult();
302 msqlStoreResult(void)
306 m_result *mr = malloc(sizeof(m_result));
311 mr->queryData = mr->cursor = NULL;
312 mr->numRows = PQntuples(queryres);
313 mr->numFields = PQnfields(queryres);
315 mf = calloc(PQnfields(queryres), sizeof(m_fdata));
316 for (count = 0; count < PQnfields(queryres); count++)
318 (m_fdata *) (mf + count)->field.name = strdup(PQfname(queryres, count));
319 (m_fdata *) (mf + count)->field.table = tfrom;
320 (m_fdata *) (mf + count)->field.type = CHAR_TYPE;
321 (m_fdata *) (mf + count)->field.length = PQfsize(queryres, count);
322 (m_fdata *) (mf + count)->next = (m_fdata *) (mf + count + 1);
324 (m_fdata *) (mf + count - 1)->next = NULL;
326 md = calloc(PQntuples(queryres), sizeof(m_data));
327 for (count = 0; count < PQntuples(queryres); count++)
329 m_row rows = calloc(PQnfields(queryres) * sizeof(m_row) + 1, 1);
332 for (c = 0; c < PQnfields(queryres); c++)
333 rows[c] = strdup(PQgetvalue(queryres, count, c));
334 (m_data *) (md + count)->data = rows;
336 (m_data *) (md + count)->width = PQnfields(queryres);
337 (m_data *) (md + count)->next = (m_data *) (md + count + 1);
339 (m_data *) (md + count - 1)->next = NULL;
341 mr->queryData = mr->cursor = md;
342 mr->fieldCursor = mr->fieldData = mf;
351 msqlDateToUnixTime(char *a)
356 msqlTimeToUnixTime(char *b)
363 return tmpnam("/tmp/msql.XXXXXX");
367 msqlLoadConfigFile(char *a)