~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/mSQL-interface/mpgsql.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <time.h>
 
2
#include <string.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include "msql.h"
 
6
#include "libpq-fe.h"
 
7
 
 
8
#define HNDMAX 10
 
9
 
 
10
PGconn     *PGh[HNDMAX] = {
 
11
        NULL, NULL, NULL, NULL, NULL,
 
12
        NULL, NULL, NULL, NULL, NULL
 
13
};
 
14
 
 
15
#define E_NOHANDLERS 0
 
16
 
 
17
char       *msqlErrors[] = {
 
18
        "Out of database handlers."
 
19
};
 
20
 
 
21
char            msqlErrMsg[BUFSIZ],
 
22
                   *tfrom = "dunno";
 
23
PGresult   *queryres = NULL;
 
24
 
 
25
int
 
26
msqlConnect(char *host)
 
27
{
 
28
        int                     count;
 
29
 
 
30
        for (count = 0; count < HNDMAX; count++)
 
31
                if (PGh[count] == NULL)
 
32
                        break;
 
33
 
 
34
        if (count == HNDMAX)
 
35
        {
 
36
                strncpy(msqlErrMsg, msqlErrors[E_NOHANDLERS], BUFSIZ);
 
37
                return -1;
 
38
        }
 
39
 
 
40
        PGh[count] = malloc(sizeof(PGconn));
 
41
        PGh[count]->pghost = host ? strdup(host) : NULL;
 
42
        return count;
 
43
}
 
44
 
 
45
int
 
46
msqlSelectDB(int handle, char *dbname)
 
47
{
 
48
        char       *options = calloc(1, BUFSIZ);
 
49
        char       *e = getenv("PG_OPTIONS");
 
50
 
 
51
        if (e == NULL)
 
52
                e = "";
 
53
 
 
54
        if (PGh[handle]->pghost)
 
55
        {
 
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;
 
61
        }
 
62
        strncat(options, "dbname=", BUFSIZ);
 
63
        strncat(options, dbname, BUFSIZ);
 
64
        strncat(options, " ", BUFSIZ);
 
65
        strncat(options, e, BUFSIZ);
 
66
        free(PGh[handle]);
 
67
        PGh[handle] = PQconnectdb(options);
 
68
        free(options);
 
69
        strncpy(msqlErrMsg, PQerrorMessage(PGh[handle]), BUFSIZ);
 
70
        return (PQstatus(PGh[handle]) == CONNECTION_BAD ? -1 : 0);
 
71
}
 
72
 
 
73
int
 
74
msqlQuery(int handle, char *query)
 
75
{
 
76
        char       *tq = strdup(query);
 
77
        char       *p = tq;
 
78
        PGresult   *res;
 
79
        PGconn     *conn = PGh[handle];
 
80
        ExecStatusType rcode;
 
81
 
 
82
        res = PQexec(conn, p);
 
83
 
 
84
        rcode = PQresultStatus(res);
 
85
 
 
86
        if (rcode == PGRES_TUPLES_OK)
 
87
        {
 
88
                queryres = res;
 
89
                return PQntuples(res);
 
90
        }
 
91
        else if (rcode == PGRES_FATAL_ERROR || rcode == PGRES_NONFATAL_ERROR)
 
92
        {
 
93
                PQclear(res);
 
94
                queryres = NULL;
 
95
                return -1;
 
96
        }
 
97
        else
 
98
        {
 
99
                PQclear(res);
 
100
                queryres = NULL;
 
101
                return 0;
 
102
        }
 
103
}
 
104
 
 
105
int
 
106
msqlCreateDB(int a, char *b)
 
107
{
 
108
        char            tbuf[BUFSIZ];
 
109
 
 
110
        snprintf(tbuf, BUFSIZ, "create database %s", b);
 
111
        return msqlQuery(a, tbuf) >= 0 ? 0 : -1;
 
112
}
 
113
 
 
114
int
 
