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 <sm_platform.h>
33 #include "extension.h"
35 #include "SqDatabase.h"
39 unsigned int strncopy(char *dest, const char *src, size_t count)
47 while ((*src) && (--count))
53 return (dest - start);
56 int busy_handler(void *unused1, int unused2)
58 #if defined PLATFORM_WINDOWS
60 #elif defined PLATFORM_LINUX
68 m_Handle = BAD_HANDLE;
70 m_bThreadSafe = false;
73 void SqDriver::Initialize()
75 m_pOpenLock = threader->MakeMutex();
77 InitializeThreadSafety();
80 void SqDriver::Shutdown()
84 m_pOpenLock->DestroyThis();
89 sqlite3_enable_shared_cache(0);
93 bool SqDriver::IsThreadSafe()
98 bool SqDriver::InitializeThreadSafety()
105 if (sqlite3_threadsafe() == 0)
110 if (sqlite3_enable_shared_cache(1) != SQLITE_OK)
115 m_bThreadSafe = true;
120 void SqDriver::ShutdownThreadSafety()
125 IdentityToken_t *SqDriver::GetIdentity()
127 return myself->GetIdentity();
130 const char *SqDriver::GetProductName()
135 const char *SqDriver::GetIdentifier()
140 Handle_t SqDriver::GetHandle()
142 if (m_Handle == BAD_HANDLE)
144 m_Handle = dbi->CreateHandle(DBHandle_Driver, this, myself->GetIdentity());
150 inline bool IsPathSepChar(char c)
152 #if defined PLATFORM_WINDOWS
153 return (c == '\\' || c == '/');
154 #elif defined PLATFORM_LINUX
159 IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *error, size_t maxlength)
161 /* We wrap most of the open process in a mutex just to be safe */
164 /* Format our path */
165 char path[PLATFORM_MAX_PATH];
166 size_t len = libsys->PathFormat(path, sizeof(path), "sqlite/%s", info->database);
168 /* Chop any filename off */
169 for (size_t i = len-1;
170 i >= 0 && i <= len-1;
173 if (IsPathSepChar(path[i]))
180 /* Test the full path */
181 char fullpath[PLATFORM_MAX_PATH];
182 g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/%s", path);
183 if (!libsys->IsPathDirectory(fullpath))
185 /* Make sure the data folder exists */
186 len = g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data");
187 if (!libsys->IsPathDirectory(fullpath))
189 if (!libsys->CreateFolder(fullpath))
191 strncopy(error, "Could not create or open \"data\" folder\"", maxlength);
192 m_pOpenLock->Unlock();
197 /* The data folder exists - create each subdir as needed! */
198 char *cur_ptr = path;
202 /* Find the next suitable path */
203 char *next_ptr = cur_ptr;
204 while (*next_ptr != '\0')
206 if (IsPathSepChar(*next_ptr))
214 if (*next_ptr == '\0')
218 len += libsys->PathFormat(&fullpath[len], sizeof(fullpath)-len, "/%s", cur_ptr);
219 if (!libsys->IsPathDirectory(fullpath) && !libsys->CreateFolder(fullpath))
227 /* Build the FINAL path. */
228 g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/sqlite/%s.sq3", info->database);
230 /* If we're requesting a persistent connection, see if something is already open */
233 /* See if anything in the cache matches */
234 List<SqDbInfo>::iterator iter;
235 for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
237 if ((*iter).path.compare(fullpath) == 0)
239 (*iter).db->IncReferenceCount();
240 m_pOpenLock->Unlock();
246 /* Try to open a new connection */
248 int err = sqlite3_open(fullpath, &sql);
249 if (err != SQLITE_OK)
251 strncopy(error, sqlite3_errmsg(sql), maxlength);
253 m_pOpenLock->Unlock();
257 sqlite3_busy_handler(sql, busy_handler, NULL);
259 SqDatabase *pdb = new SqDatabase(sql, persistent);
264 pinfo.path = fullpath;
266 m_Cache.push_back(pinfo);
269 m_pOpenLock->Unlock();
274 void SqDriver::RemovePersistent(IDatabase *pdb)
276 List<SqDbInfo>::iterator iter;
277 for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
279 if ((*iter).db == pdb)
281 iter = m_Cache.erase(iter);