describe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * \file describe.c
  3. *
  4. * \brief Low level SQLite database driver.
  5. *
  6. * This program is free software under the GNU General Public License
  7. * (>=v2). Read the file COPYING that comes with GRASS for details.
  8. *
  9. * \author Radim Blazek
  10. *
  11. * \date 2005-2007
  12. */
  13. #include <string.h>
  14. #include <grass/dbmi.h>
  15. #include <grass/datetime.h>
  16. #include <grass/glocale.h>
  17. #include "globals.h"
  18. #include "proto.h"
  19. /* function prototypes */
  20. static int affinity_type(const char *);
  21. static int parse_type(const char *, int *);
  22. static void get_column_info(sqlite3_stmt * statement, int col,
  23. int *litetype, int *sqltype, int *length);
  24. /**
  25. * \fn int db__driver_describe_table (dbString *table_name, dbTable **table)
  26. *
  27. * \brief Low level SQLite describe database table.
  28. *
  29. * \param[in] table_name
  30. * \param[in] table
  31. * \return int DB_FAILED on error; DB_OK on success
  32. */
  33. int db__driver_describe_table(dbString * table_name, dbTable ** table)
  34. {
  35. dbString sql;
  36. sqlite3_stmt *statement;
  37. const char *rest;
  38. int ret;
  39. db_init_string(&sql);
  40. db_set_string(&sql, "select * from ");
  41. db_append_string(&sql, db_get_string(table_name));
  42. db_append_string(&sql, " where oid < 0");
  43. ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
  44. if (ret != SQLITE_OK) {
  45. append_error("Error in sqlite3_prepare():");
  46. append_error(db_get_string(&sql));
  47. append_error("\n");
  48. append_error((char *)sqlite3_errmsg(sqlite));
  49. report_error();
  50. db_free_string(&sql);
  51. return DB_FAILED;
  52. }
  53. db_free_string(&sql);
  54. if (describe_table(statement, table, NULL) == DB_FAILED) {
  55. append_error("Cannot describe table\n");
  56. report_error();
  57. sqlite3_finalize(statement);
  58. return DB_FAILED;
  59. }
  60. sqlite3_finalize(statement);
  61. return DB_OK;
  62. }
  63. /**
  64. * \fn int describe_table (sqlite3_stmt *statement, dbTable **table, cursor *c)
  65. *
  66. * \brief SQLite describe table.
  67. *
  68. * NOTE: If <b>c</b> is not NULL c->cols and c->ncols are also set.
  69. *
  70. * \param[in] statement
  71. * \param[in] table
  72. * \param[in] c SQLite cursor. See NOTE.
  73. * \return int DB_FAILED on error; DB_OK on success
  74. */
  75. int describe_table(sqlite3_stmt * statement, dbTable ** table, cursor * c)
  76. {
  77. int i, ncols, nkcols;
  78. G_debug(3, "describe_table()");
  79. ncols = sqlite3_column_count(statement);
  80. G_debug(3, "ncols = %d", ncols);
  81. /* Try to get first row */
  82. sqlite3_step(statement);
  83. /* Count columns of known type */
  84. nkcols = 0;
  85. for (i = 0; i < ncols; i++) {
  86. int litetype, sqltype, length;
  87. get_column_info(statement, i, &litetype, &sqltype, &length);
  88. if (sqltype == DB_SQL_TYPE_UNKNOWN)
  89. continue;
  90. nkcols++; /* known types */
  91. }
  92. G_debug(3, "nkcols = %d", nkcols);
  93. if (c) {
  94. c->kcols = (int *)G_malloc(nkcols * sizeof(int));
  95. c->nkcols = nkcols;
  96. }
  97. if (!(*table = db_alloc_table(nkcols))) {
  98. return DB_FAILED;
  99. }
  100. /* set the table name */
  101. /* TODO */
  102. db_set_table_name(*table, "");
  103. /* set the table description */
  104. db_set_table_description(*table, "");
  105. /* TODO */
  106. /*
  107. db_set_table_delete_priv_granted (*table);
  108. db_set_table_insert_priv_granted (*table);
  109. db_set_table_delete_priv_not_granted (*table);
  110. db_set_table_insert_priv_not_granted (*table);
  111. */
  112. nkcols = 0;
  113. for (i = 0; i < ncols; i++) {
  114. const char *fname;
  115. dbColumn *column;
  116. int litetype, sqltype, fsize, precision, scale;
  117. fname = sqlite3_column_name(statement, i);
  118. get_column_info(statement, i, &litetype, &sqltype, &fsize);
  119. G_debug(2, "col: %s, nkcols %d, litetype : %d, sqltype %d",
  120. fname, nkcols, litetype, sqltype);
  121. if (sqltype == DB_SQL_TYPE_UNKNOWN) {
  122. /* Warn, ignore and continue */
  123. G_warning(_("SQLite driver: column '%s', SQLite type %d is not supported"),
  124. fname, litetype);
  125. continue;
  126. }
  127. switch (sqltype) {
  128. case DB_SQL_TYPE_SMALLINT:
  129. case DB_SQL_TYPE_INTEGER:
  130. case DB_SQL_TYPE_SERIAL:
  131. fsize = 20;
  132. break;
  133. case DB_SQL_TYPE_REAL:
  134. case DB_SQL_TYPE_DOUBLE_PRECISION:
  135. case DB_SQL_TYPE_DECIMAL:
  136. case DB_SQL_TYPE_NUMERIC:
  137. fsize = 20;
  138. break;
  139. case DB_SQL_TYPE_DATE:
  140. case DB_SQL_TYPE_TIME:
  141. case DB_SQL_TYPE_TIMESTAMP:
  142. case DB_SQL_TYPE_INTERVAL:
  143. fsize = 20;
  144. break;
  145. case DB_SQL_TYPE_CHARACTER:
  146. /* fsize is already correct */
  147. break;
  148. case DB_SQL_TYPE_TEXT:
  149. /* fudge for clients which don't understand variable-size fields */
  150. fsize = 1000;
  151. break;
  152. default:
  153. G_warning("SQLite driver: unknown type: %d", sqltype);
  154. fsize = 99999; /* sqlite doesn't care, it must be long enough to
  155. satisfy tests in GRASS */
  156. }
  157. column = db_get_table_column(*table, nkcols);
  158. db_set_column_name(column, fname);
  159. db_set_column_length(column, fsize);
  160. db_set_column_host_type(column, litetype);
  161. db_set_column_sqltype(column, sqltype);
  162. /* TODO */
  163. precision = 0;
  164. scale = 0;
  165. /*
  166. db_set_column_precision (column, precision);
  167. db_set_column_scale (column, scale);
  168. */
  169. /* TODO */
  170. db_set_column_null_allowed(column);
  171. db_set_column_has_undefined_default_value(column);
  172. db_unset_column_use_default_value(column);
  173. /* TODO */
  174. /*
  175. db_set_column_select_priv_granted (column);
  176. db_set_column_update_priv_granted (column);
  177. db_set_column_update_priv_not_granted (column);
  178. */
  179. if (c) {
  180. c->kcols[nkcols] = i;
  181. }
  182. nkcols++;
  183. }
  184. sqlite3_reset(statement);
  185. return DB_OK;
  186. }
  187. static int dbmi_type(int litetype)
  188. {
  189. switch (litetype) {
  190. case SQLITE_INTEGER:
  191. return DB_SQL_TYPE_INTEGER;
  192. case SQLITE_FLOAT:
  193. return DB_SQL_TYPE_DOUBLE_PRECISION;
  194. case SQLITE_TEXT:
  195. return DB_SQL_TYPE_TEXT;
  196. case SQLITE_NULL:
  197. return DB_SQL_TYPE_TEXT; /* good choice? */
  198. default:
  199. return DB_SQL_TYPE_UNKNOWN;
  200. }
  201. }
  202. /**
  203. * \fn void get_column_info (sqlite3_stmt *statement, int col, int *litetype, int *sqltype)
  204. *
  205. * \brief Low level SQLite get column information.
  206. *
  207. * \param[in] statement
  208. * \param[in] col
  209. * \param[in,out] litetype
  210. * \param[in,out] sqltype
  211. */
  212. static void get_column_info(sqlite3_stmt * statement, int col,
  213. int *litetype, int *sqltype, int *length)
  214. {
  215. const char *decltype;
  216. decltype = sqlite3_column_decltype(statement, col);
  217. if (decltype) {
  218. G_debug(4, "column: %s, decltype = %s",
  219. sqlite3_column_name(statement, col), decltype);
  220. *sqltype = parse_type(decltype, length);
  221. *litetype = affinity_type(decltype);
  222. }
  223. else {
  224. G_debug(4, "this is not a table column");
  225. /* If there are no results it gives 0 */
  226. *litetype = sqlite3_column_type(statement, col);
  227. *sqltype = dbmi_type(*litetype);
  228. *length = 0;
  229. }
  230. G_debug(3, "sqltype = %d", *sqltype);
  231. G_debug(3, "litetype = %d", *litetype);
  232. }
  233. /* SQLite documentation:
  234. *
  235. * The type affinity of a column is determined by the declared
  236. * type of the column, according to the following rules:
  237. *
  238. * 1. If the datatype contains the string "INT"
  239. * then it is assigned INTEGER affinity.
  240. *
  241. * 2. If the datatype of the column contains any of the strings
  242. * "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity.
  243. * Notice that the type VARCHAR contains the string "CHAR"
  244. * and is thus assigned TEXT affinity.
  245. *
  246. * 3. If the datatype for a column contains the string "BLOB"
  247. * or if no datatype is specified then the column has affinity NONE.
  248. *
  249. * 4. Otherwise, the affinity is NUMERIC.
  250. */
  251. static int affinity_type(const char *declared)
  252. {
  253. char *lc;
  254. int aff = SQLITE_FLOAT;
  255. lc = G_store(declared);
  256. G_tolcase(lc);
  257. G_debug(4, "affinity_type: %s", lc);
  258. if (strstr(lc, "int")) {
  259. aff = SQLITE_INTEGER;
  260. }
  261. else if (strstr(lc, "char") || strstr(lc, "clob")
  262. || strstr(lc, "text") || strstr(lc, "date")) {
  263. aff = SQLITE_TEXT;
  264. }
  265. else if (strstr(lc, "blob")) {
  266. aff = SQLITE_BLOB;
  267. }
  268. G_free(lc);
  269. return aff;
  270. }
  271. static int parse_type(const char *declared, int *length)
  272. {
  273. char buf[256];
  274. char word[4][256];
  275. strncpy(buf, declared, sizeof(buf));
  276. buf[sizeof(buf) - 1] = '\0';
  277. G_chop(buf);
  278. G_tolcase(buf);
  279. *length = 1;
  280. #define streq(a,b) (strcmp((a),(b)) == 0)
  281. if (streq(buf, "smallint") || streq(buf, "int2"))
  282. return DB_SQL_TYPE_SMALLINT;
  283. if (streq(buf, "integer") ||
  284. streq(buf, "int") ||
  285. streq(buf, "int4") || streq(buf, "bigint") || streq(buf, "int8"))
  286. return DB_SQL_TYPE_INTEGER;
  287. if (streq(buf, "real") || streq(buf, "float4"))
  288. return DB_SQL_TYPE_REAL;
  289. if (streq(buf, "double") || streq(buf, "float8"))
  290. return DB_SQL_TYPE_DOUBLE_PRECISION;
  291. if (streq(buf, "decimal"))
  292. return DB_SQL_TYPE_DECIMAL;
  293. if (streq(buf, "numeric"))
  294. return DB_SQL_TYPE_NUMERIC;
  295. if (streq(buf, "date"))
  296. return DB_SQL_TYPE_DATE;
  297. if (streq(buf, "time") || streq(buf, "timetz"))
  298. return DB_SQL_TYPE_TIME;
  299. if (streq(buf, "timestamp") || streq(buf, "timestamptz"))
  300. return DB_SQL_TYPE_TIMESTAMP;
  301. if (streq(buf, "interval"))
  302. return DB_SQL_TYPE_INTERVAL;
  303. if (streq(buf, "text"))
  304. return DB_SQL_TYPE_TEXT;
  305. if (streq(buf, "serial") || streq(buf, "serial4"))
  306. return DB_SQL_TYPE_SERIAL;
  307. if (streq(buf, "character")
  308. || streq(buf, "char")
  309. || streq(buf, "varchar"))
  310. return DB_SQL_TYPE_CHARACTER;
  311. if (sscanf(buf, "%s %s", word[0], word[1]) == 2) {
  312. if (streq(word[0], "double") && streq(word[1], "precision"))
  313. return DB_SQL_TYPE_DOUBLE_PRECISION;
  314. if (streq(word[0], "character") && streq(word[1], "varying"))
  315. return DB_SQL_TYPE_CHARACTER;
  316. }
  317. if (sscanf(buf, "%s %s %s %s", word[0], word[1], word[2], word[3]) == 4 &&
  318. (streq(word[1], "with") || streq(word[1], "without")) &&
  319. streq(word[2], "time") && streq(word[3], "zone")) {
  320. if (streq(word[0], "time"))
  321. return DB_SQL_TYPE_TIME;
  322. if (streq(word[0], "timestamp"))
  323. return DB_SQL_TYPE_TIMESTAMP;
  324. }
  325. if (sscanf(buf, "varchar ( %d )", length) == 1 ||
  326. sscanf(buf, "character varying ( %d )", length) == 1 ||
  327. sscanf(buf, "character ( %d )", length) == 1 ||
  328. sscanf(buf, "char ( %d )", length) == 1)
  329. return DB_SQL_TYPE_CHARACTER;
  330. if (sscanf(buf, "interval ( %d )", length) == 1)
  331. return DB_SQL_TYPE_INTERVAL;
  332. if (sscanf(buf, "numeric ( %d , %d )", length, length) == 2)
  333. return DB_SQL_TYPE_NUMERIC;
  334. if (sscanf(buf, "decimal ( %d , %d )", length, length) == 2)
  335. return DB_SQL_TYPE_DECIMAL;
  336. if (sscanf(buf, "time ( %d )", length) == 1 ||
  337. sscanf(buf, "timetz ( %d )", length) == 1)
  338. return DB_SQL_TYPE_TIME;
  339. if (sscanf(buf, "timestamp ( %d )", length) == 1 ||
  340. sscanf(buf, "timestamptz ( %d )", length) == 1)
  341. return DB_SQL_TYPE_TIMESTAMP;
  342. if (sscanf
  343. (buf, "%s ( %d ) %s %s %s", word[0], length, word[1], word[2],
  344. word[3]) == 5 && (streq(word[1], "with") ||
  345. streq(word[1], "without")) &&
  346. streq(word[2], "time") && streq(word[3], "zone")) {
  347. if (streq(word[0], "time"))
  348. return DB_SQL_TYPE_TIME;
  349. if (streq(word[0], "timestamp"))
  350. return DB_SQL_TYPE_TIMESTAMP;
  351. }
  352. #undef streq
  353. G_warning("SQLite driver: unable to parse decltype: %s", declared);
  354. return DB_SQL_TYPE_UNKNOWN;
  355. }