author | František Kučera <franta-hg@frantovo.cz> |
Mon, 08 Apr 2019 13:37:09 +0200 | |
branch | v_0 |
changeset 19 | 1cb4a1c9c696 |
parent 9 | b4f29fb16159 |
child 24 | 4353cd19a6b5 |
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; |
9
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
40 |
bool currentFileExists; |
4 | 41 |
|
42 |
/** |
|
43 |
* Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata(). |
|
44 |
* @param writer |
|
45 |
* @param field |
|
46 |
*/ |
|
9
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
47 |
virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) = 0; |
4 | 48 |
|
49 |
/** |
|
50 |
* Writes empty attribute(s) in case of non-existent file or an error. |
|
51 |
* The attribute count must match with count of AttributeMetadata returned in toMetadata(). |
|
52 |
* @param writer |
|
53 |
* @param field |
|
54 |
*/ |
|
55 |
virtual void writeEmptyField(RelationalWriter* writer, const RequestedField& field) { |
|
56 |
// TODO: better handling of null values (when null values are supported by the format specification) |
|
57 |
for (AttributeMetadata m : toMetadata(field)) { |
|
58 |
switch (m.typeId) { |
|
59 |
case TypeId::BOOLEAN: |
|
60 |
writer->writeAttribute(L"false"); |
|
61 |
break; |
|
62 |
case TypeId::INTEGER: |
|
63 |
writer->writeAttribute(L"0"); |
|
64 |
break; |
|
65 |
case TypeId::STRING: |
|
66 |
writer->writeAttribute(L""); |
|
67 |
break; |
|
68 |
default: |
|
69 |
throw RelpipeWriterException(L"Unsupported attribute type in writeEmptyField()."); |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
||
9
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
74 |
public: |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
75 |
|
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
76 |
/** |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
77 |
* Single requested fields might generate multiple attributes in the relation. |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
78 |
* But usually it is 1:1. |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
79 |
* @param field requested field from the user (usually from CLI arguments) |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
80 |
* @return attribute metadata to be used in the RelationalWriter.startRelation() |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
81 |
*/ |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
82 |
virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) = 0; |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
83 |
|
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
84 |
/** |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
85 |
* Writing of the record for current file is starting. |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
86 |
* Following writeField() calls are related to this file. |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
87 |
* @param file path to the file |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
88 |
* @param fileRaw raw file name as it was on the input |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
89 |
*/ |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
90 |
virtual void startFile(const fs::path& file, const string& fileRaw, bool exists) { |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
91 |
currentFile = file; |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
92 |
currentFileRaw = fileRaw; |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
93 |
currentFileExists = exists; |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
94 |
} |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
95 |
|
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
96 |
virtual void writeField(RelationalWriter* writer, const RequestedField& field) { |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
97 |
if (currentFileExists) writeFieldOfExistingFile(writer, field); |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
98 |
else writeEmptyField(writer, field); |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
99 |
} |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
100 |
|
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
101 |
/** |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
102 |
* Writing of the record for current file is finished. All resources linked to this file should be released. |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
103 |
*/ |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
104 |
virtual void endFile() { |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
105 |
currentFile.clear(); |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
106 |
currentFileRaw.clear(); |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
107 |
} |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
108 |
|
4 | 109 |
virtual ~AttributeFinder() { |
110 |
} |
|
111 |
}; |
|
112 |
||
113 |
} |
|
114 |
} |
|
115 |
} |