GRASS Programmer's Manual  6.4.1(2011)
dbmi_client/table.c
Go to the documentation of this file.
00001 
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <grass/gis.h>
00018 #include <grass/dbmi.h>
00019 #include <grass/glocale.h>
00020 
00032 int db_table_exists(const char *drvname, const char *dbname, const char *tabname)
00033 {
00034     dbDriver *driver;
00035     dbString *names;
00036     int i, count, found = 0;
00037     int full = 0;
00038     char buf[1000];
00039     char *bufp, *c;
00040 
00041     if (strchr(tabname, '.'))
00042         full = 1;
00043 
00044     driver = db_start_driver_open_database(drvname, dbname);
00045     if (driver == NULL) {
00046         G_warning(_("Unable open database <%s> by driver <%s>"), dbname,
00047                   drvname);
00048         return -1;
00049     }
00050 
00051     /* The table tabname can be either fully qualified in form table.schema,
00052      * or it can be only table name. If the name is fully qualified, compare whole name,
00053      * if it is not, compare only table names */
00054 
00055     /* user tables */
00056     if (db_list_tables(driver, &names, &count, 0) != DB_OK)
00057         return (-1);
00058 
00059     for (i = 0; i < count; i++) {
00060         strcpy(buf, db_get_string(&names[i]));
00061         bufp = buf;
00062         if (!full && (c = strchr(buf, '.'))) {
00063             bufp = c + 1;
00064         }
00065         G_debug(2, "table = %s -> %s", buf, bufp);
00066         if (G_strcasecmp(tabname, bufp) == 0) {
00067             found = 1;
00068             break;
00069         }
00070     }
00071     db_free_string_array(names, count);
00072 
00073     if (!found) {               /* system tables */
00074         if (db_list_tables(driver, &names, &count, 1) != DB_OK)
00075             return (-1);
00076 
00077         for (i = 0; i < count; i++) {
00078             strcpy(buf, db_get_string(&names[i]));
00079             bufp = buf;
00080             if (!full && (c = strchr(buf, '.'))) {
00081                 bufp = c + 1;
00082             }
00083             if (G_strcasecmp(tabname, bufp) == 0) {
00084                 found = 1;
00085                 break;
00086             }
00087         }
00088         db_free_string_array(names, count);
00089     }
00090     db_close_database_shutdown_driver(driver);
00091 
00092     return (found);
00093 }
00094 
00104 int db_get_table_number_of_rows(dbDriver * driver, dbString * sql)
00105 {
00106     int nrows;
00107     dbCursor cursor;
00108 
00109     if (db_open_select_cursor(driver, sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
00110         G_warning(_("Unable to open select cursor: '%s'"), db_get_string(sql));
00111         db_close_database_shutdown_driver(driver);
00112         return -1;
00113     }
00114 
00115     nrows = db_get_num_rows(&cursor);
00116     db_close_cursor(&cursor);
00117 
00118     return nrows;
00119 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines