3 * ================================================================
4 * SourceMod SQLite 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 * version 3.0, as published by the Free Software Foundation.
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, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include <sm_platform.h>
34 #include "extension.h"
36 #include "SqDatabase.h"
40 unsigned int strncopy(char *dest, const char *src, size_t count)
48 while ((*src) && (--count))
54 return (dest - start);
59 m_Handle = BAD_HANDLE;
63 void SqDriver::Initialize()
65 m_pOpenLock = threader->MakeMutex();
68 void SqDriver::Shutdown()
72 m_pOpenLock->DestroyThis();
76 bool SqDriver::IsThreadSafe()
81 bool SqDriver::InitializeThreadSafety()
83 /* sqlite should be thread safe if the locks are done right.
84 * we don't enable the "shared cache" because it can corrupt
90 void SqDriver::ShutdownThreadSafety()
95 IdentityToken_t *SqDriver::GetIdentity()
97 return myself->GetIdentity();
100 const char *SqDriver::GetProductName()
105 const char *SqDriver::GetIdentifier()
110 Handle_t SqDriver::GetHandle()
112 if (m_Handle == BAD_HANDLE)
114 m_Handle = dbi->CreateHandle(DBHandle_Driver, this, myself->GetIdentity());
120 inline bool IsPathSepChar(char c)
122 #if defined PLATFORM_WINDOWS
123 return (c == '\\' || c == '/');
124 #elif defined PLATFORM_LINUX
129 IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *error, size_t maxlength)
131 /* We wrap most of the open process in a mutex just to be safe */
134 /* Format our path */
135 char path[PLATFORM_MAX_PATH];
136 size_t len = libsys->PathFormat(path, sizeof(path), "sqlite/%s", info->database);
138 /* Chop any filename off */
139 for (size_t i = len-1;
140 i >= 0 && i <= len-1;
143 if (IsPathSepChar(path[i]))
150 /* Test the full path */
151 char fullpath[PLATFORM_MAX_PATH];
152 g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/%s", path);
153 if (!libsys->IsPathDirectory(fullpath))
155 /* Make sure the data folder exists */
156 len = g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data");
157 if (!libsys->IsPathDirectory(fullpath))
159 if (!libsys->CreateFolder(fullpath))
161 strncopy(error, "Could not create or open \"data\" folder\"", maxlength);
162 m_pOpenLock->Unlock();
167 /* The data folder exists - create each subdir as needed! */
168 char *cur_ptr = path;
172 /* Find the next suitable path */
173 char *next_ptr = cur_ptr;
174 while (*next_ptr != '\0')
176 if (IsPathSepChar(*next_ptr))
184 if (*next_ptr == '\0')
188 len += libsys->PathFormat(&fullpath[len], sizeof(fullpath)-len, "/%s", cur_ptr);
189 if (!libsys->IsPathDirectory(fullpath) && !libsys->CreateFolder(fullpath))
197 /* Build the FINAL path. */
198 g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/sqlite/%s.sq3", info->database);
200 /* If we're requesting a persistent connection, see if something is already open */
203 /* See if anything in the cache matches */
204 List<SqDbInfo>::iterator iter;
205 for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
207 if ((*iter).path.compare(fullpath) == 0)
209 (*iter).db->IncReferenceCount();
210 m_pOpenLock->Unlock();
216 /* Try to open a new connection */
218 int err = sqlite3_open(fullpath, &sql);
219 if (err != SQLITE_OK)
221 strncopy(error, sqlite3_errmsg(sql), maxlength);
223 m_pOpenLock->Unlock();
227 SqDatabase *pdb = new SqDatabase(sql, persistent);
232 pinfo.path = fullpath;
234 m_Cache.push_back(pinfo);
237 m_pOpenLock->Unlock();
242 void SqDriver::RemovePersistent(IDatabase *pdb)
244 List<SqDbInfo>::iterator iter;
245 for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
247 if ((*iter).db == pdb)
249 iter = m_Cache.erase(iter);