aboutsummaryrefslogtreecommitdiff
path: root/psql/psql.c
blob: d02b5a3236cd3cc19bee2fcb8116bd5d76119ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* [psql] - A PostgreSQL client for PD
 *
 * Copyright (C) 2006 Jamie Bullock and others
 *
 * Large portions of the code are based on [sqlsingle] by Iain Mott
 *
 * Copyright (C) 2001 Iain Mott
 * 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License, which should be included with this 
 * program, for more details. 
 * 
 */

/*  up to 10 fields may be returned. returns floats or symbols */

/* code for psql pd class */

#include "m_pd.h"
#include <string.h>

/* PSQL */

#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h" 

#define MAXSQLFIELDS 20

/*  postgres datatypes and corresponding 'Oid's */

#define PGINT4 23
#define PGFLOAT8 701
#define PGDOUBLE 1700
#define PGDATE 1082  
#define PGDATETIME 1184
#define PGVARCHAR 1043

typedef struct psql{ 

    t_object t_ob;
    t_outlet *x_outlet1,
             *x_outlet2;
    t_atom get_atom;
    t_symbol *x_sym;
    char sqlStringStore[MAXPDSTRING];
    char *pghost,
         *pgport,
         *pgoptions,
         *pgtty,
         *dbName;
    char port[20];
    PGconn *conn;
    t_int connected,
          in_query;

} t_psql;

static void psql_SQL (t_psql *x, t_symbol *s){ 

    char sqlString[MAXPDSTRING];  
    int argc = 10;
    t_atom argv[argc];
    t_atom *test;
    int descriptor_fnum;
    int starttime_fnum;
    int endtime_fnum;
    int spurtorder_fnum;
    int       nFields;
    int       i, j;
    t_symbol *t_sym;
    PGresult   *res;
    int tupplecount;
    test = argv;

    /*    make a connection to the database */
    if(!x->connected){
        post("Reconnecting to database, %s", x->dbName);
        x->conn = PQsetdb(x->pghost, x->pgport, 
                x->pgoptions, x->pgtty, x->dbName); 
    }


    if(PQstatus(x->conn) == CONNECTION_BAD){
        fprintf(stderr, "Connection to database '%s' failed.\n", x->dbName);
        fprintf(stderr, "%s", PQerrorMessage(x->conn));
    }
    else{
        x->connected = 1;

        res = PQexec(x->conn, x->sqlStringStore);
        if (PQresultStatus(res) != PGRES_TUPLES_OK && PQresultStatus(res) != 
                PGRES_COMMAND_OK)
        {
            fprintf(stderr, "psql: Action failed. PQresultStatus is %s\n", 
                    PQresStatus(PQresultStatus(res)));
        }

        else {
            tupplecount = PQntuples(res);
            if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
            {
                PQclear(res);
            }
            else {

                nFields = PQnfields(res);


                descriptor_fnum = PQfnumber(res, "descriptor");
                starttime_fnum = PQfnumber(res, "starttime");
                endtime_fnum = PQfnumber(res, "endtime");
                spurtorder_fnum = PQfnumber(res, "spurtorder");

                /* fetch the instances */
                for (i = 0; i < PQntuples(res); i++) {
                    /* merge field of a query instance into a list */
                    SETFLOAT(&argv[0], i);
                    for (j=0; j<nFields; j++)
                    {
                        int fType =  PQftype(res, j);

                        if (fType == PGINT4)
                            SETFLOAT(&argv[j+1], (float) atoi(PQgetvalue(res, i, j)));
                        else if (fType == PGFLOAT8 || fType == PGDOUBLE)
                            SETFLOAT(&argv[j+1], (float) atof(PQgetvalue(res, i, j)));
                        else if (fType == PGVARCHAR || fType == PGDATE || fType == PGDATETIME)
                        {
                            t_sym = gensym( PQgetvalue(res, i, j));
                            SETSYMBOL(&argv[j+1], t_sym);
                        }
                        else {
                            t_sym = gensym( PQgetvalue(res, i, j));
                            SETSYMBOL(&argv[j+1], t_sym);
                            post(
                    "Undefined PG data type. OID: %d. Stored in list as Symbol", fType);
                        }

                    }
                    t_sym = gensym( "A_FLOAT");
                    outlet_list(x->x_outlet1, t_sym, nFields+1, argv);
                }
                outlet_bang(x->x_outlet2);
                PQclear(res);
            }
        }
    }
}

static void psql_close(t_psql *x){
    post("Closing connection to database %s", x->dbName);
    PQfinish(x->conn);
    x->connected = 0;
}

