author | František Kučera <franta-hg@frantovo.cz> |
Wed, 16 Jan 2019 18:19:50 +0100 | |
branch | v_0 |
changeset 8 | eb1ecb37c6b7 |
parent 4 | d44ed75822e7 |
child 9 | b4f29fb16159 |
permissions | -rw-r--r-- |
4 | 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 <vector> |
|
21 |
#include <filesystem> |
|
22 |
||
23 |
#include <relpipe/writer/typedefs.h> |
|
24 |
#include <relpipe/writer/AttributeMetadata.h> |
|
25 |
#include <relpipe/writer/RelationalWriter.h> |
|
26 |
||
27 |
#include "RequestedField.h" |
|
28 |
||
29 |
namespace relpipe { |
|
30 |
namespace in { |
|
31 |
namespace filesystem { |
|
32 |
||
33 |
namespace fs = std::filesystem; |
|
34 |
using namespace relpipe::writer; |
|
35 |
||
36 |
class AttributeFinder { |
|
8
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
37 |
protected: |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
38 |
fs::path currentFile; |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
39 |
string currentFileRaw; |
4 | 40 |
public: |
41 |
||
42 |
/** |
|
43 |
* Single requested fields might generate multiple attributes in the relation. |
|
44 |
* But usually it is 1:1. |
|
45 |
* @param field requested field from the user (usually from CLI arguments) |
|
46 |
* @return attribute metadata to be used in the RelationalWriter.startRelation() |
|
47 |
*/ |
|
48 |
virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) = 0; |
|
49 |
||
50 |
/** |
|
51 |
* Writing of the record for current file is starting. |
|
52 |
* Following writeField() calls are related to this file. |
|
8
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
53 |
* @param file path to the file |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
54 |
* @param fileRaw raw file name as it was on the input |
4 | 55 |
*/ |
8
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
56 |
virtual void startFile(const fs::path& file, const string& fileRaw) { |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
57 |
currentFile = file; |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
58 |
currentFileRaw = fileRaw; |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
59 |
} |
4 | 60 |
|
61 |
/** |
|
62 |
* Writing of the record for current file is finished. All resources linked to this file should be released. |
|
63 |
*/ |
|
8
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
64 |
virtual void endFile() { |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
65 |
currentFile.clear(); |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
66 |
currentFileRaw.clear(); |
eb1ecb37c6b7
interpret empty string as current directory (e.g. result of: find -printf '%P\0')
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
67 |
} |
4 | 68 |
|
69 |
/** |
|
70 |
* Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata(). |
|
71 |
* @param writer |
|
72 |
* @param field |
|
73 |
*/ |
|
74 |
virtual void writeField(RelationalWriter* writer, const RequestedField& field) = 0; |
|
75 |
||
76 |
/** |
|
77 |
* Writes empty attribute(s) in case of non-existent file or an error. |
|
78 |
* The attribute count must match with count of AttributeMetadata returned in toMetadata(). |
|
79 |
* @param writer |
|
80 |
* @param field |
|
81 |
*/ |
|
82 |
virtual void writeEmptyField(RelationalWriter* writer, const RequestedField& field) { |
|
83 |
// TODO: better handling of null values (when null values are supported by the format specification) |
|
84 |
for (AttributeMetadata m : toMetadata(field)) { |
|
85 |
switch (m.typeId) { |
|
86 |
case TypeId::BOOLEAN: |
|
87 |
writer->writeAttribute(L"false"); |
|
88 |
break; |
|
89 |
case TypeId::INTEGER: |
|
90 |
writer->writeAttribute(L"0"); |
|
91 |
break; |
|
92 |
case TypeId::STRING: |
|
93 |
writer->writeAttribute(L""); |
|
94 |
break; |
|
95 |
default: |
|
96 |
throw RelpipeWriterException(L"Unsupported attribute type in writeEmptyField()."); |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
virtual ~AttributeFinder() { |
|
102 |
} |
|
103 |
}; |
|
104 |
||
105 |
} |
|
106 |
} |
|
107 |
} |