17 #include <grass/gis.h>
18 #include <grass/dbmi.h>
19 #include <grass/glocale.h>
21 static int cmp(
const void *pa,
const void *pb)
33 static int cmpcat(
const void *pa,
const void *pb)
35 dbCatVal *p1 = (dbCatVal *) pa;
36 dbCatVal *p2 = (dbCatVal *) pb;
38 if (p1->cat < p2->cat)
40 if (p1->cat > p2->cat)
45 static int cmpcatkey(
const void *pa,
const void *pb)
48 dbCatVal *p2 = (dbCatVal *) pb;
57 static int cmpvalueint(
const void *pa,
const void *pb)
59 dbCatVal *p1 = (dbCatVal *) pa;
60 dbCatVal *p2 = (dbCatVal *) pb;
62 if (p1->val.i < p2->val.i)
64 if (p1->val.i > p2->val.i)
70 static int cmpvaluedouble(
const void *pa,
const void *pb)
72 dbCatVal *p1 = (dbCatVal *) pa;
73 dbCatVal *p2 = (dbCatVal *) pb;
75 if (p1->val.d < p2->val.d)
77 if (p1->val.d > p2->val.d)
83 static int cmpvaluestring(
const void *pa,
const void *pb)
85 dbCatVal *
const *a = pa;
86 dbCatVal *
const *
b = pb;
88 return strcmp((
const char *)a, (
const char *)b);
104 const char *where,
int **pval)
118 if (col ==
NULL || strlen(col) == 0) {
125 val = (
int *)G_malloc(alloc *
sizeof(
int));
127 if (where ==
NULL || strlen(where) == 0)
128 G_asprintf(&buf,
"SELECT %s FROM %s", col, tab);
130 G_asprintf(&buf,
"SELECT %s FROM %s WHERE %s", col, tab, where);
143 if (column ==
NULL) {
153 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
159 if (count == alloc) {
161 val = (
int *)G_realloc(val, alloc *
sizeof(
int));
165 case (DB_C_TYPE_INT):
168 case (DB_C_TYPE_STRING):
170 val[count] = atoi(sval);
172 case (DB_C_TYPE_DOUBLE):
184 qsort((
void *)val, count,
sizeof(
int), cmp);
205 int id,
const char *col, dbValue * val)
215 if (key ==
NULL || strlen(key) == 0) {
220 if (col ==
NULL || strlen(col) == 0) {
225 G_zero(val,
sizeof(dbValue));
226 G_asprintf(&buf,
"SELECT %s FROM %s WHERE %s = %d", col, tab, key,
id);
241 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
269 const char *col,
const char *where,
270 dbCatValArray * cvarr)
272 int i,
type, more, nrows, ncols;
280 G_debug(3,
"db_select_CatValArray ()");
282 if (key ==
NULL || strlen(key) == 0) {
287 if (col ==
NULL || strlen(col) == 0) {
293 if (strcmp(key, col) == 0) {
295 G_asprintf(&buf,
"SELECT %s FROM %s", key, tab);
299 G_asprintf(&buf,
"SELECT %s, %s FROM %s", key, col, tab);
304 if (where !=
NULL && strlen(where) > 0) {
315 G_debug(3,
" %d rows selected", nrows);
317 G_warning(_(
"Unable select records from table <%s>"), tab);
330 G_debug(3,
" key type = %d", type);
332 if (type != DB_C_TYPE_INT) {
333 G_warning(_(
"Key column type is not integer"));
342 G_debug(3,
" col type = %d", type);
353 for (i = 0; i < nrows; i++) {
354 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
365 cvarr->value[i].isNull = value->isNull;
367 case (DB_C_TYPE_INT):
369 cvarr->value[i].val.i = 0;
374 case (DB_C_TYPE_DOUBLE):
376 cvarr->value[i].val.d = 0.0;
381 case (DB_C_TYPE_STRING):
382 cvarr->value[i].val.s = (dbString *) malloc(
sizeof(dbString));
385 if (!(value->isNull))
390 case (DB_C_TYPE_DATETIME):
391 cvarr->value[i].val.t =
392 (dbDateTime *) calloc(1,
sizeof(dbDateTime));
394 if (!(value->isNull))
395 memcpy(cvarr->value[i].val.t, &(value->t),
403 cvarr->n_values = nrows;
419 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal), cmpcat);
432 switch (arr->ctype) {
433 case (DB_C_TYPE_INT):
434 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
437 case (DB_C_TYPE_DOUBLE):
438 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
441 case (DB_C_TYPE_STRING):
442 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
445 case (DB_C_TYPE_DATETIME):
446 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
471 bsearch((
void *)&key, arr->value, arr->n_values,
sizeof(dbCatVal),
473 if (catval ==
NULL) {
497 bsearch((
void *)&key, arr->value, arr->n_values,
sizeof(dbCatVal),
499 if (catval ==
NULL) {
503 *val = catval->val.i;
522 G_debug(3,
"db_CatValArray_get_value_double(), key = %d", key);
525 bsearch((
void *)&key, arr->value, arr->n_values,
sizeof(dbCatVal),
527 if (catval ==
NULL) {
531 *val = catval->val.d;