main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /****************************************************************************
  2. *
  3. * MODULE: db.select
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Huidae Cho <grass4u gmail.com>,
  6. * Glynn Clements <glynn gclements.plus.com>,
  7. * Jachym Cepicky <jachym les-ejk.cz>,
  8. * Markus Neteler <neteler itc.it>
  9. * Stephan Holl
  10. * PURPOSE: Process one sql select statement
  11. * COPYRIGHT: (C) 2002-2010, 2013 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <grass/gis.h>
  22. #include <grass/dbmi.h>
  23. #include <grass/glocale.h>
  24. #include "local_proto.h"
  25. struct {
  26. char *driver, *database, *table, *sql, *fs, *vs, *nv, *input, *output;
  27. int c, d, h, test_only;
  28. } parms;
  29. /* function prototypes */
  30. static void parse_command_line(int, char **);
  31. static int sel(dbDriver *, dbString *);
  32. static int get_stmt(FILE *, dbString *);
  33. static int stmt_is_empty(dbString *);
  34. int main(int argc, char **argv)
  35. {
  36. dbString stmt;
  37. dbDriver *driver;
  38. dbHandle handle;
  39. int stat;
  40. FILE *fd;
  41. parse_command_line(argc, argv);
  42. /* read from file or stdin ? */
  43. if (parms.input && strcmp(parms.input, "-") != 0) {
  44. fd = fopen(parms.input, "r");
  45. if (fd == NULL) {
  46. G_fatal_error(_("Unable to open file <%s>: %s"),
  47. parms.input, strerror(errno));
  48. }
  49. }
  50. else
  51. fd = stdin;
  52. /* open DB connection */
  53. db_init_string(&stmt);
  54. driver = db_start_driver(parms.driver);
  55. if (driver == NULL) {
  56. G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
  57. }
  58. db_init_handle(&handle);
  59. db_set_handle(&handle, parms.database, NULL);
  60. if (db_open_database(driver, &handle) != DB_OK)
  61. G_fatal_error(_("Unable to open database <%s>"), parms.database);
  62. /* check for sql, table, and input */
  63. if (parms.sql) {
  64. /* parms.sql */
  65. db_set_string(&stmt, parms.sql);
  66. stat = sel(driver, &stmt);
  67. }
  68. else if (parms.table) {
  69. /* parms.table */
  70. db_set_string(&stmt, "SELECT * FROM ");
  71. db_append_string(&stmt, parms.table);
  72. stat = sel(driver, &stmt);
  73. }
  74. else { /* -> parms.input */
  75. stat = DB_OK;
  76. while (stat == DB_OK && get_stmt(fd, &stmt)) {
  77. if (!stmt_is_empty(&stmt))
  78. stat = sel(driver, &stmt);
  79. }
  80. }
  81. if(parms.test_only)
  82. G_verbose_message(_("Test %s."), stat ? _("failed") : _("succeeded"));
  83. db_close_database(driver);
  84. db_shutdown_driver(driver);
  85. exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE);
  86. }
  87. int sel(dbDriver * driver, dbString * stmt)
  88. {
  89. dbCursor cursor;
  90. dbTable *table;
  91. dbColumn *column;
  92. dbValue *value;
  93. dbString value_string;
  94. int col, ncols;
  95. int more;
  96. if (db_open_select_cursor(driver, stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
  97. return DB_FAILED;
  98. if (parms.test_only)
  99. return DB_OK;
  100. table = db_get_cursor_table(&cursor);
  101. ncols = db_get_table_number_of_columns(table);
  102. if (parms.d) {
  103. for (col = 0; col < ncols; col++) {
  104. column = db_get_table_column(table, col);
  105. print_column_definition(column);
  106. }
  107. return DB_OK;
  108. }
  109. if (parms.output && strcmp(parms.output, "-") != 0) {
  110. if (NULL == freopen(parms.output, "w", stdout)) {
  111. G_fatal_error(_("Unable to open file <%s> for writing"), parms.output);
  112. }
  113. }
  114. db_init_string(&value_string);
  115. /* column names if horizontal output */
  116. if (parms.h && parms.c) {
  117. for (col = 0; col < ncols; col++) {
  118. column = db_get_table_column(table, col);
  119. if (col)
  120. fprintf(stdout, "%s", parms.fs);
  121. fprintf(stdout, "%s", db_get_column_name(column));
  122. }
  123. fprintf(stdout, "\n");
  124. }
  125. /* fetch the data */
  126. while (TRUE) {
  127. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
  128. return DB_FAILED;
  129. if (!more)
  130. break;
  131. for (col = 0; col < ncols; col++) {
  132. column = db_get_table_column(table, col);
  133. value = db_get_column_value(column);
  134. db_convert_column_value_to_string(column, &value_string);
  135. if (parms.c && !parms.h)
  136. fprintf(stdout, "%s%s", db_get_column_name(column), parms.fs);
  137. if (col && parms.h)
  138. fprintf(stdout, "%s", parms.fs);
  139. if (parms.nv && db_test_value_isnull(value))
  140. fprintf(stdout, "%s", parms.nv);
  141. else
  142. fprintf(stdout, "%s", db_get_string(&value_string));
  143. if (!parms.h)
  144. fprintf(stdout, "\n");
  145. }
  146. if (parms.h)
  147. fprintf(stdout, "\n");
  148. else if (parms.vs)
  149. fprintf(stdout, "%s\n", parms.vs);
  150. }
  151. return DB_OK;
  152. }
  153. void parse_command_line(int argc, char **argv)
  154. {
  155. struct Option *driver, *database, *table, *sql,
  156. *fs, *vs, *nv, *input, *output;
  157. struct Flag *c, *d, *v, *flag_test;
  158. struct GModule *module;
  159. const char *drv, *db;
  160. /* Initialize the GIS calls */
  161. G_gisinit(argv[0]);
  162. sql = G_define_standard_option(G_OPT_DB_SQL);
  163. sql->guisection = _("Query");
  164. input = G_define_standard_option(G_OPT_F_INPUT);
  165. input->required = NO;
  166. input->label = _("Name of file containing SQL select statement(s)");
  167. input->description = _("'-' for standard input");
  168. input->guisection = _("Query");
  169. table = G_define_standard_option(G_OPT_DB_TABLE);
  170. table->description = _("Name of table to query");
  171. table->guisection = _("Query");
  172. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  173. driver->options = db_list_drivers();
  174. if ((drv = db_get_default_driver_name()))
  175. driver->answer = (char *) drv;
  176. driver->guisection = _("Connection");
  177. database = G_define_standard_option(G_OPT_DB_DATABASE);
  178. if ((db = db_get_default_database_name()))
  179. database->answer = (char *) db;
  180. database->guisection = _("Connection");
  181. fs = G_define_standard_option(G_OPT_F_SEP);
  182. fs->guisection = _("Format");
  183. vs = G_define_standard_option(G_OPT_F_SEP);
  184. vs->key = "vseparator";
  185. vs->label = _("Vertical record separator (requires -v flag)");
  186. vs->answer = NULL;
  187. vs->guisection = _("Format");
  188. nv = G_define_option();
  189. nv->key = "nv";
  190. nv->type = TYPE_STRING;
  191. nv->required = NO;
  192. nv->description = _("Null value indicator");
  193. nv->guisection = _("Format");
  194. output = G_define_standard_option(G_OPT_F_OUTPUT);
  195. output->required = NO;
  196. output->description =
  197. _("Name for output file (if omitted or \"-\" output to stdout)");
  198. c = G_define_flag();
  199. c->key = 'c';
  200. c->description = _("Do not include column names in output");
  201. c->guisection = _("Format");
  202. d = G_define_flag();
  203. d->key = 'd';
  204. d->description = _("Describe query only (don't run it)");
  205. d->guisection = _("Query");
  206. v = G_define_flag();
  207. v->key = 'v';
  208. v->description = _("Vertical output (instead of horizontal)");
  209. v->guisection = _("Format");
  210. flag_test = G_define_flag();
  211. flag_test->key = 't';
  212. flag_test->description = _("Only test query, do not execute");
  213. flag_test->guisection = _("Query");
  214. /* Set description */
  215. module = G_define_module();
  216. G_add_keyword(_("database"));
  217. G_add_keyword(_("attribute table"));
  218. G_add_keyword(_("SQL"));
  219. module->label = _("Selects data from attribute table.");
  220. module->description = _("Performs SQL query statement(s).");
  221. if (G_parser(argc, argv))
  222. exit(EXIT_FAILURE);
  223. parms.driver = driver->answer;
  224. parms.database = database->answer;
  225. parms.table = table->answer;
  226. parms.sql = sql->answer;
  227. parms.fs = G_option_to_separator(fs);
  228. parms.vs = '\0';
  229. if (vs->answer)
  230. parms.vs = G_option_to_separator(vs);
  231. parms.nv = nv->answer;
  232. parms.input = input->answer;
  233. parms.output = output->answer;
  234. if (!c->answer)
  235. parms.c = TRUE;
  236. else
  237. parms.c = FALSE;
  238. parms.d = d->answer;
  239. if (!v->answer)
  240. parms.h = TRUE;
  241. else
  242. parms.h = FALSE;
  243. parms.test_only = flag_test->answer;
  244. if (parms.input && *parms.input == 0) {
  245. G_usage();
  246. exit(EXIT_FAILURE);
  247. }
  248. if (!parms.input && !parms.sql && !parms.table)
  249. G_fatal_error(_("You must provide one of these options: <%s>, <%s>, or <%s>"),
  250. sql->key, input->key, table->key);
  251. }
  252. int get_stmt(FILE * fd, dbString * stmt)
  253. {
  254. char buf[DB_SQL_MAX], buf2[DB_SQL_MAX];
  255. size_t len;
  256. db_zero_string(stmt);
  257. if (G_getl2(buf, sizeof(buf), fd) == 0)
  258. return 0;
  259. strcpy(buf2, buf);
  260. G_chop(buf2);
  261. len = strlen(buf2);
  262. if (buf2[len - 1] == ';') { /* end of statement */
  263. buf2[len - 1] = 0; /* truncate ';' */
  264. }
  265. db_set_string(stmt, buf);
  266. return 1;
  267. }
  268. int stmt_is_empty(dbString * stmt)
  269. {
  270. char dummy[2];
  271. return (sscanf(db_get_string(stmt), "%1s", dummy) != 1);
  272. }