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)
45 static int cmpcatkey(
const void *pa,
const void *pb)
57 static int cmpvalueint(
const void *pa,
const void *pb)
70 static int cmpvaluedouble(
const void *pa,
const void *pb)
83 static int cmpvaluestring(
const void *pa,
const void *pb)
88 return strcmp((
const char *)a, (
const char *)b);
104 const char *where,
int **pval)
106 int type, more, alloc,
count;
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) {
159 if (count == alloc) {
161 val = (
int *)G_realloc(val, alloc *
sizeof(
int));
170 val[
count] = atoi(sval);
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) {
226 G_asprintf(&buf,
"SELECT %s FROM %s WHERE %s = %d", col, tab, key,
id);
269 const char *col,
const char *where,
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);
333 G_warning(
_(
"Key column type is not integer"));
342 G_debug(3,
" col type = %d", type);
353 for (i = 0; i < nrows; i++) {
432 switch (arr->
ctype) {
473 if (catval ==
NULL) {
499 if (catval ==
NULL) {
503 *val = catval->
val.
i;
522 G_debug(3,
"db_CatValArray_get_value_double(), key = %d", key);
527 if (catval ==
NULL) {
531 *val = catval->
val.
d;
int db_select_CatValArray(dbDriver *driver, const char *tab, const char *key, const char *col, const char *where, dbCatValArray *cvarr)
Select pairs key/value to array, values are sorted by key (must be integer)
int db_close_cursor(dbCursor *cursor)
Close cursor.
void G_free(void *buf)
Free allocated memory.
void db_CatValArray_sort(dbCatValArray *arr)
Sort key/value array by key.
int db_CatValArray_get_value_int(dbCatValArray *arr, int key, int *val)
Find value (integer) by key.
struct _db_date_time dbDateTime
int db_select_value(dbDriver *driver, const char *tab, const char *key, int id, const char *col, dbValue *val)
Select one (first) value from table/column for key/id.
#define DB_C_TYPE_DATETIME
int G_asprintf(char **out, const char *fmt,...)
int db_CatValArray_alloc(dbCatValArray *arr, int n)
Allocate dbCatValArray.
int db_select_int(dbDriver *driver, const char *tab, const char *col, const char *where, int **pval)
Select array of ordered integers from table/column.
char * db_get_string(const dbString *x)
Get string.
int db_CatValArray_get_value_double(dbCatValArray *arr, int key, double *val)
Find value (double) by key.
int db_append_string(dbString *x, const char *s)
Append string to dbString.
int db_sqltype_to_Ctype(int sqltype)
Get C data type based on given SQL data type.
int db_fetch(dbCursor *cursor, int position, int *more)
Fetch data from open cursor.
double db_get_value_double(dbValue *value)
Get double precision value.
int db_get_column_sqltype(dbColumn *column)
Returns column sqltype for column.
dbTable * db_get_cursor_table(dbCursor *cursor)
Get table allocated by cursor.
int G_debug(int level, const char *msg,...)
Print debugging message.
dbValue * db_get_column_value(dbColumn *column)
Returns column value for given column structure.
int db_get_num_rows(dbCursor *cursor)
Get number of selected rows.
int db_CatValArray_sort_by_value(dbCatValArray *arr)
Sort key/value array by value.
int db_set_string(dbString *x, const char *s)
Inserts string to dbString (enlarge string)
int db_get_value_int(dbValue *value)
Get integer value.
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
void db_free_string(dbString *x)
Free allocated space for dbString.
dbColumn * db_get_table_column(dbTable *table, int idx)
Returns column structure for given table and column number.
int db_CatValArray_get_value(dbCatValArray *arr, int key, dbCatVal **cv)
Find value by key.
const char * db_get_value_string(dbValue *value)
Get string value.
void G_warning(const char *msg,...)
Print a warning message to stderr.
int db_open_select_cursor(dbDriver *driver, dbString *select, dbCursor *cursor, int mode)
Open select cursor.
void db_init_string(dbString *x)
Initialize dbString.
void db_copy_value(dbValue *dst, dbValue *src)
Copy value.