ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod SQLite Extension ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "SqQuery.h" ds@1952: ds@1952: SqQuery::SqQuery(SqDatabase *parent, sqlite3_stmt *stmt) : ds@1952: m_pParent(parent), m_pStmt(stmt), m_pResults(NULL), m_AffectedRows(0), m_InsertID(0) ds@1952: { ds@1952: m_ParamCount = sqlite3_bind_parameter_count(m_pStmt); ds@1952: m_ColCount = sqlite3_column_count(m_pStmt); ds@1952: m_pParent->IncReferenceCount(); ds@1952: } ds@1952: ds@1952: SqQuery::~SqQuery() ds@1952: { ds@1952: delete m_pResults; ds@1952: sqlite3_finalize(m_pStmt); ds@1952: m_pParent->Close(); ds@1952: } ds@1952: ds@1952: IResultSet *SqQuery::GetResultSet() ds@1952: { ds@1952: return m_pResults; ds@1952: } ds@1952: ds@1952: bool SqQuery::FetchMoreResults() ds@1952: { ds@1952: /* We never have multiple result sets */ ds@1952: return false; ds@1952: } ds@1952: ds@1952: void SqQuery::Destroy() ds@1952: { ds@1952: delete this; ds@1952: } ds@1952: ds@1952: bool SqQuery::BindParamFloat(unsigned int param, float f) ds@1952: { ds@1952: /* SQLite is 1 indexed */ ds@1952: param++; ds@1952: ds@1952: if (param > m_ParamCount) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return (sqlite3_bind_double(m_pStmt, param, (double)f) == SQLITE_OK); ds@1952: } ds@1952: ds@1952: bool SqQuery::BindParamNull(unsigned int param) ds@1952: { ds@1952: /* SQLite is 1 indexed */ ds@1952: param++; ds@1952: ds@1952: if (param > m_ParamCount) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return (sqlite3_bind_null(m_pStmt, param) == SQLITE_OK); ds@1952: } ds@1952: ds@1952: bool SqQuery::BindParamString(unsigned int param, const char *text, bool copy) ds@1952: { ds@1952: /* SQLite is 1 indexed */ ds@1952: param++; ds@1952: ds@1952: if (param > m_ParamCount) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return (sqlite3_bind_text(m_pStmt, param, text, -1, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK); ds@1952: } ds@1952: ds@1952: bool SqQuery::BindParamInt(unsigned int param, int num, bool signd/* =true */) ds@1952: { ds@1952: /* SQLite is 1 indexed */ ds@1952: param++; ds@1952: ds@1952: if (param > m_ParamCount) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return (sqlite3_bind_int(m_pStmt, param, num) == SQLITE_OK); ds@1952: } ds@1952: ds@1952: bool SqQuery::BindParamBlob(unsigned int param, const void *data, size_t length, bool copy) ds@1952: { ds@1952: /* SQLite is 1 indexed */ ds@1952: param++; ds@1952: ds@1952: if (param > m_ParamCount) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return (sqlite3_bind_blob(m_pStmt, param, data, length, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK); ds@1952: } ds@1952: ds@1952: sqlite3_stmt *SqQuery::GetStmt() ds@1952: { ds@1952: return m_pStmt; ds@1952: } ds@1952: ds@1952: bool SqQuery::Execute() ds@1952: { ds@1952: int rc; ds@1952: ds@1952: /* If we don't have a result set and we have a column count, ds@1952: * create a result set pre-emptively. This is in case there ds@1952: * are no rows in the upcoming result set. ds@1952: */ ds@1952: if (!m_pResults && m_ColCount) ds@1952: { ds@1952: m_pResults = new SqResults(this); ds@1952: } ds@1952: ds@1952: /* If we've got results, throw them away */ ds@1952: if (m_pResults) ds@1952: { ds@1952: m_pResults->ResetResultCount(); ds@1952: } ds@1952: ds@1952: /* Fetch each row, if any */ ds@1952: while ((rc = sqlite3_step(m_pStmt)) == SQLITE_ROW) ds@1952: { ds@1952: /* This should NEVER happen but we're being safe. */ ds@1952: if (!m_pResults) ds@1952: { ds@1952: m_pResults = new SqResults(this); ds@1952: } ds@1952: m_pResults->PushResult(); ds@1952: } ds@1952: ds@1952: sqlite3 *db = m_pParent->GetDb(); ds@1952: if (rc != SQLITE_OK && rc != SQLITE_DONE && rc == sqlite3_errcode(db)) ds@1952: { ds@1952: /* Something happened... */ ds@1952: m_LastErrorCode = rc; ds@1952: m_LastError.assign(sqlite3_errmsg(db)); ds@1952: m_AffectedRows = 0; ds@1952: m_InsertID = 0; ds@1952: } else { ds@1952: m_LastErrorCode = SQLITE_OK; ds@1952: m_AffectedRows = (unsigned int)sqlite3_changes(db); ds@1952: m_InsertID = (unsigned int)sqlite3_last_insert_rowid(db); ds@1952: } ds@1952: ds@1952: /* Reset everything for the next execute */ ds@1952: sqlite3_reset(m_pStmt); ds@1952: sqlite3_clear_bindings(m_pStmt); ds@1952: ds@1952: return (m_LastErrorCode == SQLITE_OK); ds@1952: } ds@1952: ds@1952: const char *SqQuery::GetError(int *errCode/* =NULL */) ds@1952: { ds@1952: if (errCode) ds@1952: { ds@1952: *errCode = m_LastErrorCode; ds@1952: } ds@1952: return m_LastError.c_str(); ds@1952: } ds@1952: ds@1952: unsigned int SqQuery::GetAffectedRows() ds@1952: { ds@1952: return m_AffectedRows; ds@1952: } ds@1952: ds@1952: unsigned int SqQuery::GetInsertID() ds@1952: { ds@1952: return m_InsertID; ds@1952: } ds@1952: ds@1952: