Guest User

Meekro Dev 3

a guest
Oct 2nd, 2014
190
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- Library
  3. --
  4. meekro = meekro or {}
  5. meekro._debug = false
  6. meekro._drivers = {}
  7. meekro.error = "meekro-error"
  8. meekro.lastInsert = 0
  9. meekro._placeholders = {
  10.     string = "s",
  11.     boolean = "b",
  12.     number = "f"
  13. }
  14.  
  15. --
  16. -- MySQLOO
  17. --
  18. meekro._drivers["mysqloo"] = {
  19.     --
  20.     -- Initialize
  21.     --
  22.     init = function()
  23.         require("mysqloo")
  24.     end,
  25.  
  26.     --
  27.     -- Connect to the database
  28.     --
  29.     connect = function(host, username, password, database, port)
  30.         local connection = mysqloo.connect(host, username, password, database, port or 3306)
  31.         connection.onConnected = meekro.OnConnectSuccess
  32.         connection.onConnectionFailed = meekro.OnConnectFailed
  33.         connection:connect()
  34.        
  35.         meekro._connection = connection
  36.     end,
  37.  
  38.     --
  39.     -- Query the database
  40.     --
  41.     query = function(str)
  42.         -- no error so far
  43.         meekro.error = false
  44.  
  45.         -- create query
  46.         local qry = meekro._connection:query(str)
  47.  
  48.         -- check for connection errors
  49.         if not qry then
  50.             error("[meekro] cannot query, not connected")
  51.         end
  52.  
  53.         -- start and then wait
  54.         qry:start()
  55.         qry:wait()
  56.        
  57.         -- check for error
  58.         if qry:error() ~= "" then
  59.             meekro.error = true
  60.             return qry:error()
  61.         end
  62.  
  63.         -- set last insert
  64.         meekro.lastInsert = qry:lastInsert()
  65.  
  66.         return qry:getData()
  67.     end,
  68.    
  69.     --
  70.     -- Disconnect from database
  71.     --
  72.     disconnect = function()
  73.         MsgC(Color(255, 0, 0), "[meekro] driver does not implement disconnect\n")
  74.     end,
  75.    
  76.     --
  77.     -- Escape string
  78.     --
  79.     escape = function(str)
  80.         return meekro._connection:escape(str)
  81.     end,
  82.  
  83.     --
  84.     -- Check if connected
  85.     --
  86.     connected = function()
  87.         if meekro._connection:status() == mysqloo.DATABASE_CONNECTED then
  88.             return true
  89.         else
  90.             return false
  91.         end
  92.     end
  93. }
  94.  
  95. --
  96. -- Properties
  97. --
  98. meekro.Driver = "mysqloo"
  99. meekro.OnConnectSuccess = function() end
  100. meekro.OnConnectFailed = function() end
  101.  
  102. --
  103. -- Initialize the current driver
  104. --
  105. meekro.init = function()
  106.     meekro.driver().init()
  107. end
  108.  
  109. --
  110. -- Connect to a MySQL database
  111. --
  112. meekro.connect = function(host, username, password, database, port)
  113.     meekro.driver().connect(host, username, password, database, port)
  114. end
  115.  
  116. --
  117. -- Update the database
  118. --
  119. meekro.update = function(tbl, data, query, ...)
  120.     -- setup variables
  121.     local sets = {}
  122.  
  123.     -- extract data
  124.     for k, v in pairs(data) do
  125.         -- check key is a string
  126.         if type(k) ~= "string" then
  127.             error("[meekro] insert provided invalid key type") 
  128.         end
  129.  
  130.         -- build set
  131.         local set = "`" .. k .. "`="
  132.  
  133.         -- escape/convert types
  134.         if type(v) == "string" then
  135.             set = set .. "'" .. meekro.escape(v) .. "'"
  136.         elseif type(v) == "boolean" then
  137.             set = set .. tostring(v and 1 or 0)
  138.         else
  139.             set = set .. tostring(v)
  140.         end
  141.  
  142.         -- insert set
  143.         table.insert(sets, set)
  144.     end
  145.  
  146.     -- build query
  147.     local query = ("UPDATE `" .. tbl .. "` " ..
  148.         "SET " .. string.Implode(",", sets) ..
  149.         " WHERE " .. meekro.format(query, unpack({...})))
  150.  
  151.     -- debugging
  152.     if meekro.debug then
  153.         MsgC(Color(255, 127, 0), "[meekro] built update: " .. query .. "\n")
  154.     end
  155.    
  156.     -- execute query
  157.     return meekro.driver().query(query)
  158. end
  159.  
  160. --
  161. -- Insert into the database
  162. --
  163. meekro.insert = function(tbl, data)
  164.     -- setup variables
  165.     local fields = {}
  166.     local values = {}
  167.  
  168.     -- extract data
  169.     for k, v in pairs(data) do
  170.         -- check key is a string
  171.         if type(k) ~= "string" then
  172.             error("[meekro] insert provided invalid key type") 
  173.         end
  174.  
  175.         -- insert string
  176.         table.insert(fields, meekro.escape(k))
  177.  
  178.         -- escape/convert types
  179.         if type(v) == "string" then
  180.             table.insert(values, "'" .. meekro.escape(v) .. "'")
  181.         elseif type(v) == "boolean" then
  182.             table.insert(values, tostring(v and 1 or 0))
  183.         else
  184.             table.insert(values, tostring(v))
  185.         end
  186.     end
  187.  
  188.     -- build query
  189.     local query = ("INSERT INTO `" .. tbl .. "` " ..
  190.         "(" .. string.Implode(",", fields) .. ") " ..
  191.         "VALUES (" .. string.Implode(",", values) .. ")")
  192.  
  193.     -- debugging
  194.     if meekro.debug then
  195.         MsgC(Color(255, 127, 0), "[meekro] built insert: " .. query .. "\n")
  196.     end
  197.    
  198.     -- execute query
  199.     return meekro.driver().query(query)
  200. end
  201.  
  202. --
  203. -- Query more than one row
  204. --
  205. meekro.query = function(query, ...)
  206.     print(query)
  207.     -- build query
  208.     local query = meekro.format(query, unpack({...}))
  209.  
  210.     -- debugging
  211.     if meekro.debug then
  212.         MsgC(Color(255, 127, 0), "[meekro] built query: " .. query .. "\n")
  213.     end
  214.    
  215.     -- execute query
  216.     return meekro.driver().query(query)
  217. end
  218.  
  219. --
  220. -- Query a single row
  221. --
  222. meekro.queryRow = function(query, ...)
  223.     -- remove trailing semicolon
  224.     query = string.TrimRight(query)
  225.     query = string.TrimRight(query, ";")
  226.  
  227.     return meekro.query(query .. " LIMIT 1;", unpack({...}))
  228. end
  229.  
  230. --
  231. -- Delete data
  232. --
  233. meekro.delete = function(tbl, query, ...)
  234.     -- build query
  235.     local query = ("DELETE FROM `" .. tbl .. "` " ..
  236.         " WHERE " .. meekro.format(query, unpack({...})))
  237.  
  238.     -- debugging
  239.     if meekro.debug then
  240.         MsgC(Color(255, 127, 0), "[meekro] built delete: " .. query .. "\n")
  241.     end
  242.    
  243.     -- execute query
  244.     return meekro.driver().query(query)
  245. end
  246.  
  247. --
  248. -- Drop a table
  249. --
  250. meekro.dropTable = function(tbl)
  251.     -- build query
  252.     local query = ("DROP TABLE `" .. tbl .. "`")
  253.  
  254.     -- debugging
  255.     if meekro.debug then
  256.         MsgC(Color(255, 127, 0), "[meekro] built table drop: " .. query .. "\n")
  257.     end
  258.    
  259.     -- execute query
  260.     return meekro.driver().query(query)
  261. end
  262.  
  263. --
  264. -- Drop a database
  265. --
  266. meekro.dropDatabase = function(db)
  267.     -- build query
  268.     local query = ("DROP DATABASE `" .. db .. "`")
  269.  
  270.     -- debugging
  271.     if meekro.debug then
  272.         MsgC(Color(255, 127, 0), "[meekro] built database drop: " .. query .. "\n")
  273.     end
  274.    
  275.     -- execute query
  276.     return meekro.driver().query(query)
  277. end
  278.  
  279. --
  280. -- Check if a table exists
  281. --
  282. meekro.tableExists = function(tbl)
  283.     -- execute query
  284.     local data = meekro.query("SHOW TABLES LIKE %s", tbl)
  285.  
  286.     -- check exists
  287.     if meekro.error or type(data) ~= "table" then
  288.         return false
  289.     else
  290.         if table.Count(data) > 0 then
  291.             return true
  292.         else
  293.             return false
  294.         end
  295.     end
  296. end
  297.  
  298. --
  299. -- Get if connected
  300. --
  301. meekro.connected = function()
  302.     return meekro.driver().connected()
  303. end
  304.  
  305. --
  306. -- Get current driver table
  307. --
  308. meekro.driver = function()
  309.     return meekro._drivers[meekro.Driver]
  310. end
  311.  
  312. --
  313. -- Escape a string
  314. --
  315. meekro.escape = function(str)
  316.     return meekro.driver().escape(str)
  317. end
  318.  
  319. --
  320. -- Enable/disable debugging
  321. --
  322. meekro.debug = function(enabled)
  323.     meekro._debug = enabled
  324. end
  325.  
  326. --
  327. -- Format a query string
  328. --
  329. meekro.format = function(str, ...)
  330.     -- variables
  331.     local state = "start"
  332.     local out = ""
  333.     local args = {...}
  334.    
  335.     -- current argument
  336.     local j = 1
  337.    
  338.     -- placeholder
  339.     local p = ""
  340.    
  341.     -- loop through string
  342.     for i=1,str:len() do
  343.         -- character
  344.         local c = str:sub(i,i)
  345.        
  346.         -- states
  347.         if state == "start" then
  348.             if c == "%" then
  349.                 -- check arguments
  350.                 if j > #args then
  351.                     error("[meekro] too few arguments passed to query")
  352.                 end
  353.                
  354.                 -- transition to percent
  355.                 state = "percent"
  356.                 p = ""
  357.             else
  358.                 -- append character
  359.                 out = out .. c
  360.             end
  361.         elseif state == "percent" then
  362.             -- append placeholder
  363.             p = p .. c
  364.            
  365.             -- check for automatic placeholder
  366.             if p == "?" then
  367.                 if not meekro._placeholders[type(args[j])] then
  368.                     out = out .. "%?"
  369.                     state = "start"
  370.                     j = j + 1
  371.                     MsgC(Color(255, 0, 0), "[meekro] query uses invalid value")
  372.                 else
  373.                     p = meekro._placeholders[type(args[j])]
  374.                 end
  375.             end
  376.            
  377.             -- replace with escaped string
  378.             if p == "s" then
  379.                 out = out .. "'" .. tostring(meekro.escape(args[j])) .. "'"
  380.                 state = "start"
  381.                 j = j + 1
  382.             elseif p == "i" then
  383.                 out = out .. math.floor(tonumber(args[j]))
  384.                 state = "start"
  385.                 j = j + 1
  386.             elseif p == "d" then
  387.                 out = out .. tonumber(args[j])
  388.                 state = "start"
  389.                 j = j + 1
  390.             elseif p == "b" then
  391.                 out = out .. (args[j] and 1 or 0)
  392.                 state = "start"
  393.                 j = j + 1
  394.             elseif p == "l" then
  395.                 out = out .. tostring(args[j])
  396.                 state = "start"
  397.                 j = j + 1
  398.             end
  399.            
  400.             -- check if no placeholders found
  401.             if p:len() > 2 then
  402.                 out = out .. "%" .. p
  403.                 state = "start"
  404.                 j = j + 1
  405.                 MsgC(Color(255, 0, 0), "[meekro] query uses invalid placeholder")
  406.             end
  407.         end
  408.     end
  409.    
  410.     return out
  411. end
RAW Paste Data