|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <string> |
| 5 | +#include <unordered_map> |
| 6 | + |
| 7 | +#include "common/exception/runtime.h" |
| 8 | +#include "main/lbug.h" |
| 9 | +#include "main/prepared_statement.h" |
| 10 | +#include "main/storage_driver.h" |
| 11 | +#include "pybind_include.h" |
| 12 | + |
| 13 | +struct PyDatabaseState { |
| 14 | + std::unique_ptr<lbug::main::Database> database; |
| 15 | + std::unique_ptr<lbug::main::StorageDriver> storageDriver; |
| 16 | + |
| 17 | + ~PyDatabaseState() { closeNative(); } |
| 18 | + |
| 19 | + void closeNative() { |
| 20 | + storageDriver.reset(); |
| 21 | + database.reset(); |
| 22 | + } |
| 23 | + |
| 24 | + lbug::main::Database& ref() const { |
| 25 | + if (database == nullptr) { |
| 26 | + throw lbug::common::RuntimeException("Database is closed."); |
| 27 | + } |
| 28 | + return *database; |
| 29 | + } |
| 30 | + |
| 31 | + lbug::main::StorageDriver& storage() const { |
| 32 | + if (storageDriver == nullptr) { |
| 33 | + throw lbug::common::RuntimeException("Database is closed."); |
| 34 | + } |
| 35 | + return *storageDriver; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +struct PyConnectionState { |
| 40 | + std::shared_ptr<PyDatabaseState> database; |
| 41 | + std::unique_ptr<lbug::main::StorageDriver> storageDriver; |
| 42 | + std::unique_ptr<lbug::main::Connection> conn; |
| 43 | + std::unordered_map<std::string, py::object> arrowTableRefs; |
| 44 | + |
| 45 | + ~PyConnectionState() { closeNative(); } |
| 46 | + |
| 47 | + void closeNative() { |
| 48 | + arrowTableRefs.clear(); |
| 49 | + conn.reset(); |
| 50 | + storageDriver.reset(); |
| 51 | + database.reset(); |
| 52 | + } |
| 53 | + |
| 54 | + lbug::main::Connection& ref() const { |
| 55 | + if (conn == nullptr) { |
| 56 | + throw lbug::common::RuntimeException("Connection is closed."); |
| 57 | + } |
| 58 | + return *conn; |
| 59 | + } |
| 60 | + |
| 61 | + lbug::main::StorageDriver& storage() const { |
| 62 | + if (storageDriver == nullptr) { |
| 63 | + throw lbug::common::RuntimeException("Connection is closed."); |
| 64 | + } |
| 65 | + return *storageDriver; |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +struct PyPreparedStatementState { |
| 70 | + std::shared_ptr<PyConnectionState> connection; |
| 71 | + std::unique_ptr<lbug::main::PreparedStatement> preparedStatement; |
| 72 | + |
| 73 | + lbug::main::PreparedStatement& ref() const { |
| 74 | + if (preparedStatement == nullptr) { |
| 75 | + throw lbug::common::RuntimeException("Prepared statement is closed."); |
| 76 | + } |
| 77 | + return *preparedStatement; |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +struct PyQueryResultState { |
| 82 | + std::shared_ptr<PyConnectionState> connection; |
| 83 | + std::shared_ptr<PyQueryResultState> parent; |
| 84 | + std::unique_ptr<lbug::main::QueryResult> owned; |
| 85 | + lbug::main::QueryResult* borrowed = nullptr; |
| 86 | + |
| 87 | + lbug::main::QueryResult& ref() const { |
| 88 | + auto* result = owned != nullptr ? owned.get() : borrowed; |
| 89 | + if (result == nullptr) { |
| 90 | + throw lbug::common::RuntimeException("Query result is closed."); |
| 91 | + } |
| 92 | + return *result; |
| 93 | + } |
| 94 | +}; |
0 commit comments