src/StreamRelationalWriter.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:47:14 +0200
branchv_0
changeset 41 744b61559eb2
parent 30 3f18cb7a6963
child 47 77fbaccdeea4
permissions -rw-r--r--
fix license version: GNU LGPLv3 or GPLv2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
     1
/**
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     2
 * Relational pipes (library)
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
     4
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     6
 * it under the terms of the:
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     7
 *  - GNU Lesser General Public License as published by the Free Software Foundation;
41
744b61559eb2 fix license version: GNU LGPLv3 or GPLv2
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
     8
 *    version 3 of the License or (at your option)
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     9
 *  - GNU General Public License as published by the Free Software Foundation;
41
744b61559eb2 fix license version: GNU LGPLv3 or GPLv2
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    10
 *    version 2 of the License.
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    11
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    12
 * This program is distributed in the hope that it will be useful,
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    15
 * GNU General Public License for more details.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    16
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    17
 * You should have received a copy of the GNU General Public License
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    19
 */
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#pragma once
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <string>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <iostream>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <vector>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    26
#include <relpipe/protocol/constants.h>
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    27
20
bef6648e79b1 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    28
#include "../include/relpipe/writer/typedefs.h"
bef6648e79b1 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    29
#include "../include/relpipe/writer/RelationalWriter.h"
bef6648e79b1 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    30
#include "../include/relpipe/writer/TypeId.h"
27
a64afb2d24c9 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    31
#include "../include/relpipe/writer/AttributeMetadata.h"
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    32
#include "DataTypeWriterBase.h"
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    33
#include "types/BooleanDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
#include "types/IntegerDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    35
#include "types/StringDataTypeWriter.h"
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    37
namespace relpipe {
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    38
namespace writer {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    40
using namespace relpipe::protocol;
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    41
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    42
class StreamRelationalWriter : public RelationalWriter {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
private:
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    44
	std::ostream &output;
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    45
	types::BooleanDataTypeWriter booleanWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    46
	types::IntegerDataTypeWriter integerWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    47
	types::StringDataTypeWriter stringWriter;
25
135ef93a4ac2 imports, namespaces
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    48
	std::vector<DataTypeWriterBase*> writers = {&booleanWriter, &integerWriter, &stringWriter};
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    49
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    50
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    51
	 * count of columns in the current table
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    52
	 */
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    53
	integer_t columnCount;
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    54
	/**
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    55
	 * number of column (0 = first) that will be written; after writing, the number is increased and prepared for next one
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    56
	 */
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    57
	integer_t currentColumn;
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    58
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    59
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    60
	 * types of columns in the current table
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    61
	 */
25
135ef93a4ac2 imports, namespaces
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    62
	std::vector<TypeId> columnTypes;
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    63
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    64
	void writeString(const string_t &stringValue, const TypeId typeId) {
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    65
		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue);
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    66
		throw RelpipeWriterException(L"Unsupported data type: " + static_cast<integer_t> (typeId));
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    67
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    68
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    69
	void writeRaw(const void* value, const type_info& typeInfo, const TypeId typeId) {
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    70
		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeRaw(output, value, typeInfo);
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    71
		throw RelpipeWriterException(L"Unsupported data type: " + static_cast<integer_t> (typeId));
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    72
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    73
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
public:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    76
	StreamRelationalWriter(std::ostream &output) :
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    77
	output(output) {
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    78
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    79
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    80
	TypeId toTypeId(const string_t typeCode) override {
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    81
		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeCode)) return writer->getTypeId();
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    82
		throw RelpipeWriterException(L"Unsupported data type: " + typeCode);
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
27
a64afb2d24c9 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    85
	void startRelation(string_t name, std::vector<AttributeMetadata> attributes, boolean_t writeHeader) override {
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    86
		string_t tableName = name;
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    87
		columnCount = attributes.size();
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    88
		currentColumn = 0;
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    89
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    90
		// Write table name and column count:
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    91
		integerWriter.writeValue(output, DATA_PART_START);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    92
		stringWriter.writeValue(output, tableName);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    93
		integerWriter.writeValue(output, columnCount);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    94
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    95
		columnTypes.clear();
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    96
		columnTypes.resize(columnCount);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    97
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    98
		// Write column names:
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    99
		for (size_t c = 0; c < columnCount; c++) {
27
a64afb2d24c9 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   100
			wstring columnName = attributes[c].attributeName;
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   101
			stringWriter.writeValue(output, columnName);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   102
		}
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   103
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   104
		// Write column types:
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   105
		for (size_t c = 0; c < columnCount; c++) {
27
a64afb2d24c9 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   106
			TypeId typeId = attributes[c].typeId;
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   107
			integerWriter.writeValue(output, static_cast<integer_t> (typeId));
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   108
			columnTypes[c] = typeId;
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   109
		}
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   110
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   111
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   112
17
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   113
	void writeAttribute(const string_t& value) override {
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   114
		if (currentColumn == 0) integerWriter.writeValue(output, DATA_PART_ROW);
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   115
		writeString(value, columnTypes[currentColumn]);
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   116
		if (++currentColumn == columnCount) currentColumn = 0;
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   119
	void writeAttribute(const void* value, const std::type_info& type) override {
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   120
		if (currentColumn == 0) integerWriter.writeValue(output, DATA_PART_ROW);
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   121
		writeRaw(value, type, columnTypes[currentColumn]);
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   122
		if (++currentColumn == columnCount) currentColumn = 0;
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   123
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   124
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
};
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   127
}
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}