src/SQLHandler.h
branchv_0
changeset 0 7727b4d5560d
child 1 4c0366e1b4df
equal deleted inserted replaced
-1:000000000000 0:7727b4d5560d
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2022 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
       
     7  * the Free Software Foundation, version 3 of the License.
       
     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 <memory>
       
    20 #include <string>
       
    21 #include <vector>
       
    22 #include <iostream>
       
    23 #include <sstream>
       
    24 #include <locale>
       
    25 #include <codecvt>
       
    26 
       
    27 #include <relpipe/reader/typedefs.h>
       
    28 #include <relpipe/reader/TypeId.h>
       
    29 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
       
    30 #include <relpipe/reader/handlers/AttributeMetadata.h>
       
    31 
       
    32 #include "Configuration.h"
       
    33 #include "RelpipeSQLWriterException.h"
       
    34 
       
    35 namespace relpipe {
       
    36 namespace out {
       
    37 namespace sql {
       
    38 
       
    39 using namespace relpipe;
       
    40 using namespace relpipe::reader;
       
    41 using namespace relpipe::reader::handlers;
       
    42 
       
    43 class SQLHandler : public RelationalReaderStringHandler {
       
    44 private:
       
    45 	std::ostream& output;
       
    46 	Configuration& configuration;
       
    47 	const char QUOTE = '"';
       
    48 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // generate SQL always in UTF-8
       
    49 	std::vector<AttributeMetadata> firstAttributes;
       
    50 	integer_t valueCount = 0;
       
    51 
       
    52 	/**
       
    53 	 * @param a
       
    54 	 * @param b
       
    55 	 * @return true if relations have same number and types of attributes (names may differ)
       
    56 	 */
       
    57 	bool matches(const std::vector<AttributeMetadata>& a, const std::vector<AttributeMetadata>& b) {
       
    58 		if (a.size() != b.size()) return false;
       
    59 		for (int i = 0, limit = a.size(); i < limit; i++) if (a[i].getTypeId() != b[i].getTypeId()) return false;
       
    60 		return true;
       
    61 	}
       
    62 public:
       
    63 
       
    64 	SQLHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
       
    65 	}
       
    66 
       
    67 	void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
       
    68 		// TODO: CREATE TABLE
       
    69 		// TODO: escape identifiers
       
    70 		// TODO: ALTER TABLE / add columns on duplicate relation name
       
    71 		// TODO: custom data type mapping
       
    72 		// TODO: custom name transformation
       
    73 		// TODO: custom SQL script before/after stream/relation/record
       
    74 		// TODO: comments and/or custom comments
       
    75 		// TODO: optional transactions: BEGIN/COMMIT/ROLLBACK for stream/relation/record
       
    76 		output << "CREATE TABLE " << convertor.to_bytes(name) << " ( ... );" << std::endl;
       
    77 		
       
    78 		if (firstAttributes.empty()) {
       
    79 			firstAttributes = attributes;
       
    80 			if (configuration.writeHeader) for (auto attr : attributes) attribute(configuration.writeTypes ? attr.getAttributeName() + L"::" + attr.getTypeName() : attr.getAttributeName());
       
    81 		} else if (matches(firstAttributes, attributes)) {
       
    82 			// do UNION ALL – just append the records
       
    83 		} else {
       
    84 			// throw RelpipeSQLWriterException(L"To the SQL format we can convert only one relation or multiple relations that have same number of attributes of same types (relation and attribute names may differ – result is named after the first one).");
       
    85 		}
       
    86 	}
       
    87 
       
    88 	void attribute(const string_t& value) override {
       
    89 		valueCount++;
       
    90 		
       
    91 		if (value.size() > 0) {
       
    92 			output << QUOTE;
       
    93 			for (auto ch : convertor.to_bytes(value)) {
       
    94 				if (ch == QUOTE) output << QUOTE << QUOTE;
       
    95 				else output << ch;
       
    96 			}
       
    97 			output << QUOTE;
       
    98 		}
       
    99 
       
   100 		if (valueCount % firstAttributes.size()) {
       
   101 			output << ",";
       
   102 		} else {
       
   103 			// TODO: INSERT INTO ...
       
   104 			// TODO: escape identifiers and values
       
   105 			// TODO: optional use of function/procedure instead of INSERT
       
   106 			// TODO: optional INSERT of multiple records
       
   107 			output << std::endl << "INSERT INTO ... VALUES ... ;" << std::endl;;
       
   108 			valueCount = 0;
       
   109 		}
       
   110 	}
       
   111 
       
   112 	void endOfPipe() {
       
   113 		output.flush();
       
   114 	}
       
   115 
       
   116 };
       
   117 
       
   118 }
       
   119 }
       
   120 }