diff --git a/src/common.cpp b/src/common.cpp index 4248b64..b78badc 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -29,9 +29,6 @@ int gopt_verbose = 0; //! check processed output matches the output file bool gopt_check_output = false; -//! globally selected SQL database type and file -std::string gopt_db_connection; - //! global SQL datbase connection handle SqlDatabase* g_db = NULL; @@ -40,11 +37,11 @@ SqlDatabase* g_db = NULL; #include "sqlite.h" //! initialize global SQL database connection -bool g_db_initialize() +bool g_db_connect(const std::string& db_conninfo) { g_db_free(); - if (gopt_db_connection.size() == 0) + if (db_conninfo.size() == 0) { #if HAVE_POSTGRESQL //! first try to connect to a PostgreSQL database @@ -73,7 +70,7 @@ bool g_db_initialize() } else { - std::string sqlname = gopt_db_connection; + std::string sqlname = db_conninfo; std::string dbname; std::string::size_type colonpos = sqlname.find(':'); diff --git a/src/common.h b/src/common.h index 5db8847..c56054f 100644 --- a/src/common.h +++ b/src/common.h @@ -35,14 +35,11 @@ extern int gopt_verbose; //! check processed output matches the output file extern bool gopt_check_output; -//! globally selected SQL database type and file -extern std::string gopt_db_connection; - //! global SQL database connection handle extern SqlDatabase* g_db; //! initialize global SQL database connection -extern bool g_db_initialize(); +extern bool g_db_connect(const std::string& db_conninfo); //! free global SQL database connection extern void g_db_free(); diff --git a/src/gnuplot.cpp b/src/gnuplot.cpp index 5be40f9..658fb2a 100644 --- a/src/gnuplot.cpp +++ b/src/gnuplot.cpp @@ -69,6 +69,9 @@ class SpGnuplot //! Process # IMPORT-DATA commands int importdata(size_t ln, size_t indent, const std::string& cmdline); + //! Process # CONNECT commands + bool connect(size_t ln, size_t indent, const std::string& cmdline); + //! Struct to rewrite Gnuplot "plot" directives with new datafile/index pairs struct Dataset { @@ -121,6 +124,12 @@ int SpGnuplot::importdata(size_t /* ln */, size_t /* indent */, const std::strin return ImportData(true).main(args.size(), argv); } +//! Process # CONNECT commands +bool SpGnuplot::connect(size_t /* ln */, size_t /* indent */, const std::string& cmdline) +{ + return g_db_connect(cmdline); +} + //! Helper to rewrite Gnuplot "plot" directives with new datafile/index pairs void SpGnuplot::plot_rewrite(size_t ln, size_t indent, const std::vector& datasets, @@ -421,7 +430,13 @@ int SpGnuplot::process() else if (first_word == "IMPORT-DATA") { OUT(ln << "# " << cmd); - if(importdata(ln, indent, cmd) != EXIT_SUCCESS) + if (importdata(ln, indent, cmd) != EXIT_SUCCESS) + return EXIT_FAILURE; + } + else if (first_word == "CONNECT") + { + OUT(ln << "# " << cmd); + if (!connect(ln, indent, cmd.substr(space_pos+1))) return EXIT_FAILURE; } else if (first_word == "PLOT") @@ -492,8 +507,8 @@ SpGnuplot::SpGnuplot(const std::string& filename, TextLines& lines) } // process lines in place - if(process() != EXIT_SUCCESS) - exit(EXIT_FAILURE); + if (process() != EXIT_SUCCESS) + exit(EXIT_FAILURE); // verify processed output against file if (gopt_check_output) diff --git a/src/importdata.cpp b/src/importdata.cpp index 261d93b..8c33e5d 100644 --- a/src/importdata.cpp +++ b/src/importdata.cpp @@ -376,6 +376,9 @@ int ImportData::main(int argc, char* argv[]) { FieldSet::check_detect(); + // database connection to establish + std::string opt_db_conninfo; + //! parse command line parameters using SimpleOpt CSimpleOpt args(argc, argv, sopt_list); @@ -424,7 +427,7 @@ int ImportData::main(int argc, char* argv[]) break; case OPT_DATABASE: - gopt_db_connection = args.OptionArg(); + opt_db_conninfo = args.OptionArg(); break; } } @@ -441,7 +444,7 @@ int ImportData::main(int argc, char* argv[]) bool opt_dbconnect = false; if (!g_db) { - if (!g_db_initialize()) + if (!g_db_connect(opt_db_conninfo)) OUT_THROW("Fatal: could not connect to a SQL database"); opt_dbconnect = true; } diff --git a/src/latex.cpp b/src/latex.cpp index a20e7b9..5053745 100644 --- a/src/latex.cpp +++ b/src/latex.cpp @@ -74,7 +74,10 @@ class SpLatex void sql(size_t ln, size_t indent, const std::string& cmdline); //! Process % IMPORT-DATA commands - void importdata(size_t ln, size_t indent, const std::string& cmdline); + int importdata(size_t ln, size_t indent, const std::string& cmdline); + + //! Process % CONNECT command + bool connect(size_t ln, size_t indent, const std::string& cmdline); //! Process % TEXTTABLE commands void texttable(size_t ln, size_t indent, const std::string& cmdline); @@ -117,7 +120,7 @@ void SpLatex::sql(size_t /* ln */, size_t /* indent */, const std::string& cmdli } //! Process % IMPORT-DATA commands -void SpLatex::importdata(size_t /* ln */, size_t /* indent */, const std::string& cmdline) +int SpLatex::importdata(size_t /* ln */, size_t /* indent */, const std::string& cmdline) { // split argument at whitespaces std::vector args = split_ws(cmdline); @@ -130,7 +133,13 @@ void SpLatex::importdata(size_t /* ln */, size_t /* indent */, const std::string argv[args.size()] = NULL; - ImportData(true).main(args.size(), argv); + return ImportData(true).main(args.size(), argv); +} + +//! Process % CONNECT command +bool SpLatex::connect(size_t /* ln */, size_t /* indent */, const std::string& cmdline) +{ + return g_db_connect(cmdline); } //! Process % TEXTTABLE commands @@ -480,6 +489,12 @@ SpLatex::SpLatex(TextLines& lines) OUT(ln << " % " << cmd); importdata(ln, indent, cmd); } + else if (first_word == "CONNECT") + { + OUT(ln << " % " << cmd); + if (!connect(ln, indent, cmd.substr(space_pos+1))) + OUT_THROW("Database connection lost."); + } else if (first_word == "TEXTTABLE") { OUT(ln << " % " << cmd); diff --git a/src/sp-process.cpp b/src/sp-process.cpp index dec1698..1a6294e 100644 --- a/src/sp-process.cpp +++ b/src/sp-process.cpp @@ -123,9 +123,12 @@ sp_process_usage(const std::string& progname) static inline int sp_process(int argc, char* argv[]) { - //! output file name + // output file name std::string opt_outputfile; + // database connection to establish + std::string opt_db_conninfo; + //! parse command line parameters using SimpleOpt CSimpleOpt args(argc, argv, sopt_list); @@ -158,13 +161,13 @@ sp_process(int argc, char* argv[]) break; case OPT_DATABASE: - gopt_db_connection = args.OptionArg(); + opt_db_conninfo = args.OptionArg(); break; } } // make connection to the database - if (!g_db_initialize()) + if (!g_db_connect(opt_db_conninfo)) OUT_THROW("Fatal: could not connect to a SQL database"); // open output file or string stream diff --git a/src/sqlite.cpp b/src/sqlite.cpp index adc1975..2e4cc80 100644 --- a/src/sqlite.cpp +++ b/src/sqlite.cpp @@ -215,6 +215,7 @@ bool SQLiteDatabase::initialize(const std::string& params) { OUT("Connection to SQLite3 failed: " << sqlite3_errmsg(m_db)); sqlite3_close(m_db); + m_db = NULL; return false; } @@ -227,7 +228,9 @@ bool SQLiteDatabase::initialize(const std::string& params) //! destructor to free connection SQLiteDatabase::~SQLiteDatabase() { - sqlite3_close(m_db); + if (m_db) { + sqlite3_close(m_db); + } } //! return type of SQL database diff --git a/tests/latex/tabular2.out b/tests/latex/tabular2.out new file mode 100644 index 0000000..e783f31 --- /dev/null +++ b/tests/latex/tabular2.out @@ -0,0 +1,129 @@ +line1 +% CONNECT sqlite:test-loaded.db +line2 +% TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth LIMIT 10 +1152 & 20004857425 & 399 & 1024 \\ +1152 & 21256634665 & 376 & 1024 \\ +2176 & 20059162540 & 398 & 2048 \\ +2176 & 21206797610 & 377 & 2048 \\ +3200 & 20101648154 & 397 & 3072 \\ +3200 & 21256759869 & 376 & 3072 \\ +4224 & 20078735758 & 398 & 4096 \\ +4224 & 21256469275 & 376 & 4096 \\ +6272 & 20103335011 & 397 & 6144 \\ +6272 & 21254940419 & 376 & 6144 \\ +% END TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), are... +line3 +indentation start + % TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth + 1152 & 20004857425 & 399 & 1024 \\ line1 + 1152 & 21256634665 & 376 & 1024 \\[40pt] + 2176 & 20059162540 & 398 & 2048 \\[1pt] + 2176 & 21206797610 & 377 & 2048 \\ line4 + 3200 & 20101648154 & 397 & 3072 \\ line5 + 3200 & 21256759869 & 376 & 3072 \\ line6 + 4224 & 20078735758 & 398 & 4096 \\ + 4224 & 21256469275 & 376 & 4096 \\ + 6272 & 20103335011 & 397 & 6144 \\ + 6272 & 21254940419 & 376 & 6144 \\ + 8320 & 19844289277 & 403 & 8192 \\ + 8320 & 21033549756 & 380 & 8192 \\ + 12416 & 19934071922 & 401 & 12288 \\ + 12416 & 21105216385 & 379 & 12288 \\ + 16512 & 19970353548 & 400 & 16384 \\ + 16512 & 21142325436 & 378 & 16384 \\ + 20608 & 19997700036 & 400 & 20480 \\ + 20608 & 21164943036 & 377 & 20480 \\ + 24704 & 20017094792 & 399 & 24576 \\ + 24704 & 21177968064 & 377 & 24576 \\ + 28800 & 20024361016 & 399 & 28672 \\ + 28800 & 21188091979 & 377 & 28672 \\ + 32896 & 19588722596 & 408 & 32768 \\ + 32896 & 20167090530 & 396 & 32768 \\ + 41088 & 13352542574 & 599 & 40960 \\ + 41088 & 15434595485 & 518 & 40960 \\ + 49280 & 13352656249 & 599 & 49152 \\ + 49280 & 15445744156 & 517 & 49152 \\ + 65664 & 13353218261 & 599 & 65536 \\ + 65664 & 15470586592 & 517 & 65536 \\ + 98432 & 13353616903 & 599 & 98304 \\ + 98432 & 15476290580 & 516 & 98304 \\ + 131200 & 13353488133 & 599 & 131072 \\ + 131200 & 15478200720 & 516 & 131072 \\ + 196736 & 13353703915 & 599 & 196608 \\ + 196736 & 15452726013 & 517 & 196608 \\ + 262272 & 13353783611 & 599 & 262144 \\ + 262272 & 15403930682 & 519 & 262144 \\ + 393344 & 13354375997 & 599 & 393216 \\ + 393344 & 15424185515 & 518 & 393216 \\ + 524416 & 13355357557 & 599 & 524288 \\ + 524416 & 15423989946 & 518 & 524288 \\ + 786560 & 13357098956 & 598 & 786432 \\ + 786560 & 15426535975 & 518 & 786432 \\ + 1048704 & 13322300787 & 600 & 1048576 \\ + 1048704 & 15416610336 & 518 & 1048576 \\ + 1310848 & 12599911132 & 634 & 1310720 \\ + 1310848 & 15270216963 & 523 & 1310720 \\ + 1572992 & 12597385271 & 635 & 1572864 \\ + 1572992 & 15269613288 & 523 & 1572864 \\ + 1835136 & 12593497036 & 635 & 1835008 \\ + 1835136 & 15269091481 & 523 & 1835008 \\ + 2097280 & 11814282322 & 677 & 2097152 \\ + 2097280 & 14380475033 & 556 & 2097152 \\ + 2359424 & 11732158553 & 681 & 2359296 \\ + 2359424 & 14310057139 & 559 & 2359296 \\ + 2621568 & 11151421728 & 717 & 2621440 \\ + 2621568 & 13667203078 & 585 & 2621440 \\ + 2883712 & 9635555389 & 830 & 2883584 \\ + 2883712 & 11857582908 & 674 & 2883584 \\ + 3145856 & 8830491065 & 905 & 3145728 \\ + 3145856 & 10975456216 & 728 & 3145728 \\ + 4194432 & 5703033760 & 1402 & 4194304 \\ + 4194432 & 7462653658 & 1072 & 4194304 \\ + 5243008 & 3764860746 & 2124 & 5242880 \\ + 5243008 & 5239989138 & 1526 & 5242880 \\ + 6291584 & 2970821306 & 2692 & 6291456 \\ + 6291584 & 4381387103 & 1825 & 6291456 \\ + 7340160 & 2625465946 & 3047 & 7340032 \\ + 7340160 & 4033162958 & 1983 & 7340032 \\ + 8388736 & 2448731494 & 3266 & 8388608 \\ + 8388736 & 3847558417 & 2079 & 8388608 \\ + 9437312 & 2352617641 & 3400 & 9437184 \\ + 9437312 & 3743372332 & 2137 & 9437184 \\ + 10485888 & 2288649712 & 3495 & 10485760 \\ + 10485888 & 3666709518 & 2181 & 10485760 \\ + 12583040 & 2222953585 & 3598 & 12582912 \\ + 12583040 & 3581563981 & 2233 & 12582912 \\ + 14680192 & 2195411712 & 3643 & 14680064 \\ + 14680192 & 3543382742 & 2257 & 14680064 \\ + 16777344 & 2180601210 & 3668 & 16777216 \\ + 16777344 & 3522084255 & 2271 & 16777216 \\ + 20971648 & 2170401128 & 3685 & 20971520 \\ + 20971648 & 3507422082 & 2280 & 20971520 \\ + 25165952 & 2168353166 & 3689 & 25165824 \\ + 25165952 & 3503128913 & 2283 & 25165824 \\ + 29360256 & 2167406475 & 3691 & 29360128 \\ + 29360256 & 3502572094 & 2284 & 29360128 \\ + 33554560 & 2167236950 & 3691 & 33554432 \\ + 33554560 & 3501908751 & 2284 & 33554432 \\ + 67108992 & 2167069057 & 3691 & 67108864 \\ + 67108992 & 3502477870 & 2284 & 67108864 \\ + 134217856 & 2167397762 & 3691 & 134217728 \\ + 134217856 & 3502216012 & 2284 & 134217728 \\ + 268435584 & 2167170987 & 3691 & 268435456 \\ + 268435584 & 3502080053 & 2284 & 268435456 \\ + 536871040 & 2167207622 & 3691 & 536870912 \\ + 536871040 & 3502124096 & 2284 & 536870912 \\ + 1073741952 & 2167288244 & 3691 & 1073741824 \\ + 1073741952 & 3501543006 & 2284 & 1073741824 \\ + 2147483776 & 2167167763 & 3691 & 2147483648 \\ + 2147483776 & 3501652699 & 2284 & 2147483648 \\ + 4294967424 & 2167332378 & 3691 & 4294967296 \\ + 4294967424 & 3501536124 & 2284 & 4294967296 \\ + 8589934720 & 2167234766 & 3691 & 8589934592 \\ + 8589934720 & 3501106948 & 2284 & 8589934592 \\ + 17179869312 & 2166899399 & 3691 & 17179869184 \\ + 17179869312 & 3500085763 & 2285 & 17179869184 \\ + % END TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), are... +indentation end +line4 diff --git a/tests/latex/tabular2.tex b/tests/latex/tabular2.tex new file mode 100644 index 0000000..c0b0109 --- /dev/null +++ b/tests/latex/tabular2.tex @@ -0,0 +1,20 @@ +line1 +% CONNECT sqlite:test-loaded.db +line2 +% TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth LIMIT 10 +line3 +indentation start + % TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth + 10.1699250014423124 & 21256634665.4957 & 3.7635308344391e-10 & 1024 \\ line1 + 10.1699250014423124 & 20004857425.2276 & 3.999028750843e-10 & 1024 \\[40pt] + 11.0874628412503394 & 20059162540.3994 & 3.98820239074683e-10 & 2048 \\[1pt] + 11.0874628412503394 & 21206797610.8078 & 3.7723753236193e-10 & 2048 \\ line4 + 11.6438561897747247 & 20101648154.6247 & 3.97977317007187e-10 & 3072 \\ line5 + 11.6438561897747247 & 21256759869.173 & 3.76350866700139e-10 & 3072 \\ line6 + 12.0443941193584534 & 20078735758.2602 & 3.9843145984472e-10 & 4096 \\ + 12.0443941193584534 & 21256469275.0109 & 3.76356011739203e-10 & 4096 \\ + 12.6147098441152082 & 21254940419.1323 & 3.76383082814898e-10 & 6144 \\ + 12.6147098441152082 & 20103335011.5598 & 3.97943923005803e-10 & 6144 \\ + % END TABULAR SELECT testsize AS x, bandwidth, rate, areasize FROM test ORDER BY x ... +indentation end +line4 \ No newline at end of file diff --git a/tests/latex/test-loaded.db b/tests/latex/test-loaded.db new file mode 100644 index 0000000..b9dadbe Binary files /dev/null and b/tests/latex/test-loaded.db differ