Add atomic reference counting and port DBI (
bug 5876 part 3, r=ds).
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
4 * SourceMod SQLite Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "extension.h"
33 #include "SqDatabase.h"
36 SqDatabase::SqDatabase(sqlite3 *sq3, bool persistent) :
37 m_sq3(sq3), m_Persistent(persistent)
41 SqDatabase::~SqDatabase()
44 g_SqDriver.RemovePersistent(this);
48 void SqDatabase::IncReferenceCount()
53 bool SqDatabase::Close()
58 const char *SqDatabase::GetError(int *errorCode/* =NULL */)
60 return sqlite3_errmsg(m_sq3);
63 bool SqDatabase::LockForFullAtomicOperation()
66 m_FullLock = new ke::Mutex();
72 void SqDatabase::UnlockFromFullAtomicOperation()
78 IDBDriver *SqDatabase::GetDriver()
83 bool SqDatabase::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newSize)
85 char *res = sqlite3_snprintf(static_cast<int>(maxlen), buffer, "%q", str);
87 if (res != NULL && newSize != NULL)
89 *newSize = strlen(buffer);
95 unsigned int SqDatabase::GetInsertID()
97 return (unsigned int)sqlite3_last_insert_rowid(m_sq3);
100 unsigned int SqDatabase::GetAffectedRows()
102 return (unsigned int)sqlite3_changes(m_sq3);
105 bool SqDatabase::DoSimpleQuery(const char *query)
107 IQuery *pQuery = DoQuery(query);
116 /* this sounds like daiquiri.. i'm tired. */
117 IQuery *SqDatabase::DoQuery(const char *query)
119 IPreparedQuery *pQuery = PrepareQuery(query, NULL, 0, NULL);
124 if (!pQuery->Execute())
132 bool SqDatabase::DoSimpleQueryEx(const char *query, size_t len)
134 IQuery *pQuery = DoQueryEx(query, len);
143 IQuery *SqDatabase::DoQueryEx(const char *query, size_t len)
145 IPreparedQuery *pQuery = PrepareQueryEx(query, len, NULL, 0, NULL);
150 if (!pQuery->Execute())
158 unsigned int SqDatabase::GetAffectedRowsForQuery(IQuery *query)
160 return static_cast<SqQuery*>(query)->GetAffectedRows();
162 unsigned int SqDatabase::GetInsertIDForQuery(IQuery *query)
164 return static_cast<SqQuery*>(query)->GetInsertID();
167 IPreparedQuery *SqDatabase::PrepareQuery(const char *query,
170 int *errCode/* =NULL */)
172 sqlite3_stmt *stmt = NULL;
173 if ((m_LastErrorCode = sqlite3_prepare_v2(m_sq3, query, -1, &stmt, NULL)) != SQLITE_OK
177 if (m_LastErrorCode != SQLITE_OK)
179 msg = sqlite3_errmsg(m_sq3);
181 msg = "Invalid query string";
182 m_LastErrorCode = SQLITE_ERROR;
186 strncopy(error, msg, maxlength);
188 m_LastError.assign(msg);
191 return new SqQuery(this, stmt);
194 IPreparedQuery *SqDatabase::PrepareQueryEx(const char *query,
198 int *errCode/* =NULL */)
200 sqlite3_stmt *stmt = NULL;
201 if ((m_LastErrorCode = sqlite3_prepare_v2(m_sq3, query, len, &stmt, NULL)) != SQLITE_OK
205 if (m_LastErrorCode != SQLITE_OK)
207 msg = sqlite3_errmsg(m_sq3);
209 msg = "Invalid query string";
210 m_LastErrorCode = SQLITE_ERROR;
214 strncopy(error, msg, maxlength);
216 m_LastError.assign(msg);
219 return new SqQuery(this, stmt);
222 sqlite3 *SqDatabase::GetDb()
227 bool SqDatabase::SetCharacterSet(const char *characterset)
229 // sqlite only supports utf8 and utf16 - by the time the database is created. It's too late here.