dbmi_client/table.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include <grass/gis.h>
00004 #include <grass/dbmi.h>
00005 
00012 int
00013 db_table_exists ( char *drvname, char *dbname, char *tabname)
00014 {
00015     dbDriver *driver;
00016     dbString *names;
00017     int i, count, found = 0;
00018     int full = 0;
00019     char buf[1000];
00020     char *bufp, *c;
00021 
00022     if ( strchr ( tabname, '.' ) ) 
00023         full = 1;
00024     
00025     driver = db_start_driver_open_database ( drvname, dbname );
00026     if ( driver == NULL ) {
00027         G_warning ( "Cannot open database '%s' by driver '%s'", dbname, drvname );
00028         return -1;
00029     }
00030     
00031     /* The table tabname can be either fully qualified in form table.schema,
00032      * or it can be only table name. If the name is fully qualified, compare whole name,
00033      * if it is not, compare only table names */
00034     
00035     /* user tables */
00036     if( db_list_tables (driver, &names, &count, 0) != DB_OK) return (-1);
00037 
00038     for (i = 0; i < count; i++) {
00039         strcpy ( buf, db_get_string (&names[i]) );
00040         bufp = buf;
00041         if ( !full && (c=strchr(buf,'.')) ) {
00042                 bufp = c+1;
00043         }
00044         G_debug ( 2, "table = %s -> %s", buf, bufp );
00045         if ( G_strcasecmp( tabname, bufp) == 0 ) {
00046             found = 1;
00047             break;
00048         }
00049     }
00050     db_free_string_array(names, count);
00051     
00052     if ( !found ) {    /* system tables */
00053         if( db_list_tables (driver, &names, &count, 1) != DB_OK) return (-1);
00054 
00055         for (i = 0; i < count; i++) {
00056             strcpy ( buf, db_get_string (&names[i]) );
00057             bufp = buf;
00058             if ( !full && (c=strchr(buf,'.')) ) {
00059                 bufp = c+1;
00060             }
00061             if ( G_strcasecmp( tabname, bufp) == 0 ) {
00062                 found = 1;
00063                 break;
00064             }
00065         }
00066         db_free_string_array(names, count);
00067     }
00068     db_close_database_shutdown_driver ( driver );
00069 
00070     return (found);
00071 }

Generated on Sun Apr 6 17:31:38 2008 for GRASS by  doxygen 1.5.5