# HG changeset patch # User František Kučera # Date 1564504835 -7200 # Node ID 32b4293307f47aa14ef011d477fe305e31677242 # Parent cbc7817a3346abe71b06e399593a2d6d2643c9ea create table for each relation diff -r cbc7817a3346 -r 32b4293307f4 src/SqlHandler.h --- a/src/SqlHandler.h Tue Jul 30 17:06:41 2019 +0200 +++ b/src/SqlHandler.h Tue Jul 30 18:40:35 2019 +0200 @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -127,6 +128,7 @@ writer::RelationalWriter* relationalWriter; std::wstring_convert> convertor; // TODO: support also other encodings std::unique_ptr connection; + relpipe::writer::string_t currentInsert; void processStatement(const Statement& statement) { PreparedStatement prepared = connection->prepareStatement(convertor.to_bytes(statement.sql).c_str()); @@ -149,6 +151,19 @@ } } + relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) { + if (typeId == relpipe::reader::TypeId::BOOLEAN) return L"boolean"; + else if (typeId == relpipe::reader::TypeId::INTEGER) return L"integer"; + else return L"text"; + } + + void writeIdentifier(std::wstringstream& output, relpipe::writer::string_t identifier) { + for (auto & ch : identifier) { + if (ch == L'"') output << L"\"\""; + else output << ch; + } + } + public: SqlHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) { @@ -160,7 +175,20 @@ } void startRelation(string_t name, vector attributes) override { + std::wstringstream sql; + sql << L"CREATE TABLE "; + writeIdentifier(sql, name); + sql << L" (\n"; + for (int i = 0; i < attributes.size(); i++) { + sql << L"\t"; + writeIdentifier(sql, attributes[i].getAttributeName()); + sql << L" " << toSQLType(attributes[i].getTypeId()); + if (i < attributes.size() - 1) sql << L",\n"; + } + sql << L"\n)"; + PreparedStatement createTable = connection->prepareStatement(convertor.to_bytes(sql.str()).c_str()); + createTable.next(); } void attribute(const string_t& value) override { @@ -169,7 +197,7 @@ void endOfPipe() { for (const Statement& statement : configuration.statements) processStatement(statement); - + if (configuration.file.size() && !configuration.keepFile) { int result = unlink(convertor.to_bytes(configuration.file).c_str()); if (result) throw SqlException(L"Unable to delete SQLite file.");