Skip to content

Commit 0a39570

Browse files
committed
Adding CONNECT keyword to change database connection during processing.
1 parent f16b320 commit 0a39570

File tree

10 files changed

+204
-22
lines changed

10 files changed

+204
-22
lines changed

src/common.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ int gopt_verbose = 0;
2929
//! check processed output matches the output file
3030
bool gopt_check_output = false;
3131

32-
//! globally selected SQL database type and file
33-
std::string gopt_db_connection;
34-
3532
//! global SQL datbase connection handle
3633
SqlDatabase* g_db = NULL;
3734

@@ -40,11 +37,11 @@ SqlDatabase* g_db = NULL;
4037
#include "sqlite.h"
4138

4239
//! initialize global SQL database connection
43-
bool g_db_initialize()
40+
bool g_db_connect(const std::string& db_conninfo)
4441
{
4542
g_db_free();
4643

47-
if (gopt_db_connection.size() == 0)
44+
if (db_conninfo.size() == 0)
4845
{
4946
#if HAVE_POSTGRESQL
5047
//! first try to connect to a PostgreSQL database
@@ -73,7 +70,7 @@ bool g_db_initialize()
7370
}
7471
else
7572
{
76-
std::string sqlname = gopt_db_connection;
73+
std::string sqlname = db_conninfo;
7774
std::string dbname;
7875

7976
std::string::size_type colonpos = sqlname.find(':');

src/common.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ extern int gopt_verbose;
3535
//! check processed output matches the output file
3636
extern bool gopt_check_output;
3737

38-
//! globally selected SQL database type and file
39-
extern std::string gopt_db_connection;
40-
4138
//! global SQL database connection handle
4239
extern SqlDatabase* g_db;
4340

4441
//! initialize global SQL database connection
45-
extern bool g_db_initialize();
42+
extern bool g_db_connect(const std::string& db_conninfo);
4643

4744
//! free global SQL database connection
4845
extern void g_db_free();

src/gnuplot.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class SpGnuplot
6969
//! Process # IMPORT-DATA commands
7070
int importdata(size_t ln, size_t indent, const std::string& cmdline);
7171

72+
//! Process # CONNECT commands
73+
bool connect(size_t ln, size_t indent, const std::string& cmdline);
74+
7275
//! Struct to rewrite Gnuplot "plot" directives with new datafile/index pairs
7376
struct Dataset
7477
{
@@ -121,6 +124,12 @@ int SpGnuplot::importdata(size_t /* ln */, size_t /* indent */, const std::strin
121124
return ImportData(true).main(args.size(), argv);
122125
}
123126

127+
//! Process # CONNECT commands
128+
bool SpGnuplot::connect(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
129+
{
130+
return g_db_connect(cmdline);
131+
}
132+
124133
//! Helper to rewrite Gnuplot "plot" directives with new datafile/index pairs
125134
void SpGnuplot::plot_rewrite(size_t ln, size_t indent,
126135
const std::vector<Dataset>& datasets,
@@ -421,7 +430,13 @@ int SpGnuplot::process()
421430
else if (first_word == "IMPORT-DATA")
422431
{
423432
OUT(ln << "# " << cmd);
424-
if(importdata(ln, indent, cmd) != EXIT_SUCCESS)
433+
if (importdata(ln, indent, cmd) != EXIT_SUCCESS)
434+
return EXIT_FAILURE;
435+
}
436+
else if (first_word == "CONNECT")
437+
{
438+
OUT(ln << "# " << cmd);
439+
if (!connect(ln, indent, cmd.substr(space_pos+1)))
425440
return EXIT_FAILURE;
426441
}
427442
else if (first_word == "PLOT")
@@ -492,8 +507,8 @@ SpGnuplot::SpGnuplot(const std::string& filename, TextLines& lines)
492507
}
493508

494509
// process lines in place
495-
if(process() != EXIT_SUCCESS)
496-
exit(EXIT_FAILURE);
510+
if (process() != EXIT_SUCCESS)
511+
exit(EXIT_FAILURE);
497512

498513
// verify processed output against file
499514
if (gopt_check_output)

src/importdata.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ int ImportData::main(int argc, char* argv[])
376376
{
377377
FieldSet::check_detect();
378378

379+
// database connection to establish
380+
std::string opt_db_conninfo;
381+
379382
//! parse command line parameters using SimpleOpt
380383
CSimpleOpt args(argc, argv, sopt_list);
381384

@@ -424,7 +427,7 @@ int ImportData::main(int argc, char* argv[])
424427
break;
425428

426429
case OPT_DATABASE:
427-
gopt_db_connection = args.OptionArg();
430+
opt_db_conninfo = args.OptionArg();
428431
break;
429432
}
430433
}
@@ -441,7 +444,7 @@ int ImportData::main(int argc, char* argv[])
441444
bool opt_dbconnect = false;
442445
if (!g_db)
443446
{
444-
if (!g_db_initialize())
447+
if (!g_db_connect(opt_db_conninfo))
445448
OUT_THROW("Fatal: could not connect to a SQL database");
446449
opt_dbconnect = true;
447450
}

src/latex.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class SpLatex
7474
void sql(size_t ln, size_t indent, const std::string& cmdline);
7575

7676
//! Process % IMPORT-DATA commands
77-
void importdata(size_t ln, size_t indent, const std::string& cmdline);
77+
int importdata(size_t ln, size_t indent, const std::string& cmdline);
78+
79+
//! Process % CONNECT command
80+
bool connect(size_t ln, size_t indent, const std::string& cmdline);
7881

7982
//! Process % TEXTTABLE commands
8083
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
117120
}
118121

119122
//! Process % IMPORT-DATA commands
120-
void SpLatex::importdata(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
123+
int SpLatex::importdata(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
121124
{
122125
// split argument at whitespaces
123126
std::vector<std::string> args = split_ws(cmdline);
@@ -130,7 +133,13 @@ void SpLatex::importdata(size_t /* ln */, size_t /* indent */, const std::string
130133

131134
argv[args.size()] = NULL;
132135

133-
ImportData(true).main(args.size(), argv);
136+
return ImportData(true).main(args.size(), argv);
137+
}
138+
139+
//! Process % CONNECT command
140+
bool SpLatex::connect(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
141+
{
142+
return g_db_connect(cmdline);
134143
}
135144

136145
//! Process % TEXTTABLE commands
@@ -480,6 +489,12 @@ SpLatex::SpLatex(TextLines& lines)
480489
OUT(ln << " % " << cmd);
481490
importdata(ln, indent, cmd);
482491
}
492+
else if (first_word == "CONNECT")
493+
{
494+
OUT(ln << " % " << cmd);
495+
if (!connect(ln, indent, cmd.substr(space_pos+1)))
496+
OUT_THROW("Database connection lost.");
497+
}
483498
else if (first_word == "TEXTTABLE")
484499
{
485500
OUT(ln << " % " << cmd);

src/sp-process.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ sp_process_usage(const std::string& progname)
123123
static inline int
124124
sp_process(int argc, char* argv[])
125125
{
126-
//! output file name
126+
// output file name
127127
std::string opt_outputfile;
128128

129+
// database connection to establish
130+
std::string opt_db_conninfo;
131+
129132
//! parse command line parameters using SimpleOpt
130133
CSimpleOpt args(argc, argv, sopt_list);
131134

@@ -158,13 +161,13 @@ sp_process(int argc, char* argv[])
158161
break;
159162

160163
case OPT_DATABASE:
161-
gopt_db_connection = args.OptionArg();
164+
opt_db_conninfo = args.OptionArg();
162165
break;
163166
}
164167
}
165168

166169
// make connection to the database
167-
if (!g_db_initialize())
170+
if (!g_db_connect(opt_db_conninfo))
168171
OUT_THROW("Fatal: could not connect to a SQL database");
169172

170173
// open output file or string stream

src/sqlite.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ bool SQLiteDatabase::initialize(const std::string& params)
215215
{
216216
OUT("Connection to SQLite3 failed: " << sqlite3_errmsg(m_db));
217217
sqlite3_close(m_db);
218+
m_db = NULL;
218219
return false;
219220
}
220221

@@ -227,7 +228,9 @@ bool SQLiteDatabase::initialize(const std::string& params)
227228
//! destructor to free connection
228229
SQLiteDatabase::~SQLiteDatabase()
229230
{
230-
sqlite3_close(m_db);
231+
if (m_db) {
232+
sqlite3_close(m_db);
233+
}
231234
}
232235

233236
//! return type of SQL database

tests/latex/tabular2.out

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
line1
2+
% CONNECT sqlite:test-loaded.db
3+
line2
4+
% TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth LIMIT 10
5+
1152 & 20004857425 & 399 & 1024 \\
6+
1152 & 21256634665 & 376 & 1024 \\
7+
2176 & 20059162540 & 398 & 2048 \\
8+
2176 & 21206797610 & 377 & 2048 \\
9+
3200 & 20101648154 & 397 & 3072 \\
10+
3200 & 21256759869 & 376 & 3072 \\
11+
4224 & 20078735758 & 398 & 4096 \\
12+
4224 & 21256469275 & 376 & 4096 \\
13+
6272 & 20103335011 & 397 & 6144 \\
14+
6272 & 21254940419 & 376 & 6144 \\
15+
% END TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), are...
16+
line3
17+
indentation start
18+
% TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth
19+
1152 & 20004857425 & 399 & 1024 \\ line1
20+
1152 & 21256634665 & 376 & 1024 \\[40pt]
21+
2176 & 20059162540 & 398 & 2048 \\[1pt]
22+
2176 & 21206797610 & 377 & 2048 \\ line4
23+
3200 & 20101648154 & 397 & 3072 \\ line5
24+
3200 & 21256759869 & 376 & 3072 \\ line6
25+
4224 & 20078735758 & 398 & 4096 \\
26+
4224 & 21256469275 & 376 & 4096 \\
27+
6272 & 20103335011 & 397 & 6144 \\
28+
6272 & 21254940419 & 376 & 6144 \\
29+
8320 & 19844289277 & 403 & 8192 \\
30+
8320 & 21033549756 & 380 & 8192 \\
31+
12416 & 19934071922 & 401 & 12288 \\
32+
12416 & 21105216385 & 379 & 12288 \\
33+
16512 & 19970353548 & 400 & 16384 \\
34+
16512 & 21142325436 & 378 & 16384 \\
35+
20608 & 19997700036 & 400 & 20480 \\
36+
20608 & 21164943036 & 377 & 20480 \\
37+
24704 & 20017094792 & 399 & 24576 \\
38+
24704 & 21177968064 & 377 & 24576 \\
39+
28800 & 20024361016 & 399 & 28672 \\
40+
28800 & 21188091979 & 377 & 28672 \\
41+
32896 & 19588722596 & 408 & 32768 \\
42+
32896 & 20167090530 & 396 & 32768 \\
43+
41088 & 13352542574 & 599 & 40960 \\
44+
41088 & 15434595485 & 518 & 40960 \\
45+
49280 & 13352656249 & 599 & 49152 \\
46+
49280 & 15445744156 & 517 & 49152 \\
47+
65664 & 13353218261 & 599 & 65536 \\
48+
65664 & 15470586592 & 517 & 65536 \\
49+
98432 & 13353616903 & 599 & 98304 \\
50+
98432 & 15476290580 & 516 & 98304 \\
51+
131200 & 13353488133 & 599 & 131072 \\
52+
131200 & 15478200720 & 516 & 131072 \\
53+
196736 & 13353703915 & 599 & 196608 \\
54+
196736 & 15452726013 & 517 & 196608 \\
55+
262272 & 13353783611 & 599 & 262144 \\
56+
262272 & 15403930682 & 519 & 262144 \\
57+
393344 & 13354375997 & 599 & 393216 \\
58+
393344 & 15424185515 & 518 & 393216 \\
59+
524416 & 13355357557 & 599 & 524288 \\
60+
524416 & 15423989946 & 518 & 524288 \\
61+
786560 & 13357098956 & 598 & 786432 \\
62+
786560 & 15426535975 & 518 & 786432 \\
63+
1048704 & 13322300787 & 600 & 1048576 \\
64+
1048704 & 15416610336 & 518 & 1048576 \\
65+
1310848 & 12599911132 & 634 & 1310720 \\
66+
1310848 & 15270216963 & 523 & 1310720 \\
67+
1572992 & 12597385271 & 635 & 1572864 \\
68+
1572992 & 15269613288 & 523 & 1572864 \\
69+
1835136 & 12593497036 & 635 & 1835008 \\
70+
1835136 & 15269091481 & 523 & 1835008 \\
71+
2097280 & 11814282322 & 677 & 2097152 \\
72+
2097280 & 14380475033 & 556 & 2097152 \\
73+
2359424 & 11732158553 & 681 & 2359296 \\
74+
2359424 & 14310057139 & 559 & 2359296 \\
75+
2621568 & 11151421728 & 717 & 2621440 \\
76+
2621568 & 13667203078 & 585 & 2621440 \\
77+
2883712 & 9635555389 & 830 & 2883584 \\
78+
2883712 & 11857582908 & 674 & 2883584 \\
79+
3145856 & 8830491065 & 905 & 3145728 \\
80+
3145856 & 10975456216 & 728 & 3145728 \\
81+
4194432 & 5703033760 & 1402 & 4194304 \\
82+
4194432 & 7462653658 & 1072 & 4194304 \\
83+
5243008 & 3764860746 & 2124 & 5242880 \\
84+
5243008 & 5239989138 & 1526 & 5242880 \\
85+
6291584 & 2970821306 & 2692 & 6291456 \\
86+
6291584 & 4381387103 & 1825 & 6291456 \\
87+
7340160 & 2625465946 & 3047 & 7340032 \\
88+
7340160 & 4033162958 & 1983 & 7340032 \\
89+
8388736 & 2448731494 & 3266 & 8388608 \\
90+
8388736 & 3847558417 & 2079 & 8388608 \\
91+
9437312 & 2352617641 & 3400 & 9437184 \\
92+
9437312 & 3743372332 & 2137 & 9437184 \\
93+
10485888 & 2288649712 & 3495 & 10485760 \\
94+
10485888 & 3666709518 & 2181 & 10485760 \\
95+
12583040 & 2222953585 & 3598 & 12582912 \\
96+
12583040 & 3581563981 & 2233 & 12582912 \\
97+
14680192 & 2195411712 & 3643 & 14680064 \\
98+
14680192 & 3543382742 & 2257 & 14680064 \\
99+
16777344 & 2180601210 & 3668 & 16777216 \\
100+
16777344 & 3522084255 & 2271 & 16777216 \\
101+
20971648 & 2170401128 & 3685 & 20971520 \\
102+
20971648 & 3507422082 & 2280 & 20971520 \\
103+
25165952 & 2168353166 & 3689 & 25165824 \\
104+
25165952 & 3503128913 & 2283 & 25165824 \\
105+
29360256 & 2167406475 & 3691 & 29360128 \\
106+
29360256 & 3502572094 & 2284 & 29360128 \\
107+
33554560 & 2167236950 & 3691 & 33554432 \\
108+
33554560 & 3501908751 & 2284 & 33554432 \\
109+
67108992 & 2167069057 & 3691 & 67108864 \\
110+
67108992 & 3502477870 & 2284 & 67108864 \\
111+
134217856 & 2167397762 & 3691 & 134217728 \\
112+
134217856 & 3502216012 & 2284 & 134217728 \\
113+
268435584 & 2167170987 & 3691 & 268435456 \\
114+
268435584 & 3502080053 & 2284 & 268435456 \\
115+
536871040 & 2167207622 & 3691 & 536870912 \\
116+
536871040 & 3502124096 & 2284 & 536870912 \\
117+
1073741952 & 2167288244 & 3691 & 1073741824 \\
118+
1073741952 & 3501543006 & 2284 & 1073741824 \\
119+
2147483776 & 2167167763 & 3691 & 2147483648 \\
120+
2147483776 & 3501652699 & 2284 & 2147483648 \\
121+
4294967424 & 2167332378 & 3691 & 4294967296 \\
122+
4294967424 & 3501536124 & 2284 & 4294967296 \\
123+
8589934720 & 2167234766 & 3691 & 8589934592 \\
124+
8589934720 & 3501106948 & 2284 & 8589934592 \\
125+
17179869312 & 2166899399 & 3691 & 17179869184 \\
126+
17179869312 & 3500085763 & 2285 & 17179869184 \\
127+
% END TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), are...
128+
indentation end
129+
line4

tests/latex/tabular2.tex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
line1
2+
% CONNECT sqlite:test-loaded.db
3+
line2
4+
% TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth LIMIT 10
5+
line3
6+
indentation start
7+
% TABULAR SELECT testsize AS x, FLOOR(bandwidth), FLOOR(rate * 1e12), areasize FROM test ORDER BY x, bandwidth
8+
10.1699250014423124 & 21256634665.4957 & 3.7635308344391e-10 & 1024 \\ line1
9+
10.1699250014423124 & 20004857425.2276 & 3.999028750843e-10 & 1024 \\[40pt]
10+
11.0874628412503394 & 20059162540.3994 & 3.98820239074683e-10 & 2048 \\[1pt]
11+
11.0874628412503394 & 21206797610.8078 & 3.7723753236193e-10 & 2048 \\ line4
12+
11.6438561897747247 & 20101648154.6247 & 3.97977317007187e-10 & 3072 \\ line5
13+
11.6438561897747247 & 21256759869.173 & 3.76350866700139e-10 & 3072 \\ line6
14+
12.0443941193584534 & 20078735758.2602 & 3.9843145984472e-10 & 4096 \\
15+
12.0443941193584534 & 21256469275.0109 & 3.76356011739203e-10 & 4096 \\
16+
12.6147098441152082 & 21254940419.1323 & 3.76383082814898e-10 & 6144 \\
17+
12.6147098441152082 & 20103335011.5598 & 3.97943923005803e-10 & 6144 \\
18+
% END TABULAR SELECT testsize AS x, bandwidth, rate, areasize FROM test ORDER BY x ...
19+
indentation end
20+
line4

tests/latex/test-loaded.db

16 KB
Binary file not shown.

0 commit comments

Comments
 (0)