GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <stdlib.h> 00002 #include <grass/dbmi.h> 00003 00010 int db_test_value_isnull(dbValue * value) 00011 { 00012 return (value->isNull != 0); 00013 } 00014 00021 int db_get_value_int(dbValue * value) 00022 { 00023 return (value->i); 00024 } 00025 00032 double db_get_value_double(dbValue * value) 00033 { 00034 return (value->d); 00035 } 00036 00043 /* for given value and C type of value returns double representation */ 00044 double db_get_value_as_double(dbValue * value, int ctype) 00045 { 00046 double val; 00047 00048 switch (ctype) { 00049 case (DB_C_TYPE_INT): 00050 val = (double)db_get_value_int(value); 00051 break; 00052 case (DB_C_TYPE_STRING): 00053 val = atof(db_get_value_string(value)); 00054 break; 00055 case (DB_C_TYPE_DOUBLE): 00056 val = db_get_value_double(value); 00057 break; 00058 default: 00059 val = 0; 00060 } 00061 return val; 00062 } 00063 00070 const char *db_get_value_string(dbValue * value) 00071 { 00072 return (db_get_string(&value->s)); 00073 } 00074 00081 int db_get_value_year(dbValue * value) 00082 { 00083 return (value->t.year); 00084 } 00085 00092 int db_get_value_month(dbValue * value) 00093 { 00094 return (value->t.month); 00095 } 00096 00103 int db_get_value_day(dbValue * value) 00104 { 00105 return (value->t.day); 00106 } 00107 00114 int db_get_value_hour(dbValue * value) 00115 { 00116 return (value->t.hour); 00117 } 00118 00125 int db_get_value_minute(dbValue * value) 00126 { 00127 return (value->t.minute); 00128 } 00129 00136 double db_get_value_seconds(dbValue * value) 00137 { 00138 return (value->t.seconds); 00139 } 00140 00147 void db_set_value_null(dbValue * value) 00148 { 00149 value->isNull = 1; 00150 } 00151 00158 void db_set_value_not_null(dbValue * value) 00159 { 00160 value->isNull = 0; 00161 } 00162 00169 void db_set_value_int(dbValue * value, int i) 00170 { 00171 value->i = i; 00172 db_set_value_not_null(value); 00173 } 00174 00181 void db_set_value_double(dbValue * value, double d) 00182 { 00183 value->d = d; 00184 db_set_value_not_null(value); 00185 } 00186 00193 int db_set_value_string(dbValue * value, const char *s) 00194 { 00195 db_set_value_not_null(value); 00196 return db_set_string(&value->s, s); 00197 } 00198 00205 void db_set_value_year(dbValue * value, int year) 00206 { 00207 value->t.year = year; 00208 db_set_value_datetime_not_current(value); 00209 } 00210 00217 void db_set_value_month(dbValue * value, int month) 00218 { 00219 value->t.month = month; 00220 db_set_value_datetime_not_current(value); 00221 } 00222 00229 void db_set_value_day(dbValue * value, int day) 00230 { 00231 value->t.day = day; 00232 db_set_value_datetime_not_current(value); 00233 } 00234 00241 void db_set_value_hour(dbValue * value, int hour) 00242 { 00243 value->t.hour = hour; 00244 db_set_value_datetime_not_current(value); 00245 } 00246 00253 void db_set_value_minute(dbValue * value, int minute) 00254 { 00255 value->t.minute = minute; 00256 db_set_value_datetime_not_current(value); 00257 } 00258 00265 void db_set_value_seconds(dbValue * value, double seconds) 00266 { 00267 value->t.seconds = seconds; 00268 db_set_value_datetime_not_current(value); 00269 } 00270 00277 int db_test_value_datetime_current(dbValue * value) 00278 { 00279 return (value->t.current != 0); 00280 } 00281 00288 void db_set_value_datetime_current(dbValue * value) 00289 { 00290 value->t.current = 1; 00291 db_set_value_not_null(value); 00292 } 00293 00300 void db_set_value_datetime_not_current(dbValue * value) 00301 { 00302 value->t.current = 0; 00303 db_set_value_not_null(value); 00304 } 00305 00312 /* copy value from src to destination */ 00313 void db_copy_value(dbValue * dst, dbValue * src) 00314 { 00315 dst->isNull = src->isNull; 00316 dst->i = src->i; 00317 dst->d = src->d; 00318 if (src->s.nalloc > 0) 00319 db_copy_string(&(dst->s), &(src->s)); 00320 dst->t.current = src->t.current; 00321 dst->t.year = src->t.year; 00322 dst->t.month = src->t.month; 00323 dst->t.day = src->t.day; 00324 dst->t.hour = src->t.hour; 00325 dst->t.minute = src->t.minute; 00326 dst->t.seconds = src->t.seconds; 00327 } 00328 00335 void db_CatValArray_init(dbCatValArray * arr) 00336 { 00337 arr->n_values = 0; 00338 arr->alloc = 0; 00339 arr->value = NULL; 00340 } 00341 00348 void db_CatValArray_free(dbCatValArray * arr) 00349 { 00350 if (arr->ctype == DB_C_TYPE_STRING || arr->ctype == DB_C_TYPE_DATETIME) { 00351 int i; 00352 00353 for (i = 0; i < arr->n_values; i++) { 00354 if (arr->ctype == DB_C_TYPE_STRING && arr->value[i].val.s) { 00355 db_free_string(arr->value[i].val.s); 00356 } 00357 if (arr->ctype == DB_C_TYPE_DATETIME && arr->value[i].val.t) { 00358 db_free(arr->value[i].val.t); 00359 } 00360 } 00361 } 00362 00363 G_free(arr->value); 00364 } 00365 00372 int db_CatValArray_alloc(dbCatValArray * arr, int n) 00373 { 00374 arr->value = (dbCatVal *) G_calloc(n, sizeof(dbCatVal)); 00375 00376 arr->alloc = n; 00377 00378 return DB_OK; 00379 } 00380 00387 int db_CatValArray_realloc(dbCatValArray * arr, int n) 00388 { 00389 arr->value = (dbCatVal *) G_realloc(arr->value, n * sizeof(dbCatVal)); 00390 00391 arr->alloc = n; 00392 00393 return DB_OK; 00394 }