Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- db = {};
- -- Function: db:new
- -- Creates a database connection object
- --
- -- Required Arguments:
- -- dbtype - (string) Type of connection. ( "sqlite"/"mysql" )
- -- host - (string/table) If dbtype is "sqlite" then host is path to .db file. If dbtype is "mysql" then host is table with "database" and "host" keys. "database" - Database name, "host" - Hostname/IP address of MySQL server to connect.
- -- username - (string) Username used to connect to MySQL server.
- -- password - (string) Password used to connect to MySQL server.
- -- options - (string) Name of database to select after connecting.
- --
- -- Returns:
- -- Returns Database class object.
- function db:new( dbtype, host, username, password, options )
- local _db = {
- type = dbtype,
- result = nil,
- numrows = nil,
- errmsg = nil,
- wait = true
- };
- if ( dbtype == "sqlite" ) then
- _db.link = dbConnect( "sqlite", host )
- elseif ( dbtype == "mysql" ) then
- _db.link = dbConnect( "mysql", "dbname=" .. host.database .. ";host=" .. host.host .. "", username, password, options )
- end
- if ( _db.link ) then
- outputDebugString ( "Successfully connected to " .. ( ( type( host ) == 'table' ) and host.database or host ) .. "." )
- else
- outputDebugString ( "Database connection was failed ( Type: " .. dbtype .. "; Database: " .. ( ( type( host ) == 'table' ) and host.database or host ) .. "; )", 3 );
- end
- return setmetatable( _db, { __index = db } );
- end
- -- Function: db:query
- -- Sends a query to database.
- --
- -- Required Arguments:
- -- sql - (string) Query string.
- --
- -- Optional Arguments:
- -- waitresult - (bool/table) Wait result or not.
- -- If u want call callback function then waitresult must be table with callback(func) and args(func arguments) keys.
- --
- -- Returns:
- -- Returns result array if waitresult is true or table.
- -- Return false or true if waitresutl is false.
- function db:query( sql, waitresult )
- if ( type( waitresult ) == 'nil' ) then waitresult = self.wait; end
- if ( waitresult ) then
- if ( type( waitresult ) == 'table' ) then
- dbQuery ( waitresult.callback, waitresult.args, self.link, sql );
- else
- local qh = dbQuery ( self.link, sql );
- self.result, self.numrows, self.errmsg = dbPoll ( qh, -1 );
- return ( #self.result == 1 ) and self.result[1] or self.result;
- end
- else
- return dbExec( self.link, sql );
- end
- end
- -- Function: db:lastId
- -- Returns last insert id.
- --
- -- Returns:
- -- Returns last insert id as number.
- function db:lastId( )
- return tonumber( self:query( 'SELECT ' .. ( ( self.type == 'mysql' ) and 'LAST_INSERT_ID' or 'LAST_INSERT_ROWID' ) .. '() AS lastid' )[ 'lastid'] );
- end
- -- Function: db:free
- -- Frees the last query result.
- --
- -- Returns:
- -- Returns true if freed, false if not.
- function db:free( )
- return dbFree( self.link );
- end
- -- Function: db:error
- -- Returns last error msg.
- --
- -- Returns:
- -- Returns last error msg.
- function db:error( )
- return self.errmsg;
- end
- -- Function: db:rows
- -- Returns number of affected rows with the last query.
- --
- -- Returns:
- -- Returns number of affected rows with the last query.
- function db:rows( )
- return self.numrows;
- end
- db = db:new( config.db.type, config.db.database );
Advertisement
Add Comment
Please, Sign In to add comment