115
msqlDropDB(int a, char *b)
 
116
{
 
117
        char            tbuf[BUFSIZ];
 
118
 
 
119
        snprintf(tbuf, BUFSIZ, "drop database %s", b);
 
120
        return msqlQuery(a, tbuf) >= 0 ? 0 : -1;
 
121
}
 
122
 
 
123
int
 
124
msqlShutdown(int a)
 
125
{
 
126
}
 
127
 
 
128
int
 
129
msqlGetProtoInfo(void)
 
130
{
 
131
}
 
132
 
 
133
int
 
134
msqlReloadAcls(int a)
 
135
{
 
136
}
 
137
 
 
138
char *
 
139
msqlGetServerInfo(void)
 
140
{
 
141
}
 
142
 
 
143
char *
 
144
msqlGetHostInfo(void)
 
145
{
 
146
}
 
147
 
 
148
char *
 
149
msqlUnixTimeToDate(time_t date)
 
150
{
 
151
}
 
152
 
 
153
char *
 
154
msqlUnixTimeToTime(time_t time)
 
155
{
 
156
}
 
157
 
 
158
void
 
159
msqlClose(int a)
 
160
{
 
161
        PQfinish(PGh[a]);
 
162
        PGh[a] = NULL;
 
163
        if (queryres)
 
164
        {
 
165
                free(queryres);
 
166
                queryres = NULL;
 
167
        }
 
168
}
 
169
 
 
170
void
 
171
msqlDataSeek(m_result * result, int count)
 
172
{
 
173
        int                     c;
 
174
 
 
175
        result->cursor = result->queryData;
 
176
        for (c = 1; c < count; c++)
 
177
                if (result->cursor->next)
 
178
                        result->cursor = result->cursor->next;
 
179
}
 
180
 
 
181
void
 
182
msqlFieldSeek(m_result * result, int count)
 
183
{
 
184
        int                     c;
 
185
 
 
186
        result->fieldCursor = result->fieldData;
 
187
        for (c = 1; c < count; c++)
 
188
                if (result->fieldCursor->next)
 
189
                        result->fieldCursor = result->fieldCursor->next;
 
190
}
 
191
 
 
192
void
 
193
msqlFreeResult(m_result * result)
 
194
{
 
195
        if (result)
 
196
        {
 
197
                /* Clears fields */
 
198
                free(result->fieldData);
 
199
                result->cursor = result->queryData;
 
200
                while (result->cursor)
 
201
                {
 
202
                        int                     c;
 
203
                        m_row           m = result->cursor->data;
 
204
 
 
205
                        for (c = 0; m[c]; c++)
 
206
                                free(m[c]);
 
207
 
 
208
                        result->cursor = result->cursor->next;
 
209
                }
 
210
                free(result->queryData);
 
211
                free(result);
 
212
        }
 
213
}
 
214
 
 
215
m_row
 
216
msqlFetchRow(m_result * row)
 
217
{
 
218
        m_data     *r = row->cursor;
 
219
 
 
220
        if (r)
 
221
        {
 
222
                row->cursor = row->cursor->next;
 
223
                return (m_row) r->data;
 
224
        }
 
225
        return (m_row) NULL;
 
226
}
 
227
 
 
228
m_seq *
 
229
msqlGetSequenceInfo(int a, char *b)
 
230
{
 
231
}
 
232
 
 
233
m_field *
 
234
msqlFetchField(m_result * mr)
 
235
{
 
236
        m_field    *m = (m_field *) mr->fieldCursor;
 
237
 
 
238
        if (m)
 
239
        {
 
240
                mr->fieldCursor = mr->fieldCursor->next;
 
241
                return m;
 
242
        }
 
243
        return NULL;
 
244
}
 
245
 
 
246
m_result *
 
247
msqlListDBs(int a)
 
