core/smn_database.cpp
author David Anderson <dvander@alliedmods.net>
Fri Feb 22 02:28:33 2008 +0000 (2008-02-22)
changeset 1877 ff0bbf1bc094
parent 1874 6bd98dba0f2a
child 1912 506170374d35
permissions -rw-r--r--
added sourcemod's VDF file. yay
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 "sm_globals.h"
    33 #include "HandleSys.h"
    34 #include "Database.h"
    35 #include "ExtensionSys.h"
    36 #include "PluginSys.h"
    37 #include "sm_stringutil.h"
    38 
    39 HandleType_t hQueryType;
    40 HandleType_t hStmtType;
    41 
    42 class DatabaseHelpers : 
    43 	public SMGlobalClass,
    44 	public IHandleTypeDispatch
    45 {
    46 public:
    47 	virtual void OnSourceModAllInitialized()
    48 	{
    49 		HandleAccess acc;
    50 
    51 		/* Disable cloning */
    52 		g_HandleSys.InitAccessDefaults(NULL, &acc);
    53 		acc.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
    54 
    55 		TypeAccess tacc;
    56 
    57 		g_HandleSys.InitAccessDefaults(&tacc, NULL);
    58 		tacc.ident = g_pCoreIdent;
    59 
    60 		hQueryType = g_HandleSys.CreateType("IQuery", this, 0, &tacc, &acc, g_pCoreIdent, NULL);
    61 		hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, g_pCoreIdent, NULL);
    62 	}
    63 
    64 	virtual void OnSourceModShutdown()
    65 	{
    66 		g_HandleSys.RemoveType(hStmtType, g_pCoreIdent);
    67 		g_HandleSys.RemoveType(hQueryType, g_pCoreIdent);
    68 	}
    69 
    70 	virtual void OnHandleDestroy(HandleType_t type, void *object)
    71 	{
    72 		if (type == hQueryType)
    73 		{
    74 			IQuery *query = (IQuery *)object;
    75 			query->Destroy();
    76 		} else if (type == hStmtType) {
    77 			IPreparedQuery *query = (IPreparedQuery *)object;
    78 			query->Destroy();
    79 		}
    80 	}
    81 } s_DatabaseNativeHelpers;
    82 
    83 //is this safe for stmt handles? i think since it's single inheritance, it always will be.
    84 inline HandleError ReadQueryHndl(Handle_t hndl, IPluginContext *pContext, IQuery **query)
    85 {
    86 	HandleSecurity sec;
    87 	sec.pOwner = pContext->GetIdentity();
    88 	sec.pIdentity = g_pCoreIdent;
    89 
    90 	return g_HandleSys.ReadHandle(hndl, hQueryType, &sec, (void **)query);
    91 }
    92 
    93 inline HandleError ReadStmtHndl(Handle_t hndl, IPluginContext *pContext, IPreparedQuery **query)
    94 {
    95 	HandleSecurity sec;
    96 	sec.pOwner = pContext->GetIdentity();
    97 	sec.pIdentity = g_pCoreIdent;
    98 
    99 	return g_HandleSys.ReadHandle(hndl, hStmtType, &sec, (void **)query);
   100 }
   101 
   102 inline HandleError ReadDbOrStmtHndl(Handle_t hndl, IPluginContext *pContext, IDatabase **db, IPreparedQuery **query)
   103 {
   104 	HandleError err;
   105 	if ((err = g_DBMan.ReadHandle(hndl, DBHandle_Database, (void **)db)) == HandleError_Type)
   106 	{
   107 		*db = NULL;
   108 		return ReadStmtHndl(hndl, pContext, query);
   109 	}
   110 	return err;
   111 }
   112 
   113 class TQueryOp : public IDBThreadOperation
   114 {
   115 public:
   116 	TQueryOp(IDatabase *db, IPluginFunction *pf, const char *query, cell_t data) : 
   117 	  m_pDatabase(db), m_pFunction(pf), m_Query(query), m_Data(data),
   118 	  me(g_PluginSys.GetPluginByCtx(pf->GetParentContext()->GetContext())),
   119 	  m_pQuery(NULL)
   120 	{
   121 		/* We always increase the reference count because this is potentially
   122 		 * asynchronous.  Otherwise the original handle could be closed while 
   123 		 * we're still latched onto it.
   124 		 */
   125 		m_pDatabase->IncReferenceCount();
   126 
   127 		/* Now create our own Handle such that it can only be closed by us.
   128 		 * We allow cloning just in case someone wants to hold onto it.
   129 		 */
   130 		HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
   131 		HandleAccess access;
   132 		g_HandleSys.InitAccessDefaults(NULL, &access);
   133 		access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
   134 		m_MyHandle = g_HandleSys.CreateHandleEx(g_DBMan.GetDatabaseType(),
   135 			db,
   136 			&sec,
   137 			&access,
   138 			NULL);
   139 	}
   140 	~TQueryOp()
   141 	{
   142 		if (m_pQuery)
   143 		{
   144 			m_pQuery->Destroy();
   145 		}
   146 
   147 		/* Close our Handle if it's valid. */
   148 		if (m_MyHandle != BAD_HANDLE)
   149 		{
   150 			HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
   151 			g_HandleSys.FreeHandle(m_MyHandle, &sec);
   152 		} else {
   153 			/* Otherwise, there is an open ref to the db */
   154 			m_pDatabase->Close();
   155 		}
   156 	}
   157 	IdentityToken_t *GetOwner()
   158 	{
   159 		return me->GetIdentity();
   160 	}
   161 	IDBDriver *GetDriver()
   162 	{
   163 		return m_pDatabase->GetDriver();
   164 	}
   165 	void RunThreadPart()
   166 	{
   167 		m_pDatabase->LockForFullAtomicOperation();
   168 		m_pQuery = m_pDatabase->DoQuery(m_Query.c_str());
   169 		if (!m_pQuery)
   170 		{
   171 			UTIL_Format(error, sizeof(error), "%s", m_pDatabase->GetError());
   172 		}
   173 		m_pDatabase->UnlockFromFullAtomicOperation();
   174 	}
   175 	void CancelThinkPart()
   176 	{
   177 		m_pFunction->PushCell(BAD_HANDLE);
   178 		m_pFunction->PushCell(BAD_HANDLE);
   179 		m_pFunction->PushString("Driver is unloading");
   180 		m_pFunction->PushCell(m_Data);
   181 		m_pFunction->Execute(NULL);
   182 	}
   183 	void RunThinkPart()
   184 	{
   185 		/* Create a Handle for our query */
   186 		HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
   187 		HandleAccess access;
   188 		g_HandleSys.InitAccessDefaults(NULL, &access);
   189 		access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
   190 
   191 		Handle_t qh = BAD_HANDLE;
   192 		
   193 		if (m_pQuery)
   194 		{
   195 			qh = g_HandleSys.CreateHandle(hQueryType, m_pQuery, me->GetIdentity(), g_pCoreIdent, NULL);
   196 			if (qh != BAD_HANDLE)
   197 			{
   198 				m_pQuery = NULL;
   199 			} else {
   200 				UTIL_Format(error, sizeof(error), "Could not alloc handle");
   201 			}
   202 		}
   203 
   204 		m_pFunction->PushCell(m_MyHandle);
   205 		m_pFunction->PushCell(qh);
   206 		m_pFunction->PushString(qh == BAD_HANDLE ? error : "");
   207 		m_pFunction->PushCell(m_Data);
   208 		m_pFunction->Execute(NULL);
   209 
   210 		if (qh != BAD_HANDLE)
   211 		{
   212 			g_HandleSys.FreeHandle(qh, &sec);
   213 		}
   214 	}
   215 	void Destroy()
   216 	{
   217 		delete this;
   218 	}
   219 private:
   220 	IDatabase *m_pDatabase;
   221 	IPluginFunction *m_pFunction;
   222 	String m_Query;
   223 	cell_t m_Data;
   224 	CPlugin *me;
   225 	IQuery *m_pQuery;
   226 	char error[255];
   227 	Handle_t m_MyHandle;
   228 };
   229 
   230 class TConnectOp : public IDBThreadOperation
   231 {
   232 public:
   233 	TConnectOp(IPluginFunction *func, IDBDriver *driver, const char *_dbname, cell_t data)
   234 	{
   235 		m_pFunction = func;
   236 		m_pDriver = driver;
   237 		m_pDatabase = NULL;
   238 		m_Data = data;
   239 		error[0] = '\0';
   240 		strncopy(dbname, _dbname, sizeof(dbname));
   241 		me = g_PluginSys.GetPluginByCtx(m_pFunction->GetParentContext()->GetContext());
   242 	}
   243 	IdentityToken_t *GetOwner()
   244 	{
   245 		return me->GetIdentity();
   246 	}
   247 	IDBDriver *GetDriver()
   248 	{
   249 		return m_pDriver;
   250 	}
   251 	void RunThreadPart()
   252 	{
   253 		g_DBMan.LockConfig();
   254 		const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(dbname);
   255 		if (!pInfo)
   256 		{
   257 			UTIL_Format(error, sizeof(error), "Could not find database config \"%s\"", dbname);
   258 		} else {
   259 			m_pDatabase = m_pDriver->Connect(pInfo, false, error, sizeof(error));
   260 		}
   261 		g_DBMan.UnlockConfig();
   262 	}
   263 	void CancelThinkPart()
   264 	{
   265 		if (m_pDatabase)
   266 		{
   267 			m_pDatabase->Close();
   268 		}
   269 		m_pFunction->PushCell(BAD_HANDLE);
   270 		m_pFunction->PushCell(BAD_HANDLE);
   271 		m_pFunction->PushString("Driver is unloading");
   272 		m_pFunction->PushCell(m_Data);
   273 		m_pFunction->Execute(NULL);
   274 	}
   275 	void RunThinkPart()
   276 	{
   277 		Handle_t hndl = BAD_HANDLE;
   278 		
   279 		if (m_pDatabase)
   280 		{
   281 			if ((hndl = g_DBMan.CreateHandle(DBHandle_Database, m_pDatabase, me->GetIdentity()))
   282 				== BAD_HANDLE)
   283 			{
   284 				m_pDatabase->Close();
   285 				UTIL_Format(error, sizeof(error), "Unable to allocate Handle");
   286 			}
   287 		}
   288 
   289 		m_pFunction->PushCell(m_pDriver->GetHandle());
   290 		m_pFunction->PushCell(hndl);
   291 		m_pFunction->PushString(hndl == BAD_HANDLE ? error : "");
   292 		m_pFunction->PushCell(m_Data);
   293 		m_pFunction->Execute(NULL);
   294 	}
   295 	void Destroy()
   296 	{
   297 		delete this;
   298 	}
   299 private:
   300 	CPlugin *me;
   301 	IPluginFunction *m_pFunction;
   302 	IDBDriver *m_pDriver;
   303 	IDatabase *m_pDatabase;
   304 	char dbname[64];
   305 	char error[255];
   306 	cell_t m_Data;
   307 };
   308 
   309 static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
   310 {
   311 	char *conf, *err;
   312 
   313 	size_t maxlength = (size_t)params[4];
   314 	bool persistent = params[2] ? true : false;
   315 	pContext->LocalToString(params[1], &conf);
   316 	pContext->LocalToString(params[3], &err);
   317 	
   318 	IDBDriver *driver;
   319 	IDatabase *db;
   320 	if (!g_DBMan.Connect(conf, &driver, &db, persistent, err, maxlength))
   321 	{
   322 		return BAD_HANDLE;
   323 	}
   324 
   325 	Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
   326 	if (!hndl)
   327 	{
   328 		db->Close();
   329 		return BAD_HANDLE;
   330 	}
   331 
   332 	/* HACK! Add us to the dependency list */
   333 	CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
   334 	if (pExt)
   335 	{
   336 		g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
   337 	}
   338 
   339 	return hndl;
   340 }
   341 
   342 static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
   343 {
   344 	IPluginFunction *pf = pContext->GetFunctionById(params[1]);
   345 	if (!pf)
   346 	{
   347 		return pContext->ThrowNativeError("Function id %x is invalid", params[1]);
   348 	}
   349 
   350 	char *conf;
   351 	pContext->LocalToString(params[2], &conf);
   352 
   353 	IDBDriver *driver = NULL;
   354 	const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(conf);
   355 	char error[255];
   356 	if (pInfo != NULL)
   357 	{
   358 		if (pInfo->driver[0] == '\0')
   359 		{
   360 			driver = g_DBMan.GetDefaultDriver();
   361 		} else {
   362 			driver = g_DBMan.FindOrLoadDriver(pInfo->driver);
   363 		}
   364 		if (!driver)
   365 		{
   366 			UTIL_Format(error, 
   367 				sizeof(error), 
   368 				"Could not find driver \"%s\"", 
   369 				pInfo->driver[0] == '\0' ? g_DBMan.GetDefaultDriverName() : pInfo->driver);
   370 		} else if (!driver->IsThreadSafe()) {
   371 			UTIL_Format(error,
   372 				sizeof(error),
   373 				"Driver \"%s\" is not thread safe!",
   374 				driver->GetIdentifier());
   375 		}
   376 	} else {
   377 		UTIL_Format(error, sizeof(error), "Could not find database conf \"%s\"", conf);
   378 	}
   379 
   380 	if (!pInfo || !driver)
   381 	{
   382 		pf->PushCell(BAD_HANDLE);
   383 		pf->PushCell(BAD_HANDLE);
   384 		pf->PushString(error);
   385 		pf->PushCell(0);
   386 		pf->Execute(NULL);
   387 		return 0;
   388 	}
   389 
   390 	/* HACK! Add us to the dependency list */
   391 	CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
   392 	if (pExt)
   393 	{
   394 		g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
   395 	}
   396 
   397 	/* Finally, add to the thread if we can */
   398 	TConnectOp *op = new TConnectOp(pf, driver, conf, params[3]);
   399 	CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
   400 	if (pPlugin->GetProperty("DisallowDBThreads", NULL)
   401 		|| !g_DBMan.AddToThreadQueue(op, PrioQueue_High))
   402 	{
   403 		/* Do everything right now */
   404 		op->RunThreadPart();
   405 		op->RunThinkPart();
   406 		op->Destroy();
   407 	}
   408 
   409 	return 1;
   410 }
   411 
   412 static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
   413 {
   414 	IDBDriver *driver;
   415 	if (params[1] == BAD_HANDLE)
   416 	{
   417 		if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
   418 		{
   419 			return pContext->ThrowNativeError("Could not find any default driver");
   420 		}
   421 	} else {
   422 		HandleError err;
   423 		if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
   424 			!= HandleError_None)
   425 		{
   426 			return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
   427 		}
   428 	}
   429  
   430 	char *host, *user, *pass, *database, *error;
   431 	size_t maxlength = (size_t)params[7];
   432 	bool persistent = params[8] ? true : false;
   433 	unsigned int port = params[9];
   434 	unsigned int maxTimeout = params[10];
   435 	pContext->LocalToString(params[2], &host);
   436 	pContext->LocalToString(params[3], &user);
   437 	pContext->LocalToString(params[4], &pass);
   438 	pContext->LocalToString(params[5], &database);
   439 	pContext->LocalToString(params[6], &error);
   440 
   441 	DatabaseInfo info;
   442 	info.database = database;
   443 	info.driver = driver->GetIdentifier();
   444 	info.host = host;
   445 	info.maxTimeout = maxTimeout;
   446 	info.pass = pass;
   447 	info.port = port;
   448 	info.user = user;
   449 	
   450 	IDatabase *db = driver->Connect(&info, persistent, error, maxlength);
   451 
   452 	if (db)
   453 	{
   454 		Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
   455 		if (!hndl)
   456 		{
   457 			db->Close();
   458 			return BAD_HANDLE;
   459 		}
   460 
   461 		/* HACK! Add us to the dependency list */
   462 		CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
   463 		if (pExt)
   464 		{
   465 			g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
   466 		}
   467 
   468 		return hndl;
   469 	}
   470 	
   471 	return BAD_HANDLE;
   472 }
   473 
   474 static cell_t SQL_GetDriverIdent(IPluginContext *pContext, const cell_t *params)
   475 {
   476 	IDBDriver *driver;
   477 	if (params[1] == BAD_HANDLE)
   478 	{
   479 		if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
   480 		{
   481 			return pContext->ThrowNativeError("Could not find any default driver");
   482 		}
   483 	} else {
   484 		HandleError err;
   485 		if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
   486 			!= HandleError_None)
   487 		{
   488 			return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
   489 		}
   490 	}
   491 
   492 	pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
   493 
   494 	return 1;
   495 }
   496 
   497 static cell_t SQL_GetDriver(IPluginContext *pContext, const cell_t *params)
   498 {
   499 	char *name;
   500 	pContext->LocalToString(params[1], &name);
   501 
   502 	IDBDriver *driver = NULL;
   503 	if (name[0] == '\0')
   504 	{
   505 		driver = g_DBMan.GetDefaultDriver();
   506 	} else {
   507 		driver = g_DBMan.FindOrLoadDriver(name);
   508 	}
   509 
   510 	return (driver != NULL) ? driver->GetHandle() : BAD_HANDLE;
   511 }
   512 
   513 static cell_t SQL_GetDriverProduct(IPluginContext *pContext, const cell_t *params)
   514 {
   515 	IDBDriver *driver;
   516 	if (params[1] == BAD_HANDLE)
   517 	{
   518 		if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
   519 		{
   520 			return pContext->ThrowNativeError("Could not find any default driver");
   521 		}
   522 	} else {
   523 		HandleError err;
   524 		if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
   525 			!= HandleError_None)
   526 		{
   527 			return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
   528 		}
   529 	}
   530 
   531 	pContext->StringToLocalUTF8(params[2], params[3], driver->GetProductName(), NULL);
   532 
   533 	return 1;
   534 }
   535 
   536 static cell_t SQL_GetAffectedRows(IPluginContext *pContext, const cell_t *params)
   537 {
   538 	IDatabase *db = NULL;
   539 	IPreparedQuery *stmt = NULL;
   540 	HandleError err;
   541 
   542 	if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
   543 	{
   544 		return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
   545 	}
   546 
   547 	if (db)
   548 	{
   549 		return db->GetAffectedRows();
   550 	} else if (stmt) {
   551 		return stmt->GetAffectedRows();
   552 	}
   553 
   554 	return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
   555 }
   556 
   557 static cell_t SQL_GetInsertId(IPluginContext *pContext, const cell_t *params)
   558 {
   559 	IDatabase *db = NULL;
   560 	IPreparedQuery *stmt = NULL;
   561 	HandleError err;
   562 
   563 	if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
   564 	{
   565 		return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
   566 	}
   567 
   568 	if (db)
   569 	{
   570 		return db->GetInsertID();
   571 	} else if (stmt) {
   572 		return stmt->GetInsertID();
   573 	}
   574 
   575 	return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
   576 }
   577 
   578 static cell_t SQL_GetError(IPluginContext *pContext, const cell_t *params)
   579 {
   580 	IDatabase *db = NULL;
   581 	IPreparedQuery *stmt = NULL;
   582 	HandleError err;
   583 
   584 	if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
   585 	{
   586 		return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
   587 	}
   588 
   589 	const char *error = "";
   590 	if (db)
   591 	{
   592 		error = db->GetError();
   593 	} else if (stmt) {
   594 		error = stmt->GetError();
   595 	}
   596 
   597 	if (error[0] == '\0')
   598 	{
   599 		return false;
   600 	}
   601 
   602 	pContext->StringToLocalUTF8(params[2], params[3], error, NULL);
   603 
   604 	return 1;
   605 }
   606 
   607 static cell_t SQL_QuoteString(IPluginContext *pContext, const cell_t *params)
   608 {
   609 	IDatabase *db = NULL;
   610 	HandleError err;
   611 
   612 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   613 		!= HandleError_None)
   614 	{
   615 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   616 	}
   617 
   618 	char *input, *output;
   619 	size_t maxlength = (size_t)params[4];
   620 	pContext->LocalToString(params[2], &input);
   621 	pContext->LocalToString(params[3], &output);
   622 
   623 	size_t written;
   624 	bool s = db->QuoteString(input, output, maxlength, &written);
   625 
   626 	cell_t *addr;
   627 	pContext->LocalToPhysAddr(params[5], &addr);
   628 	*addr = (cell_t)written;
   629 
   630 	return s ? 1 : 0;
   631 }
   632 
   633 static cell_t SQL_FastQuery(IPluginContext *pContext, const cell_t *params)
   634 {
   635 	IDatabase *db = NULL;
   636 	HandleError err;
   637 
   638 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   639 		!= HandleError_None)
   640 	{
   641 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   642 	}
   643 
   644 	char *query;
   645 	pContext->LocalToString(params[2], &query);
   646 
   647 	return db->DoSimpleQuery(query) ? 1 : 0;
   648 }
   649 
   650 static cell_t SQL_Query(IPluginContext *pContext, const cell_t *params)
   651 {
   652 	IDatabase *db = NULL;
   653 	HandleError err;
   654 
   655 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   656 		!= HandleError_None)
   657 	{
   658 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   659 	}
   660 
   661 	char *query;
   662 	pContext->LocalToString(params[2], &query);
   663 
   664 	IQuery *qr = db->DoQuery(query);
   665 
   666 	if (!qr)
   667 	{
   668 		return BAD_HANDLE;
   669 	}
   670 
   671 	Handle_t hndl = g_HandleSys.CreateHandle(hQueryType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
   672 	if (hndl == BAD_HANDLE)
   673 	{
   674 		qr->Destroy();
   675 		return BAD_HANDLE;
   676 	}
   677 
   678 	return hndl;
   679 }
   680 
   681 static cell_t SQL_TQuery(IPluginContext *pContext, const cell_t *params)
   682 {
   683 	IDatabase *db = NULL;
   684 	HandleError err;
   685 
   686 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   687 		!= HandleError_None)
   688 	{
   689 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   690 	}
   691 
   692 	if (!db->GetDriver()->IsThreadSafe())
   693 	{
   694 		return pContext->ThrowNativeError("Driver \"%s\" is not thread safe!", db->GetDriver()->GetIdentifier());
   695 	}
   696 
   697 	IPluginFunction *pf = pContext->GetFunctionById(params[2]);
   698 	if (!pf)
   699 	{
   700 		return pContext->ThrowNativeError("Function id %x is invalid", params[2]);
   701 	}
   702 
   703 	char *query;
   704 	pContext->LocalToString(params[3], &query);
   705 
   706 	cell_t data = params[4];
   707 	PrioQueueLevel level = PrioQueue_Normal;
   708 	if (params[5] == (cell_t)PrioQueue_High)
   709 	{
   710 		level = PrioQueue_High;
   711 	} else if (params[5] == (cell_t)PrioQueue_Low) {
   712 		level = PrioQueue_Low;
   713 	}
   714 
   715 	CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
   716 
   717 	TQueryOp *op = new TQueryOp(db, pf, query, data);
   718 	if (pPlugin->GetProperty("DisallowDBThreads", NULL)
   719 		|| !g_DBMan.AddToThreadQueue(op, level))
   720 	{
   721 		/* Do everything right now */
   722 		op->RunThreadPart();
   723 		op->RunThinkPart();
   724 		op->Destroy();
   725 	}
   726 
   727 	return 1;
   728 }
   729 
   730 static cell_t SQL_LockDatabase(IPluginContext *pContext, const cell_t *params)
   731 {
   732 	IDatabase *db = NULL;
   733 	HandleError err;
   734 
   735 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   736 		!= HandleError_None)
   737 	{
   738 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   739 	}
   740 
   741 	db->LockForFullAtomicOperation();
   742 
   743 	return 1;
   744 }
   745 
   746 static cell_t SQL_UnlockDatabase(IPluginContext *pContext, const cell_t *params)
   747 {
   748 	IDatabase *db = NULL;
   749 	HandleError err;
   750 
   751 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   752 		!= HandleError_None)
   753 	{
   754 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   755 	}
   756 
   757 	db->UnlockFromFullAtomicOperation();
   758 
   759 	return 1;
   760 }
   761 
   762 static cell_t SQL_PrepareQuery(IPluginContext *pContext, const cell_t *params)
   763 {
   764 	IDatabase *db = NULL;
   765 	HandleError err;
   766 
   767 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
   768 		!= HandleError_None)
   769 	{
   770 		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
   771 	}
   772 
   773 	char *query, *error;
   774 	size_t maxlength = (size_t)params[4];
   775 	pContext->LocalToString(params[2], &query);
   776 	pContext->LocalToString(params[3], &error);
   777 
   778 	IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength);
   779 
   780 	if (!qr)
   781 	{
   782 		return BAD_HANDLE;
   783 	}
   784 
   785 	Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
   786 	if (hndl == BAD_HANDLE)
   787 	{
   788 		qr->Destroy();
   789 		return BAD_HANDLE;
   790 	}
   791 
   792 	return hndl;
   793 }
   794 
   795 static cell_t SQL_FetchMoreResults(IPluginContext *pContext, const cell_t *params)
   796 {
   797 	IQuery *query;
   798 	HandleError err;
   799 
   800 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   801 	{
   802 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   803 	}
   804 
   805 	return query->FetchMoreResults() ? 1 : 0;
   806 }
   807 
   808 static cell_t SQL_HasResultSet(IPluginContext *pContext, const cell_t *params)
   809 {
   810 	IQuery *query;
   811 	HandleError err;
   812 
   813 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   814 	{
   815 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   816 	}
   817 
   818 	return query->GetResultSet() != NULL ? true : false;
   819 }
   820 
   821 static cell_t SQL_GetRowCount(IPluginContext *pContext, const cell_t *params)
   822 {
   823 	IQuery *query;
   824 	HandleError err;
   825 
   826 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   827 	{
   828 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   829 	}
   830 
   831 	IResultSet *rs = query->GetResultSet();
   832 	if (!rs)
   833 	{
   834 		return 0;
   835 	}
   836 
   837 	return rs->GetRowCount();
   838 }
   839 
   840 static cell_t SQL_GetFieldCount(IPluginContext *pContext, const cell_t *params)
   841 {
   842 	IQuery *query;
   843 	HandleError err;
   844 
   845 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   846 	{
   847 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   848 	}
   849 
   850 	IResultSet *rs = query->GetResultSet();
   851 	if (!rs)
   852 	{
   853 		return 0;
   854 	}
   855 
   856 	return rs->GetFieldCount();
   857 }
   858 
   859 static cell_t SQL_FieldNumToName(IPluginContext *pContext, const cell_t *params)
   860 {
   861 	IQuery *query;
   862 	HandleError err;
   863 
   864 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   865 	{
   866 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   867 	}
   868 
   869 	IResultSet *rs = query->GetResultSet();
   870 	if (!rs)
   871 	{
   872 		return pContext->ThrowNativeError("No current result set");
   873 	}
   874 
   875 	unsigned int field = params[2];
   876 
   877 	const char *fldname;
   878 	if ((fldname = rs->FieldNumToName(field)) == NULL)
   879 	{
   880 		return pContext->ThrowNativeError("Invalid field index %d", field);
   881 	}
   882 
   883 	pContext->StringToLocalUTF8(params[3], params[4], fldname, NULL);
   884 
   885 	return 1;
   886 }
   887 
   888 static cell_t SQL_FieldNameToNum(IPluginContext *pContext, const cell_t *params)
   889 {
   890 	IQuery *query;
   891 	HandleError err;
   892 
   893 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   894 	{
   895 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   896 	}
   897 
   898 	IResultSet *rs = query->GetResultSet();
   899 	if (!rs)
   900 	{
   901 		return pContext->ThrowNativeError("No current result set");
   902 	}
   903 
   904 	char *field;
   905 	pContext->LocalToString(params[2], &field);
   906 
   907 	cell_t *num;
   908 	pContext->LocalToPhysAddr(params[3], &num);
   909 
   910 	return rs->FieldNameToNum(field, (unsigned int *)num) ? 1 : 0;
   911 }
   912 
   913 static cell_t SQL_FetchRow(IPluginContext *pContext, const cell_t *params)
   914 {
   915 	IQuery *query;
   916 	HandleError err;
   917 
   918 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   919 	{
   920 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   921 	}
   922 
   923 	IResultSet *rs = query->GetResultSet();
   924 	if (!rs)
   925 	{
   926 		return pContext->ThrowNativeError("No current result set");
   927 	}
   928 
   929 	return (rs->FetchRow() != NULL) ? true : false;
   930 }
   931 
   932 static cell_t SQL_MoreRows(IPluginContext *pContext, const cell_t *params)
   933 {
   934 	IQuery *query;
   935 	HandleError err;
   936 
   937 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   938 	{
   939 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   940 	}
   941 
   942 	IResultSet *rs = query->GetResultSet();
   943 	if (!rs)
   944 	{
   945 		return pContext->ThrowNativeError("No current result set");
   946 	}
   947 
   948 	return rs->MoreRows() ? 1 : 0;
   949 }
   950 
   951 static cell_t SQL_Rewind(IPluginContext *pContext, const cell_t *params)
   952 {
   953 	IQuery *query;
   954 	HandleError err;
   955 
   956 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   957 	{
   958 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   959 	}
   960 
   961 	IResultSet *rs = query->GetResultSet();
   962 	if (!rs)
   963 	{
   964 		return pContext->ThrowNativeError("No current result set");
   965 	}
   966 
   967 	return rs->Rewind() ? 1 : 0;
   968 }
   969 
   970 static cell_t SQL_FetchString(IPluginContext *pContext, const cell_t *params)
   971 {
   972 	IQuery *query;
   973 	HandleError err;
   974 
   975 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
   976 	{
   977 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
   978 	}
   979 
   980 	IResultSet *rs = query->GetResultSet();
   981 	if (!rs)
   982 	{
   983 		return pContext->ThrowNativeError("No current result set");
   984 	}
   985 
   986 	IResultRow *row = rs->CurrentRow();
   987 	if (!row)
   988 	{
   989 		return pContext->ThrowNativeError("Current result set has no fetched rows");
   990 	}
   991 
   992 	const char *str;
   993 	size_t length;
   994 	DBResult res = row->GetString(params[2], &str, &length);
   995 
   996 	if (res == DBVal_Error)
   997 	{
   998 		return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
   999 	} else if (res == DBVal_TypeMismatch) {
  1000 		return pContext->ThrowNativeError("Could not fetch data in field %d as a string", params[2]);
  1001 	}
  1002 
  1003 	pContext->StringToLocalUTF8(params[3], params[4], str, &length);
  1004 
  1005 	cell_t *addr;
  1006 	pContext->LocalToPhysAddr(params[5], &addr);
  1007 	*addr = (cell_t)res;
  1008 
  1009 	return (cell_t)length;
  1010 }
  1011 
  1012 static cell_t SQL_FetchFloat(IPluginContext *pContext, const cell_t *params)
  1013 {
  1014 	IQuery *query;
  1015 	HandleError err;
  1016 
  1017 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
  1018 	{
  1019 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
  1020 	}
  1021 
  1022 	IResultSet *rs = query->GetResultSet();
  1023 	if (!rs)
  1024 	{
  1025 		return pContext->ThrowNativeError("No current result set");
  1026 	}
  1027 
  1028 	IResultRow *row = rs->CurrentRow();
  1029 	if (!row)
  1030 	{
  1031 		return pContext->ThrowNativeError("Current result set has no fetched rows");
  1032 	}
  1033 
  1034 	float f;
  1035 	DBResult res = row->GetFloat(params[2], &f);
  1036 
  1037 	if (res == DBVal_Error)
  1038 	{
  1039 		return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
  1040 	} else if (res == DBVal_TypeMismatch) {
  1041 		return pContext->ThrowNativeError("Could not fetch data in field %d as a float", params[2]);
  1042 	}
  1043 
  1044 	cell_t *addr;
  1045 	pContext->LocalToPhysAddr(params[3], &addr);
  1046 	*addr = (cell_t)res;
  1047 
  1048 	return sp_ftoc(f);
  1049 }
  1050 
  1051 static cell_t SQL_FetchInt(IPluginContext *pContext, const cell_t *params)
  1052 {
  1053 	IQuery *query;
  1054 	HandleError err;
  1055 
  1056 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
  1057 	{
  1058 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
  1059 	}
  1060 
  1061 	IResultSet *rs = query->GetResultSet();
  1062 	if (!rs)
  1063 	{
  1064 		return pContext->ThrowNativeError("No current result set");
  1065 	}
  1066 
  1067 	IResultRow *row = rs->CurrentRow();
  1068 	if (!row)
  1069 	{
  1070 		return pContext->ThrowNativeError("Current result set has no fetched rows");
  1071 	}
  1072 
  1073 	int iv;
  1074 	DBResult res = row->GetInt(params[2], &iv);
  1075 
  1076 	if (res == DBVal_Error)
  1077 	{
  1078 		return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
  1079 	} else if (res == DBVal_TypeMismatch) {
  1080 		return pContext->ThrowNativeError("Could not fetch data in field %d as an integer", params[2]);
  1081 	}
  1082 
  1083 	cell_t *addr;
  1084 	pContext->LocalToPhysAddr(params[3], &addr);
  1085 	*addr = (cell_t)res;
  1086 
  1087 	return iv;
  1088 }
  1089 
  1090 static cell_t SQL_IsFieldNull(IPluginContext *pContext, const cell_t *params)
  1091 {
  1092 	IQuery *query;
  1093 	HandleError err;
  1094 
  1095 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
  1096 	{
  1097 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
  1098 	}
  1099 
  1100 	IResultSet *rs = query->GetResultSet();
  1101 	if (!rs)
  1102 	{
  1103 		return pContext->ThrowNativeError("No current result set");
  1104 	}
  1105 
  1106 	IResultRow *row = rs->CurrentRow();
  1107 	if (!row)
  1108 	{
  1109 		return pContext->ThrowNativeError("Current result set has no fetched rows");
  1110 	}
  1111 
  1112 	if ((unsigned)params[2] >= rs->GetFieldCount())
  1113 	{
  1114 		return pContext->ThrowNativeError("Invalid field index %d", params[2]);
  1115 	}
  1116 
  1117 	return row->IsNull(params[2]) ? 1 : 0;
  1118 }
  1119 
  1120 static cell_t SQL_FetchSize(IPluginContext *pContext, const cell_t *params)
  1121 {
  1122 	IQuery *query;
  1123 	HandleError err;
  1124 
  1125 	if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
  1126 	{
  1127 		return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
  1128 	}
  1129 
  1130 	IResultSet *rs = query->GetResultSet();
  1131 	if (!rs)
  1132 	{
  1133 		return pContext->ThrowNativeError("No current result set");
  1134 	}
  1135 
  1136 	IResultRow *row = rs->CurrentRow();
  1137 	if (!row)
  1138 	{
  1139 		return pContext->ThrowNativeError("Current result set has no fetched rows");
  1140 	}
  1141 
  1142 	if ((unsigned)params[2] >= rs->GetFieldCount())
  1143 	{
  1144 		return pContext->ThrowNativeError("Invalid field index %d", params[2]);
  1145 	}
  1146 
  1147 	return row->GetDataSize(params[2]);
  1148 }
  1149 
  1150 static cell_t SQL_BindParamInt(IPluginContext *pContext, const cell_t *params)
  1151 {
  1152 	IPreparedQuery *stmt;
  1153 	HandleError err;
  1154 
  1155 	if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
  1156 	{
  1157 		return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
  1158 	}
  1159 
  1160 	if (!stmt->BindParamInt(params[2], params[3], params[4] ? true : false))
  1161 	{
  1162 		return pContext->ThrowNativeError("Could not bind parameter %d as an integer", params[2]);
  1163 	}
  1164 
  1165 	return 1;
  1166 }
  1167 
  1168 static cell_t SQL_BindParamFloat(IPluginContext *pContext, const cell_t *params)
  1169 {
  1170 	IPreparedQuery *stmt;
  1171 	HandleError err;
  1172 
  1173 	if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
  1174 	{
  1175 		return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
  1176 	}
  1177 
  1178 	if (!stmt->BindParamFloat(params[2], sp_ctof(params[3])))
  1179 	{
  1180 		return pContext->ThrowNativeError("Could not bind parameter %d as a float", params[2]);
  1181 	}
  1182 
  1183 	return 1;
  1184 }
  1185 
  1186 static cell_t SQL_BindParamString(IPluginContext *pContext, const cell_t *params)
  1187 {
  1188 	IPreparedQuery *stmt;
  1189 	HandleError err;
  1190 
  1191 	if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
  1192 	{
  1193 		return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
  1194 	}
  1195 
  1196 	char *str;
  1197 	pContext->LocalToString(params[3], &str);
  1198 
  1199 	if (!stmt->BindParamString(params[2], str, params[4] ? true : false))
  1200 	{
  1201 		return pContext->ThrowNativeError("Could not bind parameter %d as a string", params[2]);
  1202 	}
  1203 
  1204 	return 1;
  1205 }
  1206 
  1207 static cell_t SQL_Execute(IPluginContext *pContext, const cell_t *params)
  1208 {
  1209 	IPreparedQuery *stmt;
  1210 	HandleError err;
  1211 
  1212 	if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
  1213 	{
  1214 		return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
  1215 	}
  1216 
  1217 	return stmt->Execute() ? 1 : 0;
  1218 }
  1219 
  1220 static cell_t SQL_IsSameConnection(IPluginContext *pContext, const cell_t *params)
  1221 {
  1222 	IDatabase *db1=NULL, *db2=NULL;
  1223 	HandleError err;
  1224 
  1225 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1))
  1226 		!= HandleError_None)
  1227 	{
  1228 		return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err);
  1229 	}
  1230 
  1231 	if ((err = g_DBMan.ReadHandle(params[2], DBHandle_Database, (void **)&db2))
  1232 		!= HandleError_None)
  1233 	{
  1234 		return pContext->ThrowNativeError("Invalid database Handle 2/%x (error: %d)", params[2], err);
  1235 	}
  1236 
  1237 	return (db1 == db2) ? true : false;
  1238 }
  1239 
  1240 static cell_t SQL_ReadDriver(IPluginContext *pContext, const cell_t *params)
  1241 {
  1242 	IDatabase *db1=NULL;
  1243 	HandleError err;
  1244 
  1245 	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1))
  1246 		!= HandleError_None)
  1247 	{
  1248 		return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err);
  1249 	}
  1250 
  1251 	IDBDriver *driver = db1->GetDriver();
  1252 
  1253 	pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
  1254 
  1255 	return driver->GetHandle();
  1256 }
  1257 
  1258 static cell_t SQL_CheckConfig(IPluginContext *pContext, const cell_t *params)
  1259 {
  1260 	char *name;
  1261 	pContext->LocalToString(params[1], &name);
  1262 
  1263 	return (g_DBMan.FindDatabaseConf(name) != NULL) ? 1 : 0;
  1264 }
  1265 
  1266 REGISTER_NATIVES(dbNatives)
  1267 {
  1268 	{"SQL_BindParamInt",		SQL_BindParamInt},
  1269 	{"SQL_BindParamFloat",		SQL_BindParamFloat},
  1270 	{"SQL_BindParamString",		SQL_BindParamString},\
  1271 	{"SQL_CheckConfig",			SQL_CheckConfig},
  1272 	{"SQL_Connect",				SQL_Connect},
  1273 	{"SQL_ConnectEx",			SQL_ConnectEx},
  1274 	{"SQL_EscapeString",		SQL_QuoteString},
  1275 	{"SQL_Execute",				SQL_Execute},
  1276 	{"SQL_FastQuery",			SQL_FastQuery},
  1277 	{"SQL_FetchFloat",			SQL_FetchFloat},
  1278 	{"SQL_FetchInt",			SQL_FetchInt},
  1279 	{"SQL_FetchMoreResults",	SQL_FetchMoreResults},
  1280 	{"SQL_FetchRow",			SQL_FetchRow},
  1281 	{"SQL_FetchSize",			SQL_FetchSize},
  1282 	{"SQL_FetchString",			SQL_FetchString},
  1283 	{"SQL_FieldNameToNum",		SQL_FieldNameToNum},
  1284 	{"SQL_FieldNumToName",		SQL_FieldNumToName},
  1285 	{"SQL_GetAffectedRows",		SQL_GetAffectedRows},
  1286 	{"SQL_GetDriver",			SQL_GetDriver},
  1287 	{"SQL_GetDriverIdent",		SQL_GetDriverIdent},
  1288 	{"SQL_GetDriverProduct",	SQL_GetDriverProduct},
  1289 	{"SQL_GetError",			SQL_GetError},
  1290 	{"SQL_GetFieldCount",		SQL_GetFieldCount},
  1291 	{"SQL_GetInsertId",			SQL_GetInsertId},
  1292 	{"SQL_GetRowCount",			SQL_GetRowCount},
  1293 	{"SQL_HasResultSet",		SQL_HasResultSet},
  1294 	{"SQL_IsFieldNull",			SQL_IsFieldNull},
  1295 	{"SQL_IsSameConnection",	SQL_IsSameConnection},
  1296 	{"SQL_LockDatabase",		SQL_LockDatabase},
  1297 	{"SQL_MoreRows",			SQL_MoreRows},
  1298 	{"SQL_PrepareQuery",		SQL_PrepareQuery},
  1299 	{"SQL_Query",				SQL_Query},
  1300 	{"SQL_QuoteString",			SQL_QuoteString},
  1301 	{"SQL_ReadDriver",			SQL_ReadDriver},
  1302 	{"SQL_Rewind",				SQL_Rewind},
  1303 	{"SQL_TConnect",			SQL_TConnect},
  1304 	{"SQL_TQuery",				SQL_TQuery},
  1305 	{"SQL_UnlockDatabase",		SQL_UnlockDatabase},
  1306 	{NULL,						NULL},
  1307 };