modules.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 19:29:32
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-04 19:30:06
  8. --]]
  9. local dataMode = require("dataMode")
  10. local lib_game_mysql = require("lib_game_mysql")
  11. local constDb = require("db")
  12. local root = {}
  13. -- 模块表是否已存在
  14. local function l_is_module_table_exist(mdlName)
  15. local tbName = string.format("mdl_%s", tostring(mdlName))
  16. local sql =
  17. string.format(
  18. "SELECT table_name FROM information_schema.TABLES WHERE TABLE_SCHEMA='%s' and table_name ='mdl_%s'",
  19. constDb["mysql"]["game"]["database"],
  20. tbName
  21. )
  22. local result = lib_game_mysql:query(sql)
  23. if result and #result > 0 then
  24. return true
  25. end
  26. return false
  27. end
  28. -- 创建模块表
  29. local function l_create_module_table(mdlName, columnNameOptions, keyName)
  30. local tbName = string.format("mdl_%s", tostring(mdlName))
  31. local columns = ""
  32. for k, v in pairs(columnNameOptions) do
  33. columns = string.format("%s %s %s,", columns, k, v)
  34. end
  35. local sql =
  36. string.format(
  37. "CREATE TABLE %s (%s PRIMARY KEY (%s)) DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_unicode_ci;",
  38. tbName,
  39. columns,
  40. keyName
  41. )
  42. local ret = lib_game_mysql:query(sql)
  43. log.info("l_create_module_table sql[%s] ret[%s]", sql, tostring(ret))
  44. return true
  45. end
  46. -- 更新模块表字段
  47. local function l_modify_module_table(mdlName, columnNameOptions)
  48. local tbName = string.format("mdl_%s", tostring(mdlName))
  49. -- 更新字段
  50. local tableColumns = {}
  51. local sql =
  52. string.format(
  53. "select COLUMN_NAME, IS_NULLABLE, COLUMN_TYPE, DATA_TYPE, COLUMN_COMMENT, COLUMN_DEFAULT from information_schema.columns where table_schema = '%s' and table_name = '%s'",
  54. constDb["mysql"]["game"]["database"],
  55. tbName
  56. )
  57. local result = lib_game_mysql:query(sql)
  58. if result then
  59. for _k, _v in pairs(result) do
  60. assert(v["COLUMN_NAME"], cjson_encode(v))
  61. local column_name = v["COLUMN_NAME"]
  62. tableColumns[column_name] = true
  63. end
  64. end
  65. -- add column
  66. local columns = ""
  67. for _k, _v in pairs(columnNameOptions) do
  68. if not tableColumns[_k] then
  69. if not is_empty(columns) then
  70. columns = columns .. ","
  71. end
  72. columns = string.format("%s %s %s", columns, _k, _v)
  73. end
  74. end
  75. if not is_empty(columns) then
  76. sql = string.format("alter table %s add %s;", tbName, columns)
  77. lib_game_mysql:query(sql)
  78. end
  79. end
  80. -- 初始数据库模块表
  81. function root:init_db_tables()
  82. for i, v in pairs(dataMode.get_module_class_map()) do
  83. local obj = v.new()
  84. local data = obj:mysql_get_table_info()
  85. if data and not is_empty(data.mdlName) then
  86. if not l_is_module_table_exist(data.mdlName) then
  87. l_create_module_table(data.mdlName, data.columnNameOptions, data.keyName)
  88. else
  89. l_modify_module_table(data.mdlName, data.columnNameOptions)
  90. end
  91. end
  92. end
  93. end
  94. return root