File: table.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (71 lines) | stat: -rw-r--r-- 1,723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include <string.h>
#include "gis.h"
#include "dbmi.h"

/*!
 \fn 
 \brief 
 \return: 1 exist, 0 doesn't exist, -1 error
 \param 
*/
int
db_table_exists ( char *drvname, char *dbname, char *tabname)
{
    dbDriver *driver;
    dbString *names;
    int i, count, found = 0;
    int full = 0;
    char buf[1000];
    char *bufp, *c;

    if ( strchr ( tabname, '.' ) ) 
	full = 1;
    
    driver = db_start_driver_open_database ( drvname, dbname );
    if ( driver == NULL ) {
        G_warning ( "Cannot open database '%s' by driver '%s'", dbname, drvname );
	return -1;
    }
    
    /* The table tabname can be either fully qualified in form table.schema,
     * or it can be only table name. If the name is fully qualified, compare whole name,
     * if it is not, compare only table names */
    
    /* user tables */
    if( db_list_tables (driver, &names, &count, 0) != DB_OK) return (-1);

    for (i = 0; i < count; i++) {
	strcpy ( buf, db_get_string (&names[i]) );
	bufp = buf;
	if ( !full && (c=strchr(buf,'.')) ) {
		bufp = c+1;
	}
	G_debug ( 2, "table = %s -> %s", buf, bufp );
	if ( G_strcasecmp( tabname, bufp) == 0 ) {
            found = 1;
	    break;
	}
    }
    db_free_string_array(names, count);
    
    if ( !found ) {    /* system tables */
	if( db_list_tables (driver, &names, &count, 1) != DB_OK) return (-1);

	for (i = 0; i < count; i++) {
	    strcpy ( buf, db_get_string (&names[i]) );
	    bufp = buf;
	    if ( !full && (c=strchr(buf,'.')) ) {
	        bufp = c+1;
	    }
	    if ( G_strcasecmp( tabname, bufp) == 0 ) {
		found = 1;
		break;
	    }
	}
	db_free_string_array(names, count);
    }
    db_close_database_shutdown_driver ( driver );

    return (found);
}