• Gm_MySql v1.0 - OO edition.
    824 replies, posted
  • Avatar of andyvincent
  • [highlight][u]Gm_MySQL v7 - OO edition[/u][/highlight] I was surprised when I came back to Facepunch & Garry's Mod (after nearly a year off) that people were still using my old [thread=241247]MySql bindings[/thread]. Having fresh inspiration, I set about re-writing it and coming up with a nicer way of doing things. [highlight][u]Crashing/Problems?[/u][/highlight] [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] [highlight][u]Changes[/u][/highlight] [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] [highlight][u]Example[/u][/highlight] [lua]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()[/lua] [highlight][u]Usage[/u][/highlight] [b][u]Basic functionality:[/u][/b] [list] [*][b]mysqloo.connect[/b] - Create a new database connection Parameters: (hostname, username, password, [i]database[/i], [i]port[/i], [i]unix socket[/i], [i]client flag[/i]) Returns: Database object [/list] [b][u]Database object:[/u][/b] [list] [*][b]Database:connect[/b] - Start connecting to the database Parameters: () Returns: Nothing [*][b]Database:escape[/b] - Make text safe for queries Parameters: (text) Returns: Escaped string [*][b]Database:abortAllQueries[/b] - Aborts all running queries Parameters: () Returns: Nothing [*][b]Database:status[/b] - 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] [*][b]Database:query[/b] - Query the database Parameters: (query) Returns: Query object [*][b]Database:onConnected[/b] - Callback when connected successfully Parameters: () Returns: Nothing [*][b]Database:onConnectionFailed[/b] - Callback when connection could not be established. Parameters: (error_message) Returns: Nothing [/list] [b][u]Query object:[/u][/b] [list] [*][b]Query:start[/b] - Start a query running Parameters: () Returns: Nothing [*][b]Query:abort[/b] - Abort a query running Parameters: () Returns: Nothing [*][b]Query:getData[/b] - Get all data from a query Parameters: () Returns: Table containing all the data that has been read so far [*][b]Query:setOption[/b] - Change query options Parameters: (Flag, [i]set[/i]) 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 [*][b]Query:status[/b] - 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] [*][b]Query:onData[/b] - Callback when a single row has been received Parameters: (data_row) Returns: Nothing [*][b]Query:onSuccess[/b] - Callback when all data has been received Parameters: () Returns: Nothing [*][b]Query:onFailure[/b] - Callback when an error occured Parameters: (error_message) Returns: Nothing [*][b]Query:onAborted[/b] - Callback when a query was aborted Parameters: () Returns: Nothing [/list] [highlight][u]Installation[/u][/highlight] [list] [*]Extract the zip file to your garrysmod\addons directory (ie: c:\steam\steamapps\username\garrysmod\garrysmod\addons) [*]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] [highlight][u]Downloads[/u][/highlight] [list] [*]Binaries [url=http://github.com/downloads/andyvincent/Garry-s-Mod-Extensions/MySqlOO-v7-win32.zip][img]http://www.brownlittlehandfootman.com/files/win32.gif[/img] Windows[/url] [url=http://github.com/downloads/andyvincent/Garry-s-Mod-Extensions/MySqlOO-v7-linux.zip][img]http://www.brownlittlehandfootman.com/files/linux.gif[/img] Linux[/url] [*][url=http://github.com/andyvincent/Garry-s-Mod-Extensions]Source Code[/url] [/list]
  • Avatar of andyvincent
  • [QUOTE=Jake1305;21736288]Awesome. Linux binary's? Because of the GMod Lin Bins today?[/QUOTE] I don't have any means of compiling on Linux at the moment... Plus some code needs to be written to handle the Linux side of things with regard to threading. If anyone wants to contribute, let me know - I suppose you can via GitHub anyway.
  • Avatar of Jake1305
  • [QUOTE=andyvincent;21736330]I don't have any means of compiling on Linux at the moment... Plus some code needs to be written to handle the Linux side of things with regard to threading. If anyone wants to contribute, let me know.[/QUOTE] Alright', glad to see you back and things back on track. Not sure if I'll switch from tmysql yet, I'll look into it, I'll post back later if I run into problems with it.
  • Avatar of Prefan
  • There seems to be a bug. Every time I try to connect to a database that is down, Garry's Mod crashes.
  • Avatar of andyvincent
  • [QUOTE=Prefan;21763711]There seems to be a bug. Every time I try to connect to a database that is down, Garry's Mod crashes.[/QUOTE] Thanks. Updated with version 2, please see the first post!
  • Avatar of Prefan
  • Awesome, thanks. [editline]03:30PM[/editline] Crashed again, after doing some testing while inserting 200 entries into a sql table. mdmp if it helps, [url]http://dl.dropbox.com/u/717734/mysqlOO.mdmp[/url].
  • Avatar of Prefan
  • Also, if this isn't much, I have a request. Would it be possible to create a non-threaded version of this module? I know I might sound crazy, but I designed my code around the non-threaded version of the old module, and it is impossible to move towards a threaded version. I think I'm going to stick with the old module for now, but eventually it would be nice to switch to this.
  • Avatar of andyvincent
  • [QUOTE=Prefan;21779589]Also, if this isn't much, I have a request. Would it be possible to create a non-threaded version of this module? I know I might sound crazy, but I designed my code around the non-threaded version of the old module, and it is impossible to move towards a threaded version. I think I'm going to stick with the old module for now, but eventually it would be nice to switch to this.[/QUOTE] Would it be possible to get a copy of your test code that causes the crash? This would help a lot. Thanks. I'm not sure about a non-threaded version, I would like to take a look at your code and see how you would adapt it be thread-capable.
  • Avatar of infinitywrai
  • Interesting take on callbacks that I'm sure a lot of people would like, very similar to Azu's gm_tmysql2 that I use.
  • Avatar of MegaDeath
  • Weird, I set a variable to the DBObject in the connection function, but whenever i go to access it later it has become null? I think the reference counting is glitched... [editline]05:19PM[/editline] I'm gunna switch to the older API till this thing is better tested... Having everything asyncronous is also a bit of a pain when something must be done in order.
  • Avatar of andyvincent
  • [QUOTE=MegaDeath;21820206]Weird, I set a variable to the DBObject in the connection function, but whenever i go to access it later it has become null? I think the reference counting is glitched...[/QUOTE] Your test code would be helpful!
  • hl2.exe keeps crashing when i try to connect to the mysql database. This is the code i am using [lua] -- core/sv_mysql.lua rp.mysql = {} local host = "localhost" local port = 3306 local name = "rp" local user = "root" local pass = "" function rp.mysql.connect() databaseObject = mysqloo.connect(host, user, pass, name, port) databaseObject:connect() end [/lua]
  • Avatar of andyvincent
  • Are you using the latest version? Is the database setup? [editline]12:47AM[/editline] Version 3 released, see first post!
  • Avatar of Prefan
  • Integrating into all my scripts now, thanks a ton for the wait functions. [editline]09:33PM[/editline] Crashed using the wait functions. [url]http://dl.dropbox.com/u/717734/hl2_4104_crash_2010_5_10T1_31_24C0.mdmp[/url]
  • [QUOTE=andyvincent;21845399]Are you using the latest version? Is the database setup? [/QUOTE] Yes, I am using the latest version, it only crashes when the database is up and running (so the crash is probrably caused by connecting.).
  • Avatar of nicatronTg
  • I can only imagine the look on your face when you saw the 2006 bindings still being used. Thanks a ton, I'll be getting around to MySQL a bit this weekend, I never actually fully integrated the old module.
  • Avatar of Skuch
  • My GMod crashes aswell when it connects to the database. I don't know whether it's a error in the connection part of the module, or the port handling part. It seems to crash when the port number isn't defined, or it's just plain numbers. But when embedded into a string it borkz out. [lua] function NS.ConnectSQL() local conn = mysqloo.connect( "127.0.0.1", "root", "", "coke" ); conn:connect(); end NS.ConnectSQL();[/lua]
  • Avatar of andyvincent
  • Does it actually crash ("HL2.exe has crashed" message), or lock up? [editline]09:16AM[/editline] Whatever I do I cannot make it crash (or lockup)... Please can you verify that you are using the latest version (3) - adding print(mysqloo.VERSION) to your script will tell you the version code (if it prints nil then you are using v1 or v2). How is your script started (autorun-client, server or both?) Are you using the libmysql.dll that was provided with this download or something else? Anything that you can tell me will help :)
  • [QUOTE=andyvincent;21852438]Does it actually crash ("HL2.exe has crashed" message), or lock up? [editline]09:16AM[/editline] Whatever I do I cannot make it crash (or lockup)... Please can you verify that you are using the latest version (3) - adding print(mysqloo.VERSION) to your script will tell you the version code (if it prints nil then you are using v1 or v2). How is your script started (autorun-client, server or both?) Are you using the libmysql.dll that was provided with this download or something else? Anything that you can tell me will help :)[/QUOTE] My hl2.exe when I start a listen server,there are also crashes when using the v3, but to be shure that i have v3 I redownloaded it. libmysql is where it shoud be (steam/steamapps/username/garrysmod/). The script is in my gamemode and its loaded serverside in its own file. I am not using any other mysql modules.
  • Avatar of andyvincent
  • Can you re-create the crash with the example code (modify it for your tables and database settings)?
  • [QUOTE=andyvincent;21855367]Can you re-create the crash with the example code (modify it for your tables and database settings)?[/QUOTE] yes i can