2 Bacula® - The Network Backup Solution
3 Copyright (C) 2009-2010 Free Software Foundation Europe e.V.
4 The main author of Bacula is Kern Sibbald, with contributions from
5 many others, a complete list can be found in the file AUTHORS.
6 This program is Free Software; you can redistribute it and/or
7 modify it under the terms of version two of the GNU General Public
8 License as published by the Free Software Foundation and included
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 Bacula® is a registered trademark of Kern Sibbald.
19 The licensor of Bacula is the Free Software Foundation Europe
20 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
21 Switzerland, email:ftf@fsfeurope.org.
24 * Bacula Catalog Database routines specific to Ingres
25 * These are Ingres specific routines
27 * Stefan Reddig, June 2009
29 /* The following is necessary so that we do not include
30 * the dummy external definition of DB.
32 #define __SQL_C /* indicate that this is sql.c */
35 /* # line 43 "myingres.sc" */
40 extern IISQLCA sqlca; /* SQL Communications Area */
47 * ---Implementations---
51 return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0;
53 short INGgetCols(B_DB *mdb, const char *query)
55 bool stmt_free = false;
56 /* # line 64 "myingres.sc" */
59 /* # line 66 "myingres.sc" */
63 sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE));
64 memset(sqlda, 0, (IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE)));
67 * See if we need to run this through the limit_filter.
69 if (strstr(query, "LIMIT") != NULL) {
70 stmt = mdb->limit_filter->replace(query);
72 stmt = bstrdup(query);
75 /* # line 86 "myingres.sc" */ /* prepare */
78 IIsqPrepare(0,(char *)"s1",(char *)0,0,stmt);
80 /* # line 87 "myingres.sc" */ /* host code */
85 /* # line 92 "myingres.sc" */ /* describe */
88 IIsqDescribe(0,(char *)"s1",sqlda,0);
90 /* # line 93 "myingres.sc" */ /* host code */
103 static inline IISQLDA *INGgetDescriptor(B_DB *mdb, short numCols, const char *query)
105 bool stmt_free = false;
106 /* # line 111 "myingres.sc" */
109 /* # line 113 "myingres.sc" */
113 sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE));
114 memset(sqlda, 0, (IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE)));
115 sqlda->sqln = numCols;
117 * See if we need to run this through the limit_filter.
119 if (strstr(query, "LIMIT") != NULL) {
120 stmt = mdb->limit_filter->replace(query);
122 stmt = bstrdup(query);
125 /* # line 133 "myingres.sc" */ /* prepare */
128 IIsqPrepare(0,(char *)"s2",sqlda,0,stmt);
130 /* # line 135 "myingres.sc" */ /* host code */
134 for (i = 0; i < sqlda->sqld; ++i) {
136 * Negative type indicates nullable coulumns, so an indicator
137 * is allocated, otherwise it's null
139 if (sqlda->sqlvar[i].sqltype > 0) {
140 sqlda->sqlvar[i].sqlind = NULL;
142 sqlda->sqlvar[i].sqlind = (short *)malloc(sizeof(short));
145 * Alloc space for variable like indicated in sqllen
146 * for date types sqllen is always 0 -> allocate by type
148 switch (abs(sqlda->sqlvar[i].sqltype)) {
150 sqlda->sqlvar[i].sqldata = (char *)malloc(IISQ_TSW_LEN);
153 sqlda->sqlvar[i].sqldata = (char *)malloc(IISQ_TSWO_LEN);
155 case IISQ_TSTMP_TYPE:
156 sqlda->sqlvar[i].sqldata = (char *)malloc(IISQ_TSTMP_LEN);
160 * plus one to avoid zero mem allocs
162 sqlda->sqlvar[i].sqldata = (char *)malloc(sqlda->sqlvar[i].sqllen+1);
168 static void INGfreeDescriptor(IISQLDA *sqlda)
174 for (i = 0; i < sqlda->sqld; ++i) {
175 if (sqlda->sqlvar[i].sqldata) {
176 free(sqlda->sqlvar[i].sqldata);
178 if (sqlda->sqlvar[i].sqlind) {
179 free(sqlda->sqlvar[i].sqlind);
185 static inline int INGgetTypeSize(IISQLVAR *ingvar)
189 * TODO: add date types (at least TSTMP,TSW TSWO)
191 switch (ingvar->sqltype) {
199 inglength = ingvar->sqllen;
204 static inline INGresult *INGgetINGresult(IISQLDA *sqlda)
207 INGresult *result = NULL;
211 result = (INGresult *)malloc(sizeof(INGresult));
212 memset(result, 0, sizeof(INGresult));
213 result->sqlda = sqlda;
214 result->num_fields = sqlda->sqld;
215 result->num_rows = 0;
216 result->first_row = NULL;
217 result->status = ING_EMPTY_RESULT;
218 result->act_row = NULL;
219 memset(result->numrowstring, 0, sizeof(result->numrowstring));
220 if (result->num_fields) {
221 result->fields = (INGRES_FIELD *)malloc(sizeof(INGRES_FIELD) * result->num_fields);
222 memset(result->fields, 0, sizeof(INGRES_FIELD) * result->num_fields);
223 for (i = 0; i < result->num_fields; ++i) {
224 memset(result->fields[i].name, 0, 34);
225 bstrncpy(result->fields[i].name, sqlda->sqlvar[i].sqlname.sqlnamec, sqlda->sqlvar[i].sqlname.sqlnamel);
226 result->fields[i].max_length = INGgetTypeSize(&sqlda->sqlvar[i]);
227 result->fields[i].type = abs(sqlda->sqlvar[i].sqltype);
228 result->fields[i].flags = (sqlda->sqlvar[i].sqltype < 0) ? 1 : 0;
233 static inline void INGfreeRowSpace(ING_ROW *row, IISQLDA *sqlda)
236 if (row == NULL || sqlda == NULL) {
239 for (i = 0; i < sqlda->sqld; ++i) {
240 if (row->sqlvar[i].sqldata) {
241 free(row->sqlvar[i].sqldata);
243 if (row->sqlvar[i].sqlind) {
244 free(row->sqlvar[i].sqlind);
250 static void INGfreeINGresult(INGresult *ing_res)
258 * Free all rows and fields, then res, not descriptor!
260 * Use of rows is a nasty workaround til I find the reason,
261 * why aggregates like max() don't work
263 rows = ing_res->num_rows;
264 ing_res->act_row = ing_res->first_row;
265 while (ing_res->act_row != NULL && rows > 0) {
266 rowtemp = ing_res->act_row->next;
267 INGfreeRowSpace(ing_res->act_row, ing_res->sqlda);
268 ing_res->act_row = rowtemp;
271 if (ing_res->fields) {
272 free(ing_res->fields);
277 static inline ING_ROW *INGgetRowSpace(INGresult *ing_res)
280 unsigned short len; /* used for VARCHAR type length */
281 IISQLDA *sqlda = ing_res->sqlda;
283 IISQLVAR *vars = NULL;
284 row = (ING_ROW *)malloc(sizeof(ING_ROW));
285 memset(row, 0, sizeof(ING_ROW));
286 vars = (IISQLVAR *)malloc(sizeof(IISQLVAR) * sqlda->sqld);
287 memset(vars, 0, sizeof(IISQLVAR) * sqlda->sqld);
290 for (i = 0; i < sqlda->sqld; ++i) {
292 * Make strings out of the data, then the space and assign
293 * (why string? at least it seems that way, looking into the sources)
295 vars[i].sqlind = (short *)malloc(sizeof(short));
296 if (sqlda->sqlvar[i].sqlind) {
297 memcpy(vars[i].sqlind,sqlda->sqlvar[i].sqlind,sizeof(short));
299 *vars[i].sqlind = NULL;
302 * if sqlind pointer exists AND points to -1 -> column is 'null'
304 if ( *vars[i].sqlind && (*vars[i].sqlind == -1)) {
305 vars[i].sqldata = NULL;
307 switch (ing_res->fields[i].type) {
309 len = ((ING_VARCHAR *)sqlda->sqlvar[i].sqldata)->len;
310 vars[i].sqldata = (char *)malloc(len+1);
311 memcpy(vars[i].sqldata,sqlda->sqlvar[i].sqldata+2,len);
312 vars[i].sqldata[len] = '\0';
315 vars[i].sqldata = (char *)malloc(ing_res->fields[i].max_length+1);
316 memcpy(vars[i].sqldata,sqlda->sqlvar[i].sqldata,sqlda->sqlvar[i].sqllen);
317 vars[i].sqldata[ing_res->fields[i].max_length] = '\0';
320 vars[i].sqldata = (char *)malloc(20);
321 memset(vars[i].sqldata, 0, 20);
322 switch (sqlda->sqlvar[i].sqllen) {
324 bsnprintf(vars[i].sqldata, 20, "%d",*(short*)sqlda->sqlvar[i].sqldata);
327 bsnprintf(vars[i].sqldata, 20, "%ld",*(int*)sqlda->sqlvar[i].sqldata);
330 bsnprintf(vars[i].sqldata, 20, "%lld",*(long*)sqlda->sqlvar[i].sqldata);
334 case IISQ_TSTMP_TYPE:
335 vars[i].sqldata = (char *)malloc(IISQ_TSTMP_LEN+1);
336 vars[i].sqldata[IISQ_TSTMP_LEN] = '\0';
339 vars[i].sqldata = (char *)malloc(IISQ_TSWO_LEN+1);
340 vars[i].sqldata[IISQ_TSWO_LEN] = '\0';
343 vars[i].sqldata = (char *)malloc(IISQ_TSW_LEN+1);
344 vars[i].sqldata[IISQ_TSW_LEN] = '\0';
351 static inline int INGfetchAll(const char *query, INGresult *ing_res)
357 desc = ing_res->sqlda;
358 /* # line 392 "myingres.sc" */ /* host code */
359 if ((check = INGcheck()) < 0) {
362 /* # line 396 "myingres.sc" */ /* open */
365 IIcsOpen((char *)"c2",4150,23546);
366 IIwritio(0,(short *)0,1,32,0,(char *)"s2");
367 IIcsQuery((char *)"c2",4150,23546);
369 /* # line 397 "myingres.sc" */ /* host code */
370 if ((check = INGcheck()) < 0) {
373 /* for (linecount = 0; sqlca.sqlcode == 0; ++linecount) */
375 /* # line 403 "myingres.sc" */ /* fetch */
378 if (IIcsRetScroll((char *)"c2",4150,23546,-1,-1) != 0) {
383 /* # line 405 "myingres.sc" */ /* host code */
384 if ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ) {
385 row = INGgetRowSpace(ing_res); /* alloc space for fetched row */
387 * Initialize list when encountered first time
389 if (ing_res->first_row == 0) {
390 ing_res->first_row = row; /* head of the list */
391 ing_res->first_row->next = NULL;
392 ing_res->act_row = ing_res->first_row;
394 ing_res->act_row->next = row; /* append row to old act_row */
395 ing_res->act_row = row; /* set row as act_row */
396 row->row_number = linecount;
399 } while ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) );
400 /* # line 423 "myingres.sc" */ /* close */
403 IIcsClose((char *)"c2",4150,23546);
405 /* # line 425 "myingres.sc" */ /* host code */
406 ing_res->status = ING_COMMAND_OK;
407 ing_res->num_rows = linecount;
410 static inline ING_STATUS INGresultStatus(INGresult *res)
413 return ING_NO_RESULT;
418 static void INGrowSeek(INGresult *res, int row_number)
420 ING_ROW *trow = NULL;
421 if (res->act_row->row_number == row_number) {
425 * TODO: real error handling
427 if (row_number<0 || row_number>res->num_rows) {
430 for (trow = res->first_row; trow->row_number != row_number; trow = trow->next) ;
433 * Note - can be null - if row_number not found, right?
436 char *INGgetvalue(INGresult *res, int row_number, int column_number)
438 if (row_number != res->act_row->row_number) {
439 INGrowSeek(res, row_number);
441 return res->act_row->sqlvar[column_number].sqldata;
443 bool INGgetisnull(INGresult *res, int row_number, int column_number)
445 if (row_number != res->act_row->row_number) {
446 INGrowSeek(res, row_number);
448 return (*res->act_row->sqlvar[column_number].sqlind == -1) ? true : false;
450 int INGntuples(const INGresult *res)
452 return res->num_rows;
454 int INGnfields(const INGresult *res)
456 return res->num_fields;
458 char *INGfname(const INGresult *res, int column_number)
460 if ((column_number > res->num_fields) || (column_number < 0)) {
463 return res->fields[column_number].name;
466 short INGftype(const INGresult *res, int column_number)
468 return res->fields[column_number].type;
470 int INGexec(B_DB *mdb, INGconn *conn, const char *query)
472 bool stmt_free = false;
474 /* # line 507 "myingres.sc" */
478 /* # line 510 "myingres.sc" */
481 * See if we need to run this through the limit_filter.
483 if (strstr(query, "LIMIT") != NULL) {
484 stmt = mdb->limit_filter->replace(query);
486 stmt = bstrdup(query);
490 /* # line 523 "myingres.sc" */ /* execute */
494 IIsyncup((char *)0,0);
496 /* # line 525 "myingres.sc" */ /* host code */
500 if ((check = INGcheck()) < 0) {
503 /* # line 533 "myingres.sc" */ /* inquire_ingres */
505 IILQisInqSqlio((short *)0,1,30,sizeof(rowcount),&rowcount,8);
507 /* # line 534 "myingres.sc" */ /* host code */
508 if ((check = INGcheck()) < 0) {
513 INGresult *INGquery(B_DB *mdb, INGconn *conn, const char *query)
516 * TODO: error handling
518 IISQLDA *desc = NULL;
519 INGresult *res = NULL;
521 int cols = INGgetCols(mdb, query);
522 desc = INGgetDescriptor(mdb, cols, query);
526 res = INGgetINGresult(desc);
530 rows = INGfetchAll(query, res);
532 INGfreeINGresult(res);
533 INGfreeDescriptor(desc);
538 void INGclear(INGresult *res)
543 INGfreeINGresult(res);
544 INGfreeDescriptor(res->sqlda);
546 INGconn *INGconnectDB(char *dbname, char *user, char *passwd)
549 if (dbname == NULL || strlen(dbname) == 0) {
552 dbconn = (INGconn *)malloc(sizeof(INGconn));
553 memset(dbconn, 0, sizeof(INGconn));
554 /* # line 590 "myingres.sc" */
561 /* # line 596 "myingres.sc" */
563 bstrncpy(ingdbname, dbname, sizeof(ingdbname));
565 bstrncpy(ingdbuser, user, sizeof(ingdbuser));
566 if (passwd != NULL) {
567 bstrncpy(ingdbpasw, passwd, sizeof(ingdbpasw));
569 memset(ingdbpasw, 0, sizeof(ingdbpasw));
571 /* # line 607 "myingres.sc" */ /* connect */
575 IIsqConnect(0,ingdbname,(char *)"-dbms_password",ingdbpasw,(char *)0,
576 (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0,
577 (char *)0, (char *)0, (char *)0, (char *)0);
579 /* # line 611 "myingres.sc" */ /* host code */
581 /* # line 612 "myingres.sc" */ /* connect */
584 IIsqConnect(0,ingdbname,(char *)0, (char *)0, (char *)0, (char *)0,
585 (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0,
586 (char *)0, (char *)0, (char *)0);
588 /* # line 613 "myingres.sc" */ /* host code */
590 /* # line 615 "myingres.sc" */ /* inquire_sql */
592 IILQisInqSqlio((short *)0,1,32,31,conn_name,13);
594 /* # line 616 "myingres.sc" */ /* inquire_sql */
596 IILQisInqSqlio((short *)0,1,30,sizeof(sess_id),&sess_id,11);
598 /* # line 618 "myingres.sc" */ /* host code */
599 bstrncpy(dbconn->dbname, ingdbname, sizeof(dbconn->dbname));
600 bstrncpy(dbconn->user, ingdbuser, sizeof(dbconn->user));
601 bstrncpy(dbconn->password, ingdbpasw, sizeof(dbconn->password));
602 bstrncpy(dbconn->connection_name, conn_name, sizeof(dbconn->connection_name));
603 dbconn->session_id = sess_id;
604 dbconn->msg = (char*)malloc(257);
605 memset(dbconn->msg, 0, 257);
608 void INGdisconnectDB(INGconn *dbconn)
611 * TODO: check for any real use of dbconn: maybe whenn multithreaded?
613 /* # line 634 "myingres.sc" */ /* disconnect */
618 /* # line 635 "myingres.sc" */ /* host code */
619 if (dbconn != NULL) {
624 char *INGerrorMessage(const INGconn *conn)
626 /* # line 643 "myingres.sc" */
629 /* # line 645 "myingres.sc" */
631 /* # line 647 "myingres.sc" */ /* inquire_ingres */
633 IILQisInqSqlio((short *)0,1,32,255,errbuf,63);
635 /* # line 648 "myingres.sc" */ /* host code */
636 memcpy(conn->msg, &errbuf, 256);
639 char *INGcmdTuples(INGresult *res)
641 return res->numrowstring;
644 int INGputCopyEnd(INGconn *conn, const char *errormsg);
645 int INGputCopyData(INGconn *conn, const char *buffer, int nbytes);
647 /* # line 662 "myingres.sc" */