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
30 /* # line 37 "myingres.sc" */
36 #define sqlca (*(IIsqlca()))
43 * ---Implementations---
47 return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0;
49 short INGgetCols(const char *query)
51 /* # line 57 "myingres.sc" */
54 /* # line 59 "myingres.sc" */
58 sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE));
59 memset(sqlda, 0, (IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE)));
61 stmt = bstrdup(query);
62 /* # line 71 "myingres.sc" */ /* prepare */
65 IIsqPrepare(0,(char *)"s1",(char *)0,0,stmt);
67 /* # line 72 "myingres.sc" */ /* host code */
72 /* # line 77 "myingres.sc" */ /* describe */
75 IIsqDescribe(0,(char *)"s1",sqlda,0);
77 /* # line 78 "myingres.sc" */ /* host code */
88 static inline IISQLDA *INGgetDescriptor(short numCols, const char *query)
90 /* # line 93 "myingres.sc" */
93 /* # line 95 "myingres.sc" */
97 sqlda = (IISQLDA *)malloc(IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE));
98 memset(sqlda, 0, (IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE)));
99 sqlda->sqln = numCols;
100 stmt = bstrdup(query);
101 /* # line 107 "myingres.sc" */ /* prepare */
104 IIsqPrepare(0,(char *)"s2",sqlda,0,stmt);
106 /* # line 109 "myingres.sc" */ /* host code */
108 for (i = 0; i < sqlda->sqld; ++i) {
110 * Negative type indicates nullable coulumns, so an indicator
111 * is allocated, otherwise it's null
113 if (sqlda->sqlvar[i].sqltype > 0) {
114 sqlda->sqlvar[i].sqlind = NULL;
116 sqlda->sqlvar[i].sqlind = (short *)malloc(sizeof(short));
119 * Alloc space for variable like indicated in sqllen
120 * for date types sqllen is always 0 -> allocate by type
122 switch (abs(sqlda->sqlvar[i].sqltype)) {
124 sqlda->sqlvar[i].sqldata = (char *)malloc(IISQ_TSW_LEN);
127 sqlda->sqlvar[i].sqldata = (char *)malloc(IISQ_TSWO_LEN);
129 case IISQ_TSTMP_TYPE:
130 sqlda->sqlvar[i].sqldata = (char *)malloc(IISQ_TSTMP_LEN);
134 * plus one to avoid zero mem allocs
136 sqlda->sqlvar[i].sqldata = (char *)malloc(sqlda->sqlvar[i].sqllen+1);
142 static void INGfreeDescriptor(IISQLDA *sqlda)
148 for (i = 0; i < sqlda->sqld; ++i) {
149 if (sqlda->sqlvar[i].sqldata) {
150 free(sqlda->sqlvar[i].sqldata);
152 if (sqlda->sqlvar[i].sqlind) {
153 free(sqlda->sqlvar[i].sqlind);
159 static inline int INGgetTypeSize(IISQLVAR *ingvar)
163 * TODO: add date types (at least TSTMP,TSW TSWO)
165 switch (ingvar->sqltype) {
173 inglength = ingvar->sqllen;
178 static inline INGresult *INGgetINGresult(IISQLDA *sqlda)
181 INGresult *result = NULL;
185 result = (INGresult *)malloc(sizeof(INGresult));
186 memset(result, 0, sizeof(INGresult));
187 result->sqlda = sqlda;
188 result->num_fields = sqlda->sqld;
189 result->num_rows = 0;
190 result->first_row = NULL;
191 result->status = ING_EMPTY_RESULT;
192 result->act_row = NULL;
193 memset(result->numrowstring, 0, sizeof(result->numrowstring));
194 if (result->num_fields) {
195 result->fields = (INGRES_FIELD *)malloc(sizeof(INGRES_FIELD) * result->num_fields);
196 memset(result->fields, 0, sizeof(INGRES_FIELD) * result->num_fields);
197 for (i = 0; i < result->num_fields; ++i) {
198 memset(result->fields[i].name, 0, 34);
199 bstrncpy(result->fields[i].name, sqlda->sqlvar[i].sqlname.sqlnamec, sqlda->sqlvar[i].sqlname.sqlnamel);
200 result->fields[i].max_length = INGgetTypeSize(&sqlda->sqlvar[i]);
201 result->fields[i].type = abs(sqlda->sqlvar[i].sqltype);
202 result->fields[i].flags = (sqlda->sqlvar[i].sqltype < 0) ? 1 : 0;
207 static inline void INGfreeRowSpace(ING_ROW *row, IISQLDA *sqlda)
210 if (row == NULL || sqlda == NULL) {
213 for (i = 0; i < sqlda->sqld; ++i) {
214 if (row->sqlvar[i].sqldata) {
215 free(row->sqlvar[i].sqldata);
217 if (row->sqlvar[i].sqlind) {
218 free(row->sqlvar[i].sqlind);
224 static void INGfreeINGresult(INGresult *ing_res)
232 * Free all rows and fields, then res, not descriptor!
234 * Use of rows is a nasty workaround til I find the reason,
235 * why aggregates like max() don't work
237 rows = ing_res->num_rows;
238 ing_res->act_row = ing_res->first_row;
239 while (ing_res->act_row != NULL && rows > 0) {
240 rowtemp = ing_res->act_row->next;
241 INGfreeRowSpace(ing_res->act_row, ing_res->sqlda);
242 ing_res->act_row = rowtemp;
245 if (ing_res->fields) {
246 free(ing_res->fields);
250 static inline ING_ROW *INGgetRowSpace(INGresult *ing_res)
253 unsigned short len; /* used for VARCHAR type length */
254 IISQLDA *sqlda = ing_res->sqlda;
256 IISQLVAR *vars = NULL;
257 row = (ING_ROW *)malloc(sizeof(ING_ROW));
258 memset(row, 0, sizeof(ING_ROW));
259 vars = (IISQLVAR *)malloc(sizeof(IISQLVAR) * sqlda->sqld);
260 memset(vars, 0, sizeof(IISQLVAR) * sqlda->sqld);
263 for (i = 0; i < sqlda->sqld; ++i) {
265 * Make strings out of the data, then the space and assign
266 * (why string? at least it seems that way, looking into the sources)
268 vars[i].sqlind = (short *)malloc(sizeof(short));
269 if (sqlda->sqlvar[i].sqlind) {
270 memcpy(vars[i].sqlind,sqlda->sqlvar[i].sqlind,sizeof(short));
272 *vars[i].sqlind = NULL;
275 * if sqlind pointer exists AND points to -1 -> column is 'null'
277 if ( *vars[i].sqlind && (*vars[i].sqlind == -1)) {
278 vars[i].sqldata = NULL;
280 switch (ing_res->fields[i].type) {
282 len = ((ING_VARCHAR *)sqlda->sqlvar[i].sqldata)->len;
283 vars[i].sqldata = (char *)malloc(len+1);
284 memcpy(vars[i].sqldata,sqlda->sqlvar[i].sqldata+2,len);
285 vars[i].sqldata[len] = '\0';
288 vars[i].sqldata = (char *)malloc(ing_res->fields[i].max_length+1);
289 memcpy(vars[i].sqldata,sqlda->sqlvar[i].sqldata,sqlda->sqlvar[i].sqllen);
290 vars[i].sqldata[ing_res->fields[i].max_length] = '\0';
293 vars[i].sqldata = (char *)malloc(20);
294 memset(vars[i].sqldata, 0, 20);
295 switch (sqlda->sqlvar[i].sqllen) {
297 bsnprintf(vars[i].sqldata, 20, "%d",*(short*)sqlda->sqlvar[i].sqldata);
300 bsnprintf(vars[i].sqldata, 20, "%ld",*(int*)sqlda->sqlvar[i].sqldata);
303 bsnprintf(vars[i].sqldata, 20, "%lld",*(long*)sqlda->sqlvar[i].sqldata);
307 case IISQ_TSTMP_TYPE:
308 vars[i].sqldata = (char *)malloc(IISQ_TSTMP_LEN+1);
309 vars[i].sqldata[IISQ_TSTMP_LEN] = '\0';
312 vars[i].sqldata = (char *)malloc(IISQ_TSWO_LEN+1);
313 vars[i].sqldata[IISQ_TSWO_LEN] = '\0';
316 vars[i].sqldata = (char *)malloc(IISQ_TSW_LEN+1);
317 vars[i].sqldata[IISQ_TSW_LEN] = '\0';
324 static inline int INGfetchAll(const char *query, INGresult *ing_res)
330 desc = ing_res->sqlda;
331 /* # line 363 "myingres.sc" */ /* host code */
332 if ((check = INGcheck()) < 0) {
335 /* # line 367 "myingres.sc" */ /* open */
338 IIcsOpen((char *)"c2",19318,7414);
339 IIwritio(0,(short *)0,1,32,0,(char *)"s2");
340 IIcsQuery((char *)"c2",19318,7414);
342 /* # line 368 "myingres.sc" */ /* host code */
343 if ((check = INGcheck()) < 0) {
346 /* for (linecount = 0; sqlca.sqlcode == 0; ++linecount) */
348 /* # line 374 "myingres.sc" */ /* fetch */
351 if (IIcsRetScroll((char *)"c2",19318,7414,-1,-1) != 0) {
356 /* # line 376 "myingres.sc" */ /* host code */
357 if ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ) {
358 row = INGgetRowSpace(ing_res); /* alloc space for fetched row */
360 * Initialize list when encountered first time
362 if (ing_res->first_row == 0) {
363 ing_res->first_row = row; /* head of the list */
364 ing_res->first_row->next = NULL;
365 ing_res->act_row = ing_res->first_row;
367 ing_res->act_row->next = row; /* append row to old act_row */
368 ing_res->act_row = row; /* set row as act_row */
369 row->row_number = linecount;
372 } while ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) );
373 /* # line 394 "myingres.sc" */ /* close */
376 IIcsClose((char *)"c2",19318,7414);
378 /* # line 396 "myingres.sc" */ /* host code */
379 ing_res->status = ING_COMMAND_OK;
380 ing_res->num_rows = linecount;
383 static inline ING_STATUS INGresultStatus(INGresult *res)
386 return ING_NO_RESULT;
391 static void INGrowSeek(INGresult *res, int row_number)
393 ING_ROW *trow = NULL;
394 if (res->act_row->row_number == row_number) {
398 * TODO: real error handling
400 if (row_number<0 || row_number>res->num_rows) {
403 for (trow = res->first_row; trow->row_number != row_number; trow = trow->next) ;
406 * Note - can be null - if row_number not found, right?
409 char *INGgetvalue(INGresult *res, int row_number, int column_number)
411 if (row_number != res->act_row->row_number) {
412 INGrowSeek(res, row_number);
414 return res->act_row->sqlvar[column_number].sqldata;
416 bool INGgetisnull(INGresult *res, int row_number, int column_number)
418 if (row_number != res->act_row->row_number) {
419 INGrowSeek(res, row_number);
421 return (*res->act_row->sqlvar[column_number].sqlind == -1) ? true : false;
423 int INGntuples(const INGresult *res)
425 return res->num_rows;
427 int INGnfields(const INGresult *res)
429 return res->num_fields;
431 char *INGfname(const INGresult *res, int column_number)
433 if ((column_number > res->num_fields) || (column_number < 0)) {
436 return res->fields[column_number].name;
439 short INGftype(const INGresult *res, int column_number)
441 return res->fields[column_number].type;
443 int INGexec(INGconn *conn, const char *query)
446 /* # line 477 "myingres.sc" */
450 /* # line 480 "myingres.sc" */
452 stmt = bstrdup(query);
454 /* # line 485 "myingres.sc" */ /* execute */
458 IIsyncup((char *)0,0);
460 /* # line 487 "myingres.sc" */ /* host code */
462 if ((check = INGcheck()) < 0) {
465 /* # line 493 "myingres.sc" */ /* inquire_ingres */
467 IILQisInqSqlio((short *)0,1,30,sizeof(rowcount),&rowcount,8);
469 /* # line 494 "myingres.sc" */ /* host code */
470 if ((check = INGcheck()) < 0) {
475 INGresult *INGquery(INGconn *conn, const char *query)
478 * TODO: error handling
480 IISQLDA *desc = NULL;
481 INGresult *res = NULL;
483 int cols = INGgetCols(query);
484 desc = INGgetDescriptor(cols, query);
488 res = INGgetINGresult(desc);
492 rows = INGfetchAll(query, res);
494 INGfreeDescriptor(desc);
495 INGfreeINGresult(res);
500 void INGclear(INGresult *res)
505 INGfreeDescriptor(res->sqlda);
506 INGfreeINGresult(res);
508 INGconn *INGconnectDB(char *dbname, char *user, char *passwd)
511 if (dbname == NULL || strlen(dbname) == 0) {
514 dbconn = (INGconn *)malloc(sizeof(INGconn));
515 memset(dbconn, 0, sizeof(INGconn));
516 /* # line 553 "myingres.sc" */
523 /* # line 559 "myingres.sc" */
525 bstrncpy(ingdbname, dbname, sizeof(ingdbname));
527 bstrncpy(ingdbuser, user, sizeof(ingdbuser));
528 if (passwd != NULL) {
529 bstrncpy(ingdbpasw, passwd, sizeof(ingdbpasw));
531 memset(ingdbpasw, 0, sizeof(ingdbpasw));
533 /* # line 570 "myingres.sc" */ /* connect */
537 IIsqConnect(0,ingdbname,(char *)"-dbms_password",ingdbpasw,(char *)0,
538 (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0,
539 (char *)0, (char *)0, (char *)0, (char *)0);
541 /* # line 574 "myingres.sc" */ /* host code */
543 /* # line 575 "myingres.sc" */ /* connect */
546 IIsqConnect(0,ingdbname,(char *)0, (char *)0, (char *)0, (char *)0,
547 (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0,
548 (char *)0, (char *)0, (char *)0);
550 /* # line 576 "myingres.sc" */ /* host code */
552 /* # line 578 "myingres.sc" */ /* inquire_sql */
554 IILQisInqSqlio((short *)0,1,32,31,conn_name,13);
556 /* # line 579 "myingres.sc" */ /* inquire_sql */
558 IILQisInqSqlio((short *)0,1,30,sizeof(sess_id),&sess_id,11);
560 /* # line 581 "myingres.sc" */ /* host code */
561 bstrncpy(dbconn->dbname, ingdbname, sizeof(dbconn->dbname));
562 bstrncpy(dbconn->user, ingdbuser, sizeof(dbconn->user));
563 bstrncpy(dbconn->password, ingdbpasw, sizeof(dbconn->password));
564 bstrncpy(dbconn->connection_name, conn_name, sizeof(dbconn->connection_name));
565 dbconn->session_id = sess_id;
566 dbconn->msg = (char*)malloc(257);
567 memset(dbconn->msg, 0, 257);
570 void INGdisconnectDB(INGconn *dbconn)
572 /* # line 594 "myingres.sc" */
575 /* # line 596 "myingres.sc" */
577 sess_id = dbconn->session_id;
578 /* # line 600 "myingres.sc" */ /* disconnect */
581 IILQsidSessID(sess_id);
584 /* # line 602 "myingres.sc" */ /* host code */
585 if (dbconn != NULL) {
590 char *INGerrorMessage(const INGconn *conn)
592 /* # line 610 "myingres.sc" */
595 /* # line 612 "myingres.sc" */
597 /* # line 614 "myingres.sc" */ /* inquire_ingres */
599 IILQisInqSqlio((short *)0,1,32,255,errbuf,63);
601 /* # line 615 "myingres.sc" */ /* host code */
602 memcpy(conn->msg, &errbuf, 256);
605 char *INGcmdTuples(INGresult *res)
607 return res->numrowstring;
610 int INGputCopyEnd(INGconn *conn, const char *errormsg);
611 int INGputCopyData(INGconn *conn, const char *buffer, int nbytes);
613 /* # line 629 "myingres.sc" */