table.c 11 KB

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