248
{
 
249
        m_result   *m;
 
250
 
 
251
        if (msqlQuery(a, "select datname from pg_database") > 0)
 
252
        {
 
253
                m = msqlStoreResult();
 
254
                return m;
 
255
        }
 
256
        else
 
257
                return NULL;
 
258
}
 
259
 
 
260
m_result *
 
261
msqlListTables(int a)
 
262
{
 
263
        m_result   *m;
 
264
        char            tbuf[BUFSIZ];
 
265
 
 
266
        snprintf(tbuf, BUFSIZ,
 
267
                "select relname from pg_class where relkind='r' and relowner=%d",
 
268
                         geteuid());
 
269
        if (msqlQuery(a, tbuf) > 0)
 
270
        {
 
271
                m = msqlStoreResult();
 
272
                return m;
 
273
        }
 
274
        else
 
275
                return NULL;
 
276
}
 
277
 
 
278
m_result *
 
279
msqlListFields(int a, char *b)
 
280
{
 
281
 
 
282
}
 
283
 
 
284
m_result *
 
285
msqlListIndex(int a, char *b, char *c)
 
286
{
 
287
        m_result   *m;
 
288
        char            tbuf[BUFSIZ];
 
289
 
 
290
        snprintf(tbuf, BUFSIZ,
 
291
                "select relname from pg_class where relkind='i' and relowner=%d",
 
292
                         geteuid());
 
293
        if (msqlQuery(a, tbuf) > 0)
 
294
        {
 
295
                m = msqlStoreResult();
 
296
                return m;
 
297
        }
 
298
        else
 
299
                return NULL;
 
300
}
 
301
 
 
302
m_result *
 
303
msqlStoreResult(void)
 
304
{
 
305
        if (queryres)
 
306
        {
 
307
                m_result   *mr = malloc(sizeof(m_result));
 
308
                m_fdata    *mf;
 
309
                m_data     *md;
 
310
                int                     count;
 
311
 
 
312
                mr->queryData = mr->cursor = NULL;
 
313
                mr->numRows = PQntuples(queryres);
 
314
                mr->numFields = PQnfields(queryres);
 
315
 
 
316
                mf = calloc(PQnfields(queryres), sizeof(m_fdata));
 
317
                for (count = 0; count < PQnfields(queryres); count++)
 
318
                {
 
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);
 
324
                }
 
325
                (m_fdata *) (mf + count - 1)->next = NULL;
 
326
 
 
327
                md = calloc(PQntuples(queryres), sizeof(m_data));
 
328
                for (count = 0; count < PQntuples(queryres); count++)
 
329
                {
 
330
                        m_row           rows = calloc(PQnfields(queryres) * sizeof(m_row) + 1, 1);
 
331
                        int                     c;
 
332
 
 
333
                        for (c = 0; c < PQnfields(queryres); c++)
 
334
                                rows[c] = strdup(PQgetvalue(queryres, count, c));
 
335
                        (m_data *) (md + count)->data = rows;
 
336
 
 
337
                        (m_data *) (md + count)->width = PQnfields(queryres);
 
338
                        (m_data *) (md + count)->next = (m_data *) (md + count + 1);
 
339
                }
 
340
                (m_data *) (md + count - 1)->next = NULL;
 
341
 
 
342
                mr->queryData = mr->cursor = md;
 
343
                mr->fieldCursor = mr->fieldData = mf;
 
344
 
 
345
                return mr;
 
346
        }
 
347
        else
 
348
                return NULL;
 
349
}
 
350
 
 
351
time_t
 
352
msqlDateToUnixTime(char *a)
 
353
{
 
354
}
 
355
 
 
356
time_t
 
357
msqlTimeToUnixTime(char *b)
 
358
{
 
359
}
 
360
 
 
361
char *
 
362
msql_tmpnam(void)
 
363
{
 
364
        return tmpnam("/tmp/msql.XXXXXX");
 
365
}
 
366
 
 
367
int
 
368
msqlLoadConfigFile(char *a)
 
369
{
 
370
}