3 * ===============================================================
4 * SourceMod SQLite Driver Extension
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ===============================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 SqQuery::SqQuery(SqDatabase *parent, sqlite3_stmt *stmt) :
27 m_pParent(parent), m_pStmt(stmt), m_pResults(NULL), m_AffectedRows(0), m_InsertID(0)
29 m_ParamCount = sqlite3_bind_parameter_count(m_pStmt);
30 m_ColCount = sqlite3_column_count(m_pStmt);
31 m_pParent->IncReferenceCount();
37 sqlite3_finalize(m_pStmt);
41 IResultSet *SqQuery::GetResultSet()
46 bool SqQuery::FetchMoreResults()
48 /* We never have multiple result sets */
52 void SqQuery::Destroy()
57 bool SqQuery::BindParamFloat(unsigned int param, float f)
59 /* SQLite is 1 indexed */
62 if (param > m_ParamCount)
67 return (sqlite3_bind_double(m_pStmt, param, (double)f) == SQLITE_OK);
70 bool SqQuery::BindParamNull(unsigned int param)
72 /* SQLite is 1 indexed */
75 if (param > m_ParamCount)
80 return (sqlite3_bind_null(m_pStmt, param) == SQLITE_OK);
83 bool SqQuery::BindParamString(unsigned int param, const char *text, bool copy)
85 /* SQLite is 1 indexed */
88 if (param > m_ParamCount)
93 return (sqlite3_bind_text(m_pStmt, param, text, -1, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
96 bool SqQuery::BindParamInt(unsigned int param, int num, bool signd/* =true */)
98 /* SQLite is 1 indexed */
101 if (param > m_ParamCount)
106 return (sqlite3_bind_int(m_pStmt, param, num) == SQLITE_OK);
109 bool SqQuery::BindParamBlob(unsigned int param, const void *data, size_t length, bool copy)
111 /* SQLite is 1 indexed */
114 if (param > m_ParamCount)
119 return (sqlite3_bind_blob(m_pStmt, param, data, length, copy ? SQLITE_TRANSIENT : SQLITE_STATIC) == SQLITE_OK);
122 sqlite3_stmt *SqQuery::GetStmt()
127 bool SqQuery::Execute()
131 /* If we don't have a result set and we have a column count,
132 * create a result set pre-emptively. This is in case there
133 * are no rows in the upcoming result set.
135 if (!m_pResults && m_ColCount)
137 m_pResults = new SqResults(this);
140 /* If we've got results, throw them away */
143 m_pResults->ResetResultCount();
146 /* Fetch each row, if any */
147 while ((rc = sqlite3_step(m_pStmt)) == SQLITE_ROW)
149 /* This should NEVER happen but we're being safe. */
152 m_pResults = new SqResults(this);
154 m_pResults->PushResult();
157 sqlite3 *db = m_pParent->GetDb();
158 if (rc != SQLITE_OK && rc != SQLITE_DONE && rc == sqlite3_errcode(db))
160 /* Something happened... */
161 m_LastErrorCode = rc;
162 m_LastError.assign(sqlite3_errmsg(db));
166 m_LastErrorCode = SQLITE_OK;
167 m_AffectedRows = (unsigned int)sqlite3_changes(db);
168 m_InsertID = (unsigned int)sqlite3_last_insert_rowid(db);
171 /* Reset everything for the next execute */
172 sqlite3_reset(m_pStmt);
173 sqlite3_clear_bindings(m_pStmt);
175 return (m_LastErrorCode == SQLITE_OK);
178 const char *SqQuery::GetError(int *errCode/* =NULL */)
182 *errCode = m_LastErrorCode;
184 return m_LastError.c_str();
187 unsigned int SqQuery::GetAffectedRows()
189 return m_AffectedRows;
192 unsigned int SqQuery::GetInsertID()