equal
deleted
inserted
replaced
158 else output << ch; |
158 else output << ch; |
159 } |
159 } |
160 output << L'"'; |
160 output << L'"'; |
161 } |
161 } |
162 |
162 |
|
163 Connection* getConnection() { |
|
164 if (configuration.dataSourceName.size()) return driverManager->getConnectionByDSN(configuration.dataSourceName); |
|
165 else if (configuration.dataSourceURL.size()) return driverManager->getConnectionByURL(configuration.dataSourceURL); |
|
166 else return driverManager->getConnectionByURL(L"Driver=SQLite3;Database=:memory:"); |
|
167 // SQLite is default/fallback oprion |
|
168 // TODO: use environmental variable to allow setting a different default |
|
169 } |
|
170 |
163 public: |
171 public: |
164 |
172 |
165 SqlHandler(writer::RelationalWriter* relationalWriter, DriverManager* driverManager, Configuration& configuration) : relationalWriter(relationalWriter), driverManager(driverManager), configuration(configuration) { |
173 SqlHandler(writer::RelationalWriter* relationalWriter, DriverManager* driverManager, Configuration& configuration) : relationalWriter(relationalWriter), driverManager(driverManager), configuration(configuration) { |
166 std::string file; |
174 connection.reset(getConnection()); |
167 if (configuration.file.size()) { |
|
168 file = convertor.to_bytes(configuration.file); |
|
169 |
|
170 // in C++17 we can use: std::filesystem::exists() |
|
171 struct stat fileStat; |
|
172 fileAlreadyExisted = (stat(file.c_str(), &fileStat) == 0); |
|
173 } else { |
|
174 file = ":memory:"; |
|
175 } |
|
176 |
|
177 connection.reset(driverManager->getConnectionByDSN(L"sqlite-memory")); // FIXME: custom DSN and files |
|
178 connection.reset(driverManager->getConnectionByDSN(L"relpipe")); // FIXME: custom DSN and files |
|
179 //connection->setAutoCommit(false); |
175 //connection->setAutoCommit(false); |
180 } |
176 } |
181 |
177 |
182 virtual ~SqlHandler() { |
178 virtual ~SqlHandler() { |
183 } |
179 } |
264 |
260 |
265 // pass-through some relations: |
261 // pass-through some relations: |
266 for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy); |
262 for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy); |
267 |
263 |
268 connection->commit(); |
264 connection->commit(); |
269 |
|
270 // delete or keep the file: |
|
271 if (configuration.file.size()) { |
|
272 if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) { |
|
273 std::wcerr << L"will unlink file" << std::endl; |
|
274 int result = unlink(convertor.to_bytes(configuration.file).c_str()); |
|
275 if (result) throw SqlException(L"Unable to delete SQLite file."); |
|
276 } |
|
277 } // else: we had no file, everything was in memory |
|
278 } |
265 } |
279 |
266 |
280 static void listDataSources(writer::RelationalWriter* relationalWriter, DriverManager* driverManager) { |
267 static void listDataSources(writer::RelationalWriter* relationalWriter, DriverManager* driverManager) { |
281 relationalWriter->startRelation(L"data_source",{ |
268 relationalWriter->startRelation(L"data_source",{ |
282 {L"name", writer::TypeId::STRING}, |
269 {L"name", writer::TypeId::STRING}, |