|
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 |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #pragma once |
|
19 |
|
20 #include <string> |
|
21 #include <vector> |
|
22 #include <iostream> |
|
23 #include <sstream> |
|
24 #include <locale> |
|
25 #include <codecvt> |
|
26 #include <regex> |
|
27 #include <cassert> |
|
28 |
|
29 #include <relpipe/reader/typedefs.h> |
|
30 #include <relpipe/reader/TypeId.h> |
|
31 #include <relpipe/reader/handlers/RelationalReaderValueHandler.h> |
|
32 #include <relpipe/reader/handlers/AttributeMetadata.h> |
|
33 |
|
34 #include "ASN1Writer.h" |
|
35 |
|
36 namespace relpipe { |
|
37 namespace out { |
|
38 namespace asn1 { |
|
39 |
|
40 using namespace relpipe::reader; |
|
41 |
|
42 class ASN1Handler : public handlers::RelationalReaderValueHadler { |
|
43 private: |
|
44 shared_ptr<ASN1Writer> asn1Writer; |
|
45 std::vector<TypeId> columnTypes; |
|
46 std::vector<string_t> columnTypeCodes; |
|
47 std::vector<string_t> columnNames; |
|
48 integer_t valueCount = 0; |
|
49 integer_t columnCount = 0; |
|
50 integer_t relationCount = 0; |
|
51 |
|
52 public: |
|
53 |
|
54 ASN1Handler(std::ostream& output) : asn1Writer(new ASN1Writer(output)) { |
|
55 } |
|
56 |
|
57 void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override { |
|
58 valueCount = 0; |
|
59 columnCount = 0; |
|
60 |
|
61 if (relationCount == 0) { |
|
62 asn1Writer->writeStartSequence(); // root |
|
63 } else { |
|
64 asn1Writer->writeEndSequence(); |
|
65 asn1Writer->writeEndSequence(); |
|
66 } |
|
67 relationCount++; |
|
68 asn1Writer->writeStartSequence(); // relation |
|
69 |
|
70 asn1Writer->writeString(name); |
|
71 |
|
72 |
|
73 asn1Writer->writeStartSequence(); // relation metadata |
|
74 columnCount = attributes.size(); |
|
75 columnTypes.resize(columnCount); |
|
76 columnTypeCodes.resize(columnCount); |
|
77 columnNames.resize(columnCount); |
|
78 for (int i = 0; i < attributes.size(); i++) { |
|
79 columnNames[i] = attributes[i].getAttributeName(); |
|
80 columnTypes[i] = attributes[i].getTypeId(); |
|
81 columnTypeCodes[i] = attributes[i].getTypeName(); |
|
82 |
|
83 asn1Writer->writeStartSequence(); // attribute-metadata |
|
84 asn1Writer->writeString(columnNames[i]); |
|
85 asn1Writer->writeString(columnTypeCodes[i]); |
|
86 asn1Writer->writeEndSequence(); |
|
87 } |
|
88 asn1Writer->writeEndSequence(); |
|
89 |
|
90 } |
|
91 |
|
92 void attribute(const void* value, const std::type_info& type) override { |
|
93 integer_t i = valueCount % columnCount; |
|
94 |
|
95 if (i == 0 && valueCount) asn1Writer->writeEndSequence(); |
|
96 if (i == 0) asn1Writer->writeStartSequence(); |
|
97 |
|
98 valueCount++; |
|
99 |
|
100 switch (columnTypes[i]) { |
|
101 case TypeId::BOOLEAN: |
|
102 { |
|
103 assert(type == typeid (boolean_t)); |
|
104 auto* typedValue = static_cast<const boolean_t*> (value); |
|
105 asn1Writer->writeBoolean(*typedValue); |
|
106 break; |
|
107 } |
|
108 case TypeId::INTEGER: |
|
109 { |
|
110 assert(type == typeid (integer_t)); |
|
111 auto* typedValue = static_cast<const integer_t*> (value); |
|
112 asn1Writer->writeInteger(*typedValue); |
|
113 break; |
|
114 } |
|
115 case TypeId::STRING: |
|
116 { |
|
117 assert(type == typeid (string_t)); |
|
118 auto* typedValue = static_cast<const string_t*> (value); |
|
119 asn1Writer->writeString(*typedValue); |
|
120 break; |
|
121 } |
|
122 default: |
|
123 throw cli::RelpipeCLIException(L"Unsupported type in ASN1Handler.attribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); |
|
124 } |
|
125 |
|
126 } |
|
127 |
|
128 void endOfPipe() { |
|
129 if (valueCount) asn1Writer->writeEndSequence(); |
|
130 if (relationCount) asn1Writer->writeEndSequence(); |
|
131 asn1Writer->writeEndSequence(); |
|
132 } |
|
133 |
|
134 }; |
|
135 |
|
136 } |
|
137 } |
|
138 } |