17 #include <grass/dbmi.h>
18 #include <grass/glocale.h>
21 static int cmp(
const void *pa,
const void *pb)
59 const char *from_tblname,
const char *to_drvname,
60 const char *to_dbname,
const char *to_tblname,
61 const char *where,
const char *select,
const char *selcol,
62 int *ivals,
int nvals)
64 int col, ncols, sqltype, ctype, more, selcol_found;
67 dbHandle from_handle, to_handle;
68 dbString tblname, sql;
69 dbString value_string;
71 dbTable *table, *out_table;
76 dbDriver *from_driver, *to_driver;
79 G_debug(3,
"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);
92 ivalues = (
int *)G_malloc(nvals *
sizeof(
int));
93 memcpy(ivalues, ivals, nvals *
sizeof(
int));
94 qsort((
void *)ivalues, nvals,
sizeof(
int), cmp);
99 if (from_driver ==
NULL) {
100 G_warning(_(
"Unable to start driver <%s>"), from_drvname);
105 G_warning(_(
"Unable to open database <%s> by driver <%s>"),
106 from_dbname, from_drvname);
112 if (strcmp(from_drvname, to_drvname) == 0
113 && strcmp(from_dbname, to_dbname) == 0) {
114 G_debug(3,
"Use the same driver");
115 to_driver = from_driver;
119 if (to_driver ==
NULL) {
120 G_warning(_(
"Unable to start driver <%s>"), to_drvname);
126 G_warning(_(
"Unable to open database <%s> by driver <%s>"),
127 to_dbname, to_drvname);
129 if (from_driver != to_driver) {
144 G_warning(_(
"Unable to get list tables in database <%s>"),
147 if (from_driver != to_driver)
153 for (i = 0; i <
count; i++) {
156 if (strcmp(to_tblname, tblname) == 0) {
157 G_warning(_(
"Table <%s> already exists in database <%s>"),
158 to_tblname, to_dbname);
160 if (from_driver != to_driver)
199 G_warning(_(
"Unable to open select cursor: '%s'"),
202 if (from_driver != to_driver) {
207 G_debug(3,
"Select cursor opened");
211 G_debug(3,
"ncols = %d", ncols);
217 for (col = 0; col < ncols; col++) {
218 dbColumn *out_column;
230 if (ctype != DB_C_TYPE_INT)
247 if (selcol && !selcol_found)
251 G_warning(_(
"Unable to create table <%s>"),
254 if (from_driver != to_driver) {
276 G_warning(_(
"Unable to open select cursor: '%s'"),
279 if (from_driver != to_driver) {
284 G_debug(3,
"Select cursor opened");
288 G_debug(3,
"ncols = %d", ncols);
294 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
295 G_warning(_(
"Unable to fetch data from table <%s>"),
299 if (from_driver != to_driver) {
307 sprintf(buf,
"insert into %s values ( ", to_tblname);
310 for (col = 0; col < ncols; col++) {
320 if (!bsearch(&(value->i), ivalues, nvals,
sizeof(
int), cmp)) {
329 case DB_C_TYPE_STRING:
330 case DB_C_TYPE_DATETIME:
342 case DB_C_TYPE_DOUBLE:
351 G_warning(_(
"Unknown column type (column <%s>)"),
355 if (from_driver != to_driver) {
366 G_warning(
"Unable to insert new record: '%s'",
370 if (from_driver != to_driver) {
383 if (from_driver != to_driver) {
404 const char *from_tblname,
const char *to_drvname,
405 const char *to_dbname,
const char *to_tblname)
408 to_drvname, to_dbname, to_tblname,
427 const char *from_tblname,
const char *to_drvname,
428 const char *to_dbname,
const char *to_tblname,
432 to_drvname, to_dbname, to_tblname,
451 const char *from_tblname,
const char *to_drvname,
452 const char *to_dbname,
const char *to_tblname,
456 to_drvname, to_dbname, to_tblname,
477 const char *from_tblname,
const char *to_drvname,
478 const char *to_dbname,
const char *to_tblname,
479 const char *selcol,
int *ivals,
int nvals)
482 to_drvname, to_dbname, to_tblname,
dbColumn * db_get_table_column(dbTable *table, int n)
returns column structure for given table and column number
void db_set_column_sqltype(dbColumn *column, int sqltype)
define column sqltype for column (the function db_sqltype_name() returns sqltype description) ...
int db_test_value_isnull(dbValue *value)
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
const char * db_sqltype_name(int sqltype)
returns sqltype description
int db_close_cursor(dbCursor *cursor)
Close cursor.
const char * db_get_column_description(dbColumn *column)
returns column description for given column
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.
void G_free(void *buf)
Free allocated memory.
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, const char *where, const char *select, const char *selcol, int *ivals, int nvals)
Copy table, used by various db_copy_table*.
void db_set_column_precision(dbColumn *column, int precision)
const char * db_get_column_name(dbColumn *column)
returns column name for given column
dbTable * db_alloc_table(int ncols)
int db_set_table_name(dbTable *table, const char *name)
int db_close_database_shutdown_driver(dbDriver *driver)
Close driver/database connection.
int db_get_column_precision(dbColumn *column)
int db_begin_transaction(dbDriver *driver)
Begin transaction.
int db_get_column_length(dbColumn *column)
int db_convert_value_to_string(dbValue *value, int sqltype, dbString *string)
int db_execute_immediate(dbDriver *driver, dbString *SQLstatement)
Execute SQL statements.
int db_get_column_scale(dbColumn *column)
int db_set_column_name(dbColumn *column, const char *name)
int db_set_column_description(dbColumn *column, const char *description)
int db_append_string(dbString *x, const char *s)
int db_sqltype_to_Ctype(int sqltype)
int db_fetch(dbCursor *cursor, int position, int *more)
Fetch data.
dbTable * db_get_cursor_table(dbCursor *cursor)
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_get_column_sqltype(dbColumn *column)
returns column sqltype for column (the function db_sqltype_name() returns sqltype description) ...
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_get_table_number_of_columns(dbTable *table)
void db_set_column_length(dbColumn *column, int length)
dbValue * db_get_column_value(dbColumn *column)
returns column value for given column structure
void db_double_quote_string(dbString *src)
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)
int db_set_handle(dbHandle *handle, const char *dbName, const char *dbSchema)
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int db_create_table(dbDriver *driver, dbTable *table)
Create table.
void db_set_column_scale(dbColumn *column, int scale)
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
char * db_get_string(dbString *x)
int G_debug(int level, const char *msg,...)
Print debugging message.
void db_init_handle(dbHandle *handle)
int db_set_string(dbString *x, const char *s)
int db_open_database(dbDriver *driver, dbHandle *handle)
Open database connection.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int db_list_tables(dbDriver *driver, dbString **names, int *count, int system)
List available tables for given connection.
dbDriver * db_start_driver(const char *name)
Initialize a new dbDriver for db transaction.
int db_commit_transaction(dbDriver *driver)
Commit transaction.
int db_open_select_cursor(dbDriver *driver, dbString *select, dbCursor *cursor, int mode)
Open select cursor.
void db_init_string(dbString *x)