GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbmi_client/table.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <grass/gis.h>
18 #include <grass/dbmi.h>
19 #include <grass/glocale.h>
20 
32 int db_table_exists(const char *drvname, const char *dbname, const char *tabname)
33 {
34  dbDriver *driver;
35  dbString *names;
36  int i, count, found = 0;
37  int full = 0;
38  char buf[1000];
39  char *bufp, *c;
40 
41  if (strchr(tabname, '.'))
42  full = 1;
43 
44  driver = db_start_driver_open_database(drvname, dbname);
45  if (driver == NULL) {
46  G_warning(_("Unable open database <%s> by driver <%s>"), dbname,
47  drvname);
48  return -1;
49  }
50 
51  /* The table tabname can be either fully qualified in form table.schema,
52  * or it can be only table name. If the name is fully qualified, compare whole name,
53  * if it is not, compare only table names */
54 
55  /* user tables */
56  if (db_list_tables(driver, &names, &count, 0) != DB_OK)
57  return (-1);
58 
59  for (i = 0; i < count; i++) {
60  strcpy(buf, db_get_string(&names[i]));
61  bufp = buf;
62  if (!full && (c = strchr(buf, '.'))) {
63  bufp = c + 1;
64  }
65  G_debug(2, "table = %s -> %s", buf, bufp);
66  if (G_strcasecmp(tabname, bufp) == 0) {
67  found = 1;
68  break;
69  }
70  }
71  db_free_string_array(names, count);
72 
73  if (!found) { /* system tables */
74  if (db_list_tables(driver, &names, &count, 1) != DB_OK)
75  return (-1);
76 
77  for (i = 0; i < count; i++) {
78  strcpy(buf, db_get_string(&names[i]));
79  bufp = buf;
80  if (!full && (c = strchr(buf, '.'))) {
81  bufp = c + 1;
82  }
83  if (G_strcasecmp(tabname, bufp) == 0) {
84  found = 1;
85  break;
86  }
87  }
88  db_free_string_array(names, count);
89  }
91 
92  return (found);
93 }
94 
104 int db_get_table_number_of_rows(dbDriver * driver, dbString * sql)
105 {
106  int nrows;
107  dbCursor cursor;
108 
109  if (db_open_select_cursor(driver, sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
110  G_warning(_("Unable to open select cursor: '%s'"), db_get_string(sql));
112  return -1;
113  }
114 
115  nrows = db_get_num_rows(&cursor);
116  db_close_cursor(&cursor);
117 
118  return nrows;
119 }
dbDriver * db_start_driver_open_database(const char *drvname, const char *dbname)
Open driver/database connection.
Definition: db.c:28
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
int db_close_cursor(dbCursor *cursor)
Close cursor.
Definition: c_close_cur.c:27
struct driver * driver
Definition: driver/init.c:26
int db_close_database_shutdown_driver(dbDriver *driver)
Close driver/database connection.
Definition: db.c:62
int count
int db_get_table_number_of_rows(dbDriver *driver, dbString *sql)
Get number of rows of table.
int db_get_num_rows(dbCursor *cursor)
Get number of selected rows.
Definition: c_rows.c:26
void db_free_string_array(dbString *a, int n)
Definition: string.c:155
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
Definition: driver.h:25
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int db_table_exists(const char *drvname, const char *dbname, const char *tabname)
Check if table exists.
char * db_get_string(dbString *x)
Definition: string.c:131
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int db_list_tables(dbDriver *driver, dbString **names, int *count, int system)
List available tables for given connection.
Definition: c_list_tabs.c:39
int db_open_select_cursor(dbDriver *driver, dbString *select, dbCursor *cursor, int mode)
Open select cursor.
Definition: c_openselect.c:29