author | František Kučera <franta-hg@frantovo.cz> |
Tue, 02 Jun 2020 23:31:55 +0200 | |
branch | v_0 |
changeset 44 | ec9694f3b343 |
parent 37 | 3de41719d7eb |
child 49 | 1d17192565bf |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
10
7da7173d84b0
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
7 |
* the Free Software Foundation, version 3. |
0 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
18 |
#include <cstdio> |
|
19 |
#include <cstdlib> |
|
20 |
#include <memory> |
|
21 |
#include <functional> |
|
13
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
22 |
#include <regex> |
0 | 23 |
|
24 |
#include <relpipe/cli/CLI.h> |
|
25 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
26 |
||
27 |
#include <relpipe/reader/Factory.h> |
|
28 |
#include <relpipe/reader/RelationalReader.h> |
|
29 |
#include <relpipe/reader/RelpipeReaderException.h> |
|
30 |
||
31 |
#include <relpipe/writer/RelationalWriter.h> |
|
32 |
#include <relpipe/writer/RelpipeWriterException.h> |
|
33 |
#include <relpipe/writer/Factory.h> |
|
34 |
#include <relpipe/writer/TypeId.h> |
|
35 |
||
36 |
#include "SqlHandler.h" |
|
37 |
#include "CLIParser.h" |
|
38 |
#include "Configuration.h" |
|
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
39 |
#include "DriverManager.h" |
0 | 40 |
|
41 |
using namespace relpipe::cli; |
|
42 |
using namespace relpipe::reader; |
|
43 |
using namespace relpipe::tr::sql; |
|
44 |
||
45 |
int main(int argc, char**argv) { |
|
46 |
setlocale(LC_ALL, ""); |
|
47 |
CLI::untieStdIO(); |
|
48 |
CLI cli(argc, argv); |
|
49 |
||
50 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
51 |
||
52 |
try { |
|
53 |
CLIParser cliParser; |
|
54 |
Configuration configuration = cliParser.parse(cli.arguments()); |
|
13
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
55 |
|
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
56 |
std::shared_ptr<DriverManager> driverManager = std::make_shared<DriverManager>(); |
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
57 |
std::shared_ptr<writer::RelationalWriter> writer(writer::Factory::create(std::cout)); |
36
91cb012d779a
use ODBC, avoid direct dependency on SQLite
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
58 |
|
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
59 |
if (configuration.listDataSources) { |
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
60 |
// --list-data-sources: |
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
61 |
SqlHandler::listDataSources(writer.get(), driverManager.get()); |
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
62 |
} else if (std::regex_match(cli.programName(), std::wregex(L"^(.*/)?relpipe-in-sql$"))) { |
13
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
63 |
// relpipe-in-sql: |
37
3de41719d7eb
add options --data-source-name and --data-source-url for custom datasource; drop options --file and --file-keep
František Kučera <franta-hg@frantovo.cz>
parents:
36
diff
changeset
|
64 |
if (configuration.statements.size() == 0 && configuration.copyRelations.size() == 0) configuration.copyRelations.push_back({L".*", L"", false}); |
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
65 |
configuration.sqlBeforeRelational = isatty(fileno(stdin)) ? nullptr : &std::wcin; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
66 |
configuration.sqlAfterRelational = nullptr; |
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
67 |
SqlHandler handler(writer.get(), driverManager.get(), configuration); |
13
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
68 |
handler.endOfPipe(); |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
69 |
} else { |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
70 |
// relpipe-tr-sql: |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
71 |
std::shared_ptr<reader::RelationalReader> reader(reader::Factory::create(std::cin)); |
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
72 |
SqlHandler handler(writer.get(), driverManager.get(), configuration); |
13
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
73 |
reader->addHandler(&handler); |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
74 |
reader->process(); |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
75 |
} |
0 | 76 |
|
77 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
|
78 |
||
79 |
} catch (RelpipeCLIException& e) { |
|
80 |
fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str()); |
|
81 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
82 |
resultCode = e.getExitCode(); |
|
83 |
} catch (SqlException& e) { |
|
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
84 |
fwprintf(stderr, L"Caught SQL exception: %ls\n", e.getMessage().c_str()); |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
85 |
for (SqlException::SqlDiagnosticsRecord dr : e.getDiagnostics()) { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
86 |
fwprintf(stderr, L"\tstate: %ls, code: %d, message: %ls\n", dr.sqlState.c_str(), dr.sqlCode, dr.message.c_str()); |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
87 |
} |
0 | 88 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
89 |
resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
90 |
} catch (RelpipeReaderException& e) { |
|
91 |
fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str()); |
|
92 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
93 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
|
94 |
} |
|
95 |
||
96 |
return resultCode; |
|
97 |
} |