All pastes #505549 Raw Edit

Mine

public text v1 · immutable
·published 2007-05-23 16:53 UTC
rendered paste body
/*         /\/\   __ _ _ __   __ _  ___ / _\ ___ _ __(_)_ __ | |_ 
 *        /    \ / _` | '_ \ / _` |/ _ \\ \ / __| '__| | '_ \| __|
 *       / /\/\ \ (_| | | | | (_| | (_) |\ \ (__| |  | | |_) | |_ 
 *       \/    \/\__,_|_| |_|\__, |\___/\__/\___|_|  |_| .__/ \__|
 *                           |___/                     |_|     
 *                        A WHOLE NEW WORLD
 *         MangoScript - Bringing a whole new world to MaNGOS
 *
 *      Lua_Database.cpp - Exports misc related functions to LUA
 *
 *      (c) Copyright 2006-2007 by the By the MangoScript Team <http://www.mangoscript.org>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *  
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *  
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *      $Id: Lua_Database.cpp 129 2007-02-23 16:43:15Z andrejkw $
 */

#include "Lua_Database.h"
#include "Lua_uint64.h"
#include "Database/DatabaseEnv.h"
#include "Database/Database.h"
#include "Database/impl/MySQLDatabase.h"
/*#include "Database/impl/PostgreDatabase.h"*/


/* Functions for QueryResult */
lua_uint64 lb_QueryResult_GetRowCount_u64(QueryResult* qr)
{
	  return lua_uint64(qr->GetRowCount());
}

luabind::object lb_QueryResult_Fetch(lua_State* L, QueryResult* qr)
{
	Field* fields = qr->Fetch();
	luabind::object lua_fields = luabind::newtable(L);

	for (int x = 0; x < qr->GetFieldCount(); x++)
		lua_fields[x] = &fields[x];

	return lua_fields;
}

/* Functions for Field */
lua_uint64 lb_Field_GetUInt64(Field* f) { return lua_uint64(f->GetUInt64()); }

/* Functions for SpellEntry */
uint32 lb_SpellEntry_Totem(SpellEntry se, uint8 i) { return ((i <= 2) ? se.Totem[i] : 0); }
uint32 lb_SpellEntry_Reagent(SpellEntry se, uint8 i) { return ((i <= 8) ? se.Reagent[i] : 0); }
uint32 lb_SpellEntry_ReagentCount(SpellEntry se, uint8 i) { return ((i <= 8) ? se.ReagentCount[i] : 0); }
uint32 lb_SpellEntry_Effect(SpellEntry se, uint8 i) { return ((i <= 3) ? se.Effect[i] : 0); }
uint32 lb_SpellEntry_EffectImplicitTargetA(SpellEntry se, uint8 i) { return ((i <= 3) ? se.EffectImplicitTargetA[i] : 0); }
uint32 lb_SpellEntry_EffectImplicitTargetB(SpellEntry se, uint8 i) { return ((i <= 3) ? se.EffectImplicitTargetB[i] : 0); }

/* Functions for Database */
/*Database*__w64 lb_sDatabase() { return &sDatabase; }
Database*__w64 lb_loginDatabase() { return &loginDatabase; }*/
 Database* lb_sDatabase() { return &sDatabase; }
 Database* lb_loginDatabase() { return &loginDatabase; }
 


void lb_QueryResult_Free(QueryResult* res) { if (res) delete res; }

/*Database* lb_OpenMysqlConnection(const char* loginInformation)*/
MySQLDatabase* lb_OpenMysqlConnection(const char* Hostname, unsigned int port,
        const char* Username, const char* Password, const char* DatabaseName,
        uint32 ConnectionCount, uint32 BufferSize)
{
	MySQLDatabase* db = new MySQLDatabase();

	//if (!db->Initialize(loginInformation, DATABASE_TYPE_MYSQL))
	if (!db->Initialize(Hostname, port, Username, Password, DatabaseName, ConnectionCount, BufferSize))
		return NULL;
	else
		return db;
}

void lb_Database_Close(Database* db)
{
	if ((db) && (db != &sDatabase) && (db != &loginDatabase))
		delete db;
}

