extensions/sqlite/driver/SqQuery.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 30 01:54:45 2009 -0700 (2009-08-30)
changeset 2790 6dd851ba15fd
parent 2289 44045ead61cd
child 4040 b6924c1a1d71
permissions -rw-r--r--
Fixed extension version reporting.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SQLite Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "SqQuery.h"
    33 
    34 SqQuery::SqQuery(SqDatabase *parent, sqlite3_stmt *stmt) : 
    35  m_pParent(parent), m_pStmt(stmt), m_pResults(NULL), m_AffectedRows(0), m_InsertID(0)
    36 {
    37 	m_ParamCount = sqlite3_bind_parameter_count(m_pStmt);
    38 	m_ColCount = sqlite3_column_count(m_pStmt);
    39 	m_pParent->IncReferenceCount();
    40 }
    41 
    42 SqQuery::~SqQuery()
    43 {
    44 	delete m_pResults;
    45 	sqlite3_finalize(m_pStmt);
    46 	m_pParent->Close();
    47 }
    48 
    49 IResultSet *SqQuery::GetResultSet()
    50 {
    51 	return m_pResults;
    52 }
    53 
    54 bool SqQuery::FetchMoreResults()
    55 {
    56 	/* We never have multiple result sets */
    57 	return false;
    58 }
    59 
    60 void SqQuery::Destroy()
    61 {
    62 	delete this;
    63 }
    64 
    65 bool SqQuery::BindParamFloat(unsigned int param, float f)
    66 {
    67 	/* SQLite is 1 indexed */
    68 	param++;
    69 
    70 	if (param > m_ParamCount)
    71 	{
    72 		return false;
    73 	}
    74 
    75 	return (sqlite3_bind_double(m_pStmt, param, (double)f) == SQLITE_OK);
    76 }
    77 
    78 bool SqQuery::BindParamNull(unsigned int param)
    79 {
    80 	/* SQLite is 1 indexed */
    81 	param++;
    82 
    83 	if (param > m_ParamCount)
    84 	{
    85 		return false;
    86 	}
    87 
    88 	return (sqlite3_bind_null(m_pStmt, param) == SQLITE_OK);
    89 }
    90 
    91 bool SqQuery::BindParamString(unsigned int param, const char *text, bool copy)
    92 {
    93 	/* SQLite is 1 indexed */
    94 	param++;
    95 
    96 	if (param > m_ParamCount)
    97 	{
    98 		return false;
    99 	}
   100 
   101 	return (sqlite3_bind_text(m_pStmt, param, text, -1, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
   102 }
   103 
   104 bool SqQuery::BindParamInt(unsigned int param, int num, bool signd/* =true */)
   105 {
   106 	/* SQLite is 1 indexed */
   107 	param++;
   108 
   109 	if (param > m_ParamCount)
   110 	{
   111 		return false;
   112 	}
   113 
   114 	return (sqlite3_bind_int(m_pStmt, param, num) == SQLITE_OK);
   115 }
   116 
   117 bool SqQuery::BindParamBlob(unsigned int param, const void *data, size_t length, bool copy)
   118 {
   119 	/* SQLite is 1 indexed */
   120 	param++;
   121 
   122 	if (param > m_ParamCount)
   123 	{
   124 		return false;
   125 	}
   126 
   127 	return (sqlite3_bind_blob(m_pStmt, param, data, length, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
   128 }
   129 
   130 sqlite3_stmt *SqQuery::GetStmt()
   131 {
   132 	return m_pStmt;
   133 }
   134 
   135 bool SqQuery::Execute()
   136 {
   137 	int rc;
   138 
   139 	/* If we don't have a result set and we have a column count, 
   140 	 * create a result set pre-emptively.  This is in case there
   141 	 * are no rows in the upcoming result set.
   142 	 */
   143 	if (!m_pResults && m_ColCount)
   144 	{
   145 		m_pResults = new SqResults(this);
   146 	}
   147 
   148 	/* If we've got results, throw them away */
   149 	if (m_pResults)
   150 	{
   151 		m_pResults->ResetResultCount();
   152 	}
   153 
   154 	/* Fetch each row, if any */
   155 	while ((rc = sqlite3_step(m_pStmt)) == SQLITE_ROW)
   156 	{
   157 		/* This should NEVER happen but we're being safe. */
   158 		if (!m_pResults)
   159 		{
   160 			m_pResults = new SqResults(this);
   161 		}
   162 		m_pResults->PushResult();
   163 	}
   164 
   165 	sqlite3 *db = m_pParent->GetDb();
   166 	if (rc != SQLITE_OK && rc != SQLITE_DONE && rc == sqlite3_errcode(db))
   167 	{
   168 		/* Something happened... */
   169 		m_LastErrorCode = rc;
   170 		m_LastError.assign(sqlite3_errmsg(db));
   171 		m_AffectedRows = 0;
   172 		m_InsertID = 0;
   173 	} else {
   174 		m_LastErrorCode = SQLITE_OK;
   175 		m_AffectedRows = (unsigned int)sqlite3_changes(db);
   176 		m_InsertID = (unsigned int)sqlite3_last_insert_rowid(db);
   177 	}
   178 
   179 	/* Reset everything for the next execute */
   180 	sqlite3_reset(m_pStmt);
   181 	sqlite3_clear_bindings(m_pStmt);
   182 
   183 	return (m_LastErrorCode == SQLITE_OK);
   184 }
   185 
   186 const char *SqQuery::GetError(int *errCode/* =NULL */)
   187 {
   188 	if (errCode)
   189 	{
   190 		*errCode = m_LastErrorCode;
   191 	}
   192 	return m_LastError.c_str();
   193 }
   194 
   195 unsigned int SqQuery::GetAffectedRows()
   196 {
   197 	return m_AffectedRows;
   198 }
   199 
   200 unsigned int SqQuery::GetInsertID()
   201 {
   202 	return m_InsertID;
   203 }
   204 
   205