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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* dbd_firebird: Firebird/Interbase database support
* Copyright (C) 2004-2005, Christian M. Stamgren <christian@stamgren.com>
* http://libdbi-drivers.sourceforge.net
*
*/
#include <stdio.h>
#include <string.h>
#include <dbi/dbi.h>
#include <dbi/dbi-dev.h>
#include <dbi/dbd.h>
#include <ibase.h>
/* #include <gds.h> */
#include "dbd_firebird.h"
#include "utility.h"
/* this is defined by the Makefile and passed via -D */
/* #define DBDIR /usr/local/var/lib/libdbi/firebird */
static const char default_dbdir[] = DBDIR;
char version[MAXLEN];
int _dbd_real_connect(dbi_conn_t *conn, char *enc)
{
char dpb_buffer[256], *dpb, *p, *fb_encoding;
char dbase[256];
short dpb_length;
char db_fullpath[PATH_MAX];
isc_db_handle db = 0L; /* database handle */
isc_tr_handle trans = 0L; /* transaction handle */
ibase_conn_t *iconn = (ibase_conn_t * ) malloc(sizeof(ibase_conn_t));
ISC_STATUS status_vector[ISC_STATUS_LENGTH];
const char *dbname = dbi_conn_get_option(conn, "dbname");
const char *host = dbi_conn_get_option(conn, "host");
const char *username = dbi_conn_get_option(conn, "username");
const char *password = dbi_conn_get_option(conn, "password");
const char *encoding = dbi_conn_get_option(conn, "encoding");
if(encoding == NULL || *encoding == '\0')
encoding = "NONE";
dpb = dpb_buffer;
*dpb++ = isc_dpb_version1;
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = 90;
*dpb++ = isc_dpb_user_name;
*dpb++ = strlen(username);
for (p = (char*)username; *p; *dpb++ = *p++);
*dpb++ = isc_dpb_password;
*dpb++ = strlen(password);
for (p = (char*)password; *p; *dpb++ = *p++);
*dpb++ = isc_dpb_lc_ctype;
fb_encoding = (char*)dbd_encoding_from_iana(encoding);
*dpb++ = strlen(fb_encoding);
for (p = fb_encoding; *p; *dpb++ = *p++);
dpb_length = dpb - dpb_buffer;
dpb = dpb_buffer;
/* could be used here, but is tagged deprecated */
/* isc_expand_dpb(&dpb, &dpb_length, */
/* isc_dpb_user_name, username, */
/* isc_dpb_password, password, */
/* isc_dpb_lc_ctype, dbd_encoding_from_iana(encoding), */
/* NULL); */
if (!dbname) {
_dbd_internal_error_handler(conn, "no database specified", DBI_ERROR_DBD);
return -1;
}
_firebird_populate_db_string( conn, dbname, db_fullpath );
if (host == NULL || !*host) {
snprintf(dbase, 256, "%s", db_fullpath);
}
else {
snprintf(dbase, 256, "%s:%s", host, db_fullpath);
}
/* printf("dbase went to: %s<<; host: %s, username: %s, passwd: %s, encoding: %s; dbp:%s<<\n", dbase, host, username, password, encoding, dpb); */
/* fflush(stdout); */
isc_attach_database(status_vector, strlen(dbase), dbase, &db, dpb_length, dpb);
if (status_vector[0] == 1 && status_vector[1]) {
char msg[512];
long* pvector = status_vector;
dealocate_iconn( iconn );
isc_interprete(msg, &pvector);
_dbd_internal_error_handler(conn, msg, DBI_ERROR_DBD);
return -1;
}
else {
isc_start_transaction(status_vector, &trans, 1, &db, 0, NULL);
}
iconn->trans = trans;
iconn->db = db;
iconn->charset = strdup(encoding);
conn->connection = (void *)iconn;
if (dbase) conn->current_db = strdup(dbase);
return 0;
}
char *_firebird_populate_db_string( dbi_conn_t *conn, const char *dbname, char *db_fullpath )
{
/* ToDo: make OS-independent */
const char dirsep[] = "/";
const char *dbdir = dbi_conn_get_option(conn, "firebird_dbdir");
if (!dbdir || !*dbdir) {
/* use default directory instead */
dbdir = default_dbdir;
}
bzero(db_fullpath, PATH_MAX);
if (dbdir && *dbdir) {
strcpy(db_fullpath, dbdir);
}
if (db_fullpath[strlen(db_fullpath)-1] != *dirsep && db_fullpath[0] != '\0')
strcat(db_fullpath, dirsep);
strcat(db_fullpath, dbname);
return db_fullpath;
}
void _get_firebird_version( void *ptr, char *vrs )
{
static int counter;
if( counter == 0 ) {
strncpy(version,vrs, MAXLEN-1);
++counter;
}
return;
}
/* CORE FIREBIRD DATA FETCHING STUFF */
void _translate_firebird_type(int fieldtype, unsigned short *type, unsigned int *attribs)
{
unsigned int _type = 0;
unsigned int _attribs = 0;
switch (fieldtype) {
case SQL_TEXT:
_type = DBI_TYPE_STRING;
_attribs |= DBI_STRING_FIXEDSIZE;
case SQL_VARYING:
_type = DBI_TYPE_STRING;
break;
case SQL_SHORT:
_type = DBI_TYPE_INTEGER;
_attribs |= DBI_INTEGER_SIZE2;
break;
case SQL_LONG:
_type = DBI_TYPE_INTEGER;
_attribs |= DBI_INTEGER_SIZE4;
break;
case SQL_INT64:
_type = DBI_TYPE_INTEGER;
_attribs |= DBI_INTEGER_SIZE8;
break;
case SQL_FLOAT:
_type = DBI_TYPE_DECIMAL;
_attribs |= DBI_DECIMAL_SIZE4;
break;
case SQL_DOUBLE:
_type = DBI_TYPE_DECIMAL;
_attribs |= DBI_DECIMAL_SIZE8;
break;
case SQL_TIMESTAMP:
_type = DBI_TYPE_DATETIME;
_attribs |= DBI_DATETIME_TIME;
_attribs |= DBI_DATETIME_DATE;
break;
case SQL_TYPE_TIME:
_type = DBI_TYPE_DATETIME;
_attribs |= DBI_DATETIME_TIME;
break;
case SQL_TYPE_DATE:
_type = DBI_TYPE_DATETIME;
_attribs |= DBI_DATETIME_DATE;
break;
case SQL_BLOB:
case SQL_ARRAY:
_type = DBI_TYPE_BINARY;
break;
}
*type = _type;
*attribs = _attribs;
}
void _get_field_info(dbi_result_t *result)
{
unsigned int idx = 0;
unsigned short fieldtype;
unsigned int fieldattribs;
ibase_stmt_t *istmt = result->result_handle;
while (idx < result->numfields) {
_translate_firebird_type((istmt->osqlda->sqlvar[idx].sqltype & ~1), &fieldtype, &fieldattribs);
_dbd_result_add_field(result, idx, istmt->osqlda->sqlvar[idx].sqlname, fieldtype, fieldattribs);
idx++;
}
}
int _get_row_data(dbi_result_t *result, dbi_row_t *row, unsigned long long rowidx)
{
unsigned int curfield = 0;
XSQLVAR var;
long fetch_stat = 0, blob_stat = 0;
ISC_QUAD bid;
isc_blob_handle blob_handle = NULL; /* Blob handle. */
char blob_segment[80];
unsigned short actual_seg_len;
struct tm times;
char date_s[25];
unsigned int sizeattrib;
dbi_data_t *data = NULL;
ibase_stmt_t *istmt = (ibase_stmt_t *)result->result_handle;
ibase_conn_t *iconn = (ibase_conn_t *)result->conn->connection;
fetch_stat = isc_dsql_fetch(iconn->status_vector, &(istmt->stmt), SQL_DIALECT_V6, istmt->osqlda);
if (fetch_stat != 0) {
result->numrows_matched--;
return 0;
}
while (curfield < result->numfields) {
var = istmt->osqlda->sqlvar[curfield];
data = &row->field_values[curfield];
/**
* If the column holds a NULL value mark it as NULL
*/
if ( (var.sqltype & 1) && ( *var.sqlind < 0)) {
_set_field_flag( row, curfield, DBI_VALUE_NULL, 1);
curfield++;
continue;
}
switch ( result->field_types[curfield] ) {
case DBI_TYPE_STRING:
if(result->field_attribs[curfield] & DBI_STRING_FIXEDSIZE) {
data->d_string = strdup(var.sqldata);
data->d_string[var.sqllen] = '\0'; /* hack */
row->field_sizes[curfield] = (unsigned long long) var.sqllen;
} else {
vary_t *vary = NULL;
vary = (vary_t *) var.sqldata;
data->d_string = malloc(vary->vary_length+1);
memcpy(data->d_string, vary->vary_string, vary->vary_length);
data->d_string[vary->vary_length] = '\0';
row->field_sizes[curfield] = (unsigned long long) vary->vary_length;
}
break;
case DBI_TYPE_INTEGER:
sizeattrib = _isolate_attrib(result->field_attribs[curfield], DBI_INTEGER_SIZE1, DBI_INTEGER_SIZE8);
switch (sizeattrib) {
case DBI_INTEGER_SIZE1:
case DBI_INTEGER_SIZE2:
data->d_short = *(short *) var.sqldata; break;
case DBI_INTEGER_SIZE3:
case DBI_INTEGER_SIZE4:
data->d_long = *(int *) var.sqldata; break;
case DBI_INTEGER_SIZE8:
data->d_longlong = *(ISC_INT64 *) var.sqldata; break;
default:
break;
}
break;
case DBI_TYPE_DECIMAL:
sizeattrib = _isolate_attrib(result->field_attribs[curfield], DBI_DECIMAL_SIZE4, DBI_DECIMAL_SIZE8);
switch (sizeattrib) {
case DBI_DECIMAL_SIZE4:
data->d_float = *(float *) (var.sqldata); break;
case DBI_DECIMAL_SIZE8:
data->d_double = *(double *) (var.sqldata); break;
default:
break;
}
break;
case DBI_TYPE_DATETIME:
sizeattrib = _isolate_attrib(result->field_attribs[curfield], DBI_DATETIME_DATE, DBI_DATETIME_TIME);
if (sizeattrib&DBI_DATETIME_TIME
&& sizeattrib&DBI_DATETIME_DATE) {
isc_decode_timestamp((ISC_TIMESTAMP *)var.sqldata, ×);
sprintf(date_s, "%04d-%02d-%02d %02d:%02d:%02d",
times.tm_year + 1900,
times.tm_mon+1,
times.tm_mday,
times.tm_hour,
times.tm_min,
times.tm_sec);
}
else if (sizeattrib&DBI_DATETIME_TIME) {
isc_decode_sql_time((ISC_TIME *)var.sqldata, ×);
sprintf(date_s, "%02d:%02d:%02d",
times.tm_hour,
times.tm_min,
times.tm_sec);
}
else {
isc_decode_sql_date((ISC_DATE *)var.sqldata, ×);
sprintf(date_s, "%04d-%02d-%02d",
times.tm_year + 1900,
times.tm_mon+1,
times.tm_mday);
}
data->d_datetime = _dbd_parse_datetime(date_s, sizeattrib);
break;
case DBI_TYPE_BINARY:
bid = *(ISC_QUAD *) var.sqldata;
isc_open_blob2( iconn->status_vector, &(iconn->db), &(iconn->trans), &blob_handle, &bid, 0, NULL );
blob_stat = isc_get_segment( iconn->status_vector, &blob_handle, &actual_seg_len,
sizeof(blob_segment), blob_segment );
data->d_string = malloc((size_t)actual_seg_len);
memcpy(data->d_string, blob_segment, actual_seg_len);
row->field_sizes[curfield] = actual_seg_len;
while (blob_stat == 0 || iconn->status_vector[1] == isc_segment) {
blob_stat = isc_get_segment(iconn->status_vector, &blob_handle,
&actual_seg_len,
sizeof(blob_segment), blob_segment);
if (!actual_seg_len) {
break;
}
data->d_string = realloc(data->d_string,
row->field_sizes[curfield] +
actual_seg_len);
memcpy(data->d_string+row->field_sizes[curfield],
blob_segment, actual_seg_len);
row->field_sizes[curfield] += actual_seg_len;
}
isc_close_blob(iconn->status_vector, &blob_handle);
/* terminate encoded blob string */
data->d_string = realloc(data->d_string, (row->field_sizes[curfield])+1);
data->d_string[row->field_sizes[curfield]] = '\0';
row->field_sizes[curfield] = _dbd_decode_binary(data->d_string, data->d_string);
break;
default:
break;
}
curfield++;
}
if( fetch_stat != 100L ) {
result->rows = realloc(result->rows, (sizeof(dbi_row_t *) * (result->numrows_matched+1)));
result->numrows_matched++;
}
return result->numrows_matched;
}
unsigned long long return_generator_value(dbi_conn_t *conn, const char *sequence, int type)
{
unsigned long long retval = 0;
char *sql_cmd = NULL;
dbi_result_t *result;
ibase_stmt_t *istmt = NULL;
ibase_conn_t *iconn = conn->connection;
asprintf(&sql_cmd, "SELECT GEN_ID( %s ,%d ) FROM RDB$DATABASE",sequence, type );
result = dbd_query(conn, sql_cmd);
istmt = result->result_handle;
if(! isc_dsql_fetch(iconn->status_vector, &(istmt->stmt), SQL_DIALECT_V6, istmt->osqlda) ) {
retval = *(long *) istmt->osqlda->sqlvar[0].sqldata;
}
dbi_result_free(result);
free(sql_cmd);
return retval;
}
void dealocate_iconn( ibase_conn_t *iconn )
{
if( iconn != NULL) {
if(iconn->charset) free ( (void *)iconn->charset );
free(iconn);
iconn = NULL;
}
return;
}
|