Guest User

Untitled

a guest
Jun 25th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. db = {};
  2.  
  3. --  Function: db:new
  4. --  Creates a database connection object
  5. -- 
  6. --  Required Arguments:
  7. --     dbtype - (string) Type of connection. ( "sqlite"/"mysql" )
  8. --     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.
  9. --     username - (string) Username used to connect to MySQL server.
  10. --     password - (string) Password used to connect to MySQL server.
  11. --     options - (string) Name of database to select after connecting.
  12. --
  13. --  Returns:
  14. --      Returns Database class object.
  15.  
  16. function db:new( dbtype, host, username, password, options )
  17.     local _db = {
  18.         type = dbtype,
  19.         result = nil,
  20.         numrows = nil,
  21.         errmsg = nil,
  22.         wait = true
  23.     };
  24.     if ( dbtype == "sqlite" ) then
  25.         _db.link = dbConnect( "sqlite", host )
  26.     elseif ( dbtype == "mysql" ) then
  27.         _db.link = dbConnect( "mysql", "dbname=" .. host.database .. ";host=" .. host.host .. "", username, password, options )
  28.     end
  29.    
  30.     if ( _db.link ) then
  31.         outputDebugString ( "Successfully connected to " .. ( ( type( host ) == 'table' ) and host.database or host ) .. "." )
  32.     else
  33.         outputDebugString ( "Database connection was failed ( Type: " .. dbtype .. "; Database: " .. ( ( type( host ) == 'table' ) and host.database or host ) .. "; )", 3 );
  34.     end
  35.     return setmetatable( _db, { __index = db } );
  36. end
  37.  
  38.  
  39. --  Function: db:query
  40. --  Sends a query to database.
  41. -- 
  42. --  Required Arguments:
  43. --     sql - (string) Query string.
  44. --
  45. --  Optional Arguments:
  46. --     waitresult - (bool/table) Wait result or not.
  47. --     If u want call callback function then waitresult must be table with callback(func) and args(func arguments) keys.
  48. --
  49. --  Returns:
  50. --      Returns result array if waitresult is true or table.
  51. --      Return false or true if waitresutl is false.
  52.  
  53. function db:query( sql, waitresult )
  54.     if ( type( waitresult ) == 'nil' ) then waitresult = self.wait; end
  55.     if ( waitresult ) then
  56.         if ( type( waitresult ) == 'table' ) then
  57.             dbQuery ( waitresult.callback, waitresult.args, self.link, sql );
  58.         else
  59.             local qh = dbQuery ( self.link, sql );
  60.             self.result, self.numrows, self.errmsg = dbPoll ( qh, -1 );
  61.             return ( #self.result == 1 ) and self.result[1] or self.result;
  62.         end
  63.     else
  64.         return dbExec( self.link, sql );
  65.     end
  66. end
  67.  
  68. --  Function: db:lastId
  69. --  Returns last insert id.
  70. --
  71. --  Returns:
  72. --      Returns last insert id as number.
  73. function db:lastId( )
  74.     return tonumber( self:query( 'SELECT ' .. ( ( self.type == 'mysql' ) and 'LAST_INSERT_ID' or 'LAST_INSERT_ROWID' ) .. '() AS lastid' )[ 'lastid'] );
  75. end
  76.  
  77. --  Function: db:free
  78. --  Frees the last query result.
  79. -- 
  80. --  Returns:
  81. --      Returns true if freed, false if not.
  82. function db:free( )
  83.     return dbFree( self.link );
  84. end
  85.  
  86. --  Function: db:error
  87. --  Returns last error msg.
  88. --
  89. --  Returns:
  90. --      Returns last error msg.
  91. function db:error( )
  92.     return self.errmsg;
  93. end
  94.  
  95. --  Function: db:rows
  96. --  Returns number of affected rows with the last query.
  97. --
  98. --  Returns:
  99. --      Returns number of affected rows with the last query.
  100. function db:rows( )
  101.     return self.numrows;
  102. end
  103.  
  104. db = db:new( config.db.type, config.db.database );
Advertisement
Add Comment
Please, Sign In to add comment