cursor.c 7.9 KB

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