rssDatabase Access Module (database)
Category: | misc |
---|---|
Author(s): | Ch3ck3r |
Downloads: | 1551 |
Rate: | You need to be logged in to vote |
Rating: | 3.7 (10 Votes) |
Description:
This is a simple database wrapper/interface for the module MTA-MySQL (by Alberto Alonso (ryden)).
I was bugged by the fact, that all the mysql functions require the mysql handler (opened by mysql_connect) as a parameter. The problem is, that this parameter is just available in the one resource that opened the database connection but i needed it in all my resources that have to do something with the database. I just did not want to have every resource its own connection due to performance reasons.
So i wrote a wrapper/interface that handles all database requests by exported functions.
To save somebody else's time i decided to upload this one for all that need it.
How does it work:
Set the connection parameters in config/database.xml. The connection will be opened at resource start up and will be closed on resource stop. If your script has anything to do with a your mysql database it can use the database resource to handle it requests. It even checks if the database connection is still active before any operation that needs a valid connection and reconnects automaticall if needed.
This resource uses a DIFFERENT names convention than the original mysql functions! Instead of naming the function with underscores they are named in camel-syntax.
mysql_escape_string becomes escapeString
mysql_affected_rows becomes affectedRows
mysql_query becomes query
mysql_fetch_row becomes fetchRow
and so on.
IMPORTANT:
The handlers returned by these functions are not comptabile with normal mysql_* functions because they are plain integers instead of mysql-handlers (due to exportet function's compatibility)
If errors or warnings occur in a query or any operation the script will throw that error immediately. Look at debugscript to see them.
=========================================================
Example of a resource using the database interface/wrapper
[lua]
db = exports.database
local username = "PlayersUserName"
local query = "SELECT `money` FROM `players` WHERE `username` = '" .. db:escapeString(username) .. "'"
local result = db:query(query)
local row = db:fetchRow(result)
local money = row[1]
db:freeResult(result)
[/lua]
I hope, this resource helps some people.
I was bugged by the fact, that all the mysql functions require the mysql handler (opened by mysql_connect) as a parameter. The problem is, that this parameter is just available in the one resource that opened the database connection but i needed it in all my resources that have to do something with the database. I just did not want to have every resource its own connection due to performance reasons.
So i wrote a wrapper/interface that handles all database requests by exported functions.
To save somebody else's time i decided to upload this one for all that need it.
How does it work:
Set the connection parameters in config/database.xml. The connection will be opened at resource start up and will be closed on resource stop. If your script has anything to do with a your mysql database it can use the database resource to handle it requests. It even checks if the database connection is still active before any operation that needs a valid connection and reconnects automaticall if needed.
This resource uses a DIFFERENT names convention than the original mysql functions! Instead of naming the function with underscores they are named in camel-syntax.
mysql_escape_string becomes escapeString
mysql_affected_rows becomes affectedRows
mysql_query becomes query
mysql_fetch_row becomes fetchRow
and so on.
IMPORTANT:
The handlers returned by these functions are not comptabile with normal mysql_* functions because they are plain integers instead of mysql-handlers (due to exportet function's compatibility)
If errors or warnings occur in a query or any operation the script will throw that error immediately. Look at debugscript to see them.
=========================================================
Example of a resource using the database interface/wrapper
[lua]
db = exports.database
local username = "PlayersUserName"
local query = "SELECT `money` FROM `players` WHERE `username` = '" .. db:escapeString(username) .. "'"
local result = db:query(query)
local row = db:fetchRow(result)
local money = row[1]
db:freeResult(result)
[/lua]
I hope, this resource helps some people.