Skip to content

Commit

Permalink
Lua rework almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
FredyH committed Nov 5, 2021
1 parent 26833a5 commit fd245cb
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 217 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
.vscode
*.zip
cmake-build-debug
.idea
.idea
.vs
20 changes: 10 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cmake_minimum_required(VERSION 3.13)
project(MySQLOO)
project(mysqloo)
add_subdirectory(GmodLUA)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

file(GLOB_RECURSE MySQLOO_SRC "src/*.h" "src/*.cpp")
set(SOURCE_FILES ${MySQLOO_SRC} src/Main.cpp)
file(GLOB_RECURSE MYSQLOO_SRC "src/*.h" "src/*.cpp")
set(SOURCE_FILES ${MYSQLOO_SRC} src/Main.cpp)
set(CMAKE_BUILD_TYPE Debug)

add_executable(MySQLOO ${SOURCE_FILES})
target_link_libraries(MySQLOO gmod-module-base)
add_library(mysqloo SHARED ${SOURCE_FILES})
target_link_libraries(mysqloo gmod-module-base)

target_include_directories(MySQLOO PRIVATE MySQL/include)
target_include_directories(mysqloo PRIVATE MySQL/include)

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (WIN32)
Expand All @@ -30,11 +30,11 @@ elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
endif ()

if (WIN32)
target_link_libraries(MySQLOO ${MARIADB_CLIENT_LIB} crypt32 ws2_32 shlwapi bcrypt secur32)
target_link_libraries(mysqloo ${MARIADB_CLIENT_LIB} crypt32 ws2_32 shlwapi bcrypt secur32)
else ()
find_package(Threads REQUIRED)
target_link_libraries(MySQLOO ${MARIADB_CLIENT_LIB} ${SSL_LIB} ${CRYPTO_LIB} Threads::Threads ${CMAKE_DL_LIBS})
target_link_libraries(mysqloo ${MARIADB_CLIENT_LIB} ${SSL_LIB} ${CRYPTO_LIB} Threads::Threads ${CMAKE_DL_LIBS})
endif ()


set_gmod_suffix_prefix(MySQLOO)
set_gmod_suffix_prefix(mysqloo)
38 changes: 10 additions & 28 deletions src/lua/GMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,6 @@ GMOD_MODULE_CLOSE() {
return 0;
}

/* Connects to the database and returns a Database instance that can be used
* as an interface to the mysql server.
*/
LUA_FUNCTION(mysqlooConnect) {
LUA->CheckType(1, GarrysMod::Lua::Type::String);
LUA->CheckType(2, GarrysMod::Lua::Type::String);
LUA->CheckType(3, GarrysMod::Lua::Type::String);
LUA->CheckType(4, GarrysMod::Lua::Type::String);
std::string host = LUA->GetString(1);
std::string username = LUA->GetString(2);
std::string pw = LUA->GetString(3);
std::string database = LUA->GetString(4);
unsigned int port = 3306;
std::string unixSocket;
if (LUA->IsType(5, GarrysMod::Lua::Type::Number)) {
port = (int)LUA->GetNumber(5);
}
if (LUA->IsType(6, GarrysMod::Lua::Type::String)) {
unixSocket = LUA->GetString(6);
}
auto object = Database::createDatabase(host, username, pw, database, port, unixSocket);
return 1;
}

/* Returns the amount of LuaObjectBase objects that are currently in use
* This includes Database and Query instances
*/
Expand Down Expand Up @@ -91,13 +67,13 @@ static void printMessage(GarrysMod::Lua::ILuaBase* LUA, const char* str, int r,
LUA->ReferenceFree(ref);
}

