extensions/sqlite/driver/SqQuery.cpp
author David Anderson <dvander@alliedmods.net>
Fri Aug 23 00:18:13 2013 -0700 (2013-08-23)
changeset 4040 b6924c1a1d71
parent 2289 44045ead61cd
permissions -rw-r--r--
Add atomic reference counting and port DBI (bug 5876 part 3, r=ds).
     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 }
    40 
    41 SqQuery::~SqQuery()
    42 {
    43 	delete m_pResults;
    44 	sqlite3_finalize(m_pStmt);
    45 }
    46 
    47 IResultSet *SqQuery::GetResultSet()
    48 {
    49 	return m_pResults;
    50 }
    51 
    52 bool SqQuery::FetchMoreResults()
    53 {
    54 	/* We never have multiple result sets */
    55 	return false;
    56 }
    57 
    58 void SqQuery::Destroy()
    59 {
    60 	delete this;
    61 }
    62 
    63 bool SqQuery::BindParamFloat(unsigned int param, float f)
    64 {
    65 	/* SQLite is 1 indexed */
    66 	param++;
    67 
    68 	if (param > m_ParamCount)
    69 	{
    70 		return false;
    71 	}
    72 
    73 	return (sqlite3_bind_double(m_pStmt, param, (double)f) == SQLITE_OK);
    74 }
    75 
    76 bool SqQuery::BindParamNull(unsigned int param)
    77 {
    78 	/* SQLite is 1 indexed */
    79 	param++;
    80 
    81 	if (param > m_ParamCount)
    82 	{
    83 		return false;
    84 	}
    85 
    86 	return (sqlite3_bind_null(m_pStmt, param) == SQLITE_OK);
    87 }
    88 
    89 bool SqQuery::BindParamString(unsigned int param, const char *text, bool copy)
    90 {
    91 	/* SQLite is 1 indexed */
    92 	param++;
    93 
    94 	if (param > m_ParamCount)
    95 	{
    96 		return false;
    97 	}
    98 
    99 	return (sqlite3_bind_text(m_pStmt, param, text, -1, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
   100 }
   101 
   102 bool SqQuery::BindParamInt(unsigned int param, int num, bool signd/* =true */)
   103 {
   104 	/* SQLite is 1 indexed */
   105 	param++;
   106 
   107 	if (param > m_ParamCount)
   108 	{
   109 		return false;
   110 	}
   111 
   112 	return (sqlite3_bind_int(m_pStmt, param, num) == SQLITE_OK);
   113 }
   114 
   115 bool SqQuery::BindParamBlob(unsigned int param, const void *data, size_t length, bool copy)
   116 {
   117 	/* SQLite is 1 indexed */
   118 	param++;
   119 
   120 	if (param > m_ParamCount)
   121 	{
   122 		return false;
   123 	}
   124 
   125 	return (sqlite3_bind_blob(m_pStmt, param, data, length, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
   126 }
   127 
   128 sqlite3_stmt *SqQuery::GetStmt()
   129 {
   130 	return m_pStmt;
   131 }
   132 
   133 bool SqQuery::Execute()
   134 {
   135 	int rc;
   136 
   137 	/* If we don't have a result set and we have a column count, 
   138 	 * create a result set pre-emptively.  This is in case there
   139 	 * are no rows in the upcoming result set.
   140 	 */
   141 	if (!m_pResults && m_ColCount)
   142 	{
   143 		m_pResults = new SqResults(this);
   144 	}
   145 
   146 	/* If we've got results, throw them away */
   147 	if (m_pResults)
   148 	{
   149 		m_pResults->ResetResultCount();
   150 	}
   151 
   152 	/* Fetch each row, if any */
   153 	while ((rc = sqlite3_step(m_pStmt)) == SQLITE_ROW)
   154 	{
   155 		/* This should NEVER happen but we're being safe. */
   156 		if (!m_pResults)
   157 		{
   158 			m_pResults = new SqResults(this);
   159 		}
   160 		m_pResults->PushResult();
   161 	}
   162 
   163 	sqlite3 *db = m_pParent->GetDb();
   164 	if (rc != SQLITE_OK && rc != SQLITE_DONE && rc == sqlite3_errcode(db))
   165 	{
   166 		/* Something happened... */
   167 		m_LastErrorCode = rc;
   168 		m_LastError.assign(sqlite3_errmsg(db));
   169 		m_AffectedRows = 0;
   170 		m_InsertID = 0;
   171 	} else {
   172 		m_LastErrorCode = SQLITE_OK;
   173 		m_AffectedRows = (unsigned int)sqlite3_changes(db);
   174 		m_InsertID = (unsigned int)sqlite3_last_insert_rowid(db);
   175 	}
   176 
   177 	/* Reset everything for the next execute */
   178 	sqlite3_reset(m_pStmt);
   179 	sqlite3_clear_bindings(m_pStmt);
   180 
   181 	return (m_LastErrorCode == SQLITE_OK);
   182 }
   183 
   184 const char *SqQuery::GetError(int *errCode/* =NULL */)
   185 {
   186 	if (errCode)
   187 	{
   188 		*errCode = m_LastErrorCode;
   189 	}
   190 	return m_LastError.c_str();
   191 }
   192 
   193 unsigned int SqQuery::GetAffectedRows()
   194 {
   195 	return m_AffectedRows;
   196 }
   197 
   198 unsigned int SqQuery::GetInsertID()
   199 {
   200 	return m_InsertID;
   201 }
   202 
   203