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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
#include <stdlib.h>
#include <string.h>
#include <gis.h>
#include <dbmi.h>
static int cmp();
/*!
\fn
\brief
\return
\param
*/
/* selet array of ordered integers
*
* return: number of selected values
* -1 on error
*/
int db_select_int (dbDriver *driver, char *tab, char *col, char *where, int **pval)
{
int type, more, alloc, count;
int *val;
char buf[1024], *sval;
dbString stmt;
dbCursor cursor;
dbColumn *column;
dbValue *value;
dbTable *table;
G_debug (3, "db_select_int()" );
/* allocate */
alloc = 1000;
val = (int *) G_malloc ( alloc * sizeof(int));
if ( where == NULL || strlen(where) == 0 )
snprintf(buf,1023, "SELECT %s FROM %s", col, tab);
else
snprintf(buf,1023, "SELECT %s FROM %s WHERE %s", col, tab, where);
G_debug (3, " SQL: %s", buf );
db_init_string ( &stmt);
db_append_string ( &stmt, buf);
if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
return (-1);
table = db_get_cursor_table (&cursor);
column = db_get_table_column(table, 0); /* first column */
value = db_get_column_value(column);
type = db_get_column_sqltype(column);
type = db_sqltype_to_Ctype(type);
/* fetch the data */
count = 0;
while(1)
{
if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
return (-1);
if (!more) break;
if ( count == alloc )
{
alloc += 1000;
val = (int *) G_realloc ( val, alloc * sizeof(int));
}
switch ( type )
{
case ( DB_C_TYPE_INT ):
val[count] = db_get_value_int(value);
break;
case ( DB_C_TYPE_STRING ):
sval = db_get_value_string(value);
val[count] = atoi(sval);
break;
case ( DB_C_TYPE_DOUBLE ):
val[count] = (int) db_get_value_double(value);
break;
default:
return (-1);
}
count++;
}
db_close_cursor(&cursor);
db_free_string ( &stmt );
qsort( (void *)val, count, sizeof(int), cmp);
*pval = val;
return (count);
}
/*!
\fn
\brief
\return
\param
*/
/* selet one (first) value from table/column for key/id
*
* return: number of selected values
* -1 on error
*/
int db_select_value (dbDriver *driver,
char *tab, /* table name */
char *key, /* key column name (integer) */
int id, /* identifier in key column */
char *col, /* the name of column to select the value from */
dbValue *val) /* pointer to existing dbValue to store within */
{
int more, count;
char buf[1024];
dbString stmt;
dbCursor cursor;
dbColumn *column;
dbValue *value;
dbTable *table;
sprintf( buf, "SELECT %s FROM %s WHERE %s = %d\n", col, tab, key, id);
db_init_string ( &stmt);
db_append_string ( &stmt, buf);
if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
return (-1);
table = db_get_cursor_table (&cursor);
column = db_get_table_column(table, 0); /* first column */
value = db_get_column_value(column);
/* fetch the data */
count = 0;
while(1) {
if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
return (-1);
if (!more) break;
if ( count == 0 ) db_copy_value ( val, value );
count++;
}
db_close_cursor(&cursor);
db_free_string ( &stmt );
return (count);
}
int cmp ( const void *pa, const void *pb)
{
int *p1 = (int *) pa;
int *p2 = (int *) pb;
if( *p1 < *p2 ) return -1;
if( *p1 > *p2 ) return 1;
return 0;
}
int cmpcat ( const void *pa, const void *pb)
{
dbCatVal *p1 = (dbCatVal *) pa;
dbCatVal *p2 = (dbCatVal *) pb;
if( p1->cat < p2->cat ) return -1;
if( p1->cat > p2->cat ) return 1;
return 0;
}
int cmpcatkey ( const void *pa, const void *pb)
{
int *p1 = (int *) pa;
dbCatVal *p2 = (dbCatVal *) pb;
if( *p1 < p2->cat ) return -1;
if( *p1 > p2->cat ) return 1;
return 0;
}
/*!
\fn
\brief
\return
\param
*/
/* selet pairs key/value to array, values are sorted by key
*
* return: number of selected values
* -1 on error
*/
int db_select_CatValArray ( dbDriver *driver, char *tab, char *key, char *col, char *where,
dbCatValArray *cvarr )
{
int i, type, more, nrows;
char buf[1024];
dbString stmt;
dbCursor cursor;
dbColumn *column;
dbValue *value;
dbTable *table;
G_debug (3, "db_select_db_select_CatValArray ()" );
db_init_string ( &stmt);
sprintf( buf, "SELECT %s, %s FROM %s", key, col, tab);
db_set_string ( &stmt, buf);
if ( where != NULL && strlen(where) > 0 ) {
db_append_string ( &stmt, " WHERE ");
db_append_string ( &stmt, where );
}
G_debug (3, " SQL: %s", db_get_string ( &stmt ) );
if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
return (-1);
nrows = db_get_num_rows ( &cursor );
G_debug (3, " %d rows selected", nrows );
if ( nrows < 0 ) G_fatal_error ( "Cannot select rows from database");
db_CatValArray_alloc( cvarr, nrows );
table = db_get_cursor_table (&cursor);
/* Check if key column is integer */
column = db_get_table_column(table, 0);
type = db_sqltype_to_Ctype( db_get_column_sqltype(column) );
G_debug (3, " key type = %d", type );
if ( type != DB_C_TYPE_INT ) {
G_fatal_error ( "Key column type is not integer" );
}
column = db_get_table_column(table, 1);
type = db_sqltype_to_Ctype( db_get_column_sqltype(column) );
G_debug (3, " col type = %d", type );
if ( type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE ) {
G_fatal_error ( "Column type not supported by db_select_to_array()" );
}
cvarr->ctype = type;
/* fetch the data */
for ( i = 0; i < nrows; i++ ) {
if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
return (-1);
column = db_get_table_column(table, 0); /* first column */
value = db_get_column_value(column);
cvarr->value[i].cat = db_get_value_int(value);
column = db_get_table_column(table, 1);
value = db_get_column_value(column);
cvarr->value[i].isNull = value->isNull;
switch ( type ) {
case ( DB_C_TYPE_INT ):
if ( value->isNull )
cvarr->value[i].val.i = 0;
else
cvarr->value[i].val.i = db_get_value_int(value);
break;
case ( DB_C_TYPE_DOUBLE ):
if ( value->isNull )
cvarr->value[i].val.d = 0.0;
else
cvarr->value[i].val.d = db_get_value_double(value);
break;
default:
return (-1);
}
}
cvarr->n_values = nrows;
db_close_cursor(&cursor);
db_free_string ( &stmt );
if ( type == DB_C_TYPE_INT )
qsort( (void *) cvarr->value, nrows, sizeof(dbCatVal), cmpcat);
else if ( type == DB_C_TYPE_DOUBLE )
qsort( (void *) cvarr->value, nrows, sizeof(dbCatVal), cmpcat);
return (nrows);
}
/* Sort dbCatValArray by category */
void
db_CatValArray_sort ( dbCatValArray *arr )
{
qsort( (void *) arr->value, arr->n_values, sizeof(dbCatVal), cmpcat);
}
/* find value by key
* returns: DB_FAILED, DB_OK
*/
int
db_CatValArray_get_value ( dbCatValArray *arr, int key, dbCatVal **cv )
{
dbCatVal *catval;
catval = bsearch ( (void *) &key, arr->value, arr->n_values, sizeof ( dbCatVal ), cmpcat );
if ( catval == NULL ) { return DB_FAILED; }
*cv = catval;
return DB_OK;
}
/* find value by key
* returns: DB_FAILED, DB_OK
*/
int
db_CatValArray_get_value_int ( dbCatValArray *arr, int key, int *val )
{
dbCatVal *catval;
catval = bsearch ( (void *) &key, arr->value, arr->n_values, sizeof ( dbCatVal ), cmpcat );
if ( catval == NULL ) { return DB_FAILED; }
*val = catval->val.i;
return DB_OK;
}
/* find value by key
* returns: 0 not found, 1 OK
*/
int
db_CatValArray_get_value_double ( dbCatValArray *arr, int key, double *val )
{
dbCatVal *catval;
G_debug (3, "db_CatValArray_get_value_double(), key = %d", key );
catval = bsearch ( (void *) &key, arr->value, arr->n_values, sizeof ( dbCatVal ), cmpcatkey );
if ( catval == NULL ) { return DB_FAILED; }
*val = catval->val.d;
return DB_OK;
}
|