extensions/sqlite/driver/SqQuery.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sqlite/driver/SqQuery.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,205 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod SQLite Extension
     1.8 - * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#include "SqQuery.h"
    1.36 -
    1.37 -SqQuery::SqQuery(SqDatabase *parent, sqlite3_stmt *stmt) : 
    1.38 - m_pParent(parent), m_pStmt(stmt), m_pResults(NULL), m_AffectedRows(0), m_InsertID(0)
    1.39 -{
    1.40 -	m_ParamCount = sqlite3_bind_parameter_count(m_pStmt);
    1.41 -	m_ColCount = sqlite3_column_count(m_pStmt);
    1.42 -	m_pParent->IncReferenceCount();
    1.43 -}
    1.44 -
    1.45 -SqQuery::~SqQuery()
    1.46 -{
    1.47 -	delete m_pResults;
    1.48 -	sqlite3_finalize(m_pStmt);
    1.49 -	m_pParent->Close();
    1.50 -}
    1.51 -
    1.52 -IResultSet *SqQuery::GetResultSet()
    1.53 -{
    1.54 -	return m_pResults;
    1.55 -}
    1.56 -
    1.57 -bool SqQuery::FetchMoreResults()
    1.58 -{
    1.59 -	/* We never have multiple result sets */
    1.60 -	return false;
    1.61 -}
    1.62 -
    1.63 -void SqQuery::Destroy()
    1.64 -{
    1.65 -	delete this;
    1.66 -}
    1.67 -
    1.68 -bool SqQuery::BindParamFloat(unsigned int param, float f)
    1.69 -{
    1.70 -	/* SQLite is 1 indexed */
    1.71 -	param++;
    1.72 -
    1.73 -	if (param > m_ParamCount)
    1.74 -	{
    1.75 -		return false;
    1.76 -	}
    1.77 -
    1.78 -	return (sqlite3_bind_double(m_pStmt, param, (double)f) == SQLITE_OK);
    1.79 -}
    1.80 -
    1.81 -bool SqQuery::BindParamNull(unsigned int param)
    1.82 -{
    1.83 -	/* SQLite is 1 indexed */
    1.84 -	param++;
    1.85 -
    1.86 -	if (param > m_ParamCount)
    1.87 -	{
    1.88 -		return false;
    1.89 -	}
    1.90 -
    1.91 -	return (sqlite3_bind_null(m_pStmt, param) == SQLITE_OK);
    1.92 -}
    1.93 -
    1.94 -bool SqQuery::BindParamString(unsigned int param, const char *text, bool copy)
    1.95 -{
    1.96 -	/* SQLite is 1 indexed */
    1.97 -	param++;
    1.98 -
    1.99 -	if (param > m_ParamCount)
   1.100 -	{
   1.101 -		return false;
   1.102 -	}
   1.103 -
   1.104 -	return (sqlite3_bind_text(m_pStmt, param, text, -1, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
   1.105 -}
   1.106 -
   1.107 -bool SqQuery::BindParamInt(unsigned int param, int num, bool signd/* =true */)
   1.108 -{
   1.109 -	/* SQLite is 1 indexed */
   1.110 -	param++;
   1.111 -
   1.112 -	if (param > m_ParamCount)
   1.113 -	{
   1.114 -		return false;
   1.115 -	}
   1.116 -
   1.117 -	return (sqlite3_bind_int(m_pStmt, param, num) == SQLITE_OK);
   1.118 -}
   1.119 -
   1.120 -bool SqQuery::BindParamBlob(unsigned int param, const void *data, size_t length, bool copy)
   1.121 -{
   1.122 -	/* SQLite is 1 indexed */
   1.123 -	param++;
   1.124 -
   1.125 -	if (param > m_ParamCount)
   1.126 -	{
   1.127 -		return false;
   1.128 -	}
   1.129 -
   1.130 -	return (sqlite3_bind_blob(m_pStmt, param, data, length, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
   1.131 -}
   1.132 -
   1.133 -sqlite3_stmt *SqQuery::GetStmt()
   1.134 -{
   1.135 -	return m_pStmt;
   1.136 -}
   1.137 -
   1.138 -bool SqQuery::Execute()
   1.139 -{
   1.140 -	int rc;
   1.141 -
   1.142 -	/* If we don't have a result set and we have a column count, 
   1.143 -	 * create a result set pre-emptively.  This is in case there
   1.144 -	 * are no rows in the upcoming result set.
   1.145 -	 */
   1.146 -	if (!m_pResults && m_ColCount)
   1.147 -	{
   1.148 -		m_pResults = new SqResults(this);
   1.149 -	}
   1.150 -
   1.151 -	/* If we've got results, throw them away */
   1.152 -	if (m_pResults)
   1.153 -	{
   1.154 -		m_pResults->ResetResultCount();
   1.155 -	}
   1.156 -
   1.157 -	/* Fetch each row, if any */
   1.158 -	while ((rc = sqlite3_step(m_pStmt)) == SQLITE_ROW)
   1.159 -	{
   1.160 -		/* This should NEVER happen but we're being safe. */
   1.161 -		if (!m_pResults)
   1.162 -		{
   1.163 -			m_pResults = new SqResults(this);
   1.164 -		}
   1.165 -		m_pResults->PushResult();
   1.166 -	}
   1.167 -
   1.168 -	sqlite3 *db = m_pParent->GetDb();
   1.169 -	if (rc != SQLITE_OK && rc != SQLITE_DONE && rc == sqlite3_errcode(db))
   1.170 -	{
   1.171 -		/* Something happened... */
   1.172 -		m_LastErrorCode = rc;
   1.173 -		m_LastError.assign(sqlite3_errmsg(db));
   1.174 -		m_AffectedRows = 0;
   1.175 -		m_InsertID = 0;
   1.176 -	} else {
   1.177 -		m_LastErrorCode = SQLITE_OK;
   1.178 -		m_AffectedRows = (unsigned int)sqlite3_changes(db);
   1.179 -		m_InsertID = (unsigned int)sqlite3_last_insert_rowid(db);
   1.180 -	}
   1.181 -
   1.182 -	/* Reset everything for the next execute */
   1.183 -	sqlite3_reset(m_pStmt);
   1.184 -	sqlite3_clear_bindings(m_pStmt);
   1.185 -
   1.186 -	return (m_LastErrorCode == SQLITE_OK);
   1.187 -}
   1.188 -
   1.189 -const char *SqQuery::GetError(int *errCode/* =NULL */)
   1.190 -{
   1.191 -	if (errCode)
   1.192 -	{
   1.193 -		*errCode = m_LastErrorCode;
   1.194 -	}
   1.195 -	return m_LastError.c_str();
   1.196 -}
   1.197 -
   1.198 -unsigned int SqQuery::GetAffectedRows()
   1.199 -{
   1.200 -	return m_AffectedRows;
   1.201 -}
   1.202 -
   1.203 -unsigned int SqQuery::GetInsertID()
   1.204 -{
   1.205 -	return m_InsertID;
   1.206 -}
   1.207 -
   1.208 -