author | František Kučera <franta-hg@frantovo.cz> |
Thu, 29 Nov 2018 22:30:12 +0100 | |
branch | v_0 |
changeset 29 | 755978b0935c |
parent 28 | c9e03557c1e1 |
child 30 | ef99f5a6c9f6 |
permissions | -rw-r--r-- |
29
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
1 |
/** |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
2 |
* Relational pipes |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
4 |
* |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
8 |
* (at your option) any later version. |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
9 |
* |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
13 |
* GNU General Public License for more details. |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
14 |
* |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
755978b0935c
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
17 |
*/ |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#pragma once |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <string> |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <iostream> |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <vector> |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <relpipe/protocol/constants.h> |
26
019edca46769
AttributeMetadata: use smart pointer and avoid memory-leak
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
25 |
#include <memory> |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
#include "../include/relpipe/reader/typedefs.h" |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include "../include/relpipe/reader/RelationalReader.h" |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include "../include/relpipe/reader/TypeId.h" |
16 | 30 |
#include "../include/relpipe/reader/handlers/RelationalReaderBaseHandler.h" |
31 |
#include "../include/relpipe/reader/handlers/RelationalReaderStringHandler.h" |
|
32 |
#include "../include/relpipe/reader/handlers/RelationalReaderValueHandler.h" |
|
24
6f7acc3b274c
AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
33 |
#include "AttributeMetadataPrivate.h" |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
#include "DataTypeReaderBase.h" |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
#include "types/BooleanDataTypeReader.h" |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
#include "types/IntegerDataTypeReader.h" |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
#include "types/StringDataTypeReader.h" |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
namespace relpipe { |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
namespace reader { |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
using namespace relpipe::protocol; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
|
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
44 |
using StringHandler = relpipe::reader::handlers::RelationalReaderStringHadler; |
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
45 |
using ValuesHandler = relpipe::reader::handlers::RelationalReaderValueHadler; |
26
019edca46769
AttributeMetadata: use smart pointer and avoid memory-leak
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
46 |
using AttributeMetadata = relpipe::reader::handlers::AttributeMetadata; |
019edca46769
AttributeMetadata: use smart pointer and avoid memory-leak
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
47 |
using AttributeMetadataPrivate = relpipe::reader::handlers::AttributeMetadataPrivate; |
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
48 |
|
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
class StreamRelationalReader : public RelationalReader { |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
private: |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
std::istream &input; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
types::BooleanDataTypeReader booleanReader; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
types::IntegerDataTypeReader integerReader; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
types::StringDataTypeReader stringReader; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
std::vector<DataTypeReaderBase*> readers = {&booleanReader, &integerReader, &stringReader}; |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
56 |
|
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
57 |
std::vector<StringHandler*> stringHandlers; |
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
58 |
std::vector<ValuesHandler*> valuesHandlers; |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
/** |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
* count of columns in the current table |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
*/ |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
integer_t columnCount; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
/** |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
* number of column (0 = first) that will be written; after writing, the number is increased and prepared for next one |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
*/ |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
integer_t currentColumn; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
/** |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
* types of columns in the current table |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
*/ |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
std::vector<TypeId> columnTypes; |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
73 |
std::vector<string_t> columnNames; |
26
019edca46769
AttributeMetadata: use smart pointer and avoid memory-leak
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
74 |
std::vector<AttributeMetadata> columns; |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
75 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
76 |
/** |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
77 |
* TODO: remove? |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
78 |
*/ |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
79 |
string_t readString(std::istream &input, const TypeId typeId) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
80 |
for (DataTypeReaderBase* reader : readers) if (reader->supports(typeId)) return reader->readString(input); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
81 |
throw RelpipeReaderException(L"Unsupported data type: " + (int) typeId); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
82 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
83 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
84 |
void read(std::istream &input, std::function<void(const void *, const std::type_info&) > handler, const TypeId typeId) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
85 |
for (DataTypeReaderBase* reader : readers) if (reader->supports(typeId)) return reader->read(input, handler); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
86 |
throw RelpipeReaderException(L"Unsupported data type: " + (int) typeId); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
87 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
88 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
89 |
void read(std::istream &input, std::function<void(const string_t&, const void *, const std::type_info&) > handler, const TypeId typeId) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
90 |
for (DataTypeReaderBase* reader : readers) if (reader->supports(typeId)) return reader->read(input, handler); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
91 |
throw RelpipeReaderException(L"Unsupported data type: " + (int) typeId); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
92 |
} |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
|
19
3e1308e7606d
add endOfPipe() to handlers
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
94 |
void endOfPipe() { |
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
95 |
for (StringHandler* handler : stringHandlers) handler->endOfPipe(); |
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
96 |
for (ValuesHandler* handler : valuesHandlers) handler->endOfPipe(); |
19
3e1308e7606d
add endOfPipe() to handlers
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
97 |
} |
3e1308e7606d
add endOfPipe() to handlers
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
98 |
|
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
public: |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
StreamRelationalReader(std::istream &input) : |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
input(input) { |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
} |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
string_t toTypeCode(const TypeId typeId) override { |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
for (DataTypeReaderBase* reader : readers) if (reader->supports(typeId)) return reader->getTypeCode(); |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
throw RelpipeReaderException(L"Unsupported data type: " + static_cast<integer_t> (typeId)); |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
} |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
|
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
110 |
void addHandler(StringHandler* handler) override { |
17
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
111 |
stringHandlers.push_back(handler); |
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
112 |
} |
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
113 |
|
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
114 |
void addHandler(ValuesHandler* handler) override { |
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
115 |
valuesHandlers.push_back(handler); |
17
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
116 |
} |
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
117 |
|
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
118 |
void process() override { |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
119 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
120 |
while (true) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
121 |
integer_t dataPart; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
122 |
try { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
123 |
dataPart = integerReader.readValue(input); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
124 |
// output << "dataPart: " << dataPart << endl; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
125 |
} catch (RelpipeReaderException e) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
126 |
if (input.eof() && input.gcount() == 0) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
127 |
if (dataPart == DATA_PART_ROW) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
128 |
// last part was row |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
129 |
// input was fully read |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
130 |
// we are finished |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
131 |
// TODO: printCachedData(output); ??? |
19
3e1308e7606d
add endOfPipe() to handlers
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
132 |
endOfPipe(); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
133 |
return; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
134 |
} else if (dataPart == DATA_PART_START) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
135 |
// Empty relation might be weird but it is valid data. |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
136 |
// Actually, it is not so weird as it looks. |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
137 |
// fwprintf(stderr, L"Warning: The table has no rows. Weird… but OK.\n"); |
19
3e1308e7606d
add endOfPipe() to handlers
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
138 |
endOfPipe(); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
139 |
return; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
140 |
} else { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
141 |
// in current format, there is no other data part |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
142 |
// so this will never happen |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
143 |
// but maybe later… |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
144 |
throw RelpipeReaderException(L"Unexpected EOF"); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
145 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
146 |
} else if (input.eof()) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
147 |
fwprintf(stderr, L"Error: found some unexpected data on the input stream: %d\n", input.gcount()); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
148 |
throw e; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
149 |
} else { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
150 |
// other error |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
151 |
throw e; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
152 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
153 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
154 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
155 |
if (dataPart == DATA_PART_START) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
156 |
// Print data of previous table |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
157 |
// TODO: if (values.size() > 0) printCachedData(output); ??? |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
158 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
159 |
// Read table name |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
160 |
string_t tableName = stringReader.readValue(input); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
161 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
162 |
// Read column count |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
163 |
columnCount = integerReader.readValue(input); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
164 |
|
28
c9e03557c1e1
AttributeMetadata: call vector.clear() before vector.reserve(), fixes error (if there are multiple relations in the stream)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
165 |
columnTypes.clear(); |
c9e03557c1e1
AttributeMetadata: call vector.clear() before vector.reserve(), fixes error (if there are multiple relations in the stream)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
166 |
columnNames.clear(); |
c9e03557c1e1
AttributeMetadata: call vector.clear() before vector.reserve(), fixes error (if there are multiple relations in the stream)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
167 |
columns.clear(); |
25
fc0d05b72214
AttributeMetadata: use vector.reserve() instead of resize() and [i] and thus avoid unwanted AttributeMetadata() constructor call
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
168 |
columnTypes.reserve(columnCount); |
fc0d05b72214
AttributeMetadata: use vector.reserve() instead of resize() and [i] and thus avoid unwanted AttributeMetadata() constructor call
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
169 |
columnNames.reserve(columnCount); |
fc0d05b72214
AttributeMetadata: use vector.reserve() instead of resize() and [i] and thus avoid unwanted AttributeMetadata() constructor call
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
170 |
columns.reserve(columnCount); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
171 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
172 |
// Read column names |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
173 |
for (int i = 0; i < columnCount; i++) { |
25
fc0d05b72214
AttributeMetadata: use vector.reserve() instead of resize() and [i] and thus avoid unwanted AttributeMetadata() constructor call
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
174 |
columnNames.push_back(stringReader.readValue(input)); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
175 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
176 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
177 |
// Read column types |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
178 |
for (int i = 0; i < columnCount; i++) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
179 |
TypeId typeId = (TypeId) integerReader.readValue(input); // TODO: přetypování OK? |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
180 |
string_t typeCode = toTypeCode(typeId); // validate typeId TODO: je potřeba? |
25
fc0d05b72214
AttributeMetadata: use vector.reserve() instead of resize() and [i] and thus avoid unwanted AttributeMetadata() constructor call
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
181 |
columnTypes.push_back(typeId); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
182 |
|
22
e81019af67dc
use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
183 |
// put together names, type ids and type codes: |
26
019edca46769
AttributeMetadata: use smart pointer and avoid memory-leak
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
184 |
columns.push_back(std::shared_ptr<AttributeMetadataPrivate>(new AttributeMetadataPrivate({columnNames[i], columnTypes[i], typeCode}))); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
185 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
186 |
|
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
187 |
for (StringHandler* handler : stringHandlers) handler->startRelation(tableName, columns); |
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
188 |
for (ValuesHandler* handler : valuesHandlers) handler->startRelation(tableName, columns); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
189 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
190 |
} else if (dataPart == DATA_PART_ROW) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
191 |
for (int i = 0; i < columnCount; i++) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
192 |
TypeId typeId = columnTypes[i]; |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
193 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
194 |
if (stringHandlers.empty()) { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
195 |
read(input, [&](const void * rawValue, const std::type_info & typeInfo) { |
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
196 |
for (ValuesHandler* handler : valuesHandlers) handler->attribute(rawValue, typeInfo); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
197 |
}, typeId); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
198 |
} else { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
199 |
read(input, [&](const string_t& stringValue, const void * rawValue, const std::type_info & typeInfo) { |
21
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
200 |
for (StringHandler* handler : stringHandlers) handler->attribute(stringValue); |
abd3e7f26584
use for-each and type aliases
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
201 |
for (ValuesHandler* handler : valuesHandlers) handler->attribute(rawValue, typeInfo); |
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
202 |
}, typeId); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
203 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
204 |
} |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
205 |
|
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
206 |
} else { |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
207 |
throw RelpipeReaderException(L"Unknown data part"); |
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
208 |
} |
17
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
209 |
} |
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
210 |
|
18
e11f1ad20826
read using std::function
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
211 |
throw RelpipeReaderException(L"Unexpected exception"); // should never happen |
17
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
212 |
|
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
213 |
} |
ec750c536705
interface + dummy implementation
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
214 |
|
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
215 |
}; |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
216 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
217 |
} |
19
3e1308e7606d
add endOfPipe() to handlers
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
218 |
} |