author | František Kučera <franta-hg@frantovo.cz> |
Wed, 29 Jan 2020 18:41:42 +0100 | |
branch | v_0 |
changeset 73 | 1a067a217454 |
parent 32 | bccda5688d71 |
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 |
|
24
4353cd19a6b5
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
4 | 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 |
#pragma once |
|
18 |
||
19 |
#include <vector> |
|
20 |
#include <filesystem> |
|
21 |
||
22 |
#include <relpipe/writer/typedefs.h> |
|
23 |
#include <relpipe/writer/AttributeMetadata.h> |
|
24 |
#include <relpipe/writer/RelationalWriter.h> |
|
25 |
||
26 |
#include "RequestedField.h" |
|
27 |
||
28 |
namespace relpipe { |
|
29 |
namespace in { |
|
30 |
namespace filesystem { |
|
31 |
||
32 |
namespace fs = std::filesystem; |
|
33 |
using namespace relpipe::writer; |
|
34 |
||
35 |
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
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
bool currentFileExists; |
4 | 40 |
|
41 |
/** |
|
42 |
* Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata(). |
|
43 |
* @param writer |
|
44 |
* @param field |
|
45 |
*/ |
|
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
46 |
virtual void writeFieldOfExistingFile(RelationalWriter* writer, const string_t& relationName, const RequestedField& field) = 0; |
4 | 47 |
|
48 |
/** |
|
49 |
* Writes empty attribute(s) in case of non-existent file or an error. |
|
50 |
* The attribute count must match with count of AttributeMetadata returned in toMetadata(). |
|
51 |
* @param writer |
|
52 |
* @param field |
|
53 |
*/ |
|
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
54 |
virtual void writeEmptyField(RelationalWriter* writer, const string_t& relationName, const RequestedField& field) { |
4 | 55 |
// TODO: better handling of null values (when null values are supported by the format specification) |
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
56 |
for (AttributeMetadata m : toMetadata(writer, relationName, field)) { |
4 | 57 |
switch (m.typeId) { |
58 |
case TypeId::BOOLEAN: |
|
59 |
writer->writeAttribute(L"false"); |
|
60 |
break; |
|
61 |
case TypeId::INTEGER: |
|
62 |
writer->writeAttribute(L"0"); |
|
63 |
break; |
|
64 |
case TypeId::STRING: |
|
65 |
writer->writeAttribute(L""); |
|
66 |
break; |
|
67 |
default: |
|
68 |
throw RelpipeWriterException(L"Unsupported attribute type in writeEmptyField()."); |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
||
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
|
73 |
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
|
74 |
|
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 |
* 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
|
77 |
* But usually it is 1:1. |
29
6f15f18d2abf
field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
78 |
* @param writer can be used for TypeId coversion from string_t |
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
79 |
* @param relationName default one or set by the user |
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
|
80 |
* @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
|
81 |
* @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
|
82 |
*/ |
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
83 |
virtual vector<AttributeMetadata> toMetadata(RelationalWriter* writer, const string_t& relationName, const RequestedField& field) = 0; |
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
|
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 |
/** |
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 |
* 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
|
87 |
* 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
|
88 |
* @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
|
89 |
* @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
|
90 |
*/ |
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 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
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 |
|
32
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
97 |
virtual void writeField(RelationalWriter* writer, const string_t& relationName, const RequestedField& field) { |
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
98 |
if (currentFileExists) writeFieldOfExistingFile(writer, relationName, field); |
bccda5688d71
propagate the relation name to the finders and streamlets
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
99 |
else writeEmptyField(writer, relationName, field); |
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
|
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 |
/** |
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 |
* 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
|
104 |
*/ |
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 |
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
|
106 |
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
|
107 |
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
|
108 |
} |
b4f29fb16159
process also links to non-existent files and non-readable links
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
109 |
|
4 | 110 |
virtual ~AttributeFinder() { |
111 |
} |
|
112 |
}; |
|
113 |
||
114 |
} |
|
115 |
} |
|
116 |
} |