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;
79 "db_copy_table():\n from driver = %s, db = %s, table = %s\n"
80 " to driver = %s, db = %s, table = %s, where = %s, select = %s",
81 from_drvname, from_dbname, from_tblname, to_drvname, to_dbname,
82 to_tblname, where, select);
91 if (!ivals || (ivals && nvals == 0)) {
92 G_warning(
_(
"Array of values to select from column <%s> is empty"),
98 ivalues = (
int *)
G_malloc(nvals *
sizeof(
int));
99 memcpy(ivalues, ivals, nvals *
sizeof(
int));
100 qsort((
void *)ivalues, nvals,
sizeof(
int), cmp);
108 if (from_driver ==
NULL) {
109 G_warning(
_(
"Unable to start driver <%s>"), from_drvname);
114 G_warning(
_(
"Unable to open database <%s> by driver <%s>"), from_dbname,
121 if (strcmp(from_drvname, to_drvname) == 0 &&
122 strcmp(from_dbname, to_dbname) == 0) {
123 G_debug(3,
"Use the same driver");
124 to_driver = from_driver;
128 if (to_driver ==
NULL) {
129 G_warning(
_(
"Unable to start driver <%s>"), to_drvname);
135 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
136 to_dbname, to_drvname);
138 if (from_driver != to_driver) {
153 G_warning(
_(
"Unable to get list tables in database <%s>"), to_dbname);
155 if (from_driver != to_driver)
161 for (i = 0; i <
count; i++) {
166 if (strcmp(to_drvname,
"pg") == 0) {
172 p = strstr(tbl,
".");
177 sprintf(buf,
"%s.%s",
181 if (strcmp(buf, tbl) == 0)
190 if (strcmp(to_tblname, tblname_i) == 0) {
192 G_warning(
_(
"Table <%s> already exists in database and will be "
198 G_warning(
_(
"Table <%s> already exists in database <%s>"),
199 to_tblname, to_dbname);
204 if (from_driver != to_driver)
248 if (from_driver != to_driver) {
253 G_debug(3,
"Select cursor opened");
257 G_debug(3,
"ncols = %d", ncols);
263 for (col = 0; col < ncols; col++) {
292 if (selcol && !selcol_found)
296 G_warning(
_(
"Unable to create table <%s>"), to_tblname);
298 if (from_driver != to_driver) {
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>"), from_tblname);
341 if (from_driver != to_driver) {
349 sprintf(buf,
"insert into %s values ( ", to_tblname);
352 for (col = 0; col < ncols; col++) {
362 if (!bsearch(&(value->
i), ivalues, nvals,
sizeof(
int), cmp)) {
393 G_warning(
_(
"Unknown column type (column <%s>)"), colname);
396 if (from_driver != to_driver) {
410 if (from_driver != to_driver) {
423 if (from_driver != to_driver) {
444 const char *from_tblname,
const char *to_drvname,
445 const char *to_dbname,
const char *to_tblname)
447 return copy_table(from_drvname, from_dbname, from_tblname, to_drvname,
466 const char *from_tblname,
const char *to_drvname,
467 const char *to_dbname,
const char *to_tblname,
470 return copy_table(from_drvname, from_dbname, from_tblname, to_drvname,
489 const char *from_tblname,
const char *to_drvname,
490 const char *to_dbname,
const char *to_tblname,
493 return copy_table(from_drvname, from_dbname, from_tblname, to_drvname,
514 const char *from_tblname,
const char *to_drvname,
515 const char *to_dbname,
const char *to_tblname,
516 const char *selcol,
int *ivals,
int nvals)
518 return copy_table(from_drvname, from_dbname, from_tblname, to_drvname,
519 to_dbname, to_tblname,
NULL,
NULL, selcol, ivals, nvals);
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_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.
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_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)
#define DB_C_TYPE_DATETIME
const char * db_sqltype_name(int)
Get SQL data type description.
int db_test_value_isnull(dbValue *)
Check of value is null.
dbValue * db_get_column_value(dbColumn *)
Returns column value for given column structure.
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
int db_convert_value_to_string(dbValue *, int, dbString *)
Convert value to string.
int db_commit_transaction(dbDriver *)
Commit transaction.
int db_get_column_length(dbColumn *)
Get column's length.
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
void db_set_column_length(dbColumn *, int)
Set column's length.
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
void db_set_column_sqltype(dbColumn *, int)
Define column sqltype for column.
void db_set_column_precision(dbColumn *, int)
Set column precision.
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
dbTable * db_get_cursor_table(dbCursor *)
Get table allocated by cursor.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int db_begin_transaction(dbDriver *)
Begin transaction.
int db_drop_table(dbDriver *, dbString *)
Drop table.
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
int db_set_column_description(dbColumn *, const char *)
Set column description.
int db_set_table_name(dbTable *, const char *)
Set the name of the table.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
int db_execute_immediate(dbDriver *, dbString *)
Execute SQL statements.
dbTable * db_alloc_table(int)
Allocate a table with a specific number of columns.
int db_get_column_scale(dbColumn *)
Get column scale.
int db_list_tables(dbDriver *, dbString **, int *, int)
List available tables for given connection.
void db_double_quote_string(dbString *)
Replace each ' is replaced by ''.
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
void db_init_string(dbString *)
Initialize dbString.
int db_close_cursor(dbCursor *)
Close cursor.
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
int db_set_column_name(dbColumn *, const char *)
Set column name.
int db_get_column_precision(dbColumn *)
Get column precision.
void db_set_column_scale(dbColumn *, int)
Set column scale.
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
int db_append_string(dbString *, const char *)
Append string to dbString.
char * db_get_string(const dbString *)
Get string.
int db_create_table(dbDriver *, dbTable *)
Create table.
int db_fetch(dbCursor *, int, int *)
Fetch data from open cursor.
const char * db_get_column_description(dbColumn *)
Returns column description for given column.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_get_overwrite(void)
Get overwrite value.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.