aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2021-06-03 12:14:50 +0200
committerEven Rouault <even.rouault@spatialys.com>2021-06-03 12:23:24 +0200
commit61e25ec859a34e68fa8c2dc99f951f3e970875df (patch)
tree9a79a6027673017552533bec2320e5255f3589a9 /src
parentdd416fd8d31601d5b4723ebb674e82bb03735e6a (diff)
downloadPROJ-61e25ec859a34e68fa8c2dc99f951f3e970875df.tar.gz
PROJ-61e25ec859a34e68fa8c2dc99f951f3e970875df.zip
factory.cpp: lint: make memoryHandle_ a unique_ptr
Diffstat (limited to 'src')
-rw-r--r--src/iso19111/factory.cpp34
1 files changed, 25 insertions, 9 deletions
diff --git a/src/iso19111/factory.cpp b/src/iso19111/factory.cpp
index 1b10f081..42055f90 100644
--- a/src/iso19111/factory.cpp
+++ b/src/iso19111/factory.cpp
@@ -252,6 +252,9 @@ class SQLiteHandle {
initFromExisting(sqlite3 *sqlite_handle, bool close_handle,
int nLayoutVersionMajor, int nLayoutVersionMinor);
+ static std::unique_ptr<SQLiteHandle>
+ initFromExistingUniquePtr(sqlite3 *sqlite_handle, bool close_handle);
+
void checkDatabaseLayout(const std::string &mainDbPath,
const std::string &path,
const std::string &dbNamePrefix);
@@ -337,6 +340,17 @@ SQLiteHandle::initFromExisting(sqlite3 *sqlite_handle, bool close_handle,
// ---------------------------------------------------------------------------
+std::unique_ptr<SQLiteHandle>
+SQLiteHandle::initFromExistingUniquePtr(sqlite3 *sqlite_handle,
+ bool close_handle) {
+ auto handle = std::unique_ptr<SQLiteHandle>(
+ new SQLiteHandle(sqlite_handle, close_handle));
+ handle->registerFunctions();
+ return handle;
+}
+
+// ---------------------------------------------------------------------------
+
SQLResultSet SQLiteHandle::run(sqlite3_stmt *stmt, const std::string &sql,
const ListOfParams &parameters,
bool useMaxFloatPrecision) {
@@ -729,7 +743,7 @@ struct DatabaseContext::Private {
// Used by startInsertStatementsSession() and related functions
std::string memoryDbForInsertPath_{};
- sqlite3 *memoryDbHandle_ = nullptr;
+ std::unique_ptr<SQLiteHandle> memoryDbHandle_{};
using LRUCacheOfObjects = lru11::Cache<std::string, util::BaseObjectPtr>;
@@ -1315,8 +1329,8 @@ void DatabaseContext::Private::appendSql(
std::vector<std::string> &sqlStatements, const std::string &sql) {
sqlStatements.emplace_back(sql);
char *errMsg = nullptr;
- if (sqlite3_exec(memoryDbHandle_, sql.c_str(), nullptr, nullptr, &errMsg) !=
- SQLITE_OK) {
+ if (sqlite3_exec(memoryDbHandle_->handle(), sql.c_str(), nullptr, nullptr,
+ &errMsg) != SQLITE_OK) {
std::string s("Cannot execute " + sql);
if (errMsg) {
s += " : ";
@@ -2809,18 +2823,21 @@ void DatabaseContext::startInsertStatementsSession() {
buffer << this;
buffer << ".db?mode=memory&cache=shared";
d->memoryDbForInsertPath_ = buffer.str();
+ sqlite3 *memoryDbHandle = nullptr;
sqlite3_open_v2(
- d->memoryDbForInsertPath_.c_str(), &d->memoryDbHandle_,
+ d->memoryDbForInsertPath_.c_str(), &memoryDbHandle,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, nullptr);
- if (d->memoryDbHandle_ == nullptr) {
+ if (memoryDbHandle == nullptr) {
throw FactoryException("Cannot create in-memory database");
}
+ d->memoryDbHandle_ =
+ SQLiteHandle::initFromExistingUniquePtr(memoryDbHandle, true);
// Fill the structure of this database
for (const auto &sql : sqlStatements) {
char *errmsg = nullptr;
- if (sqlite3_exec(d->memoryDbHandle_, sql.c_str(), nullptr, nullptr,
- &errmsg) != SQLITE_OK) {
+ if (sqlite3_exec(d->memoryDbHandle_->handle(), sql.c_str(), nullptr,
+ nullptr, &errmsg) != SQLITE_OK) {
const auto sErrMsg =
"Cannot execute " + sql + ": " + (errmsg ? errmsg : "");
sqlite3_free(errmsg);
@@ -3050,8 +3067,7 @@ void DatabaseContext::stopInsertStatementsSession() {
if (d->memoryDbHandle_) {
d->clearCaches();
d->attachExtraDatabases(d->auxiliaryDatabasePaths_);
- sqlite3_close(d->memoryDbHandle_);
- d->memoryDbHandle_ = nullptr;
+ d->memoryDbHandle_.reset();
d->memoryDbForInsertPath_.clear();
}
}