static void psql_anything(t_psql *x, t_symbol *s, int ac, t_atom *av, t_floatarg f){ 
    char sqlString[MAXPDSTRING]; 
    int i;
    char buf[MAXPDSTRING];
    char mybuf[MAXPDSTRING];

    if(!strcmp(s->s_name,"close") && !x->in_query)
        psql_close(x);
    else{
        if (strcmp(s->s_name, "sql")){


            strcat(x->sqlStringStore,  ", ");

            /* replace the truncated first symbol */

            strcat(x->sqlStringStore,  s->s_name); 
            strcat(x->sqlStringStore, " ");

            /* see if it ends OK  */
            atom_string(av+ac-1, buf, MAXPDSTRING);

            if (!strcmp(buf, "sqlend")){

                int tc = ac-1;


                for (i = 0; i < tc; i++){

                    atom_string(av+i, buf, MAXPDSTRING);
                    strcat(x->sqlStringStore, buf);
                    if (i < tc - 1)
                        strcat(x->sqlStringStore, " ");

                }

                psql_SQL (x, s);

                x->in_query = 0;

            }
            else {
                for (i = 0; i < ac; i++)
                {
                    atom_string(av+i, buf, MAXPDSTRING);
                    strcat(x->sqlStringStore, buf);
                    if (i < ac - 1)
                        strcat(x->sqlStringStore, " ");
                }
            }

        }
        else {
            /*  if s->s_name DOES equal "sql" -  first clear sqlStringStore then check 
             *  if end of string terminates with "sqlend" */

            x->in_query = 1;

            strcpy(x->sqlStringStore, "");
            atom_string(av+ac-1, buf, MAXPDSTRING);
            if (strcmp(buf, "sqlend"))
            { 
                for (i = 0; i < ac; i++)
                {
                    atom_string(av+i, buf, MAXPDSTRING);
                    strcat(x->sqlStringStore, buf);
                    if (i < ac - 1)
                        strcat(x->sqlStringStore, " ");
                }
            }

            else 
            {
                ac -= 1;
                for (i = 0; i < ac; i++)
                {
                    atom_string(av+i, buf, MAXPDSTRING);
                    strcat(x->sqlStringStore, buf);
                    if (i < ac - 1)
                        strcat(x->sqlStringStore, " ");
                }
                psql_SQL (x, s);

                x->in_query = 0;
            }
        }

        atom_string(av+ac-1, buf, MAXPDSTRING);
    }
}

/*  this one is needed if the new line begins with a number */

static void psql_list(t_psql *x, t_symbol *s, int ac, t_atom *av)
{
    int i;
    char buf[MAXPDSTRING];

    strcat(x->sqlStringStore, ",");

    if (strcmp(x->sqlStringStore, "")){ 

        atom_string(av+ac-1, buf, MAXPDSTRING);
        if (strcmp(buf, "sqlend") == 0){ 

            ac = ac -1;

            for (i = 0; i < ac; i++){
                strcat(x->sqlStringStore, " ");
                atom_string(av+i, buf, MAXPDSTRING);
                strcat(x->sqlStringStore, buf);

            }

            x->in_query = 0;

        }
        else{

            for (i = 0; i < ac; i++){
                strcat(x->sqlStringStore, " ");
                atom_string(av+i, buf, MAXPDSTRING);
                strcat(x->sqlStringStore, buf);

            }
        }
    }
    else
        post("psql: Not SQL");

}

t_class *psql_class;

static void *psql_new(t_symbol *s, int argc, t_atom *argv)
{
    t_psql *x = (t_psql *)pd_new(psql_class);
    x->x_sym = gensym("psql");
    x->x_outlet1 = outlet_new(&x->t_ob, &s_list);
    x->x_outlet2 = outlet_new(&x->t_ob, &s_bang);

    x->in_query = 0;

    if(argc == 0)
    {
        x->pghost = NULL;      /* host name of the backend server */
        x->pgport = NULL;          /* port of the backend server */
        x->pgoptions = NULL;      /* special options to start up the backend server */
        x->pgtty = NULL;          /* debugging tty for the backend server */
        x->dbName = "template1";
        post("using dbase template1 on local UNIX socket");
    }
    else if(argc == 1 && argv[0].a_type == A_SYMBOL)
    {
        x->pghost = NULL;      /* host name of the backend server */
        x->pgport = NULL;          /* port of the backend server */
        x->pgoptions = NULL;      /* special options to start up the backend server */
        x->pgtty = NULL;          /* debugging tty for the backend server */
        x->dbName = argv[0].a_w.w_symbol->s_name;
    }
    else if(argc == 3 && argv[0].a_type == A_SYMBOL && argv[1].a_type == A_SYMBOL
            && argv[2].a_type == A_FLOAT)
    {
        x->pghost = argv[1].a_w.w_symbol->s_name;   /* host name of the backend server */
        sprintf(x->port, "%d", (int)argv[2].a_w.w_float);
        x->pgport =  x->port;          /* port of the backend server */
        //    strncpy(x->pgport, tmp);          /* port of the backend server */
        x->pgoptions = NULL;      /* special options to start up the backend server */
        x->pgtty = NULL;          /* debugging tty for the backend server */
        x->dbName = argv[0].a_w.w_symbol->s_name;
    }

    else
    {
        x->pghost = NULL;      /* host name of the backend server */
        x->pgport = NULL;          /* port of the backend server */
        x->pgoptions = NULL;      /* special options to start up the backend server */
        x->pgtty = NULL;          /* debugging tty for the backend server */
        x->dbName = "template1";
        post("psql: invalid arguments using default template1 dbase on localhost");
    }

    /* check postmaster is running on specified port and machine by attempting to 
     * x->connect to template1 */
    x->conn = PQsetdb(x->pghost, x->pgport, x->pgoptions, x->pgtty, "template1");
    if (PQstatus(x->conn) == CONNECTION_BAD)
    {
        fprintf(stderr, 
                "psql: Connection to template1 failed. Perhaps the postmaster is not running on the specified port and machine \n");
        fprintf(stderr, "psql: Connect error is: %s", PQerrorMessage(x->conn));
    }
    else
        psql_close(x);
    return (x);
}

static void psql_free(t_psql *x){
    if(x->connected)
        psql_close(x);
}

void psql_setup(void) {
    psql_class = class_new(gensym("psql"), (t_newmethod)psql_new, (t_method)psql_free,
            sizeof(t_psql),  0, A_GIMME, 0);
    class_addanything(psql_class, psql_anything);
    class_addlist(psql_class, psql_list);
}