table.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*!
  2. \file lib/db/dbmi_base/table.c
  3. \brief DBMI Library (base) - table management
  4. (C) 1999-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Joel Jones (CERL/UIUC), Radim Blazek
  8. \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/dbmi.h>
  14. /*!
  15. \brief Allocate a table with a specific number of columns
  16. \param ncols number of columns which should be allocated
  17. \return allocated dbTable
  18. \return NULL in case of an error
  19. */
  20. dbTable *db_alloc_table(int ncols)
  21. {
  22. dbTable *table;
  23. int i;
  24. table = (dbTable *) db_malloc(sizeof(dbTable));
  25. if (table == NULL)
  26. return (table = NULL);
  27. db_init_table(table);
  28. table->columns = (dbColumn *) db_calloc(sizeof(dbColumn), ncols);
  29. if (table->columns == NULL) {
  30. db_free(table);
  31. return (table = NULL);
  32. }
  33. table->numColumns = ncols;
  34. for (i = 0; i < ncols; i++)
  35. db_init_column(&table->columns[i]);
  36. return table;
  37. }
  38. /*!
  39. \brief Initialize the table to zero
  40. \param table pointer to dbTable
  41. */
  42. void db_init_table(dbTable * table)
  43. {
  44. db_zero((void *)table, sizeof(dbTable));
  45. db_init_string(&table->tableName);
  46. db_init_string(&table->description);
  47. }
  48. /*!
  49. \brief Free the table
  50. \param table pointer to dbTable
  51. */
  52. void db_free_table(dbTable * table)
  53. {
  54. int i;
  55. db_free_string(&table->tableName);
  56. db_free_string(&table->description);
  57. for (i = 0; i < table->numColumns; i++)
  58. db_free_column(&table->columns[i]);
  59. if (table->columns)
  60. db_free(table->columns);
  61. db_free(table);
  62. }
  63. /*!
  64. \brief Set the name of the table
  65. \param table pointer to dbTable
  66. \param name The name of the table
  67. \return DB_OK on success
  68. */
  69. int db_set_table_name(dbTable * table, const char *name)
  70. {
  71. return db_set_string(&table->tableName, name);
  72. }
  73. /*!
  74. \brief Get the name of the table
  75. \param table pointer to dbTable
  76. \return name of the table
  77. */
  78. const char *db_get_table_name(dbTable * table)
  79. {
  80. return db_get_string(&table->tableName);
  81. }
  82. /*!
  83. \brief Set the description of the table
  84. \param table pointer to dbTable
  85. \param name description of the table
  86. \return DB_OK
  87. */
  88. int db_set_table_description(dbTable * table, const char *description)
  89. {
  90. return db_set_string(&table->description, description);
  91. }
  92. /*!
  93. \brief Get the description of the table
  94. \param table pointer to dbTable
  95. \return description of the table
  96. */
  97. const char *db_get_table_description(dbTable * table)
  98. {
  99. return db_get_string(&table->description);
  100. }
  101. /*!
  102. \brief Return the number of columns of the table
  103. \param table pointer to dbTable
  104. \return number of columns
  105. */
  106. int db_get_table_number_of_columns(dbTable * table)
  107. {
  108. return table->numColumns;
  109. }
  110. static void set_all_column_privs(dbTable * table, void (*set_column_priv) ())
  111. {
  112. int col, ncols;
  113. dbColumn *column;
  114. ncols = db_get_table_number_of_columns(table);
  115. for (col = 0; col < ncols; col++) {
  116. column = db_get_table_column(table, col);
  117. set_column_priv(column);
  118. }
  119. }
  120. static int get_all_column_privs(dbTable * table, int (*get_column_priv) ())
  121. {
  122. int priv, col, ncols;
  123. dbColumn *column;
  124. ncols = db_get_table_number_of_columns(table);
  125. for (col = 0; col < ncols; col++) {
  126. column = db_get_table_column(table, col);
  127. priv = get_column_priv(column);
  128. if (priv != DB_GRANTED)
  129. return priv;
  130. }
  131. return DB_GRANTED;
  132. }
  133. /*!
  134. \brief Grant selection privileges for all columns
  135. \param table pointer to dbTable
  136. */
  137. void db_set_table_select_priv_granted(dbTable * table)
  138. {
  139. set_all_column_privs(table, db_set_column_select_priv_granted);
  140. }
  141. /*!
  142. \brief Set selection privileges not granted for all columns
  143. \param table pointer to dbTable
  144. */
  145. void db_set_table_select_priv_not_granted(dbTable * table)
  146. {
  147. set_all_column_privs(table, db_set_column_select_priv_not_granted);
  148. }
  149. /*!
  150. \brief Get table select privileges
  151. \param table pointer to dbTable
  152. \return privilages
  153. */
  154. int db_get_table_select_priv(dbTable * table)
  155. {
  156. return get_all_column_privs(table, db_get_column_select_priv);
  157. }
  158. /*!
  159. \brief Grant update privileges for all columns
  160. \param table pointer to dbTable
  161. */
  162. void db_set_table_update_priv_granted(dbTable * table)
  163. {
  164. set_all_column_privs(table, db_set_column_update_priv_granted);
  165. }
  166. /*!
  167. \brief Set update privileges not granted for all columns
  168. \param table pointer to dbTable
  169. */
  170. void db_set_table_update_priv_not_granted(dbTable * table)
  171. {
  172. set_all_column_privs(table, db_set_column_update_priv_not_granted);
  173. }
  174. /*!
  175. \brief Get table update privileges
  176. \param table pointer to dbTable
  177. \return privilages
  178. */
  179. int db_get_table_update_priv(dbTable * table)
  180. {
  181. return get_all_column_privs(table, db_get_column_update_priv);
  182. }
  183. /*!
  184. \brief Grant insert privileges for table
  185. \param table pointer to dbTable
  186. */
  187. void db_set_table_insert_priv_granted(dbTable * table)
  188. {
  189. table->priv_insert = DB_GRANTED;
  190. }
  191. /*!
  192. \brief Set insert privileges not granted for table
  193. \param table pointer to dbTable
  194. */
  195. void db_set_table_insert_priv_not_granted(dbTable * table)
  196. {
  197. table->priv_insert = DB_NOT_GRANTED;
  198. }
  199. /*!
  200. \brief Get table insert privileges
  201. \param table pointer to dbTable
  202. \return prilileges
  203. */
  204. int db_get_table_insert_priv(dbTable * table)
  205. {
  206. return table->priv_insert;
  207. }
  208. /*!
  209. \brief Grant delete privileges for table
  210. \param table pointer to dbTable
  211. */
  212. void db_set_table_delete_priv_granted(dbTable * table)
  213. {
  214. table->priv_delete = DB_GRANTED;
  215. }
  216. /*!
  217. \brief Set delete privileges not granted for table
  218. \param table pointer to dbTable
  219. */
  220. void db_set_table_delete_priv_not_granted(dbTable * table)
  221. {
  222. table->priv_delete = DB_NOT_GRANTED;
  223. }
  224. /*!
  225. \brief Get table delete privileges
  226. \param table pointer to dbTable
  227. \return privileges
  228. */
  229. int db_get_table_delete_priv(dbTable * table)
  230. {
  231. return table->priv_delete;
  232. }
  233. /*!
  234. \brief Returns column structure for given table and column number
  235. \param table pointer to dbTable
  236. \param idx column index (starting with '0')
  237. \return pointer to dbColumn
  238. \return NULL if not found
  239. */
  240. dbColumn *db_get_table_column(dbTable * table, int idx)
  241. {
  242. if (idx < 0 || idx >= table->numColumns)
  243. return ((dbColumn *) NULL);
  244. return &table->columns[idx];
  245. }
  246. /*!
  247. \brief Returns column structure for given table and column name
  248. \param table pointer to dbTable
  249. \param name the name of the column
  250. \return pointer to dbColumn
  251. \return NULL if not found
  252. */
  253. dbColumn *db_get_table_column_by_name(dbTable * table, const char* name)
  254. {
  255. dbColumn *c = NULL;
  256. int i, columns = table->numColumns;
  257. for(i = 0; i < columns; i++ ) {
  258. c = db_get_table_column(table, i);
  259. if(c == NULL)
  260. return c;
  261. if(strcmp(name, db_get_string(&c->columnName)) == 0)
  262. break;
  263. c = NULL;
  264. }
  265. return c;
  266. }
  267. /*!
  268. \brief Set a specific column for given table and column number
  269. \param table Pointer to dbTable
  270. \param idx Column index (starting with '0'). The index must be in range.
  271. \param column Pointer to a dbColumn to insert.
  272. A copy of the column stored, so the original column can be deleted.
  273. \return DB_OK on success
  274. \return DB_FAILURE on error
  275. */
  276. int db_set_table_column(dbTable * table, int idx, dbColumn *column)
  277. {
  278. if (idx < 0 || idx >= table->numColumns)
  279. return DB_FAILED;
  280. db_copy_column(&table->columns[idx], column);
  281. return DB_OK;
  282. }
  283. /*!
  284. \brief Append a specific column to given table
  285. \param table Pointer to dbTable
  286. \param column Pointer to a dbColumn to append.
  287. A copy of the column is stored, so the original column can be deleted.
  288. \return DB_OK on success
  289. \return DB_FAILURE on error
  290. */
  291. int db_append_table_column(dbTable * table, dbColumn *column)
  292. {
  293. table->columns = (dbColumn*)db_realloc((void*)table->columns, sizeof(dbColumn)*(table->numColumns + 1));
  294. if(table->columns == NULL)
  295. return DB_FAILED;
  296. db_copy_column(&table->columns[table->numColumns], column);
  297. table->numColumns++;
  298. return DB_OK;
  299. }
  300. /*!
  301. \brief Make a new exact copy of an existing table
  302. New memory is allocated for the clone, the columns-content will be copied too.
  303. \param src Pointer to dbTable
  304. \return A new alloacted clone of the given table on success
  305. \return NULL on error
  306. */
  307. dbTable *db_clone_table(dbTable *src)
  308. {
  309. int i, n = db_get_table_number_of_columns(src);
  310. dbTable *new = db_alloc_table(n);
  311. if(new == NULL)
  312. return (new = NULL);
  313. db_copy_string(&new->description, &src->description);
  314. db_copy_string(&new->tableName, &src->tableName);
  315. /* Deep copy the columns */
  316. for(i = 0; i < n; i++)
  317. {
  318. db_copy_column(&new->columns[i], &src->columns[i]);
  319. }
  320. new->numColumns = n;
  321. new->priv_delete = src->priv_delete;
  322. new->priv_insert = src->priv_insert;
  323. return new;
  324. }
  325. /*!
  326. \brief Create SQL CREATE sring from table definition
  327. \param table pointer to dbTable
  328. \param sql dbString to store the SQL CREATE string
  329. \return DB_OK on success
  330. \return DB_FAILED on error
  331. */
  332. int db_table_to_sql(dbTable * table, dbString * sql)
  333. {
  334. int col, ncols;
  335. dbColumn *column;
  336. const char *colname;
  337. int sqltype, ctype;
  338. char buf[500];
  339. db_set_string(sql, "create table ");
  340. db_append_string(sql, db_get_table_name(table));
  341. db_append_string(sql, " ( ");
  342. ncols = db_get_table_number_of_columns(table);
  343. for (col = 0; col < ncols; col++) {
  344. column = db_get_table_column(table, col);
  345. colname = db_get_column_name(column);
  346. sqltype = db_get_column_sqltype(column);
  347. ctype = db_sqltype_to_Ctype(sqltype);
  348. G_debug(3, "%s (%s)", colname, db_sqltype_name(sqltype));
  349. if (col > 0)
  350. db_append_string(sql, ", ");
  351. db_append_string(sql, colname);
  352. db_append_string(sql, " ");
  353. /* Note: I found on Web:
  354. * These are the ANSI data types: BIT, CHARACTER, DATE, DECIMAL, DOUBLE PRECISION, FLOAT,
  355. * INTEGER, INTERVAL, NUMERIC, REAL, SMALLINT, TIMESTAMP, TIME, VARBIT, VARCHAR, CHAR
  356. * ...
  357. * Thus, the only data types you can use with the assurance that they will
  358. * work everywhere are as follows:
  359. * DOUBLE PRECISION, FLOAT, INTEGER, NUMERIC, REAL, SMALLINT, VARCHAR, CHAR */
  360. switch (sqltype) {
  361. case DB_SQL_TYPE_CHARACTER:
  362. sprintf(buf, "varchar(%d)", db_get_column_length(column));
  363. db_append_string(sql, buf);
  364. break;
  365. case DB_SQL_TYPE_TEXT:
  366. G_warning("Type TEXT converted to 'VARCHAR(250)'");
  367. db_append_string(sql, "varchar(250)");
  368. break;
  369. case DB_SQL_TYPE_SMALLINT:
  370. case DB_SQL_TYPE_INTEGER:
  371. db_append_string(sql, "integer");
  372. break;
  373. case DB_SQL_TYPE_REAL:
  374. case DB_SQL_TYPE_DOUBLE_PRECISION:
  375. case DB_SQL_TYPE_DECIMAL:
  376. case DB_SQL_TYPE_NUMERIC:
  377. case DB_SQL_TYPE_INTERVAL:
  378. db_append_string(sql, "double precision");
  379. break;
  380. case DB_SQL_TYPE_DATE:
  381. db_append_string(sql, "date");
  382. break;
  383. case DB_SQL_TYPE_TIME:
  384. db_append_string(sql, "time");
  385. break;
  386. case DB_SQL_TYPE_TIMESTAMP:
  387. db_append_string(sql, "datetime");
  388. break;
  389. default:
  390. G_warning("Unknown column type (%s)", colname);
  391. return DB_FAILED;
  392. }
  393. }
  394. db_append_string(sql, " )");
  395. G_debug(3, "sql statement: %s", db_get_string(sql));
  396. return DB_OK;
  397. }