Gm_MySQL v7 - OO edition
I was surprised when I came back to Facepunch & Garry's Mod (after nearly a year off) that people were still using my old MySql bindings. Having fresh inspiration, I set about re-writing it and coming up with a nicer way of doing things.
Crashing/Problems?
[list][*]Please give me as much information as possible:
[list][*]Test scripts / Example code[*]MDMP files[/list][*]Make sure you're running the most up-to-date version[/list]
Changes
[list][*]v7
[list][*]Added calls to mysql_library_init/mysql_library_end[*](Hopefully) fixed crash that could occur randomly[*]Updated to version 6.0.2 of libmysql[*]Fixed problem with on Linux that would stop queries from running[/list][*]v6
[list][*]Fixed a potential problem with the Linux DLLs[*]Fixed a problem with the Garbage Collection (thanks haza55)[/list][*]v5.1
[list][*]Linux binaries now available[/list][*]v5
[list][*]Finally tracked down the crash (it was due to libmysql.dll, so downgraded to 5.0). Thanks to everyone who helped.[*]Added version code (mysqloo.VERSION, mysqloo.MYSQL_VERSION, mysqloo.MYSQL_INFO)[*]Added server version (Database:serverVersion, Database:serverInfo, Database:hostInfo)[/list][*]v4
[list][*]Attempt to fix the crash problems that people are having...[/list][*]v3
[list][*]Added function: Database:wait() - blocks until connection has been completed.[*]Added function: Query:wait() - blocks until query has been completed (and all data read back).[/list][*]v2
[list][*]Bug fix - objects would be garbage collected whilst still running threads. This would show up more often if you were attempting to connect to a server that did not exist.[/list][*]v1
[list][*]Initial release[/list][/list]
Example
require("mysqloo") local DATABASE_HOST = "localhost" local DATABASE_PORT = 3308 local DATABASE_NAME = "test" local DATABASE_USERNAME = "root" local DATABASE_PASSWORD = "1234" function printQuery(query) PrintTable(query:getData()) end function afterConnected(database) local query1 = database:query("SELECT ID, Name, Cost FROM test WHERE Cost > 0.50") query1.onData = function(Q,D) print("Q1") PrintTable(D) end query1.onSuccess = printQuery query1.onError = function(Q,E) print("Q1") print(E) end query1:start() local query2 = database:query("SELECT ID, Name, Cost FROM test") query2.onData = function(Q,D) print("Q2") PrintTable(D) end query2.onError = function(Q,E) print("Q1") print(E) end query2:start() end function connectToDatabase() local databaseObject = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT) databaseObject.onConnected = afterConnected databaseObject:connect() end connectToDatabase()
Usage
Basic functionality:
[list][*]mysqloo.connect - Create a new database connection
Parameters: (hostname, username, password, database, port,
unix socket, client flag)
Returns: Database object[/list]Database object:
[list][*]Database:connect - Start connecting to the database
Parameters: ()
Returns: Nothing[*]Database:escape - Make text safe for queries
Parameters: (text)
Returns: Escaped string[*]Database:abortAllQueries - Aborts all running queries
Parameters: ()
Returns: Nothing[*]Database:status - Check database status
Parameters: ()
Returns: One of:
[list][*]mysqloo.DATABASE_CONNECTED - (0) - Connected to the database.[*]mysqloo.DATABASE_CONNECTING - (1) - Current is attempting to connect[*]mysqloo.DATABASE_NOT_CONNECTED - (2) - Connection was not successful or has failed[*]mysqloo.DATABASE_INTERNAL_ERROR - (3) - Some internal error occured[/list][*]Database:query - Query the database
Parameters: (query)
Returns: Query object[*]Database:onConnected - Callback when connected successfully
Parameters: ()
Returns: Nothing[*]Database:onConnectionFailed - Callback when connection could not be established.
Parameters: (error_message)
Returns: Nothing[/list]Query object:
[list][*]Query:start - Start a query running
Parameters: ()
Returns: Nothing[*]Query:abort - Abort a query running
Parameters: ()
Returns: Nothing[*]Query:getData - Get all data from a query
Parameters: ()
Returns: Table containing all the data that has been read so far[*]Query:setOption - Change query options
Parameters: (Flag, set)
Flag is one of:
[list][*]mysqloo.OPTION_NUMERIC_FIELDS - (1) - Use numeric fields when presenting data [default:off][*]mysqloo.OPTION_NAMED_FIELDS - (2) - Use named fields when presenting data [default:on][*]mysqloo.OPTION_INTERPRET_DATA - (4) - Auto-convert int/float fields to int/float representation [default:on][*]mysqloo.OPTION_CACHE - (8) - Cache the data after reading it [default:on][/list]Returns: Nothing[*]Query:status - Check query status
Parameters: ()
Returns: One of:
[list][*]mysqloo.QUERY_NOT_RUNNING - (0) - Query has not been started yet.[*]mysqloo.QUERY_RUNNING - (1) - The query is executing on the database.[*]mysqloo.QUERY_READING_DATA - (2) - The query has been run, and is now reading the data back.[*]mysqloo.QUERY_COMPLETE - (3) - The query has completed (successfully or with an error).[*]mysqloo.QUERY_ABORTED - (4) - The query has been aborted.[/list][*]Query:onData - Callback when a single row has been received
Parameters: (data_row)
Returns: Nothing[*]Query:onSuccess - Callback when all data has been received
Parameters: ()
Returns: Nothing[*]Query:onFailure - Callback when an error occured
Parameters: (error_message)
Returns: Nothing[*]Query:onAborted - Callback when a query was aborted
Parameters: ()
Returns: Nothing[/list]
Installation
[list][*]Extract the zip file to your garrysmod\addons directory (ie: c:\steam\steamapps\username\garrysmod\garrysmod\ad dons)[*]Take the libmySQL.dll, and put it in the same directory as HL2.EXE (ie: c:\steam\steamapps\username\garrysmod)[*]Move the files gmsv_mysqloo.dll and gmcl_mysqloo.dll to the garrysmod\lua\includes\modules folder.[/list]
Downloads
[list][*]Binaries
Windows
Linux[*]Source Code[/list]

Register
Events
Popular
More
Post #1


Windows
Linux


x 34
x 17
x 3
x 2
x 2
x 2
x 1
x 1
x 1

Disagree x 2
Late x 1







