75 static int oracle_select(
void *theconn,
const Octstr *sql,
List *binds,
List **res);
87 static void oracle_checkerr(OCIError *errhp, sword
status)
95 case OCI_SUCCESS_WITH_INFO:
96 error(0,
"Error - OCI_SUCCESS_WITH_INFO");
99 error(0,
"Error - OCI_NEED_DATA");
102 error(0,
"Error - OCI_NODATA");
105 if (errhp == NULL)
break;
106 (void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (
text *) NULL, &errcode,
107 errbuf, (ub4)
sizeof(errbuf), OCI_HTYPE_ERROR);
108 error(0,
"Error - %.*s", 512, errbuf);
110 case OCI_INVALID_HANDLE:
111 error(0,
"Error - OCI_INVALID_HANDLE");
113 case OCI_STILL_EXECUTING:
114 error(0,
"Error - OCI_STILL_EXECUTE");
117 error(0,
"Error - OCI_CONTINUE");
128 static void *oracle_malloc(
void *ctx,
size_t size)
130 void *ret = gw_malloc(
size);
131 debug(
"dbpool.oracle",0,
"oracle_malloc called size=%ld @%08lx", (
long)
size,
140 static void oracle_free(
void *ctx,
void *ptr)
142 debug(
"dbpool.oracle",0,
"oracle_free called @%08lx", (
long) ptr);
150 static void *oracle_realloc(
void *ctx,
void *ptr,
size_t size)
152 void *ret = gw_realloc(ptr,
size);
153 debug(
"dbpool.oracle",0,
"oracle_realloc called size=%ld", (
long)
size);
157 static void* oracle_open_conn(
const DBConf *db_conf)
162 struct ora_conn *conn = gw_malloc(
sizeof(
struct ora_conn));
165 memset(conn, 0,
sizeof(
struct ora_conn));
167 debug(
"dbpool.oracle",0,
"oracle_open_conn called");
170 errorcode = OCIEnvCreate(&conn->envp,
171 OCI_THREADED|OCI_ENV_NO_MUTEX,
177 if (errorcode != OCI_SUCCESS) {
178 oracle_checkerr(NULL, errorcode);
179 error(0,
"Got error while OCIEnvCreate %d", errorcode);
184 debug(
"dbpool.oracle",0,
"oci environment created");
187 errorcode = OCIHandleAlloc(conn->envp, (dvoid**) &conn->errhp,
188 OCI_HTYPE_ERROR, 0, 0);
189 if (errorcode != OCI_SUCCESS) {
190 oracle_checkerr(NULL, errorcode);
191 OCIHandleFree(conn->envp, OCI_HTYPE_ENV);
196 debug(
"dbpool.oracle",0,
"oci error handle allocated");
199 errorcode = OCILogon(conn->envp, conn->errhp, &conn->svchp,
204 if (errorcode != OCI_SUCCESS) {
205 oracle_checkerr(conn->errhp, errorcode);
206 OCIHandleFree(conn->errhp, OCI_HTYPE_ERROR);
207 OCIHandleFree(conn->envp, OCI_HTYPE_ENV);
212 debug(
"dbpool.oracle",0,
"connected to database");
214 errorcode = OCIServerVersion(conn->svchp, conn->errhp, version,
215 sizeof(version), OCI_HTYPE_SVCCTX);
216 if (errorcode != OCI_SUCCESS) {
217 oracle_checkerr(conn->errhp, errorcode);
219 info(0,
"Connected to: %s", version);
226 static void oracle_close_conn(
void *theconn)
228 struct ora_conn *conn = (
struct ora_conn*) theconn;
232 if (conn->svchp != NULL)
233 oracle_checkerr(conn->errhp, OCILogoff(conn->svchp, conn->errhp));
235 OCIHandleFree(conn->errhp, OCI_HTYPE_ERROR);
236 OCIHandleFree(conn->envp, OCI_HTYPE_ENV);
243 static int oracle_check_conn(
void *theconn)
248 struct ora_conn *conn = (
struct ora_conn*) theconn;
253 result = OCIPing(conn->svchp, conn->errhp, OCI_DEFAULT);
254 if (result != OCI_SUCCESS) {
255 oracle_checkerr(conn->errhp, result);
266 ret = oracle_select(conn, sql, NULL, &res);
281 static void oracle_conf_destroy(
DBConf *theconf)
294 static int oracle_select(
void *theconn,
const Octstr *sql,
List *binds,
List **res)
309 struct ora_conn *conn = (
struct ora_conn*) theconn;
310 int binds_len = (binds ?
gwlist_len(binds) : 0);
315 status = OCIHandleAlloc(conn->envp, (dvoid**)&stmt, OCI_HTYPE_STMT, 0,0);
316 if (OCI_SUCCESS !=
status) {
317 oracle_checkerr(conn->errhp,
status);
322 octstr_len(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
323 if (OCI_SUCCESS !=
status) {
324 oracle_checkerr(conn->errhp,
status);
325 OCIHandleFree(stmt, OCI_HTYPE_STMT);
330 for (i = 0; i < binds_len; i++) {
331 OCIBind *bndhp = NULL;
333 status = OCIBindByPos(stmt, &bndhp,
335 (sword)
octstr_len(bind)+1, SQLT_STR, (dvoid *) 0, (ub2 *)0,
336 (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT);
337 if (OCI_SUCCESS !=
status) {
338 oracle_checkerr(conn->errhp,
status);
339 OCIHandleFree(stmt, OCI_HTYPE_STMT);
344 status = OCIStmtExecute(conn->svchp, stmt, conn->errhp, 0, 0, NULL, NULL,
347 oracle_checkerr(conn->errhp,
status);
348 OCIHandleFree(stmt, OCI_HTYPE_STMT);
352 status = OCIAttrGet(stmt, OCI_HTYPE_STMT, &columns, 0, OCI_ATTR_PARAM_COUNT,
354 if (
status != OCI_SUCCESS) {
355 oracle_checkerr(conn->errhp,
status);
356 OCIHandleFree(stmt, OCI_HTYPE_STMT);
360 debug(
"dbpool.oracle",0,
"SQL has %d columns", columns);
363 debug(
"dbpool.oracle",0,
"alloc size=%ld",
sizeof(
text*)*columns);
364 data = gw_malloc(
sizeof(
struct data_s)*columns);
366 debug(
"dbpool.oracle",0,
"retrieve data_size");
368 for (i=0 ; i < columns; i++) {
371 status = OCIParamGet(stmt, OCI_HTYPE_STMT, conn->errhp,
372 (dvoid**) &dparam, i+1);
373 if (
status != OCI_SUCCESS) {
374 oracle_checkerr(conn->errhp,
status);
376 for (i = 0; i < columns; i++)
377 gw_free(data[i].data);
379 OCIHandleFree(stmt, OCI_HTYPE_STMT);
383 status = OCIAttrGet(dparam, OCI_DTYPE_PARAM, (dvoid*) &data[i].
size,
384 0, OCI_ATTR_DATA_SIZE, conn->errhp);
385 if (
status != OCI_SUCCESS) {
386 oracle_checkerr(conn->errhp,
status);
388 for (i = 0; i < columns; i++)
389 gw_free(data[i].data);
391 OCIHandleFree(stmt, OCI_HTYPE_STMT);
395 status = OCIAttrGet(dparam, OCI_DTYPE_PARAM, (dvoid*) &data[i].
type,
396 0, OCI_ATTR_DATA_TYPE, conn->errhp);
397 if (
status != OCI_SUCCESS) {
398 oracle_checkerr(conn->errhp,
status);
400 for (i = 0; i < columns; i++)
401 gw_free(data[i].data);
403 OCIHandleFree(stmt, OCI_HTYPE_STMT);
408 if (data[i].
type != SQLT_DAT) {
410 data[i].type = SQLT_STR;
413 debug(
"dbpool.oracle",0,
"alloc size=%d", data[i].
size);
414 data[i].data = gw_malloc(data[i].
size);
417 status = OCIDefineByPos(stmt, &defh, conn->errhp, i+1, data[i].data,
418 data[i].size, data[i].type, &data[i].ind,
420 if (
status != OCI_SUCCESS) {
421 oracle_checkerr(conn->errhp,
status);
423 for (i = 0; i <= columns; i++)
424 gw_free(data[i].data);
426 OCIHandleFree(stmt, OCI_HTYPE_STMT);
433 while ((
status = OCIStmtFetch(stmt, conn->errhp, 1,
434 OCI_FETCH_NEXT, OCI_DEFAULT)) == OCI_SUCCESS ||
435 status == OCI_SUCCESS_WITH_INFO) {
438 for (i = 0; i < columns; i++) {
439 if (data[i].data == NULL || data[i].ind == -1) {
451 if (
status != OCI_NO_DATA) {
453 oracle_checkerr(conn->errhp,
status);
454 for (i = 0; i < columns; i++)
455 gw_free(data[i].data);
461 OCIHandleFree(stmt, OCI_HTYPE_STMT);
465 for (i = 0; i < columns; i++)
466 gw_free(data[i].data);
469 OCIHandleFree(stmt, OCI_HTYPE_STMT);
475 static int oracle_update(
void *theconn,
const Octstr *sql,
List *binds)
480 struct ora_conn *conn = (
struct ora_conn*) theconn;
481 int binds_len = (binds ?
gwlist_len(binds) : 0);
484 status = OCIHandleAlloc(conn->envp, (dvoid**)&stmt, OCI_HTYPE_STMT, 0,0);
485 if (OCI_SUCCESS !=
status) {
486 oracle_checkerr(conn->errhp,
status);
489 debug(
"dbpool.oracle",0,
"OCIStmt allocated");
492 octstr_len(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
493 if (OCI_SUCCESS !=
status) {
494 oracle_checkerr(conn->errhp,
status);
495 OCIHandleFree(stmt, OCI_HTYPE_STMT);
498 debug(
"dbpool.oracle",0,
"OCIStmtPrepare done");
501 for (i = 0; i < binds_len; i++) {
503 OCIBind *bndhp = NULL;
504 status = OCIBindByPos(stmt, &bndhp,
506 (sword)
octstr_len(bind)+1, SQLT_STR, (dvoid *) 0, (ub2 *)0,
507 (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT);
508 if (OCI_SUCCESS !=
status) {
509 oracle_checkerr(conn->errhp,
status);
510 OCIHandleFree(stmt, OCI_HTYPE_STMT);
516 status = OCIStmtExecute(conn->svchp, stmt, conn->errhp, 1, 0, NULL, NULL,
517 OCI_COMMIT_ON_SUCCESS);
519 oracle_checkerr(conn->errhp,
status);
520 OCIHandleFree(stmt, OCI_HTYPE_STMT);
523 debug(
"dbpool.oracle",0,
"OCIStmtExecute done");
525 status = OCIAttrGet(stmt, OCI_HTYPE_STMT, &rows, 0, OCI_ATTR_ROW_COUNT,
527 if (
status != OCI_SUCCESS) {
528 oracle_checkerr(conn->errhp,
status);
531 debug(
"dbpool.oracle",0,
"rows processed = %d", rows);
533 OCIHandleFree(stmt, OCI_HTYPE_STMT);
538 static struct db_ops oracle_ops = {
539 .
open = oracle_open_conn,
540 .close = oracle_close_conn,
541 .check = oracle_check_conn,
542 .conf_destroy = oracle_conf_destroy,
543 .select = oracle_select,
544 .update = oracle_update
void error(int err, const char *fmt,...)
void info(int err, const char *fmt,...)
gw_assert(wtls_machine->packet_to_send !=NULL)
void gwlist_append(List *list, void *item)
long gwlist_len(List *list)
void * gwlist_get(List *list, long pos)
#define octstr_get_cstr(ostr)
void * gwlist_extract_first(List *list)
void octstr_destroy(Octstr *ostr)
#define octstr_create(cstr)
void octstr_destroy_item(void *os)
void gwlist_insert(List *list, long pos, void *item)
long octstr_len(const Octstr *ostr)
void debug(const char *place, int err, const char *fmt,...)
#define octstr_create_from_data(data, len)
void *(* open)(const DBConf *conf)
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)