static int printOutdatatedVersion(lua_State* state) {
static int printOutdatedVersion(lua_State* state) {
GarrysMod::Lua::ILuaBase* LUA = state->luabase;
LUA->SetState(state);
printMessage(LUA, "Your server is using an outdated mysqloo9 version\n", 255, 0, 0);
printMessage(LUA, "Download the latest version from here:\n", 255, 0, 0);
printMessage(LUA, "https://github.com/FredyH/MySQLOO/releases\n", 86, 156, 214);
runInTimer(LUA, 300, printOutdatatedVersion);
runInTimer(LUA, 300, printOutdatedVersion);
return 0;
}

Expand All @@ -107,7 +83,7 @@ static int fetchSuccessful(lua_State* state) {
std::string version = LUA->GetString(1);
//version.size() < 3 so that the 404 response gets ignored
if (version != MYSQLOO_MINOR_VERSION && version.size() <= 3) {
printOutdatatedVersion(state);
printOutdatedVersion(state);
} else {
printMessage(LUA, "Your server is using the latest mysqloo9 version\n", 0, 255, 0);
}
Expand Down Expand Up @@ -151,12 +127,18 @@ static int doVersionCheck(lua_State* state) {
return 0;
}

//TODO List:
// - memory leak with queries because sometimes first data is not cleared
// - onData callbacks
// - PreparedQueries do not properly clear their results

GMOD_MODULE_OPEN() {
if (mysql_library_init(0, nullptr, nullptr)) {
LUA->ThrowError("Could not initialize mysql library.");
}

//Creating MetaTables
LuaObject::createUserDataMetaTable(LUA);
LuaDatabase::createMetaTable(LUA);
LuaQuery::createMetaTable(LUA);
LuaPreparedQuery::createMetaTable(LUA);
Expand Down Expand Up @@ -194,7 +176,7 @@ GMOD_MODULE_OPEN() {
LUA->PushNumber(OPTION_NAMED_FIELDS); LUA->SetField(-2, "OPTION_NAMED_FIELDS"); //Not used anymore
LUA->PushNumber(OPTION_CACHE); LUA->SetField(-2, "OPTION_CACHE"); //Not used anymore

LUA->PushCFunction(mysqlooConnect); LUA->SetField(-2, "connect");
LUA->PushCFunction(LuaDatabase::create); LUA->SetField(-2, "connect");
LUA->PushCFunction(objectCount); LUA->SetField(-2, "objectCount");

LUA->SetField(-2, "mysqloo");
Expand Down
47 changes: 23 additions & 24 deletions src/lua/LuaDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static void pushLuaObjectTable(ILuaBase *LUA, void *data, int type) {
LUA->CreateTable();
LUA->PushUserType(data, type);
LUA->PushUserType(data, LuaObject::TYPE_USERDATA);
LUA->SetField(-2, "__CppObject");
LUA->PushMetaTable(type);
LUA->SetMetaTable(-2);
Expand Down Expand Up @@ -37,7 +37,7 @@ LUA_CLASS_FUNCTION(LuaDatabase, create) {
}

MYSQLOO_LUA_FUNCTION(query) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->CheckType(2, GarrysMod::Lua::Type::String);
unsigned int outLen = 0;
const char *queryStr = LUA->GetString(2, &outLen);
Expand All @@ -49,7 +49,7 @@ MYSQLOO_LUA_FUNCTION(query) {
}

MYSQLOO_LUA_FUNCTION(prepare) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->CheckType(2, GarrysMod::Lua::Type::String);
unsigned int outLen = 0;
const char *queryStr = LUA->GetString(2, &outLen);
Expand All @@ -61,7 +61,7 @@ MYSQLOO_LUA_FUNCTION(prepare) {
}

MYSQLOO_LUA_FUNCTION(createTransaction) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
auto transaction = Transaction::create(database->m_database);
auto luaTransaction = LuaTransaction::create(transaction);

Expand All @@ -70,7 +70,7 @@ MYSQLOO_LUA_FUNCTION(createTransaction) {
}

MYSQLOO_LUA_FUNCTION(connect) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
if (database->m_tableReference == 0) {
LUA->Push(1);
database->m_tableReference = LUA->ReferenceCreate();
Expand All @@ -80,7 +80,7 @@ MYSQLOO_LUA_FUNCTION(connect) {
}

MYSQLOO_LUA_FUNCTION(escape) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
unsigned int nQueryLength;
const char *sQuery = LUA->GetString(2, &nQueryLength);
auto escaped = database->m_database->escape(std::string(sQuery, nQueryLength));
Expand All @@ -89,7 +89,7 @@ MYSQLOO_LUA_FUNCTION(escape) {
}

MYSQLOO_LUA_FUNCTION(setCharacterSet) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->CheckType(2, GarrysMod::Lua::Type::String);
const char *charset = LUA->GetString(2);
bool success = database->m_database->setCharacterSet(charset);
Expand All @@ -99,7 +99,7 @@ MYSQLOO_LUA_FUNCTION(setCharacterSet) {
}

MYSQLOO_LUA_FUNCTION(setSSLSettings) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
SSLSettings sslSettings;
if (LUA->IsType(2, GarrysMod::Lua::Type::String)) {
sslSettings.key = LUA->GetString(2);
Expand All @@ -121,7 +121,7 @@ MYSQLOO_LUA_FUNCTION(setSSLSettings) {
}

MYSQLOO_LUA_FUNCTION(disconnect) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
bool wait = false;
if (LUA->IsType(2, GarrysMod::Lua::Type::Bool)) {
wait = LUA->GetBool(2);
Expand All @@ -131,52 +131,52 @@ MYSQLOO_LUA_FUNCTION(disconnect) {
}

MYSQLOO_LUA_FUNCTION(status) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->PushNumber(database->m_database->m_status);
return 1;
}

MYSQLOO_LUA_FUNCTION(serverVersion) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->PushNumber(database->m_database->serverVersion());
return 1;
}

MYSQLOO_LUA_FUNCTION(serverInfo) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->PushString(database->m_database->serverInfo().c_str());
return 1;
}

MYSQLOO_LUA_FUNCTION(hostInfo) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->PushString(database->m_database->hostInfo().c_str());
return 1;
}

MYSQLOO_LUA_FUNCTION(setAutoReconnect) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->CheckType(2, GarrysMod::Lua::Type::Bool);
database->m_database->setAutoReconnect(LUA->GetBool(2));
return 0;
}

MYSQLOO_LUA_FUNCTION(setMultiStatements) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->CheckType(2, GarrysMod::Lua::Type::Bool);
database->m_database->setMultiStatements(LUA->GetBool(2));
return 0;
}

MYSQLOO_LUA_FUNCTION(setCachePreparedStatements) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->CheckType(2, GarrysMod::Lua::Type::Bool);
database->m_database->setCachePreparedStatements(LUA->GetBool(2));
return 0;
}

MYSQLOO_LUA_FUNCTION(abortAllQueries) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
auto abortedQueries = database->m_database->abortAllQueries();
for (auto pair: abortedQueries) {
//TODO:
Expand All @@ -187,19 +187,19 @@ MYSQLOO_LUA_FUNCTION(abortAllQueries) {
}

MYSQLOO_LUA_FUNCTION(queueSize) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->PushNumber((double) database->m_database->queueSize());
return 1;
}

MYSQLOO_LUA_FUNCTION(ping) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
LUA->PushBool(database->m_database->ping());
return 1;
}