/* Functions for DBCStorage */
DBCStorage <SpellEntry>* lb_sSpellStore() { return &sSpellStore; }
DBCStorage <SpellCastTimesEntry>* lb_sCastTimesStore() { return &sCastTimesStore; }

int lb_Export_Database(lua_State* L)
{
	using namespace luabind;

	/* Lua (Database) Global Functions */
	module(L)
	[
		def("sDatabase", &lb_sDatabase),
		def("loginDatabase", &lb_loginDatabase),

		def("sSpellStore", &lb_sSpellStore),
		def("sCastTimesStore", &lb_sCastTimesStore),

		def("OpenMysqlConnection", &lb_OpenMysqlConnection),

		def("GetDuration", &GetDuration),
		def("GetMaxDuration", &GetMaxDuration),
		/* def("IsRankSpellDueToSpell", &IsRankSpellDueToSpell),
		def("IsNoStackSpellDueToSpell", &IsNoStackSpellDueToSpell), */
		def("IsNoStackAuraDueToAura", &IsNoStackAuraDueToAura),
		def("CompareAuraRanks", &CompareAuraRanks),
		def("IsSpellSingleEffectPerCaster", &IsSpellSingleEffectPerCaster),
		def("IsPassiveSpell", &IsPassiveSpell),
		def("IsPositiveSpell", &IsPositiveSpell),
		def("IsPositiveEffect", &IsPositiveEffect),
		def("IsSingleTarget", &IsSingleTarget),
		def("CanUsedWhileStealthed", &CanUsedWhileStealthed)
	];

	/* From MySQLDatabase.h */
	module(L)
	[
		class_<MySQLDatabase>("MySQLDatabase")
			.def("Close", &lb_Database_Close)
			/*.def("PQuery", &MySQLDatabase::PQuery)*/
			.def("Query", &MySQLDatabase::Query)		
			.def("Execute", &MySQLDatabase::Execute)
			/*.def("PExecute", &MySQLDatabase::PExecute)*/
			/*.def("BeginTransaction", &MySQLDatabase::StartTransaction)*/
			/*.def("CommitTransaction", &MySQLDatabase::CommitTransaction)*/
			/*.def("RollbackTransaction", &MySQLDatabase::RollbackTransaction)*/
			/*.def("escape_string", (void (MySQLDatabase::*)(std::string&))&MySQLDatabase::escape_string)*/
			/*.def("ThreadStart", &MySQLDatabase::ThreadStart)*/
			/*.def("ThreadEnd", &MySQLDatabase::ThreadEnd)*/
	];

		/* From Database.h */
	module(L)
	[
		class_<Database>("Database")
			/*.def("Close", &lb_Database_Close)*/
			/*.def("PQuery", &Database::PQuery)*/
			/*.def("Query", &Database::Query)*/
			/*.def("Execute", &Database::Execute)*/
			/*.def("PExecute", &Database::PExecute)*/
			/*.def("BeginTransaction", &Database::StartTransaction)*/
			/*.def("CommitTransaction", &Database::CommitTransaction)*/
			/*.def("RollbackTransaction", &Database::RollbackTransaction)*/
			.def("escape_string", (void (Database::*)(std::string&))&Database::escape_string)
			/*.def("ThreadStart", &Database::ThreadStart)*/
			/*.def("ThreadEnd", &Database::ThreadEnd)*/
	];

	/* From Field.h */
	module(L)
	[
		class_<Field>("Field")
			.def(constructor<>())
			.def(constructor<Field&>())
			.def(constructor<const char*, const char*, Field::DataTypes>())
			.def("GetName", &Field::GetName)
			.def("GetType", &Field::GetType)
			.def("GetString", &Field::GetString)
			.def("GetFloat", &Field::GetFloat)
			.def("GetInt32", &Field::GetInt32)
			.def("GetUInt8", &Field::GetUInt8)
			.def("GetUInt16", &Field::GetUInt16)
			.def("GetUInt32", &Field::GetUInt16)
			.def("GetUInt64", &lb_Field_GetUInt64)
			.def("SetName", &Field::SetName)
			.def("SetType", &Field::SetType)
			.def("SetValue", &Field::SetValue)
	];

	/* From QueryResult.h */
	module(L)
	[
		class_<QueryResult>("QueryResult")
			/*.def(constructor<uint64, uint32>)())*/
			.def("NextRow", &QueryResult::NextRow)
			.def("Free", &lb_QueryResult_Free)
			.def("Fetch", &lb_QueryResult_Fetch, raw(_1))
			.def("GetFieldCount", &QueryResult::GetFieldCount)
			.def("GetRowCount", (uint32 (QueryResult::*)(void))&QueryResult::GetRowCount)
			.def("GetRowCount_u64", &lb_QueryResult_GetRowCount_u64)
	];

	/* From DBCStructure.h */
	// TODO: need to export more information for SpellEntry
	// TODO: maybe (if possible) simplify the way DBCStorage is exported
	module(L)
	[
		class_<SpellEntry>("SpellEntry")
			.def_readonly("Id", &SpellEntry::Id)
			.def_readonly("School", &SpellEntry::School)
			.def_readonly("Category", &SpellEntry::Category)
			.def_readonly("Dispel", &SpellEntry::Dispel)
			.def_readonly("Mechanic", &SpellEntry::Mechanic)
			.def_readonly("Attributes", &SpellEntry::Attributes)
			//...
			.def_readonly("Stances", &SpellEntry::Stances)
			.def_readonly("Targets", &SpellEntry::Targets)
			.def_readonly("TargetCreatureType", &SpellEntry::TargetCreatureType)
			.def_readonly("RequiresSpellFocus", &SpellEntry::RequiresSpellFocus)
			.def_readonly("CasterAuraState", &SpellEntry::CasterAuraState)
			.def_readonly("CastingTimeIndex", &SpellEntry::CastingTimeIndex)
			.def_readonly("RecoveryTime", &SpellEntry::RecoveryTime)
			.def_readonly("CategoryRecoveryTime", &SpellEntry::CategoryRecoveryTime)
			.def_readonly("InterruptFlags", &SpellEntry::InterruptFlags)
			//...
			.def("Totem", &lb_SpellEntry_Totem)
			.def("Reagent", &lb_SpellEntry_Reagent)
			.def("ReagentCount", &lb_SpellEntry_ReagentCount)
			.def_readonly("EquippedItemClass", &SpellEntry::EquippedItemClass)
			.def_readonly("EquippedItemSubClassMask", &SpellEntry::EquippedItemSubClassMask)
			.def_readonly("EquippedItemInventoryTypeMask", &SpellEntry::EquippedItemInventoryTypeMask)
			.def("Effect", &lb_SpellEntry_Effect)
			//...
			.def("EffectImplicitTargetA", &lb_SpellEntry_EffectImplicitTargetA)
			.def("EffectImplicitTargetB", &lb_SpellEntry_EffectImplicitTargetB),

		// Export for DBCStorage with SpellEntry
		class_<DBCStorage<SpellEntry> >("DBCStorage")
			.def("LookupEntry", &DBCStorage<SpellEntry>::LookupEntry)
			.def("GetNumRows", &DBCStorage<SpellEntry>::GetNumRows)
			.def("Load", &DBCStorage<SpellEntry>::Load)
			.def("Clear", &DBCStorage<SpellEntry>::Clear)
	];

	module(L)
	[
		class_<SpellCastTimesEntry>("SpellCastTimesEntry")
			.def_readonly("ID", &SpellCastTimesEntry::ID)
			.def_readonly("CastTime", &SpellCastTimesEntry::CastTime),

		// Export for DBCStorage with SpellCastTimesEntry
		class_<DBCStorage<SpellCastTimesEntry> >("DBCStorage")
			.def("LookupEntry", &DBCStorage<SpellCastTimesEntry>::LookupEntry)
			.def("GetNumRows", &DBCStorage<SpellCastTimesEntry>::GetNumRows)
			.def("Load", &DBCStorage<SpellCastTimesEntry>::Load)
			.def("Clear", &DBCStorage<SpellCastTimesEntry>::Clear)
	];

	return 0;
}