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_snprintf(buf, 1023,
"SELECT %s FROM %s", col, tab);
130 G_snprintf(buf, 1023,
"SELECT %s FROM %s WHERE %s", col, tab, where);
149 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
155 if (count == alloc) {
157 val = (
int *)G_realloc(val, alloc *
sizeof(
int));
161 case (DB_C_TYPE_INT):
164 case (DB_C_TYPE_STRING):
166 val[count] = atoi(sval);
168 case (DB_C_TYPE_DOUBLE):
180 qsort((
void *)val, count,
sizeof(
int), cmp);
201 int id,
const char *col, dbValue * val)
211 if (key ==
NULL || strlen(key) == 0) {
216 if (col ==
NULL || strlen(col) == 0) {
221 G_zero(val,
sizeof(dbValue));
222 sprintf(buf,
"SELECT %s FROM %s WHERE %s = %d\n", col, tab, key,
id);
236 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
264 const char *col,
const char *where,
265 dbCatValArray * cvarr)
267 int i,
type, more, nrows;
275 G_debug(3,
"db_select_CatValArray ()");
277 if (key ==
NULL || strlen(key) == 0) {
282 if (col ==
NULL || strlen(col) == 0) {
289 sprintf(buf,
"SELECT %s, %s FROM %s", key, col, tab);
292 if (where !=
NULL && strlen(where) > 0) {
303 G_debug(3,
" %d rows selected", nrows);
305 G_fatal_error(_(
"Unable select records from table <%s>"), tab);
314 G_debug(3,
" key type = %d", type);
316 if (type != DB_C_TYPE_INT) {
322 G_debug(3,
" col type = %d", type);
333 for (i = 0; i < nrows; i++) {
334 if (
db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
343 cvarr->value[i].isNull = value->isNull;
345 case (DB_C_TYPE_INT):
347 cvarr->value[i].val.i = 0;
352 case (DB_C_TYPE_DOUBLE):
354 cvarr->value[i].val.d = 0.0;
359 case (DB_C_TYPE_STRING):
360 cvarr->value[i].val.s = (dbString *) malloc(
sizeof(dbString));
363 if (!(value->isNull))
368 case (DB_C_TYPE_DATETIME):
369 cvarr->value[i].val.t =
370 (dbDateTime *) calloc(1,
sizeof(dbDateTime));
372 if (!(value->isNull))
373 memcpy(cvarr->value[i].val.t, &(value->t),
381 cvarr->n_values = nrows;
397 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal), cmpcat);
410 switch (arr->ctype) {
411 case (DB_C_TYPE_INT):
412 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
415 case (DB_C_TYPE_DOUBLE):
416 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
419 case (DB_C_TYPE_STRING):
420 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
423 case (DB_C_TYPE_DATETIME):
424 qsort((
void *)arr->value, arr->n_values,
sizeof(dbCatVal),
449 bsearch((
void *)&key, arr->value, arr->n_values,
sizeof(dbCatVal),
451 if (catval ==
NULL) {
475 bsearch((
void *)&key, arr->value, arr->n_values,
sizeof(dbCatVal),
477 if (catval ==
NULL) {
481 *val = catval->val.i;
500 G_debug(3,
"db_CatValArray_get_value_double(), key = %d", key);
503 bsearch((
void *)&key, arr->value, arr->n_values,
sizeof(dbCatVal),
505 if (catval ==
NULL) {
509 *val = catval->val.d;