~ubuntu-branches/ubuntu/precise/libdbi-drivers/precise

« back to all changes in this revision

Viewing changes to drivers/firebird/utility.c

  • Committer: Stefano Rivera
  • Date: 2010-08-07 14:06:29 UTC
  • mfrom: (5.1.1 libdbi-drivers)
  • Revision ID: stefanor@ubuntu.com-20100807140629-dshfwn5j6v34pqrd
Tags: 0.8.3-1-0ubuntu1
New upstream release. -- Fixes ABORT problems with sqlite3+rrdtool

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#include <dbi/dbd.h>
15
15
 
16
16
#include <ibase.h>
17
 
#include <gds.h>
 
17
/* #include <gds.h> */
18
18
 
19
19
#include "dbd_firebird.h"
20
20
#include "utility.h"
24
24
 
25
25
int _dbd_real_connect(dbi_conn_t *conn, char *enc) 
26
26
{
27
 
        char dpb_buffer[256], *dpb; 
 
27
        char dpb_buffer[256], *dpb, *p, *fb_encoding; 
28
28
        char dbase[256];
29
29
        short dpb_length; 
30
30
 
31
31
        char db_fullpath[PATH_MAX];
32
32
 
33
 
        isc_db_handle db = NULL; /* database handle */
34
 
        isc_tr_handle trans = NULL; /* transaction handle */
 
33
        isc_db_handle db = 0L; /* database handle */
 
34
        isc_tr_handle trans = 0L; /* transaction handle */
35
35
        ibase_conn_t *iconn = (ibase_conn_t * ) malloc(sizeof(ibase_conn_t));
 
36
        ISC_STATUS status_vector[ISC_STATUS_LENGTH];
36
37
 
37
38
        const char *dbname =  dbi_conn_get_option(conn, "dbname");
38
39
        const char *host =  dbi_conn_get_option(conn, "host");
48
49
        *dpb++ = isc_dpb_num_buffers;
49
50
        *dpb++ = 1;
50
51
        *dpb++ = 90;
 
52
        
 
53
        *dpb++ = isc_dpb_user_name;
 
54
        *dpb++ = strlen(username);
 
55
        for (p = (char*)username; *p; *dpb++ = *p++);
 
56
 
 
57
        *dpb++ = isc_dpb_password;
 
58
        *dpb++ = strlen(password);
 
59
        for (p = (char*)password; *p; *dpb++ = *p++);
 
60
 
 
61
        *dpb++ = isc_dpb_lc_ctype;
 
62
        fb_encoding = (char*)dbd_encoding_from_iana(encoding);
 
63
        *dpb++ = strlen(fb_encoding);
 
64
        for (p = fb_encoding; *p; *dpb++ = *p++);
 
65
 
51
66
        dpb_length = dpb - dpb_buffer;
52
67
        dpb = dpb_buffer;
53
68
 
54
 
        isc_expand_dpb(&dpb, &dpb_length,
55
 
                       isc_dpb_user_name, username,
56
 
                       isc_dpb_password, password,
57
 
                       isc_dpb_lc_ctype, dbd_encoding_from_iana(encoding),
58
 
                       NULL);
 
69
        /* could be used here, but is tagged deprecated */
 
70
/*      isc_expand_dpb(&dpb, &dpb_length, */
 
71
/*                     isc_dpb_user_name, username, */
 
72
/*                     isc_dpb_password, password, */
 
73
/*                     isc_dpb_lc_ctype, dbd_encoding_from_iana(encoding), */
 
74
/*                     NULL); */
59
75
 
 
76
        
60
77
        if (!dbname) {
61
 
                _dbd_internal_error_handler(conn, "no database specified", 0);
 
78
                _dbd_internal_error_handler(conn, "no database specified", DBI_ERROR_DBD);
62
79
                return -1;
63
80
        }
64
81
        
65
82
        _firebird_populate_db_string( conn, dbname, db_fullpath );
66
 
        snprintf(dbase, 256, "%s:%s", ( (host == NULL || *host == '\0') 
67
 
                                        ? "localhost" : host) , db_fullpath);
68
 
 
69
 
        if ( isc_attach_database(iconn->status, strlen(dbase), dbase, &db, dpb_length, dpb_buffer) ||
70
 
             isc_start_transaction(iconn->status, &trans, 1, &db, 0, NULL)) {
 
83
 
 
84
        if (host == NULL || !*host) {
 
85
                snprintf(dbase, 256, "%s", db_fullpath);
 
86
        }
 
87
        else {
 
88
                snprintf(dbase, 256, "%s:%s", host, db_fullpath);
 
89
        }
 
90
 
 
91
/*      printf("dbase went to: %s<<; host: %s, username: %s, passwd: %s, encoding: %s; dbp:%s<<\n", dbase, host, username, password, encoding, dpb); */
 
92
/*      fflush(stdout); */
 
93
        isc_attach_database(status_vector, strlen(dbase), dbase, &db, dpb_length, dpb);
 
94
        if (status_vector[0] == 1 && status_vector[1]) {
 
95
                char msg[512];
 
96
                long* pvector = status_vector;
71
97
                dealocate_iconn( iconn );
 
98
                isc_interprete(msg, &pvector);
 
99
                _dbd_internal_error_handler(conn, msg, DBI_ERROR_DBD);
72
100
                return -1;
73
101
        }
 
102
        else {
 
103
                isc_start_transaction(status_vector, &trans, 1, &db, 0, NULL);
 
104
        }
74
105
 
75
106
        iconn->trans = trans;
76
107
        iconn->db = db;
197
228
        char date_s[25];
198
229
        unsigned int sizeattrib;
199
230
        dbi_data_t *data = NULL;
200
 
        ibase_stmt_t *istmt = result->result_handle;
 
231
        ibase_stmt_t *istmt = (ibase_stmt_t *)result->result_handle;
201
232
        ibase_conn_t *iconn = (ibase_conn_t *)result->conn->connection;
202
233
 
203
 
        fetch_stat = isc_dsql_fetch(iconn->status, &(istmt->stmt), SQL_DIALECT_V6, istmt->osqlda);
 
234
        fetch_stat = isc_dsql_fetch(iconn->status_vector, &(istmt->stmt), SQL_DIALECT_V6, istmt->osqlda);
204
235
 
205
236
        if (fetch_stat != 0) {
206
237
                result->numrows_matched--;
220
251
                        continue;
221
252
                }
222
253
                
223
 
                
224
254
                switch ( result->field_types[curfield] ) {
225
255
                case DBI_TYPE_STRING: 
226
256
                        if(result->field_attribs[curfield] & DBI_STRING_FIXEDSIZE) {
229
259
                        } else {
230
260
                                vary_t *vary = NULL;
231
261
                                vary = (vary_t *) var.sqldata;
232
 
                                vary->vary_string[vary->vary_length] = '\0';
233
 
                                data->d_string = strdup(vary->vary_string);
 
262
                                data->d_string = malloc(vary->vary_length+1);
 
263
                                memcpy(data->d_string, vary->vary_string, vary->vary_length);
 
264
                                data->d_string[vary->vary_length] = '\0';
234
265
                                row->field_sizes[curfield] = (unsigned long long) vary->vary_length;
235
266
                        }
236
267
                        break;
265
296
                case DBI_TYPE_DATETIME:
266
297
                        sizeattrib = _isolate_attrib(result->field_attribs[curfield], DBI_DATETIME_DATE, DBI_DATETIME_TIME);
267
298
                        
268
 
                        switch(sizeattrib) {
269
 
                        case DBI_DATETIME_TIME: 
 
299
                        if (sizeattrib&DBI_DATETIME_TIME
 
300
                            && sizeattrib&DBI_DATETIME_DATE) {
 
301
                                isc_decode_timestamp((ISC_TIMESTAMP *)var.sqldata, &times);
 
302
                                sprintf(date_s, "%04d-%02d-%02d %02d:%02d:%02d",
 
303
                                        times.tm_year + 1900,
 
304
                                        times.tm_mon+1,
 
305
                                        times.tm_mday,
 
306
                                        times.tm_hour,
 
307
                                        times.tm_min,
 
308
                                        times.tm_sec);
 
309
                        }
 
310
                        else if (sizeattrib&DBI_DATETIME_TIME) {
270
311
                                isc_decode_sql_time((ISC_TIME *)var.sqldata, &times);
271
 
/*                              sprintf(date_s, "%02d:%02d:%02d.%04d", */
272
 
/*                                      times.tm_hour, */
273
 
/*                                      times.tm_min, */
274
 
/*                                      times.tm_sec, */
275
 
/*                                      (*((ISC_TIME *)var.sqldata)) % 10000); */
276
312
                                sprintf(date_s, "%02d:%02d:%02d",
277
313
                                        times.tm_hour,
278
314
                                        times.tm_min,
279
315
                                        times.tm_sec);
280
 
                                break;
281
 
                                
282
 
                        case DBI_DATETIME_DATE:
 
316
                        }
 
317
                        else {
283
318
                                isc_decode_sql_date((ISC_DATE *)var.sqldata, &times);
284
319
                                sprintf(date_s, "%04d-%02d-%02d",
285
320
                                        times.tm_year + 1900,
286
321
                                        times.tm_mon+1,
287
322
                                        times.tm_mday);
288
 
                                break;
289
 
                        default:
290
 
                                isc_decode_timestamp((ISC_TIMESTAMP *)var.sqldata, &times);
291
 
/*                              sprintf(date_s, "%04d-%02d-%02d %02d:%02d:%02d.%04d", */
292
 
/*                                      times.tm_year + 1900, */
293
 
/*                                      times.tm_mon+1, */
294
 
/*                                      times.tm_mday, */
295
 
/*                                      times.tm_hour, */
296
 
/*                                      times.tm_min, */
297
 
/*                                      times.tm_sec, */
298
 
/*                                      ((ISC_TIMESTAMP *)var.sqldata)->timestamp_time % 10000); */
299
 
                                sprintf(date_s, "%04d-%02d-%02d %02d:%02d:%02d",
300
 
                                        times.tm_year + 1900,
301
 
                                        times.tm_mon+1,
302
 
                                        times.tm_mday,
303
 
                                        times.tm_hour,
304
 
                                        times.tm_min,
305
 
                                        times.tm_sec);
306
 
                                break;  
307
323
                        }
308
324
                        data->d_datetime = _dbd_parse_datetime(date_s, sizeattrib);             
309
325
                        break;
311
327
                case DBI_TYPE_BINARY:
312
328
                        bid = *(ISC_QUAD *) var.sqldata;
313
329
                        
314
 
                        isc_open_blob2( iconn->status, &(iconn->db), &(iconn->trans), &blob_handle, &bid, 0, NULL );
315
 
                        blob_stat = isc_get_segment( iconn->status, &blob_handle,  &actual_seg_len,  
 
330
                        isc_open_blob2( iconn->status_vector, &(iconn->db), &(iconn->trans), &blob_handle, &bid, 0, NULL );
 
331
                        blob_stat = isc_get_segment( iconn->status_vector, &blob_handle,  &actual_seg_len,  
316
332
                                                     sizeof(blob_segment), blob_segment  );
317
333
 
318
334
                        data->d_string = malloc(sizeof(actual_seg_len));
319
335
                        memcpy(data->d_string, blob_segment, actual_seg_len);
320
336
                        row->field_sizes[curfield] = actual_seg_len;
321
337
 
322
 
                        while (blob_stat == 0 || iconn->status[1] == isc_segment) { 
323
 
                                blob_stat = isc_get_segment(iconn->status, &blob_handle, 
 
338
                        while (blob_stat == 0 || iconn->status_vector[1] == isc_segment) { 
 
339
                                blob_stat = isc_get_segment(iconn->status_vector, &blob_handle, 
324
340
                                                            &actual_seg_len, 
325
341
                                                            sizeof(blob_segment), blob_segment); 
326
342
 
331
347
                                       blob_segment, actual_seg_len);
332
348
                                row->field_sizes[curfield] += actual_seg_len;
333
349
                        } 
334
 
                        isc_close_blob(iconn->status, &blob_handle);
 
350
                        isc_close_blob(iconn->status_vector, &blob_handle);
335
351
                        row->field_sizes[curfield] = _dbd_decode_binary(data->d_string, data->d_string);
336
352
                        break;
337
353
                                
363
379
        result = dbd_query(conn, sql_cmd);
364
380
 
365
381
        istmt = result->result_handle;
366
 
        if(! isc_dsql_fetch(iconn->status, &(istmt->stmt), SQL_DIALECT_V6, istmt->osqlda) ) {
 
382
        if(! isc_dsql_fetch(iconn->status_vector, &(istmt->stmt), SQL_DIALECT_V6, istmt->osqlda) ) {
367
383
                retval = *(long *) istmt->osqlda->sqlvar[0].sqldata;
368
384
        }
369
385
        dbi_result_free(result);