MYSQLOO_LUA_FUNCTION(wait) {
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA, LuaObject::TYPE_DATABASE);
auto database = LuaObject::getLuaObject<LuaDatabase>(LUA);
database->m_database->wait();
return 0;
}
Expand Down Expand Up @@ -277,7 +277,7 @@ void LuaDatabase::think(ILuaBase *LUA) {
LUA->GetField(-1, "onConnected");
if (LUA->GetType(-1) == GarrysMod::Lua::Type::Function) {
LUA->ReferencePush(this->m_tableReference);
pcallWithErrorReporter(LUA, 1, 0);
pcallWithErrorReporter(LUA, 1);
}
LUA->Pop(); //Callback function
} else {
Expand All @@ -286,7 +286,7 @@ void LuaDatabase::think(ILuaBase *LUA) {
LUA->ReferencePush(this->m_tableReference);
auto error = database->connectionError();
LUA->PushString(error.c_str());
pcallWithErrorReporter(LUA, 2, 0);
pcallWithErrorReporter(LUA, 2);
}
LUA->Pop(); //Callback function
}
Expand All @@ -300,10 +300,9 @@ void LuaDatabase::think(ILuaBase *LUA) {
auto finishedQueries = database->takeFinishedQueries();
for (auto &pair: finishedQueries) {
auto data = pair.second;
auto query = pair.first;
if (data->m_tableReference != 0) {
LUA->ReferencePush(data->m_tableReference);
auto luaQuery = LuaIQuery::getLuaIQuery(LUA, -1);
auto luaQuery = LuaIQuery::getLuaObject<LuaIQuery>(LUA, -1);
LUA->Pop();
luaQuery->runCallback(LUA, data);
}
Expand Down
Loading

0 comments on commit fd245cb

Please sign in to comment.