author | František Kučera <franta-hg@frantovo.cz> |
Wed, 30 Oct 2019 16:46:13 +0100 | |
branch | v_0 |
changeset 21 | 0cfba89d5691 |
parent 19 | 44db9e1e7c7f |
child 34 | 24c05e69d68f |
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" |
|
39 |
||
40 |
using namespace relpipe::cli; |
|
41 |
using namespace relpipe::reader; |
|
42 |
using namespace relpipe::tr::sql; |
|
43 |
||
44 |
int main(int argc, char**argv) { |
|
45 |
setlocale(LC_ALL, ""); |
|
46 |
CLI::untieStdIO(); |
|
47 |
CLI cli(argc, argv); |
|
48 |
||
49 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
50 |
||
51 |
try { |
|
52 |
CLIParser cliParser; |
|
53 |
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
|
54 |
|
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 |
if (std::regex_match(cli.programName(), std::wregex(L"^(.*/)?relpipe-in-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
|
56 |
// relpipe-in-sql: |
19
44db9e1e7c7f
implicit pass-through of all relations in the relpipe-in-sql mode (if called with no arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
57 |
if (cli.arguments().size() == 0) configuration.copyRelations.push_back({L".*", L"", false}); |
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
|
58 |
std::shared_ptr<writer::RelationalWriter> writer(writer::Factory::create(std::cout)); |
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
59 |
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
|
60 |
configuration.sqlAfterRelational = nullptr; |
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
|
61 |
SqlHandler handler(writer.get(), configuration); |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
62 |
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
|
63 |
} 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
|
64 |
// 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
|
65 |
std::shared_ptr<reader::RelationalReader> reader(reader::Factory::create(std::cin)); |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
66 |
std::shared_ptr<writer::RelationalWriter> writer(writer::Factory::create(std::cout)); |
19580b27ade2
relpipe-in-sql mode: read .sqlite file and generate relational data
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
67 |
SqlHandler handler(writer.get(), configuration); |
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 |
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
|
69 |
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
|
70 |
} |
0 | 71 |
|
72 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
|
73 |
||
74 |
} catch (RelpipeCLIException& e) { |
|
75 |
fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str()); |
|
76 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
77 |
resultCode = e.getExitCode(); |
|
78 |
} catch (SqlException& e) { |
|
79 |
fwprintf(stderr, L"Caught SQL exception: %ls\n", e.getMessge().c_str()); |
|
80 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
81 |
resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
82 |
} catch (RelpipeReaderException& e) { |
|
83 |
fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str()); |
|
84 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
85 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
|
86 |
} |
|
87 |
||
88 |
return resultCode; |
|
89 |
} |