int fetch( void ) { long fetch_stat; long status[ 20 ]; fetch_stat = isc_dsql_fetch( status, &stmt, dialect, sqlda ); if( fetch_stat != 100L ) ERREXIT( status, 1 ); return fetch_stat; }
int fb_fetch(rlm_sql_firebird_conn_t *conn) { long fetch_stat; if (conn->statement_type!= isc_info_sql_stmt_select) { return 100; } fetch_stat = isc_dsql_fetch(conn->status, &conn->stmt, SQL_DIALECT_V6, conn->sqlda_out); if (fetch_stat) { if (fetch_stat!= 100L) { fb_error(conn); } else { conn->sql_code = 0; } } return fetch_stat; }
// Called by IB_ResultSet::next() IB_BOOLEAN IB_Statement::dsqlFetch () { ISC_STATUS returnCode; returnCode = isc_dsql_fetch (status_->vector(), &stmtHandle_, // CJL-IB6 add support for SQLDialect, obsolete sqldaVersion__ connection_->attachmentSQLDialect_, // sqldaVersion__, // CJL-IB6 end sqldaOut_); if (returnCode == 100L) return 0; else if (returnCode == 0) return 1; throw new IB_SQLException (IB_SQLException::engine__default_0__, status_); return 0; // just to please compiler }
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; }
/* ** Returns a row of data from the query ** Lua Returns: ** list of results or table of results depending on call ** nil and error message otherwise. */ static int cur_fetch (lua_State *L) { ISC_STATUS fetch_stat; int i; cur_data *cur = getcursor(L,1); const char *opts = luaL_optstring (L, 3, "n"); int num = strchr(opts, 'n') != NULL; int alpha = strchr(opts, 'a') != NULL; if ((fetch_stat = isc_dsql_fetch(cur->env->status_vector, &cur->stmt, 1, cur->out_sqlda)) == 0) { if (lua_istable (L, 2)) { /* remove the option string */ lua_settop(L, 2); /* loop through the columns */ for (i = 0; i < cur->out_sqlda->sqld; i++) { push_column(L, i, cur); if( num ) { lua_pushnumber(L, i+1); lua_pushvalue(L, -2); lua_settable(L, 2); } if( alpha ) { lua_pushlstring(L, cur->out_sqlda->sqlvar[i].aliasname, cur->out_sqlda->sqlvar[i].aliasname_length); lua_pushvalue(L, -2); lua_settable(L, 2); } lua_pop(L, 1); } /* returning given table */ return 1; } else { for (i = 0; i < cur->out_sqlda->sqld; i++) push_column(L, i, cur); /* returning a list of values */ return cur->out_sqlda->sqld; } } /* isc_dsql_fetch returns 100 if no more rows remain to be retrieved so this can be ignored */ if (fetch_stat != 100L) return return_db_error(L, cur->env->status_vector); /* last row has been fetched, close cursor */ isc_dsql_free_statement(cur->env->status_vector, &cur->stmt, DSQL_drop); if ( CHECK_DB_ERROR(cur->env->status_vector) ) return return_db_error(L, cur->env->status_vector); /* free the cursor data */ free_cur(cur); cur->closed = 1; /* remove cursor from lock count */ --cur->conn->lock; /* return sucsess */ return 0; }
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); 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(sizeof(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); 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); 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; }
static int perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr, uschar ** errmsg, BOOL * defer_break) { isc_stmt_handle stmth = NULL; XSQLDA *out_sqlda; XSQLVAR *var; char buffer[256]; ISC_STATUS status[20], *statusp = status; int i; int ssize = 0; int offset = 0; int yield = DEFER; uschar *result = NULL; ibase_connection *cn; uschar *server_copy = NULL; uschar *sdata[3]; /* Disaggregate the parameters from the server argument. The order is host, database, user, password. We can write to the string, since it is in a nextinlist temporary buffer. The copy of the string that is used for caching has the password removed. This copy is also used for debugging output. */ for (i = 2; i > 0; i--) { uschar *pp = Ustrrchr(server, '|'); if (pp == NULL) { *errmsg = string_sprintf("incomplete Interbase server data: %s", (i == 3) ? server : server_copy); *defer_break = TRUE; return DEFER; } *pp++ = 0; sdata[i] = pp; if (i == 2) server_copy = string_copy(server); /* sans password */ } sdata[0] = server; /* What's left at the start */ /* See if we have a cached connection to the server */ for (cn = ibase_connections; cn != NULL; cn = cn->next) { if (Ustrcmp(cn->server, server_copy) == 0) { break; } } /* Use a previously cached connection ? */ if (cn != NULL) { static char db_info_options[] = { isc_info_base_level }; /* test if the connection is alive */ if (isc_database_info (status, &cn->dbh, sizeof(db_info_options), db_info_options, sizeof(buffer), buffer)) { /* error occurred: assume connection is down */ DEBUG(D_lookup) debug_printf ("Interbase cleaning up cached connection: %s\n", cn->server); isc_detach_database(status, &cn->dbh); } else { DEBUG(D_lookup) debug_printf("Interbase using cached connection for %s\n", server_copy); } } else { cn = store_get(sizeof(ibase_connection)); cn->server = server_copy; cn->dbh = NULL; cn->transh = NULL; cn->next = ibase_connections; ibase_connections = cn; } /* If no cached connection, we must set one up. */ if (cn->dbh == NULL || cn->transh == NULL) { char *dpb, *p; short dpb_length; static char trans_options[] = { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed, isc_tpb_rec_version }; /* Construct the database parameter buffer. */ dpb = buffer; *dpb++ = isc_dpb_version1; *dpb++ = isc_dpb_user_name; *dpb++ = strlen(sdata[1]); for (p = sdata[1]; *p;) *dpb++ = *p++; *dpb++ = isc_dpb_password; *dpb++ = strlen(sdata[2]); for (p = sdata[2]; *p;) *dpb++ = *p++; dpb_length = dpb - buffer; DEBUG(D_lookup) debug_printf("new Interbase connection: database=%s user=%s\n", sdata[0], sdata[1]); /* Connect to the database */ if (isc_attach_database (status, 0, sdata[0], &cn->dbh, dpb_length, buffer)) { isc_interprete(buffer, &statusp); *errmsg = string_sprintf("Interbase attach() failed: %s", buffer); *defer_break = FALSE; goto IBASE_EXIT; } /* Now start a read-only read-committed transaction */ if (isc_start_transaction (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options), trans_options)) { isc_interprete(buffer, &statusp); isc_detach_database(status, &cn->dbh); *errmsg = string_sprintf("Interbase start_transaction() failed: %s", buffer); *defer_break = FALSE; goto IBASE_EXIT; } } /* Run the query */ if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth)) { isc_interprete(buffer, &statusp); *errmsg = string_sprintf("Interbase alloc_statement() failed: %s", buffer); *defer_break = FALSE; goto IBASE_EXIT; } out_sqlda = store_get(XSQLDA_LENGTH(1)); out_sqlda->version = SQLDA_VERSION1; out_sqlda->sqln = 1; if (isc_dsql_prepare (status, &cn->transh, &stmth, 0, query, 1, out_sqlda)) { isc_interprete(buffer, &statusp); store_reset(out_sqlda); out_sqlda = NULL; *errmsg = string_sprintf("Interbase prepare_statement() failed: %s", buffer); *defer_break = FALSE; goto IBASE_EXIT; } /* re-allocate the output structure if there's more than one field */ if (out_sqlda->sqln < out_sqlda->sqld) { XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld)); if (isc_dsql_describe (status, &stmth, out_sqlda->version, new_sqlda)) { isc_interprete(buffer, &statusp); isc_dsql_free_statement(status, &stmth, DSQL_drop); store_reset(out_sqlda); out_sqlda = NULL; *errmsg = string_sprintf("Interbase describe_statement() failed: %s", buffer); *defer_break = FALSE; goto IBASE_EXIT; } out_sqlda = new_sqlda; } /* allocate storage for every returned field */ for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++) { switch (var->sqltype & ~1) { case SQL_VARYING: var->sqldata = (char *) store_get(sizeof(char) * var->sqllen + 2); break; case SQL_TEXT: var->sqldata = (char *) store_get(sizeof(char) * var->sqllen); break; case SQL_SHORT: var->sqldata = (char *) store_get(sizeof(short)); break; case SQL_LONG: var->sqldata = (char *) store_get(sizeof(ISC_LONG)); break; #ifdef SQL_INT64 case SQL_INT64: var->sqldata = (char *) store_get(sizeof(ISC_INT64)); break; #endif case SQL_FLOAT: var->sqldata = (char *) store_get(sizeof(float)); break; case SQL_DOUBLE: var->sqldata = (char *) store_get(sizeof(double)); break; #ifdef SQL_TIMESTAMP case SQL_DATE: var->sqldata = (char *) store_get(sizeof(ISC_QUAD)); break; #else case SQL_TIMESTAMP: var->sqldata = (char *) store_get(sizeof(ISC_TIMESTAMP)); break; case SQL_TYPE_DATE: var->sqldata = (char *) store_get(sizeof(ISC_DATE)); break; case SQL_TYPE_TIME: var->sqldata = (char *) store_get(sizeof(ISC_TIME)); break; #endif } if (var->sqltype & 1) { var->sqlind = (short *) store_get(sizeof(short)); } } /* finally, we're ready to execute the statement */ if (isc_dsql_execute (status, &cn->transh, &stmth, out_sqlda->version, NULL)) { isc_interprete(buffer, &statusp); *errmsg = string_sprintf("Interbase describe_statement() failed: %s", buffer); isc_dsql_free_statement(status, &stmth, DSQL_drop); *defer_break = FALSE; goto IBASE_EXIT; } while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) != 100L) { /* check if an error occurred */ if (status[0] & status[1]) { isc_interprete(buffer, &statusp); *errmsg = string_sprintf("Interbase fetch() failed: %s", buffer); isc_dsql_free_statement(status, &stmth, DSQL_drop); *defer_break = FALSE; goto IBASE_EXIT; } if (result != NULL) result = string_catn(result, &ssize, &offset, US "\n", 1); /* Find the number of fields returned. If this is one, we don't add field names to the data. Otherwise we do. */ if (out_sqlda->sqld == 1) { if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1) /* NULL value yields nothing */ result = string_catn(result, &ssize, &offset, US buffer, fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[0])); } else for (i = 0; i < out_sqlda->sqld; i++) { int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]); result = string_cat(result, &ssize, &offset, US out_sqlda->sqlvar[i].aliasname, out_sqlda->sqlvar[i].aliasname_length); result = string_catn(result, &ssize, &offset, US "=", 1); /* Quote the value if it contains spaces or is empty */ if (*out_sqlda->sqlvar[i].sqlind == -1) { /* NULL value */ result = string_catn(result, &ssize, &offset, US "\"\"", 2); } else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL) { int j; result = string_catn(result, &ssize, &offset, US "\"", 1); for (j = 0; j < len; j++) { if (buffer[j] == '\"' || buffer[j] == '\\') result = string_cat(result, &ssize, &offset, US "\\", 1); result = string_cat(result, &ssize, &offset, US buffer + j, 1); } result = string_catn(result, &ssize, &offset, US "\"", 1); } else { result = string_catn(result, &ssize, &offset, US buffer, len); } result = string_catn(result, &ssize, &offset, US " ", 1); } } /* If result is NULL then no data has been found and so we return FAIL. Otherwise, we must terminate the string which has been built; string_cat() always leaves enough room for a terminating zero. */ if (result == NULL) { yield = FAIL; *errmsg = US "Interbase: no data found"; } else { result[offset] = 0; store_reset(result + offset + 1); } /* Get here by goto from various error checks. */ IBASE_EXIT: if (stmth != NULL) isc_dsql_free_statement(status, &stmth, DSQL_drop); /* Non-NULL result indicates a successful result */ if (result != NULL) { *resultptr = result; return OK; } else { DEBUG(D_lookup) debug_printf("%s\n", *errmsg); return yield; /* FAIL or DEFER */ } }