src/StreamRelationalWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 01 Dec 2018 15:35:05 +0100
branchv_0
changeset 30 3f18cb7a6963
parent 29 142bdbba520f
child 41 744b61559eb2
permissions -rw-r--r--
license of protocol, reader and writer: GNU LGPLv3+ or GNU 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;
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     8
 *    either version 3 of the License, or (at your option) any later version;
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
 *    or (at your option)
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    10
 *  - GNU General Public License as published by the Free Software Foundation;
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    11
 *    either version 2 of the License, or (at your option) any later version.
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    12
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    13
 * 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
    14
 * 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
    15
 * 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
    16
 * GNU General Public License for more details.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    17
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    18
 * 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
    19
 * 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
    20
 */
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#pragma once
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <string>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <iostream>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <vector>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    27
#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
    28
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
    29
#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
    30
#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
    31
#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
    32
#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
    33
#include "DataTypeWriterBase.h"
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
#include "types/BooleanDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    35
#include "types/IntegerDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    36
#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
    37
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    38
namespace relpipe {
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    39
namespace writer {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    41
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
    42
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    43
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
    44
private:
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    45
	std::ostream &output;
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    46
	types::BooleanDataTypeWriter booleanWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    47
	types::IntegerDataTypeWriter integerWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    48
	types::StringDataTypeWriter stringWriter;
25
135ef93a4ac2 imports, namespaces
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    49
	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
    50
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    51
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    52
	 * 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
    53
	 */
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    54
	integer_t columnCount;
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    55
	/**
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    56
	 * 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
    57
	 */
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    58
	integer_t currentColumn;
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    59
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    60
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    61
	 * 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
    62
	 */
25
135ef93a4ac2 imports, namespaces
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    63
	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
    64
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    65
	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
    66
		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
    67
		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
    68
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    69
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    70
	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
    71
		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
    72
		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
    73
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    74
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
public:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    77
	StreamRelationalWriter(std::ostream &output) :
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    78
	output(output) {
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    79
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    80
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    81
	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
    82
		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
    83
		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
    84
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
27
a64afb2d24c9 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    86
	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
    87
		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
    88
		columnCount = attributes.size();
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    89
		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
    90
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    91
		// 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
    92
		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
    93
		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
    94
		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
    95
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.clear();
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    97
		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
    98
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    99
		// 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
   100
		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
   101
			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
   102
			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
   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
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   105
		// 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
   106
		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
   107
			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
   108
			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
   109
			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
   110
		}
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   111
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   112
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   113
17
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   114
	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
   115
		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
   116
		writeString(value, columnTypes[currentColumn]);
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   117
		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
   118
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   120
	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
   121
		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
   122
		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
   123
		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
   124
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   125
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
};
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   128
}
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
}