diff options
Diffstat (limited to 'source/fud_sqlite.cpp')
-rw-r--r-- | source/fud_sqlite.cpp | 87 |
1 files changed, 68 insertions, 19 deletions
diff --git a/source/fud_sqlite.cpp b/source/fud_sqlite.cpp index 3e39474..449be8b 100644 --- a/source/fud_sqlite.cpp +++ b/source/fud_sqlite.cpp @@ -19,21 +19,43 @@ namespace fud { -SqliteDb::SqliteDb(const String& name, SqliteOpenMode mode, int extraFlags) : - m_name{name}, m_mode{mode}, m_extraFlags{extraFlags} +SqliteDbResult SqliteDb::make(const String& name, SqliteOpenMode mode, int extraFlags) { - initialize(); + SqliteDb sqlDb{}; + sqlDb.m_name = name; + sqlDb.m_mode = mode; + sqlDb.m_extraFlags = extraFlags; + auto status = sqlDb.initialize(); + if (status != FudStatus::Success) { + return SqliteDbResult::error(status); + } + return SqliteDbResult::okay(std::move(sqlDb)); } -SqliteDb::SqliteDb(const char* name, SqliteOpenMode mode, int extraFlags) : - m_name{name}, m_mode{mode}, m_extraFlags{extraFlags} +SqliteDbResult SqliteDb::make(const char* cStrName, SqliteOpenMode mode, int extraFlags) { - initialize(); + auto nameResult = String::makeFromCString(cStrName); + if (nameResult.isError()) { + return SqliteDbResult::error(nameResult); + } + SqliteDb sqlDb{}; + sqlDb.m_name = nameResult.takeOkay(); + sqlDb.m_mode = mode; + sqlDb.m_extraFlags = extraFlags; + auto status = sqlDb.initialize(); + if (status != FudStatus::Success) { + return SqliteDbResult::error(status); + } + return SqliteDbResult::okay(std::move(sqlDb)); } SqliteDb::SqliteDb(SqliteDb&& rhs) noexcept : - m_name{std::move(rhs.m_name)}, m_nameValid{rhs.m_nameValid}, m_dbHandle{rhs.m_dbHandle}, - m_errorCode{rhs.m_errorCode}, m_mode{rhs.m_mode}, m_extraFlags{rhs.m_extraFlags} + m_name{std::move(rhs.m_name)}, + m_nameValid{rhs.m_nameValid}, + m_dbHandle{rhs.m_dbHandle}, + m_errorCode{rhs.m_errorCode}, + m_mode{rhs.m_mode}, + m_extraFlags{rhs.m_extraFlags} { rhs.m_nameValid = false; rhs.m_dbHandle = nullptr; @@ -85,7 +107,7 @@ FudStatus SqliteDb::exec( const String& statement, int (*callback)(void*, int, char**, char**), void* context, - String* errorMessage) + std::unique_ptr<SqliteErrorMsg> errorMessage) { if (!valid()) { return FudStatus::ObjectInvalid; @@ -101,29 +123,29 @@ FudStatus SqliteDb::exec( errorMsgPtrAddress = &errorMsgPtr; } - m_errorCode = sqlite3_exec( - m_dbHandle, - statement.c_str(), - callback, - context, - errorMsgPtrAddress); + m_errorCode = sqlite3_exec(m_dbHandle, statement.c_str(), callback, context, errorMsgPtrAddress); if (errorMessage != nullptr) { - *errorMessage = String{errorMsgPtr}; + errorMessage = std::make_unique<SqliteErrorMsg>(errorMsgPtr); } return m_errorCode == SQLITE_OK ? FudStatus::Success : FudStatus::Failure; } -void SqliteDb::initialize() +FudStatus SqliteDb::initialize() { m_nameValid = m_name.utf8Valid(); if (!m_nameValid) { - return; + return FudStatus::InvalidInput; } m_errorCode = open(); + if (m_errorCode != SQLITE_OK) { + return FudStatus::Failure; + } + + return FudStatus::Success; } int SqliteDb::open() @@ -165,7 +187,10 @@ SqliteStatement::SqliteStatement(const SqliteDb& sqliteDb, const String& input) } SqliteStatement::SqliteStatement(SqliteStatement&& rhs) noexcept : - m_input{std::move(rhs.m_input)}, m_tail{rhs.m_tail}, m_status{rhs.m_status}, m_errorCode{rhs.m_errorCode}, + m_input{std::move(rhs.m_input)}, + m_tail{rhs.m_tail}, + m_status{rhs.m_status}, + m_errorCode{rhs.m_errorCode}, m_preparedStatement{rhs.m_preparedStatement} { rhs.m_tail = nullptr; @@ -213,4 +238,28 @@ FudStatus SqliteStatement::reset() return FudStatus::Success; } +SqliteErrorMsg::SqliteErrorMsg(SqliteErrorMsg&& rhs) noexcept : m_errorMsg{rhs.m_errorMsg} +{ + rhs.m_errorMsg = nullptr; +} + +SqliteErrorMsg& SqliteErrorMsg::operator=(SqliteErrorMsg&& rhs) noexcept { + if (m_errorMsg != nullptr) { + sqlite3_free(m_errorMsg); + m_errorMsg = nullptr; + } + m_errorMsg = rhs.m_errorMsg; + rhs.m_errorMsg = nullptr; + + return *this; +} + +SqliteErrorMsg::~SqliteErrorMsg() +{ + if (m_errorMsg != nullptr) { + sqlite3_free(m_errorMsg); + m_errorMsg = nullptr; + } +} + } // namespace fud |