src/StreamRelationalWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 21 Jul 2018 23:01:47 +0200
branchv_0
changeset 12 640e88aedf8f
parent 11 27bc67e7c80f
child 13 e7234dd45166
permissions -rw-r--r--
code for table generation ported from the prototype: RelationalGenerator.h
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
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
     7
#include "../include/typedefs.h"
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     8
#include "../include/RelationalWriter.h"
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
     9
#include "format.h"
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    10
#include "DataTypeWriterBase.h"
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    11
#include "types/BooleanDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    12
#include "types/IntegerDataTypeWriter.h"
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    13
#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
    14
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    15
namespace relpipe {
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    16
namespace writer {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    18
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
    19
private:
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    20
	std::ostream &output;
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    21
	types::BooleanDataTypeWriter booleanWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    22
	types::IntegerDataTypeWriter integerWriter;
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    23
	types::StringDataTypeWriter stringWriter;
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    24
	vector<DataTypeWriterBase*> writers = {&booleanWriter, &integerWriter, &stringWriter};
12
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    25
	
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    26
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    27
	 * 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
    28
	 */
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    29
	integer_t columnCount;
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
	/**
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    32
	 * 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
    33
	 */
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    34
	vector<integer_t> columnTypes;
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    35
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    36
	void writeString(const string_t &stringValue, const integer_t typeId) {
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    37
		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue);
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    38
		throw RelpipeWriterException(L"Unsupported data type: " + typeId);
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    39
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    40
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
public:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
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
	StreamRelationalWriter(std::ostream &output) :
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    44
	output(output) {
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    45
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    46
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    47
	integer_t toTypeId(const string_t typeCode) override {
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    48
		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
    49
		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
    50
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    52
	string_t toTypeCode(const integer_t typeId) override {
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    53
		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->getTypeCode();
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    54
		throw RelpipeWriterException(L"Unsupported data type: " + typeId);
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
11
27bc67e7c80f add table name to startRelation()
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    57
	void startRelation(string_t name, std::vector<std::pair<string_t, string_t> > 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
    58
		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
    59
		columnCount = attributes.size();
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
		// 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
    62
		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
    63
		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
    64
		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
    65
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    66
		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
    67
		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
    68
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    69
		// 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
    70
		for (size_t c = 0; c < columnCount; c++) {
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    71
			wstring columnName = attributes[c].first;
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    72
			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
    73
		}
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 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
    76
		for (size_t c = 0; c < columnCount; c++) {
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    77
			wstring typeCode = attributes[c].second;
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    78
			integer_t typeId = toTypeId(typeCode);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    79
			integerWriter.writeValue(output, typeId);
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[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
    81
		}
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    82
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    83
	}
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    84
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    85
	void writeRecord(std::vector<string_t> attributes) 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
		// FIXME: check vector size
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    87
		for (size_t c = 0; c < columnCount; c++) {
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    88
			// TODO: support multiple rows in a single method call?
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    89
			if (c % columnCount == 0) {
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    90
				integerWriter.writeValue(output, DATA_PART_ROW);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    91
			}
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    92
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    93
			wstring stringValue = attributes[c];
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    94
			integer_t typeId = columnTypes[c % 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
			writeString(stringValue, typeId);
640e88aedf8f code for table generation ported from the prototype: RelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    96
		}
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
};
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   101
}
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
}