int main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; // this block provides a command line api via `command_mode` if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } // if we get here, then we are in interactive mode.... // start with an announcement message announce(); // initialize the db. Using 0 here means open an exiting db (actually it // will create one, but it won't delete old data for you). To clear the // dbs and initialize a new one, use cmd line mode with -i. if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } // loop over an interactive console menu // the semantics are kind of similar to the ch6 menu, where there's // an "active" cd, and the operations you can do affect that cd while(current_option != mo_exit) { current_option = get_menu_choice(¤t_cdc_entry); switch(current_option) { case mo_add_cat: // add a new catalog item. Lots of error checking here. if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { // print message, but also make sure to zero out data fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: case mo_invalid: default: // in all three cases, do nothing break; } } // close db and exit database_close(); exit(EXIT_SUCCESS); } // end of main
void main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } /* We're now ready to process user input. We sit in a loop, asking for a menu choice and processing it, until the user selects the exit option. We pass the current_cdc_entry structure to the show_menu function. We do this to allow the menu choices to change if a catalog entry is currently selected. */ while(current_option != mo_exit) { current_option = show_menu(¤t_cdc_entry); switch(current_option) { case mo_add_cat: if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } /* switch */ } /* while */ /* When the main loop exits, we close the database and exit back to the environment. The welcoming sentence is printed by the announce function. */ database_close(); exit(EXIT_SUCCESS); } /* main */
int query(struct Map_info *Map) { int i, j, idx, cat_no, nlines, type; register int line_num; struct line_pnts *Points; struct line_cats *Cats; struct field_info *Fi; dbString stmt, value_string; dbDriver *driver; /* Initialize the Point struct */ Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); G_message(_("Reading features...")); /* Cycle through all lines and make a list of categories of 'qfield' for each category given by 'field' */ nlines = Vect_get_num_lines(Map); for (line_num = 1; line_num <= nlines; line_num++) { type = Vect_read_line(Map, Points, Cats, line_num); if (!(type & options.type)) continue; for (i = 0; i < Cats->n_cats; i++) { if (Cats->field[i] == options.field) { cat_no = Cats->cat[i]; idx = find_cat(cat_no, 1); for (j = 0; j < Cats->n_cats; j++) { if (Cats->field[j] == options.qfield) { /* Add to list */ if (Values[idx].nqcats == Values[idx].aqcats) { /* Alloc space */ Values[idx].aqcats += 2; Values[idx].qcat = (int *)G_realloc(Values[idx].qcat, Values[idx].aqcats * sizeof(int)); } Values[idx].qcat[Values[idx].nqcats] = Cats->cat[j]; Values[idx].nqcats++; } } } } /* If there is no field cat add cat -1, values for cat -1 are reported at the end */ Vect_cat_get(Cats, options.field, &cat_no); if (cat_no == -1) { idx = find_cat(cat_no, 1); for (j = 0; j < Cats->n_cats; j++) { if (Cats->field[j] == options.qfield) { /* Add to list */ if (Values[idx].nqcats == Values[idx].aqcats) { /* Alloc space */ Values[idx].aqcats += 2; Values[idx].qcat = (int *)G_realloc(Values[idx].qcat, Values[idx].aqcats * sizeof(int)); } Values[idx].qcat[Values[idx].nqcats] = Cats->cat[j]; Values[idx].nqcats++; } } } G_percent(line_num, nlines, 2); } db_init_string(&stmt); db_init_string(&value_string); if ((Fi = Vect_get_field(Map, options.qfield)) == NULL) G_fatal_error(_("Database connection not defined for layer %d. Use v.db.connect first."), options.qfield); /* Open driver */ driver = db_start_driver_open_database(Fi->driver, Fi->database); if (driver == NULL) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); /* Query the database for each category */ G_message(_("Querying database... ")); for (i = 0; i < vstat.rcat; i++) { int j, ctype, nrows, more; char buf[2000]; dbCursor cursor; dbTable *table; dbColumn *column; dbValue *value; G_debug(3, "cat %d", Values[i].cat); G_percent(i + 1, vstat.rcat, 1); /* Skip if cat is zero and large number of query categories (many features without category). * It would cause problems on server side and take long time. Postgres limit is 10000 */ /* TODO: verify because no category is encoded as cat = -1, not cat = zero */ if (Values[i].cat == 0 && Values[i].nqcats > 1000) { G_warning(_("Query for category '0' (no category) was not executed because of too many " "(%d) query categories. All later reported values for cat 0 are not valid."), Values[i].nqcats); continue; } if (Values[i].nqcats > 0) { sprintf(buf, "SELECT %s FROM %s WHERE", options.qcol, Fi->table); db_set_string(&stmt, buf); for (j = 0; j < Values[i].nqcats; j++) { G_debug(4, " qcat %d", Values[i].qcat[j]); if (j > 0) db_append_string(&stmt, " OR"); sprintf(buf, " %s = %d", Fi->key, Values[i].qcat[j]); db_append_string(&stmt, buf); } G_debug(4, " SQL: '%s'", db_get_string(&stmt)); if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK) G_fatal_error("Cannot open cursor: '%s'", db_get_string(&stmt)); table = db_get_cursor_table(&cursor); column = db_get_table_column(table, 0); /* first column */ value = db_get_column_value(column); ctype = db_sqltype_to_Ctype(db_get_column_sqltype(column)); vstat.qtype = ctype; nrows = db_get_num_rows(&cursor); G_debug(4, " nrows = %d, columnt type = %d", nrows, ctype); if (nrows != 1) { if (nrows > 1) { G_warning(_("Multiple query results, output value set to NULL (category [%d])"), Values[i].cat); } Values[i].null = 1; } else { if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) G_fatal_error(_("Unable to fetch record")); db_convert_column_value_to_string(column, &stmt); G_debug(4, " value = %s", db_get_string(&stmt)); if (db_test_value_isnull(value)) { Values[i].null = 1; } else { switch (ctype) { case (DB_C_TYPE_INT): Values[i].i1 = db_get_value_int(value); break; case (DB_C_TYPE_DOUBLE): Values[i].d1 = db_get_value_double(value); break; case (DB_C_TYPE_STRING): Values[i].str1 = G_store(db_get_value_string(value)); break; case (DB_C_TYPE_DATETIME): db_convert_column_value_to_string(column, &value_string); Values[i].str1 = G_store(db_get_string(&value_string)); } Values[i].null = 0; } } db_close_cursor(&cursor); } else { /* no qcats -> upload NULL */ Values[i].null = 1; } } db_close_database_shutdown_driver(driver); return 0; }
int main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); // if there's an arg, switch to command mode and exit if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } // print a greeting, and initialize the database (it must already exist) announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } /* loop over menu choices, calling show_menu every time. * the behavior of show_menu depends on whether a track is currently * selected (which is determined by the var `current_cdc_entry`) */ while(current_option != mo_exit) { current_option = show_menu(¤t_cdc_entry); switch(current_option) { case mo_add_cat: if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } /* switch */ } /* while */ // when the loop exits, clean up and quit database_close(); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); if (argc > 1) { command_result = command_mode(argc, argv); exit(command_result); } announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); } while (current_option != mo_exit) { current_option = show_menu(¤t_cdc_entry); switch (current_option) { case mo_add_cat: if (enter_new_cat_entry(¤t_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(¤t_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(¤t_cdc_entry); break; case mo_del_cat: del_cat_entry(¤t_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(¤t_cdc_entry); break; case mo_del_tracks: del_track_entries(¤t_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } } database_close(); return 0; }