src/SqlHandler.h
branchv_0
changeset 14 eacacf060755
parent 12 0b38339b871b
child 15 0ecde5272f8e
equal deleted inserted replaced
13:19580b27ade2 14:eacacf060755
    17 #pragma once
    17 #pragma once
    18 
    18 
    19 #include <memory>
    19 #include <memory>
    20 #include <string>
    20 #include <string>
    21 #include <sstream>
    21 #include <sstream>
       
    22 #include <regex>
    22 #include <vector>
    23 #include <vector>
    23 #include <locale>
    24 #include <locale>
    24 #include <codecvt>
    25 #include <codecvt>
    25 #include <unistd.h>
    26 #include <unistd.h>
    26 #include <cassert>
    27 #include <cassert>
   164 		relationalWriter->startRelation(statement.relation, metadata, true);
   165 		relationalWriter->startRelation(statement.relation, metadata, true);
   165 
   166 
   166 		while (prepared->next()) {
   167 		while (prepared->next()) {
   167 			for (int i = 0; i < columnCount; i++) {
   168 			for (int i = 0; i < columnCount; i++) {
   168 				relationalWriter->writeAttribute(convertor.from_bytes(prepared->getString(i)));
   169 				relationalWriter->writeAttribute(convertor.from_bytes(prepared->getString(i)));
       
   170 			}
       
   171 		}
       
   172 	}
       
   173 
       
   174 	std::vector<string_t> getAllRelations() {
       
   175 		std::vector<string_t> relations;
       
   176 		std::unique_ptr<PreparedStatement> prepared(connection->prepareStatement("SELECT name FROM sqlite_master WHERE type IN ('table', 'view')"));
       
   177 		while (prepared->next()) relations.push_back(convertor.from_bytes(prepared->getString(0)));
       
   178 		return relations;
       
   179 	}
       
   180 
       
   181 	void dumpRelations() {
       
   182 		std::wregex pattern(configuration.dumpRelations);
       
   183 		for (string_t relation : getAllRelations()) {
       
   184 			if (regex_match(relation, pattern)) {
       
   185 				std::wstringstream select;
       
   186 				select << L"SELECT * FROM ";
       
   187 				writeIdentifier(select, relation);
       
   188 
       
   189 				Statement statement;
       
   190 				statement.relation = relation;
       
   191 				statement.sql = select.str();
       
   192 				processStatement(statement);
   169 			}
   193 			}
   170 		}
   194 		}
   171 	}
   195 	}
   172 
   196 
   173 	relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) {
   197 	relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) {
   277 
   301 
   278 	void endOfPipe() {
   302 	void endOfPipe() {
   279 		// run the transformation – process all statements:
   303 		// run the transformation – process all statements:
   280 		for (const Statement& statement : configuration.statements) processStatement(statement);
   304 		for (const Statement& statement : configuration.statements) processStatement(statement);
   281 
   305 
       
   306 		// pass-through some relations:
       
   307 		if (configuration.dumpRelations.size()) dumpRelations();
       
   308 
   282 		// delete or keep the file:
   309 		// delete or keep the file:
   283 		if (configuration.file.size()) {
   310 		if (configuration.file.size()) {
   284 			if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) {
   311 			if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) {
   285 				std::wcerr << L"will unlink file" << std::endl;
   312 				std::wcerr << L"will unlink file" << std::endl;
   286 				int result = unlink(convertor.to_bytes(configuration.file).c_str());
   313 				int result = unlink(convertor.to_bytes(configuration.file).c_str());