modules.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ='%s'",
  19. constDb["mysql"]["game"]["database"],
  20. tbName
  21. )
  22. local result = lib_game_mysql:query(sql)
  23. -- log.info("l_is_module_table_exist sql[%s] result[%s]", sql, tostring(result))
  24. if result and #result > 0 then
  25. return true
  26. end
  27. return false
  28. end
  29. -- 创建模块表
  30. local function l_create_module_table(mdlName, columnNameOptions, keyName, secondaryKeys)
  31. local tbName = string.format("mdl_%s", tostring(mdlName))
  32. local columns = ""
  33. for k, v in pairs(columnNameOptions) do
  34. columns = string.format("%s %s %s,", columns, k, v)
  35. end
  36. local sql =
  37. string.format(
  38. "CREATE TABLE %s (%s PRIMARY KEY (%s)) DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_unicode_ci;",
  39. tbName,
  40. columns,
  41. keyName
  42. )
  43. if not is_empty(secondaryKeys) then
  44. sql =
  45. string.format(
  46. "CREATE TABLE %s (%s PRIMARY KEY (%s), %s) DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_unicode_ci;",
  47. tbName,
  48. columns,
  49. keyName,
  50. secondaryKeys
  51. )
  52. end
  53. local ret = lib_game_mysql:query(sql)
  54. log.info("l_create_module_table sql[%s] ret[%s]", sql, tostring(ret))
  55. return true
  56. end
  57. -- 更新模块表字段
  58. local function l_modify_module_table(mdlName, columnNameOptions)
  59. local tbName = string.format("mdl_%s", tostring(mdlName))
  60. -- 更新字段
  61. local mapColumns = {}
  62. local sql =
  63. string.format(
  64. "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'",
  65. constDb["mysql"]["game"]["database"],
  66. tbName
  67. )
  68. local result = lib_game_mysql:query(sql)
  69. if result then
  70. for _, v in pairs(result) do
  71. assert(v["COLUMN_NAME"], cjson_encode(v))
  72. local column_name = v["COLUMN_NAME"]
  73. mapColumns[column_name] = true
  74. end
  75. end
  76. -- add column
  77. local columns = ""
  78. for k, v in pairs(columnNameOptions) do
  79. if not mapColumns[k] then
  80. if not is_empty(columns) then
  81. columns = columns .. ","
  82. end
  83. columns = string.format("%s %s %s", columns, k, v)
  84. end
  85. end
  86. if not is_empty(columns) then
  87. sql = string.format("alter table %s add %s;", tbName, columns)
  88. lib_game_mysql:query(sql)
  89. end
  90. end
  91. -- 初始数据库模块表
  92. function root:init_db_tables()
  93. for i, v in pairs(dataMode.get_module_class_map()) do
  94. local obj = v.new()
  95. local data = obj:mysql_get_table_info()
  96. if data and not is_empty(data.mdlName) then
  97. if not l_is_module_table_exist(data.mdlName) then
  98. l_create_module_table(data.mdlName, data.columnNameOptions, data.keyName, data.secondaryKeys)
  99. else
  100. l_modify_module_table(data.mdlName, data.columnNameOptions)
  101. end
  102. end
  103. end
  104. end
  105. return root