src/StreamRelationalWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 25 Aug 2018 18:21:09 +0200
branchv_0
changeset 25 135ef93a4ac2
parent 23 d505eaedc35e
child 26 8ae93a43fed2
permissions -rw-r--r--
imports, namespaces
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#pragma once
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <string>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
#include <iostream>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#include <vector>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
     7
#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
     8
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
     9
#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
    10
#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
    11
#include "../include/relpipe/writer/TypeId.h"
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    12
#include "DataTypeWriterBase.h"
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    13
#include "types/BooleanDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    14
#include "types/IntegerDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    15
#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
    16
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    17
namespace relpipe {
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    18
namespace writer {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    20
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
    21
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    22
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
    23
private:
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    24
	std::ostream &output;
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    25
	types::BooleanDataTypeWriter booleanWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    26
	types::IntegerDataTypeWriter integerWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    27
	types::StringDataTypeWriter stringWriter;
25
135ef93a4ac2 imports, namespaces
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    28
	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
    29
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    30
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    31
	 * 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
    32
	 */
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    33
	integer_t columnCount;
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    34
	/**
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    35
	 * 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
    36
	 */
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    37
	integer_t currentColumn;
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    38
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    39
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    40
	 * 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
    41
	 */
25
135ef93a4ac2 imports, namespaces
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    42
	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
    43
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    44
	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
    45
		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
    46
		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
    47
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    48
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    49
	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
    50
		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
    51
		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
    52
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    53
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
public:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    56
	StreamRelationalWriter(std::ostream &output) :
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    57
	output(output) {
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    58
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    59
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    60
	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
    61
		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
    62
		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
    63
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    65
	string_t toTypeCode(const TypeId typeId) override {
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    66
		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->getTypeCode();
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));
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    69
13
e7234dd45166 use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    70
	void startRelation(string_t name, std::vector<std::pair<string_t, TypeId> > 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
    71
		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
    72
		columnCount = attributes.size();
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    73
		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
    74
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    75
		// 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
    76
		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
    77
		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
    78
		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
    79
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    80
		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
    81
		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
    82
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    83
		// 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
    84
		for (size_t c = 0; c < columnCount; c++) {
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    85
			wstring columnName = attributes[c].first;
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
			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
    87
		}
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    88
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    89
		// 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
    90
		for (size_t c = 0; c < columnCount; c++) {
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    91
			TypeId typeId = attributes[c].second;
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    92
			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
    93
			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
    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
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    96
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    97
17
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    98
	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
    99
		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
   100
		writeString(value, columnTypes[currentColumn]);
f2cccaa9dd38 replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   101
		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
   102
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   104
	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
   105
		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
   106
		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
   107
		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
   108
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   109
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
};
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   112
}
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
}