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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/*!
* \file db/dbmi_client/select.c
*
* \brief DBMI Library (client) - select records from table
*
* (C) 1999-2008 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
* \author Joel Jones (CERL/UIUC), Radim Blazek
*/
#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
static 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;
}
static 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;
}
static 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;
}
static int cmpvalueint(const void *pa, const void *pb)
{
dbCatVal *p1 = (dbCatVal *) pa;
dbCatVal *p2 = (dbCatVal *) pb;
if (p1->val.i < p2->val.i)
return -1;
if (p1->val.i > p2->val.i)
return 1;
return 0;
}
static int cmpvaluedouble(const void *pa, const void *pb)
{
dbCatVal *p1 = (dbCatVal *) pa;
dbCatVal *p2 = (dbCatVal *) pb;
if (p1->val.d < p2->val.d)
return -1;
if (p1->val.d > p2->val.d)
return 1;
return 0;
}
static int cmpvaluestring(const void *pa, const void *pb)
{
dbCatVal *const *a = pa;
dbCatVal *const *b = pb;
return strcmp((const char *)a, (const char *)b);
}
/*!
\brief Select array of ordered integers from table/column
\param driver DB driver
\param tab table name
\param col column name
\param where where statement
\param[out] pval array of ordered integer values
\return number of selected values
\return -1 on error
*/
int db_select_int(dbDriver * driver, const char *tab, const char *col,
const char *where, int **pval)
{
int type, more, alloc, count;
int *val;
char *buf = NULL;
const char *sval;
dbString stmt;
dbCursor cursor;
dbColumn *column;
dbValue *value;
dbTable *table;
G_debug(3, "db_select_int()");
if (col == NULL || strlen(col) == 0) {
G_warning(_("Missing column name"));
return -1;
}
/* allocate */
alloc = 1000;
val = (int *)G_malloc(alloc * sizeof(int));
if (where == NULL || strlen(where) == 0)
G_asprintf(&buf, "SELECT %s FROM %s", col, tab);
else
G_asprintf(&buf, "SELECT %s FROM %s WHERE %s", col, tab, where);
G_debug(3, " SQL: %s", buf);
db_init_string(&stmt);
db_set_string(&stmt, buf);
G_free(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 */
if (column == NULL) {
return -1;
}
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);
}
/*!
\brief Select one (first) value from table/column for key/id
\param driver DB driver
\param tab table name
\param key key column name
\param id identifier in key column
\param col name of column to select the value from
\param[out] val dbValue to store within
\return number of selected values
\return -1 on error
*/
int db_select_value(dbDriver * driver, const char *tab, const char *key,
int id, const char *col, dbValue * val)
{
int more, count;
char *buf = NULL;
dbString stmt;
dbCursor cursor;
dbColumn *column;
dbValue *value;
dbTable *table;
if (key == NULL || strlen(key) == 0) {
G_warning(_("Missing key column name"));
return -1;
}
if (col == NULL || strlen(col) == 0) {
G_warning(_("Missing column name"));
return -1;
}
G_zero(val, sizeof(dbValue));
G_asprintf(&buf, "SELECT %s FROM %s WHERE %s = %d", col, tab, key, id);
db_init_string(&stmt);
db_set_string(&stmt, buf);
G_free(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);
}
/*!
\brief Select pairs key/value to array, values are sorted by key (must be integer)
\param driver DB driver
\param tab table name
\param key key column name
\param col value column name
\param[out] cvarr dbCatValArray to store within
\return number of selected values
\return -1 on error
*/
int db_select_CatValArray(dbDriver * driver, const char *tab, const char *key,
const char *col, const char *where,
dbCatValArray * cvarr)
{
int i, type, more, nrows, ncols;
char *buf = NULL;
dbString stmt;
dbCursor cursor;
dbColumn *column;
dbValue *value;
dbTable *table;
G_debug(3, "db_select_CatValArray ()");
if (key == NULL || strlen(key) == 0) {
G_warning(_("Missing key column name"));
return -1;
}
if (col == NULL || strlen(col) == 0) {
G_warning(_("Missing column name"));
return -1;
}
db_init_string(&stmt);
if (strcmp(key, col) == 0) {
ncols = 1;
G_asprintf(&buf, "SELECT %s FROM %s", key, tab);
}
else {
ncols = 2;
G_asprintf(&buf, "SELECT %s, %s FROM %s", key, col, tab);
}
db_set_string(&stmt, buf);
G_free(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_warning(_("Unable select records from table <%s>"), tab);
db_close_cursor(&cursor);
db_free_string(&stmt);
return -1;
}
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_warning(_("Key column type is not integer"));
db_close_cursor(&cursor);
db_free_string(&stmt);
return -1;
}
if (ncols == 2) {
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);
if (ncols == 2) {
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;
case (DB_C_TYPE_STRING):
cvarr->value[i].val.s = (dbString *) malloc(sizeof(dbString));
db_init_string(cvarr->value[i].val.s);
if (!(value->isNull))
db_set_string(cvarr->value[i].val.s,
db_get_value_string(value));
break;
case (DB_C_TYPE_DATETIME):
cvarr->value[i].val.t =
(dbDateTime *) calloc(1, sizeof(dbDateTime));
if (!(value->isNull))
memcpy(cvarr->value[i].val.t, &(value->t),
sizeof(dbDateTime));
break;
default:
return (-1);
}
}
cvarr->n_values = nrows;
db_close_cursor(&cursor);
db_free_string(&stmt);
db_CatValArray_sort(cvarr);
return nrows;
}
/*!
\brief Sort key/value array by key
\param[in,out] arr dbCatValArray (key/value array)
*/
void db_CatValArray_sort(dbCatValArray * arr)
{
qsort((void *)arr->value, arr->n_values, sizeof(dbCatVal), cmpcat);
}
/*!
\brief Sort key/value array by value
\param[in,out] arr dbCatValArray (key/value array)
\return DB_OK on success
\return DB_FAILED on error
*/
int db_CatValArray_sort_by_value(dbCatValArray * arr)
{
switch (arr->ctype) {
case (DB_C_TYPE_INT):
qsort((void *)arr->value, arr->n_values, sizeof(dbCatVal),
cmpvalueint);
break;
case (DB_C_TYPE_DOUBLE):
qsort((void *)arr->value, arr->n_values, sizeof(dbCatVal),
cmpvaluedouble);
break;
case (DB_C_TYPE_STRING):
qsort((void *)arr->value, arr->n_values, sizeof(dbCatVal),
cmpvaluestring);
break;
case (DB_C_TYPE_DATETIME): /* is cmpvaluestring right here ? */
qsort((void *)arr->value, arr->n_values, sizeof(dbCatVal),
cmpvaluestring);
break;
default:
return (DB_FAILED);
}
return (DB_OK);
}
/*!
\brief Find value by key
\param arr dbCatValArray (key/value array)
\param key key value
\param[out] cv dbCatVal structure (key/value) to store within
\return DB_OK on success
\return DB_FAILED on error
*/
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;
}
/*!
\brief Find value (integer) by key
\param arr dbCatValArray (key/value array)
\param key key value
\param[out] val found value (integer)
\return DB_OK on success
\return DB_FAILED on error
*/
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;
}
/*!
\brief Find value (double) by key
\param arr dbCatValArray (key/value array)
\param key key value
\param[out] val found value (double)
\return DB_OK on success
\return DB_FAILED on error
*/
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;
}
|