cursor.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*!
  2. \file db/dbmi_base/cursor.c
  3. \brief DBMI Library (base) - cursors management
  4. (C) 1999-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author Joel Jones (CERL/UIUC), Radim Blazek
  9. */
  10. #include <stdlib.h>
  11. #include <grass/dbmi.h>
  12. /*!
  13. \brief Initialize cursor
  14. \param cursor pointer to dbCursor
  15. */
  16. void db_init_cursor(dbCursor *cursor)
  17. {
  18. cursor->driver = NULL;
  19. cursor->token = -1;
  20. cursor->type = 0;
  21. cursor->mode = 0;
  22. cursor->table = NULL;
  23. cursor->column_flags = NULL;
  24. }
  25. /*!
  26. \brief Allocate table for cursor
  27. \param cursor pointer to dbCursor
  28. \param ncol number of column in table
  29. \return DB_OK on success
  30. \return error code on error
  31. */
  32. int db_alloc_cursor_table(dbCursor *cursor, int ncols)
  33. {
  34. cursor->table = db_alloc_table(ncols);
  35. if (cursor->table == NULL)
  36. return db_get_error_code();
  37. return DB_OK;
  38. }
  39. /*!
  40. \brief Free allocated dbCursor
  41. \param cursor pointer to dbCursor
  42. */
  43. void db_free_cursor(dbCursor *cursor)
  44. {
  45. if (cursor->table)
  46. db_free_table(cursor->table);
  47. if (cursor->column_flags)
  48. db_free_cursor_column_flags(cursor);
  49. db_init_cursor(cursor);
  50. }
  51. /*!
  52. \brief Get table allocated by cursor
  53. \param cursor pointer to dbCursor
  54. \return pointer to dbTable
  55. */
  56. dbTable *db_get_cursor_table(dbCursor *cursor)
  57. {
  58. return cursor->table;
  59. }
  60. /*!
  61. \brief Set table for given cursor
  62. \param cursor pointer to dbCursor
  63. \param table pointer to dbTable
  64. */
  65. void db_set_cursor_table(dbCursor *cursor, dbTable *table)
  66. {
  67. cursor->table = table;
  68. }
  69. /*!
  70. \brief Get cursor token
  71. \param cursor pointer to dbCursor
  72. \return pointer to dbToken
  73. */
  74. dbToken db_get_cursor_token(dbCursor *cursor)
  75. {
  76. return cursor->token;
  77. }
  78. /*!
  79. \brief Set cursor token
  80. \param cursor pointer to dbCursor
  81. \param token pointer to dbToken
  82. */
  83. void db_set_cursor_token(dbCursor *cursor, dbToken token)
  84. {
  85. cursor->token = token;
  86. }
  87. /*!
  88. \brief Set cursor to be read-only (select)
  89. \param cursor pointer to dbCursor
  90. */
  91. void db_set_cursor_type_readonly(dbCursor *cursor)
  92. {
  93. cursor->type = DB_READONLY;
  94. }
  95. /*!
  96. \brief Set cursor to be writeable (update)
  97. \param cursor pointer to dbCursor
  98. */
  99. void db_set_cursor_type_update(dbCursor *cursor)
  100. {
  101. cursor->type = DB_UPDATE;
  102. }
  103. /*!
  104. \brief Set cursor to be writeable (insert)
  105. \param cursor pointer to dbCursor
  106. */
  107. void db_set_cursor_type_insert(dbCursor *cursor)
  108. {
  109. cursor->type = DB_INSERT;
  110. }
  111. /*!
  112. \brief Check cursor type
  113. \param cursor pointer to dbCursor
  114. \return 1 for known cursor type
  115. \return 0 for unknown cursor type
  116. */
  117. int db_test_cursor_type_fetch(dbCursor *cursor)
  118. {
  119. return (cursor->type == DB_READONLY ||
  120. cursor->type == DB_UPDATE ||
  121. cursor->type == DB_INSERT);
  122. }
  123. /*!
  124. \brief Check if cursor type is 'update'
  125. \param cursor pointer to dbCursor
  126. \return 1 if cursor type is 'update'
  127. \return 0 otherwise
  128. */
  129. int db_test_cursor_type_update(dbCursor *cursor)
  130. {
  131. return (cursor->type == DB_UPDATE);
  132. }
  133. /*!
  134. \brief Check if cursor type is 'insert'
  135. \param cursor pointer to dbCursor
  136. \return 1 if cursor type is 'insert'
  137. \return 0 otherwise
  138. */
  139. int db_test_cursor_type_insert(dbCursor *cursor)
  140. {
  141. return (cursor->type == DB_INSERT);
  142. }
  143. /*!
  144. \brief Set cursor mode
  145. Modes:
  146. - DB_SCROLL
  147. - DB_INSENSITIVE
  148. \param cursor pointer to dbCursor
  149. \param mode cursor mode
  150. */
  151. void db_set_cursor_mode(dbCursor *cursor, int mode)
  152. {
  153. cursor->mode = mode;
  154. }
  155. /*!
  156. \brief Set 'scroll' cursor mode
  157. \param cursor pointer to dbCursor
  158. */
  159. void db_set_cursor_mode_scroll(dbCursor *cursor)
  160. {
  161. cursor->mode |= DB_SCROLL;
  162. }
  163. /*!
  164. \brief Unset 'scroll' cursor mode
  165. \param cursor pointer to dbCursor
  166. */
  167. void db_unset_cursor_mode_scroll(dbCursor *cursor)
  168. {
  169. cursor->mode &= ~DB_SCROLL;
  170. }
  171. /*!
  172. \brief Unset cursor mode
  173. \param cursor pointer to dbCursor
  174. */
  175. void db_unset_cursor_mode(dbCursor *cursor)
  176. {
  177. cursor->mode = 0;
  178. }
  179. /*!
  180. \brief Set 'intensive' cursor mode
  181. \param cursor pointer to dbCursor
  182. */
  183. void db_set_cursor_mode_insensitive(dbCursor *cursor)
  184. {
  185. cursor->mode |= DB_INSENSITIVE;
  186. }
  187. /*!
  188. \brief Unset 'intensive' cursor mode
  189. \param cursor pointer to dbCursor
  190. */
  191. void db_unset_cursor_mode_insensitive(dbCursor *cursor)
  192. {
  193. cursor->mode &= ~DB_INSENSITIVE;
  194. }
  195. /*!
  196. \brief Check if cursor mode is 'scroll'
  197. \param cursor pointer to dbCursor
  198. \return 1 if true
  199. \return 0 if false
  200. */
  201. int db_test_cursor_mode_scroll(dbCursor *cursor)
  202. {
  203. return (cursor->mode & DB_SCROLL);
  204. }
  205. /*!
  206. \brief Check if cursor mode is 'intensive'
  207. \param cursor pointer to dbCursor
  208. \return 1 if true
  209. \return 0 if false
  210. */
  211. int db_test_cursor_mode_insensitive(dbCursor *cursor)
  212. {
  213. return (cursor->mode & DB_INSENSITIVE);
  214. }
  215. /*!
  216. \brief Allocate columns' flags for cursor
  217. \param cursor pointer to dbCursor
  218. \return DB_OK on success
  219. \return error code on failure
  220. */
  221. int db_alloc_cursor_column_flags(dbCursor *cursor)
  222. {
  223. int ncols;
  224. int col;
  225. ncols = db_get_cursor_number_of_columns(cursor);
  226. cursor->column_flags = (short *)db_calloc(ncols, sizeof(short));
  227. if (cursor->column_flags == NULL)
  228. return db_get_error_code();
  229. for (col = 0; col < ncols; col++)
  230. db_unset_cursor_column_flag(cursor, col);
  231. return DB_OK;
  232. }
  233. /*!
  234. \brief Free columns' flags of cursor
  235. \param cursor pointer to dbCursor
  236. */
  237. void db_free_cursor_column_flags(dbCursor *cursor)
  238. {
  239. if (cursor->column_flags)
  240. free(cursor->column_flags);
  241. cursor->column_flags = NULL;
  242. }
  243. /*!
  244. \brief Set Column flag to 'update'
  245. \param cursor pointer to dbCursor
  246. \param col column index (starting with '0')
  247. */
  248. void db_set_cursor_column_for_update(dbCursor *cursor, int col)
  249. {
  250. db_set_cursor_column_flag(cursor, col);
  251. }
  252. /*!
  253. \brief Unset 'update' column flag
  254. \param cursor pointer to dbCursor
  255. \param col column index (starting with '0')
  256. */
  257. void db_unset_cursor_column_for_update(dbCursor *cursor, int col)
  258. {
  259. db_unset_cursor_column_flag(cursor, col);
  260. }
  261. /*!
  262. \brief Check if column flag is 'update'
  263. \param cursor pointer to dbCursor
  264. \param col column index (starting with '0')
  265. \return 1 if true
  266. \return 0 if false
  267. */
  268. int db_test_cursor_column_for_update(dbCursor *cursor, int col)
  269. {
  270. return db_test_cursor_column_flag(cursor, col);
  271. }
  272. /*!
  273. \brief Check if columns' flag is 'update'
  274. \param cursor pointer to dbCursor
  275. \return 1 if true
  276. \return 0 if false
  277. */
  278. int db_test_cursor_any_column_for_update(dbCursor *cursor)
  279. {
  280. return db_test_cursor_any_column_flag(cursor);
  281. }
  282. /*!
  283. \brief Set column's flag
  284. \param cursor pointer to dbCursor
  285. \param col column index (starting with '0')
  286. */
  287. void db_set_cursor_column_flag(dbCursor *cursor, int col)
  288. {
  289. if (cursor->column_flags)
  290. cursor->column_flags[col] = 1;
  291. }
  292. /*!
  293. \brief Unset column's flag
  294. \param cursor pointer to dbCursor
  295. \param col column index (starting with '0')
  296. */
  297. void db_unset_cursor_column_flag(dbCursor *cursor, int col)
  298. {
  299. if (cursor->column_flags)
  300. cursor->column_flags[col] = 0;
  301. }
  302. /*!
  303. \brief Checks column's flag
  304. \param cursor pointer to dbCursor
  305. \param col column index (starting with '0')
  306. \return 1 if flag is defined
  307. \return 0 otherwise
  308. */
  309. int db_test_cursor_column_flag(dbCursor *cursor, int col)
  310. {
  311. return cursor->column_flags && cursor->column_flags[col] ? 1 : 0;
  312. }
  313. /*!
  314. \brief Get number of columns
  315. \param cursor pointer to dbCursor
  316. */
  317. int db_get_cursor_number_of_columns(dbCursor *cursor)
  318. {
  319. dbTable *table;
  320. table = db_get_cursor_table(cursor);
  321. if (table)
  322. return db_get_table_number_of_columns(table);
  323. return 0;
  324. }
  325. /*!
  326. \brief Checks columns' flag
  327. Is any cursor column flag set?
  328. \param cursor pointer to dbCursor
  329. \return 1 if true
  330. \return 0 if false
  331. */
  332. int db_test_cursor_any_column_flag(dbCursor *cursor)
  333. {
  334. int ncols, col;
  335. ncols = db_get_cursor_number_of_columns(cursor);
  336. for (col = 0; col < ncols; col++)
  337. if (db_test_cursor_column_flag(cursor, col))
  338. return 1;
  339. return 0;
  340. }