int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode) { cursor *c; dbTable *table; char *str; /* allocate cursor */ c = alloc_cursor(); if (c == NULL) return DB_FAILED; db_set_cursor_mode(dbc, mode); db_set_cursor_type_readonly(dbc); /* \ must be escaped, see explanation in * db_driver_execute_immediate() */ str = G_str_replace(db_get_string(sel), "\\", "\\\\"); G_debug(3, "Escaped SQL: %s", str); if (mysql_query(connection, str) != 0) { db_d_append_error("%s\n%s\n%s", _("Unable to select data:"), db_get_string(sel), mysql_error(connection)); if (str) G_free(str); db_d_report_error(); return DB_FAILED; } if (str) G_free(str); c->res = mysql_store_result(connection); if (c->res == NULL) { db_d_append_error("%s\n%s", db_get_string(sel), mysql_error(connection)); db_d_report_error(); return DB_FAILED; } if (describe_table(c->res, &table, c) == DB_FAILED) { db_d_append_error(_("Unable to describe table.")); db_d_report_error(); mysql_free_result(c->res); return DB_FAILED; } c->nrows = (int)mysql_num_rows(c->res); /* record table with dbCursor */ db_set_cursor_table(dbc, table); /* set dbCursor's token for my cursor */ db_set_cursor_token(dbc, c->token); return DB_OK; }
/*! \brief Open select cursor \param sel select statement (given as dbString) \param[out] dbc pointer to dbCursor \param mode open mode \return DB_OK on success \return DB_FAILED on failure */ int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode) { cursor *c; dbTable *table; init_error(); /* allocate cursor */ c = alloc_cursor(); if (c == NULL) return DB_FAILED; db_set_cursor_mode(dbc, mode); db_set_cursor_type_readonly(dbc); G_debug(3, "SQL: '%s'", db_get_string(sel)); c->hLayer = OGR_DS_ExecuteSQL(hDs, db_get_string(sel), NULL, NULL); if (c->hLayer == NULL) { append_error(_("Unable to select: \n")); append_error(db_get_string(sel)); append_error("\n"); report_error(); return DB_FAILED; } if (describe_table(c->hLayer, &table, c) == DB_FAILED) { append_error(_("Unable to describe table\n")); report_error(); OGR_DS_ReleaseResultSet(hDs, c->hLayer); return DB_FAILED; } /* record table with dbCursor */ db_set_cursor_table(dbc, table); /* set dbCursor's token for my cursor */ db_set_cursor_token(dbc, c->token); return DB_OK; }
int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode) { PGresult *res; cursor *c; dbTable *table; char *str; /* Set datetime style */ res = PQexec(pg_conn, "SET DATESTYLE TO ISO"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { db_d_append_error(_("Unable set DATESTYLE")); db_d_report_error(); PQclear(res); return DB_FAILED; } PQclear(res); /* allocate cursor */ c = alloc_cursor(); if (c == NULL) return DB_FAILED; db_set_cursor_mode(dbc, mode); db_set_cursor_type_readonly(dbc); /* \ must be escaped, see explanation in db_driver_execute_immediate() */ str = G_str_replace(db_get_string(sel), "\\", "\\\\"); G_debug(3, "Escaped SQL: %s", str); c->res = PQexec(pg_conn, str); if (!c->res || PQresultStatus(c->res) != PGRES_TUPLES_OK) { db_d_append_error("%s\n%s\n%s", _("Unable to select:"), db_get_string(sel), PQerrorMessage(pg_conn)); db_d_report_error(); PQclear(c->res); if (str) G_free(str); return DB_FAILED; } if (str) G_free(str); if (describe_table(c->res, &table, c) == DB_FAILED) { db_d_append_error(_("Unable to describe table")); db_d_report_error(); PQclear(res); return DB_FAILED; } c->nrows = PQntuples(c->res); c->row = -1; /* record table with dbCursor */ db_set_cursor_table(dbc, table); /* set dbCursor's token for my cursor */ db_set_cursor_token(dbc, c->token); return DB_OK; }