File: column.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 (120 lines) | stat: -rw-r--r-- 2,878 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h>
#include <string.h>
#include "gis.h"
#include "dbmi.h"

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
/* returns column sqltype  or -1 on error*/
int
db_column_sqltype ( 
    dbDriver *driver,  
    char *tab, /* table name */ 
    char *col) /* column  name*/
{
    dbTable *table;
    dbString table_name;
    dbColumn *column;
    int ncol, cl, type;

    db_init_string(&table_name);
    db_set_string(&table_name, tab);
    
    if(db_describe_table (driver, &table_name, &table) != DB_OK)
       return -1;
    
    db_free_string ( &table_name );
    ncol = db_get_table_number_of_columns(table);
    for (cl = 0; cl < ncol; cl++) {
	column = db_get_table_column (table, cl);
	if ( strcmp (  db_get_column_name(column), col ) == 0 ) {
	    type = db_get_column_sqltype(column);
	    return type;
	}
    }
    
    return -1;
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
/* returns column Ctype  or -1 on error */
int
db_column_Ctype ( 
    dbDriver *driver,  
    char *tab, /* table name */ 
    char *col) /* column  name*/
{
    int type;
    if ( ( type = db_column_sqltype ( driver, tab, col ) ) >= 0 ) {
	type = db_sqltype_to_Ctype(type); 
	return type;
    }

    return -1;
}

/*!
 \fn 
 \brief Get column structure by table and column name.
         Column is set to new dbColumn structure or NULL if column was not found
 \return: DB_OK
          DB_FAILED
 \param 
*/
int
db_get_column ( dbDriver *Driver, char *tname, char *cname, dbColumn **Column )
{
    int   i, ncols;
    dbTable *Table;
    dbColumn *Col, *NCol;
    dbString tabname;

    db_init_string(&tabname);
    db_set_string(&tabname, tname);

    if(db_describe_table (Driver, &tabname, &Table) != DB_OK) {
	 G_warning("Cannot describe table %s", tname);
	 return DB_FAILED;
    }

    *Column = NULL;

    ncols = db_get_table_number_of_columns(Table);
    G_debug (3, "ncol = %d", ncols );
	     
    for (i = 0; i < ncols; i++) {
        Col = db_get_table_column (Table, i);
	if ( G_strcasecmp ( db_get_column_name(Col), cname ) == 0 ) {
	    NCol = (dbColumn *) malloc ( sizeof ( dbColumn ) );
            db_init_column ( NCol );
	    db_set_string ( &(NCol->columnName), db_get_column_name(Col) );
	    db_set_string ( &(NCol->description), db_get_column_description(Col) );
	    NCol->sqlDataType = Col->sqlDataType;
	    NCol->hostDataType = Col->hostDataType;
	    db_copy_value ( &(NCol->value), &(Col->value) );
	    NCol->dataLen = Col->dataLen;
	    NCol->precision = Col->precision;
	    NCol->scale = Col->scale;
	    NCol->nullAllowed = Col->nullAllowed;
	    NCol->hasDefaultValue = Col->hasDefaultValue;
	    NCol->useDefaultValue = Col->useDefaultValue;
	    db_copy_value ( &(NCol->defaultValue), &(Col->defaultValue) );
	    NCol->select = Col->select;
	    NCol->update = Col->update;

	    *Column = NCol;
	    return DB_OK;
	}
    }
    return DB_OK;
}