10
PGconn *PGh[HNDMAX] = {
11
NULL, NULL, NULL, NULL, NULL,
12
NULL, NULL, NULL, NULL, NULL
15
#define E_NOHANDLERS 0
17
char *msqlErrors[] = {
18
"Out of database handlers."
21
char msqlErrMsg[BUFSIZ],
23
PGresult *queryres = NULL;
26
msqlConnect(char *host)
30
for (count = 0; count < HNDMAX; count++)
31
if (PGh[count] == NULL)
36
strncpy(msqlErrMsg, msqlErrors[E_NOHANDLERS], BUFSIZ);
40
PGh[count] = malloc(sizeof(PGconn));
41
PGh[count]->pghost = host ? strdup(host) : NULL;
46
msqlSelectDB(int handle, char *dbname)
48
char *options = calloc(1, BUFSIZ);
49
char *e = getenv("PG_OPTIONS");
54
if (PGh[handle]->pghost)
56
strcat(options, "host=");
57
strncat(options, PGh[handle]->pghost, BUFSIZ);
58
strncat(options, " ", BUFSIZ);
59
free(PGh[handle]->pghost);
60
PGh[handle]->pghost = NULL;
62
strncat(options, "dbname=", BUFSIZ);
63
strncat(options, dbname, BUFSIZ);
64
strncat(options, " ", BUFSIZ);
65
strncat(options, e, BUFSIZ);
67
PGh[handle] = PQconnectdb(options);
69
strncpy(msqlErrMsg, PQerrorMessage(PGh[handle]), BUFSIZ);
70
return (PQstatus(PGh[handle]) == CONNECTION_BAD ? -1 : 0);
74
msqlQuery(int handle, char *query)
76
char *tq = strdup(query);
79
PGconn *conn = PGh[handle];
82
res = PQexec(conn, p);
84
rcode = PQresultStatus(res);
86
if (rcode == PGRES_TUPLES_OK)
89
return PQntuples(res);
91
else if (rcode == PGRES_FATAL_ERROR || rcode == PGRES_NONFATAL_ERROR)
106
msqlCreateDB(int a, char *b)
110
snprintf(tbuf, BUFSIZ, "create database %s", b);
111
return msqlQuery(a, tbuf) >= 0 ? 0 : -1;
115
msqlDropDB(int a, char *b)
119
snprintf(tbuf, BUFSIZ, "drop database %s", b);
120
return msqlQuery(a, tbuf) >= 0 ? 0 : -1;
129
msqlGetProtoInfo(void)
134
msqlReloadAcls(int a)
139
msqlGetServerInfo(void)
144
msqlGetHostInfo(void)
149
msqlUnixTimeToDate(time_t date)
154
msqlUnixTimeToTime(time_t time)
171
msqlDataSeek(m_result * result, int count)
175
result->cursor = result->queryData;
176
for (c = 1; c < count; c++)
177
if (result->cursor->next)
178
result->cursor = result->cursor->next;
182
msqlFieldSeek(m_result * result, int count)
186
result->fieldCursor = result->fieldData;
187
for (c = 1; c < count; c++)
188
if (result->fieldCursor->next)
189
result->fieldCursor = result->fieldCursor->next;
193
msqlFreeResult(m_result * result)
198
free(result->fieldData);
199
result->cursor = result->queryData;
200
while (result->cursor)
203
m_row m = result->cursor->data;
205
for (c = 0; m[c]; c++)
208
result->cursor = result->cursor->next;
210
free(result->queryData);
216
msqlFetchRow(m_result * row)
218
m_data *r = row->cursor;
222
row->cursor = row->cursor->next;
223
return (m_row) r->data;
229
msqlGetSequenceInfo(int a, char *b)
234
msqlFetchField(m_result * mr)
236
m_field *m = (m_field *) mr->fieldCursor;
240
mr->fieldCursor = mr->fieldCursor->next;
251
if (msqlQuery(a, "select datname from pg_database") > 0)
253
m = msqlStoreResult();
261
msqlListTables(int a)
266
snprintf(tbuf, BUFSIZ,
267
"select relname from pg_class where relkind='r' and relowner=%d",
269
if (msqlQuery(a, tbuf) > 0)
271
m = msqlStoreResult();
279
msqlListFields(int a, char *b)
285
msqlListIndex(int a, char *b, char *c)
290
snprintf(tbuf, BUFSIZ,
291
"select relname from pg_class where relkind='i' and relowner=%d",
293
if (msqlQuery(a, tbuf) > 0)
295
m = msqlStoreResult();
303
msqlStoreResult(void)
307
m_result *mr = malloc(sizeof(m_result));
312
mr->queryData = mr->cursor = NULL;
313
mr->numRows = PQntuples(queryres);
314
mr->numFields = PQnfields(queryres);
316
mf = calloc(PQnfields(queryres), sizeof(m_fdata));
317
for (count = 0; count < PQnfields(queryres); count++)
319
(m_fdata *) (mf + count)->field.name = strdup(PQfname(queryres, count));
320
(m_fdata *) (mf + count)->field.table = tfrom;
321
(m_fdata *) (mf + count)->field.type = CHAR_TYPE;
322
(m_fdata *) (mf + count)->field.length = PQfsize(queryres, count);
323
(m_fdata *) (mf + count)->next = (m_fdata *) (mf + count + 1);
325
(m_fdata *) (mf + count - 1)->next = NULL;
327
md = calloc(PQntuples(queryres), sizeof(m_data));
328
for (count = 0; count < PQntuples(queryres); count++)
330
m_row rows = calloc(PQnfields(queryres) * sizeof(m_row) + 1, 1);
333
for (c = 0; c < PQnfields(queryres); c++)
334
rows[c] = strdup(PQgetvalue(queryres, count, c));
335
(m_data *) (md + count)->data = rows;
337
(m_data *) (md + count)->width = PQnfields(queryres);
338
(m_data *) (md + count)->next = (m_data *) (md + count + 1);
340
(m_data *) (md + count - 1)->next = NULL;
342
mr->queryData = mr->cursor = md;
343
mr->fieldCursor = mr->fieldData = mf;
352
msqlDateToUnixTime(char *a)
357
msqlTimeToUnixTime(char *b)
364
return tmpnam("/tmp/msql.XXXXXX");
368
msqlLoadConfigFile(char *a)