| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../../Precompiled.h"
- #include "../../Database/DatabaseEvents.h"
- #include "../../IO/Log.h"
- namespace Urho3D
- {
- DbConnection::DbConnection(Context* context, const String& connectionString) :
- Object(context),
- connectionString_(connectionString),
- connectionImpl_(nullptr)
- {
- if (sqlite3_open(connectionString.CString(), &connectionImpl_) != SQLITE_OK)
- {
- URHO3D_LOGERRORF("Could not connect: %s", sqlite3_errmsg(connectionImpl_));
- sqlite3_close(connectionImpl_);
- connectionImpl_ = nullptr;
- }
- }
- DbConnection::~DbConnection()
- {
- Finalize();
- if (sqlite3_close(connectionImpl_) != SQLITE_OK)
- {
- // This should not happen after finalizing the connection, log error in Release but assert in Debug
- URHO3D_LOGERRORF("Could not disconnect: %s", sqlite3_errmsg(connectionImpl_));
- assert(false);
- }
- connectionImpl_ = nullptr;
- }
- void DbConnection::Finalize()
- {
- // TODO
- }
- DbResult DbConnection::Execute(const String& sql, bool useCursorEvent)
- {
- DbResult result;
- const char* zLeftover = nullptr;
- sqlite3_stmt* pStmt = nullptr;
- assert(connectionImpl_);
- // 2016-10-09: Prevent string corruption when trimmed is returned.
- String trimmedSqlStr = sql.Trimmed();
- int rc = sqlite3_prepare_v2(connectionImpl_, trimmedSqlStr.CString(), -1, &pStmt, &zLeftover);
- if (rc != SQLITE_OK)
- {
- URHO3D_LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
- assert(!pStmt);
- return result;
- }
- if (*zLeftover)
- {
- URHO3D_LOGERROR("Could not execute: only one SQL statement is allowed");
- sqlite3_finalize(pStmt);
- return result;
- }
- auto numCols = (unsigned)sqlite3_column_count(pStmt);
- result.columns_.Resize(numCols);
- for (unsigned i = 0; i < numCols; ++i)
- result.columns_[i] = sqlite3_column_name(pStmt, i);
- bool filtered = false;
- bool aborted = false;
- while (1)
- {
- rc = sqlite3_step(pStmt);
- if (rc == SQLITE_ROW)
- {
- VariantVector colValues(numCols);
- for (unsigned i = 0; i < numCols; ++i)
- {
- int type = sqlite3_column_type(pStmt, i);
- if (type != SQLITE_NULL)
- {
- // We can only bind primitive data type that our Variant class supports
- switch (type)
- {
- case SQLITE_INTEGER:
- colValues[i] = sqlite3_column_int(pStmt, i);
- if (String(sqlite3_column_decltype(pStmt, i)).Compare("BOOLEAN", false) == 0)
- colValues[i] = colValues[i] != 0;
- break;
- case SQLITE_FLOAT:
- colValues[i] = sqlite3_column_double(pStmt, i);
- break;
- default:
- // All other types are stored using their string representation in the Variant
- colValues[i] = (const char*)sqlite3_column_text(pStmt, i);
- break;
- }
- }
- }
- if (useCursorEvent)
- {
- using namespace DbCursor;
- VariantMap& eventData = GetEventDataMap();
- eventData[P_DBCONNECTION] = this;
- eventData[P_RESULTIMPL] = pStmt;
- eventData[P_SQL] = sql;
- eventData[P_NUMCOLS] = numCols;
- eventData[P_COLVALUES] = colValues;
- eventData[P_COLHEADERS] = result.columns_;
- eventData[P_FILTER] = false;
- eventData[P_ABORT] = false;
- SendEvent(E_DBCURSOR, eventData);
- filtered = eventData[P_FILTER].GetBool();
- aborted = eventData[P_ABORT].GetBool();
- }
- if (!filtered)
- result.rows_.Push(colValues);
- if (aborted)
- {
- sqlite3_finalize(pStmt);
- break;
- }
- }
- else if (rc != SQLITE_DONE)
- URHO3D_LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
- if (rc != SQLITE_ROW)
- {
- sqlite3_finalize(pStmt);
- break;
- }
- }
- result.numAffectedRows_ = numCols ? -1 : sqlite3_changes(connectionImpl_);
- return result;
- }
- }
|