00001 /*****************************************************************************\ 00002 * * 00003 * Name : db_query using freetds db-lib * 00004 * Author : Chris Koeritz * 00005 * * 00006 ******************************************************************************* 00007 * Copyright (c) 2008-$now By Author. This program is free software; you can * 00008 * redistribute it and/or modify it under the terms of the GNU General Public * 00009 * License as published by the Free Software Foundation; either version 2 of * 00010 * the License or (at your option) any later version. This is online at: * 00011 * http://www.fsf.org/copyleft/gpl.html * 00012 * Please send any updates to: fred@gruntose.com * 00013 \*****************************************************************************/ 00014 00016 00017 #include <db_freetds/common.h> 00018 #include <db_freetds/query_handler.h> 00019 #include <db_freetds/login_info.h> 00020 #include <loggers/file_logger.h> 00021 #include <opsystem/application_base.h> 00022 #include <opsystem/path_configuration.h> 00023 00024 HOOPLE_STARTUP_CODE; 00025 00026 #define LOG(s) STAMPED_EMERGENCY_LOG(program_wide_logger(), s) 00027 00028 const char *DB_SECTION = "db_query"; 00029 00030 database_login_info _global_db_info(DB_SECTION); 00031 00032 // we use this to set up an error logger, but share the db engine log. 00033 #define SET_LOGGER { \ 00034 log_base *old_log = set_PW_logger_for_combo \ 00035 (path_configuration::make_logfile_name("db_engine.log"), \ 00036 file_logger::LOG_FILE_SIZE, true); \ 00037 WHACK(old_log); \ 00038 } 00039 00041 00042 int main(int argc, char **argv) 00043 { 00044 SET_LOGGER; 00045 00046 if (argc < 3) { 00047 printf("\ 00048 This program needs two parameters. The first is the configuration file\n\ 00049 where the database authentication parameters resides. The second parameter\n\ 00050 is the database query in SQL to run against the database.\n"); 00051 return 1; 00052 } 00053 const istring config_file = argv[1]; 00054 const istring query = argv[2]; 00055 00056 LOG("=== db_query starting up ==="); 00057 00058 query_handler *dbmang = new query_handler; 00059 // get the database open. 00060 bool db_open = dbmang->prepare_for_query(config_file, _global_db_info); 00061 if (!db_open) { 00062 LOG(istring("db_query ERROR: failed to open database ") + _global_db_info.database); 00063 return 1; 00064 } 00065 00066 console_logger out; // used for producing results. 00067 bool query_ok = dbmang->process_query(query.s(), out); 00068 00069 WHACK(dbmang); 00070 00071 LOG("=== db_query finished ==="); 00072 00073 return !query_ok; // reverse sense of bool for exit value. 00074 } 00075
1.5.1