21 static int cmp(
const void *pa,
const void *pb)
57 static int copy_table(
const char *from_drvname,
const char *from_dbname,
58 const char *from_tblname,
const char *to_drvname,
59 const char *to_dbname,
const char *to_tblname,
60 const char *where,
const char *select,
const char *selcol,
61 int *ivals,
int nvals)
63 int col, ncols, sqltype, ctype, more, selcol_found;
78 G_debug(3,
"db_copy_table():\n from driver = %s, db = %s, table = %s\n" 79 " to driver = %s, db = %s, table = %s, where = %s, select = %s",
80 from_drvname, from_dbname, from_tblname, to_drvname, to_dbname,
81 to_tblname, where, select);
90 if (!ivals || (ivals && nvals == 0)) {
91 G_warning(
_(
"Array of values to select from column <%s> is empty"), selcol);
96 ivalues = (
int *)
G_malloc(nvals *
sizeof(
int));
97 memcpy(ivalues, ivals, nvals *
sizeof(
int));
98 qsort((
void *)ivalues, nvals,
sizeof(
int), cmp);
106 if (from_driver ==
NULL) {
107 G_warning(
_(
"Unable to start driver <%s>"), from_drvname);
112 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
113 from_dbname, from_drvname);
119 if (strcmp(from_drvname, to_drvname) == 0
120 && strcmp(from_dbname, to_dbname) == 0) {
121 G_debug(3,
"Use the same driver");
122 to_driver = from_driver;
126 if (to_driver ==
NULL) {
127 G_warning(
_(
"Unable to start driver <%s>"), to_drvname);
133 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
134 to_dbname, to_drvname);
136 if (from_driver != to_driver) {
151 G_warning(
_(
"Unable to get list tables in database <%s>"),
154 if (from_driver != to_driver)
160 for (i = 0; i <
count; i++) {
165 if (strcmp(to_drvname,
"pg") == 0) {
171 p = strstr(tbl,
".");
178 if (strcmp(buf, tbl) == 0)
187 if (strcmp(to_tblname, tblname_i) == 0) {
189 G_warning(
_(
"Table <%s> already exists in database and will be overwritten"),
194 G_warning(
_(
"Table <%s> already exists in database <%s>"),
195 to_tblname, to_dbname);
200 if (from_driver != to_driver)
242 G_warning(
_(
"Unable to open select cursor: '%s'"),
245 if (from_driver != to_driver) {
250 G_debug(3,
"Select cursor opened");
254 G_debug(3,
"ncols = %d", ncols);
260 for (col = 0; col < ncols; col++) {
290 if (selcol && !selcol_found)
297 if (from_driver != to_driver) {
319 G_warning(
_(
"Unable to open select cursor: '%s'"),
322 if (from_driver != to_driver) {
327 G_debug(3,
"Select cursor opened");
331 G_debug(3,
"ncols = %d", ncols);
338 G_warning(
_(
"Unable to fetch data from table <%s>"),
342 if (from_driver != to_driver) {
350 sprintf(buf,
"insert into %s values ( ", to_tblname);
353 for (col = 0; col < ncols; col++) {
363 if (!bsearch(&(value->
i), ivalues, nvals,
sizeof(
int), cmp)) {
394 G_warning(
_(
"Unknown column type (column <%s>)"),
398 if (from_driver != to_driver) {
409 G_warning(
"Unable to insert new record: '%s'",
413 if (from_driver != to_driver) {
426 if (from_driver != to_driver) {
447 const char *from_tblname,
const char *to_drvname,
448 const char *to_dbname,
const char *to_tblname)
450 return copy_table(from_drvname, from_dbname, from_tblname,
451 to_drvname, to_dbname, to_tblname,
470 const char *from_tblname,
const char *to_drvname,
471 const char *to_dbname,
const char *to_tblname,
474 return copy_table(from_drvname, from_dbname, from_tblname,
475 to_drvname, to_dbname, to_tblname,
494 const char *from_tblname,
const char *to_drvname,
495 const char *to_dbname,
const char *to_tblname,
498 return copy_table(from_drvname, from_dbname, from_tblname,
499 to_drvname, to_dbname, to_tblname,
520 const char *from_tblname,
const char *to_drvname,
521 const char *to_dbname,
const char *to_tblname,
522 const char *selcol,
int *ivals,
int nvals)
524 return copy_table(from_drvname, from_dbname, from_tblname,
525 to_drvname, to_dbname, to_tblname,
int db_begin_transaction(dbDriver *)
Begin transaction.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int db_copy_table(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname)
Copy a table.
dbValue * db_get_column_value(dbColumn *)
Returns column value for given column structure.
int db_create_table(dbDriver *, dbTable *)
Create table.
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
int db_list_tables(dbDriver *, dbString **, int *, int)
List available tables for given connection.
void db_set_column_sqltype(dbColumn *, int)
Define column sqltype for column.
int db_get_column_length(dbColumn *)
Get column's length.
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
void db_init_string(dbString *)
Initialize dbString.
int db_fetch(dbCursor *, int, int *)
Fetch data from open cursor.
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
char * db_get_string(const dbString *)
Get string.
#define DB_C_TYPE_DATETIME
void G_free(void *)
Free allocated memory.
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
int db_get_column_precision(dbColumn *)
Get column precision.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int db_set_column_name(dbColumn *, const char *)
Set column name.
int G_get_overwrite()
Get overwrite value.
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
int db_copy_table_where(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname, const char *where)
Copy a table (by where statement)
int db_test_value_isnull(dbValue *)
Check of value is null.
const char * db_sqltype_name(int)
Get SQL data type description.
int db_append_string(dbString *, const char *)
Append string to dbString.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
int db_copy_table_by_ints(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname, const char *selcol, int *ivals, int nvals)
Copy a table (by keys)
int db_convert_value_to_string(dbValue *, int, dbString *)
Convert value to string.
int db_execute_immediate(dbDriver *, dbString *)
Execute SQL statements.
void db_set_column_scale(dbColumn *, int)
Set column scale.
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
int db_copy_table_select(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname, const char *select)
Copy a table (by select statement)
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
int db_set_table_name(dbTable *, const char *)
Set the name of the table.
dbTable * db_alloc_table(int)
Allocate a table with a specific number of columns.
int db_set_column_description(dbColumn *, const char *)
Set column description.
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
int db_commit_transaction(dbDriver *)
Commit transaction.
const char * db_get_column_description(dbColumn *)
Returns column description for given column.
void G_warning(const char *,...) __attribute__((format(printf
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
void db_double_quote_string(dbString *)
Replace each ' is replaced by ''.
int db_drop_table(dbDriver *, dbString *)
Drop table.
char * G_store(const char *)
Copy string to allocated memory.
void db_set_column_precision(dbColumn *, int)
Set column precision.
dbTable * db_get_cursor_table(dbCursor *)
Get table allocated by cursor.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
int G_debug(int, const char *,...) __attribute__((format(printf
int db_get_column_scale(dbColumn *)
Get column scale.
void db_set_column_length(dbColumn *, int)
Set column's length.
int db_close_cursor(dbCursor *)
Close cursor.