describe.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /**
  22. * \fn int db__driver_describe_table (dbString *table_name, dbTable **table)
  23. *
  24. * \brief Low level SQLite describe database table.
  25. *
  26. * \param[in] table_name
  27. * \param[in] table
  28. * \return int DB_FAILED on error; DB_OK on success
  29. */
  30. int db__driver_describe_table (dbString *table_name, dbTable **table)
  31. {
  32. dbString sql;
  33. sqlite3_stmt *statement;
  34. const char *rest;
  35. int ret;
  36. db_init_string ( &sql );
  37. db_set_string( &sql, "select * from ");
  38. db_append_string ( &sql, db_get_string(table_name) );
  39. db_append_string( &sql, " where oid < 0");
  40. ret = sqlite3_prepare ( sqlite, db_get_string(&sql), -1,
  41. &statement, &rest );
  42. if ( ret != SQLITE_OK )
  43. {
  44. append_error("Error in sqlite3_prepare():");
  45. append_error( db_get_string(&sql) );
  46. append_error( "\n" );
  47. append_error ((char *) sqlite3_errmsg (sqlite));
  48. report_error( );
  49. db_free_string ( &sql );
  50. return DB_FAILED;
  51. }
  52. db_free_string ( &sql );
  53. if ( describe_table( statement, table, NULL) == DB_FAILED ) {
  54. append_error("Cannot describe table\n");
  55. report_error();
  56. sqlite3_finalize ( statement );
  57. return DB_FAILED;
  58. }
  59. sqlite3_finalize ( statement );
  60. return DB_OK;
  61. }
  62. /**
  63. * \fn int describe_table (sqlite3_stmt *statement, dbTable **table, cursor *c)
  64. *
  65. * \brief SQLite describe table.
  66. *
  67. * NOTE: If <b>c</b> is not NULL c->cols and c->ncols are also set.
  68. *
  69. * \param[in] statement
  70. * \param[in] table
  71. * \param[in] c SQLite cursor. See NOTE.
  72. * \return int DB_FAILED on error; DB_OK on success
  73. */
  74. int describe_table( sqlite3_stmt *statement,
  75. 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. {
  87. int litetype, sqltype;
  88. get_column_info ( statement, i, &litetype, &sqltype );
  89. if ( sqltype == DB_SQL_TYPE_UNKNOWN ) 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. {
  115. const char *fname;
  116. dbColumn *column;
  117. int litetype, sqltype, fsize, precision, scale;
  118. fname = sqlite3_column_name ( statement, i );
  119. get_column_info ( statement, i, &litetype, &sqltype );
  120. G_debug(2, "col: %s, nkcols %d, litetype : %d, sqltype %d",
  121. fname, nkcols, litetype, sqltype );
  122. if ( sqltype == DB_SQL_TYPE_UNKNOWN )
  123. {
  124. /* Warn, ignore and continue */
  125. G_warning ( _("SQLite driver: column '%s', SQLite type %d is not supported"),
  126. fname, litetype);
  127. continue;
  128. }
  129. switch ( litetype) {
  130. case SQLITE_INTEGER:
  131. fsize = 20;
  132. break;
  133. case SQLITE_FLOAT:
  134. fsize = 20;
  135. break;
  136. case SQLITE_TEXT:
  137. fsize = 255;
  138. break;
  139. /* SQLITE_BLOB, SQLITE_NULL needed here? */
  140. default:
  141. fsize = 99999; /* sqlite doesn't care, it must be long enough to
  142. satisfy tests in GRASS */
  143. }
  144. column = db_get_table_column(*table, nkcols);
  145. db_set_column_name(column, fname);
  146. db_set_column_length(column, fsize);
  147. db_set_column_host_type(column, litetype);
  148. db_set_column_sqltype(column, sqltype);
  149. /* TODO */
  150. precision = 0;
  151. scale = 0;
  152. /*
  153. db_set_column_precision (column, precision);
  154. db_set_column_scale (column, scale);
  155. */
  156. /* TODO */
  157. db_set_column_null_allowed(column);
  158. db_set_column_has_undefined_default_value(column);
  159. db_unset_column_use_default_value(column);
  160. /* TODO */
  161. /*
  162. db_set_column_select_priv_granted (column);
  163. db_set_column_update_priv_granted (column);
  164. db_set_column_update_priv_not_granted (column);
  165. */
  166. if ( c ) {
  167. c->kcols[nkcols] = i;
  168. }
  169. nkcols++;
  170. }
  171. sqlite3_reset ( statement );
  172. return DB_OK;
  173. }
  174. /**
  175. * \fn void get_column_info (sqlite3_stmt *statement, int col, int *litetype, int *sqltype)
  176. *
  177. * \brief Low level SQLite get column information.
  178. *
  179. * \param[in] statement
  180. * \param[in] col
  181. * \param[in,out] litetype
  182. * \param[in,out] sqltype
  183. */
  184. void get_column_info ( sqlite3_stmt *statement, int col,
  185. int *litetype, int *sqltype )
  186. {
  187. const char *decltype;
  188. decltype = sqlite3_column_decltype ( statement, col );
  189. if ( decltype )
  190. {
  191. G_debug ( 4, "column: %s, decltype = %s", sqlite3_column_name ( statement, col), decltype );
  192. *litetype = affinity_type ( decltype );
  193. }
  194. else
  195. {
  196. G_debug ( 4, "this is not a table column" );
  197. /* If there are no results it gives 0 */
  198. *litetype = sqlite3_column_type ( statement, col );
  199. }
  200. G_debug ( 3, "litetype = %d", *litetype );
  201. /* http://www.sqlite.org/capi3ref.html#sqlite3_column_type
  202. SQLITE_INTEGER 1
  203. SQLITE_FLOAT 2
  204. SQLITE_TEXT 3
  205. SQLITE_BLOB 4
  206. SQLITE_NULL 5
  207. lib/db/dbmi_base/sqltype.c:
  208. DB_SQL_TYPE_CHARACTER: 0 "CHARACTER"
  209. DB_SQL_TYPE_NUMERIC: 1 "NUMERIC"
  210. DB_SQL_TYPE_DECIMAL: 2 "DECIMAL"
  211. DB_SQL_TYPE_SMALLINT: 3 "SMALLINT"
  212. DB_SQL_TYPE_INTEGER: 4 "INTEGER"
  213. DB_SQL_TYPE_REAL: 5 "REAL"
  214. DB_SQL_TYPE_DOUBLE_PRECISION: 6 "DOUBLE PRECISION"
  215. DB_SQL_TYPE_DATE: 7 "DATE"
  216. DB_SQL_TYPE_TIME: 8 "TIME"
  217. DB_SQL_TYPE_SERIAL: 9 "SERIAL"
  218. DB_SQL_TYPE_TEXT: 10 "TEXT"
  219. DB_SQL_TYPE_TIMESTAMP: 11 "TIMESTAMP"
  220. DB_SQL_TYPE_INTERVAL: 12 "INTERVAL"
  221. default 13 "unknown"
  222. */
  223. switch ( *litetype) {
  224. case SQLITE_INTEGER:
  225. *sqltype = DB_SQL_TYPE_INTEGER;
  226. break;
  227. case SQLITE_FLOAT:
  228. *sqltype = DB_SQL_TYPE_DOUBLE_PRECISION;
  229. break;
  230. case SQLITE_TEXT:
  231. *sqltype = DB_SQL_TYPE_TEXT;
  232. break;
  233. case SQLITE_NULL:
  234. *sqltype = DB_SQL_TYPE_TEXT; /* good choice? */
  235. break;
  236. default:
  237. *sqltype = DB_SQL_TYPE_UNKNOWN;
  238. }
  239. }
  240. /* SQLite documentation:
  241. *
  242. * The type affinity of a column is determined by the declared
  243. * type of the column, according to the following rules:
  244. *
  245. * 1. If the datatype contains the string "INT"
  246. * then it is assigned INTEGER affinity.
  247. *
  248. * 2. If the datatype of the column contains any of the strings
  249. * "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity.
  250. * Notice that the type VARCHAR contains the string "CHAR"
  251. * and is thus assigned TEXT affinity.
  252. *
  253. * 3. If the datatype for a column contains the string "BLOB"
  254. * or if no datatype is specified then the column has affinity NONE.
  255. *
  256. * 4. Otherwise, the affinity is NUMERIC.
  257. */
  258. static int affinity_type (const char *declared)
  259. {
  260. char *lc;
  261. int aff = SQLITE_FLOAT;
  262. lc = strdup ( declared );
  263. G_tolcase ( lc );
  264. G_debug(4, "affinity_type: %s", lc);
  265. if ( strstr(lc,"int") )
  266. {
  267. aff = SQLITE_INTEGER;
  268. }
  269. else if ( strstr(lc,"char") || strstr(lc,"clob")
  270. || strstr(lc,"text") || strstr(lc,"date") )
  271. {
  272. aff = SQLITE_TEXT;
  273. }
  274. else if ( strstr(lc,"blob") )
  275. {
  276. aff = SQLITE_BLOB;
  277. }
  278. return aff;
  279. }