Advertisement
Guest User

Untitled

a guest
Aug 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. local module = {}
  2. module.acctable = "accounts"
  3. module.oop = {}
  4.  
  5. function module.init ()
  6.     assert(module.connection, "MySql подключение не существует")
  7.  
  8.     module.check_table ('db', module.acctable, [[(
  9.             `id` int(11) NOT NULL AUTO_INCREMENT,
  10.             `vkid` int(11) NOT NULL,
  11.             PRIMARY KEY (`id`)
  12.         ) ENGINE=InnoDB DEFAULT CHARSET=utf8;]]
  13.     );
  14. end
  15.  
  16. function module.start()
  17.     -- OOP
  18.     function module.oop:set (field, value)
  19.         self[field] = value
  20.         module("UPDATE `%s` SET `%s`='%s' WHERE `vkid`=%i", module.acctable, field, value, self.vkid)
  21.     end
  22.  
  23.     function module.oop:add (field, value)
  24.         self[field] = self[field] + value
  25.         module("UPDATE `%s` SET `%s`=`%s`+'%s' WHERE `vkid`=%i", module.acctable, field, field, value, self.vkid)
  26.     end
  27.  
  28.     -- Modules tools
  29.     if botcmd then
  30.         botcmd.arg_types['U'] = function (args, arg, offset)
  31.             if arg:find("%[(.-)|.-%]") then arg = arg:gsub("%[(.-)|.-%]", "%1") end
  32.             return ca (module.get_user(getId(arg)), "пользователь не найден")
  33.         end
  34.     end
  35. end
  36. module.CheckInstall = module.init -- TOREMOVE
  37.  
  38. function module.find_table (table_name)
  39.     local index = "Tables_in_"..db.settings.database
  40.     local tables = module("SHOW TABLES")
  41.     for i = 1,#tables do
  42.         if tables[i][index] == table_name then
  43.             return tables[i][index]
  44.         end
  45.     end
  46. end,
  47.  
  48. function moduke.find_column (table_name, column_name)
  49.     local columns = module("SHOW COLUMNS FROM `%s`", table_name)
  50.     for i = 1, #columns do
  51.         if columns[i].Field == column_name then
  52.             return columns[i]
  53.         end
  54.     end
  55. end
  56.  
  57. function module.check_column (module_name, table_name, column_name, args)
  58.     if module.find_column(table_name, column_name) then return true end
  59.     console.log(module_name, "Создаю поле %s в %s...", column_name, table_name)
  60.     module("ALTER TABLE `%s` ADD `%s` %s", table_name, column_name, args)
  61.     console.log(module_name, "Поле %s в было успешно создано.", column_name)
  62. end
  63.  
  64. function module.check_table (module_name, table_name, command)
  65.     if module.find_table(table_name) then return true end
  66.     console.log(module_name, "Таблица %s не найдена.", table_name)
  67.     console.log(module_name, "Создание таблицы %s...", table_name)
  68.     module("CREATE TABLE `%s` " .. command, table_name)
  69.     console.log(module_name, "Таблица %s успешно создана!", table_name)
  70.     relua()
  71. end
  72.  
  73. function module.connect(ip, username, password, database)
  74.     module.connection = mysql(ip, username, password, database)
  75.     module("SET NAMES utf8mb4")
  76.     module.settings = { ip = ip, username = username, password = password, database = database }
  77. end
  78.  
  79. function module.get_user_safe(vkid)
  80.     if not vkid then return end
  81.     local user = module('SELECT * FROM `%s` WHERE vkid=%i', module.acctable, vkid)[1]
  82.     if not user then return false end
  83.     setmetatable(user, { __index = module.oop })
  84.     return user
  85. end
  86.  
  87. function module.get_user_from_url(url)
  88.     if not url then return end
  89.     if url:find("%[(.-)|.-%]") then url = url:gsub("%[(.-)|.-%]", "%1") end
  90.     return url and module.get_user_safe(getId(url))
  91. end
  92.  
  93. function module.get_user(vkid)
  94.     if not vkid then return end
  95.     local user = module.get_user_safe(vkid)
  96.     if not user then
  97.         module("INSERT INTO `%s` (`vkid`) VALUES (%i)", module.acctable, vkid)
  98.         user = module.get_user_safe(vkid)
  99.     end
  100.     return user;
  101. end
  102.  
  103. function db.select(what, table, where, ...)
  104.     return module.mc("SELECT %s FROM `%s` " .. (where and "WHERE "..where or ''), what, table, ...)
  105. end
  106.  
  107. function db.select_one(what, table, where, ...)
  108.     return module.mc("SELECT %s FROM `%s` " .. (where and "WHERE "..where or ''), what, table, ...)[1]
  109. end
  110.  
  111. function db.get_count(table, where, ...)
  112.     return module.mc("SELECT COUNT(id) FROM `%s` " .. (where and "WHERE "..where or ''), table, ...)[1]['COUNT(id)']
  113. end
  114.  
  115. setmetatable(module, { __call = function (_, query, ...) return module.connection(query, ...) end })
  116. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement