modules.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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)
  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. local ret = lib_game_mysql:query(sql)
  44. log.info("l_create_module_table sql[%s] ret[%s]", sql, tostring(ret))
  45. return true
  46. end
  47. -- 更新模块表字段
  48. local function l_modify_module_table(mdlName, columnNameOptions)
  49. local tbName = string.format("mdl_%s", tostring(mdlName))
  50. -- 更新字段
  51. local mapColumns = {}
  52. local sql =
  53. string.format(
  54. "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'",
  55. constDb["mysql"]["game"]["database"],
  56. tbName
  57. )
  58. local result = lib_game_mysql:query(sql)
  59. if result then
  60. for _, v in pairs(result) do
  61. assert(v["COLUMN_NAME"], cjson_encode(v))
  62. local column_name = v["COLUMN_NAME"]
  63. mapColumns[column_name] = true
  64. end
  65. end
  66. -- add column
  67. local columns = ""
  68. for k, v in pairs(columnNameOptions) do
  69. if not mapColumns[k] then
  70. if not is_empty(columns) then
  71. columns = columns .. ","
  72. end
  73. columns = string.format("%s %s %s", columns, k, v)
  74. end
  75. end
  76. if not is_empty(columns) then
  77. sql = string.format("alter table %s add %s;", tbName, columns)
  78. lib_game_mysql:query(sql)
  79. end
  80. end
  81. -- 初始数据库模块表
  82. function root:init_db_tables()
  83. for i, v in pairs(dataMode.get_module_class_map()) do
  84. local obj = v.new()
  85. local data = obj:mysql_get_table_info()
  86. if data and not is_empty(data.mdlName) then
  87. if not l_is_module_table_exist(data.mdlName) then
  88. l_create_module_table(data.mdlName, data.columnNameOptions, data.keyName)
  89. else
  90. l_modify_module_table(data.mdlName, data.columnNameOptions)
  91. end
  92. end
  93. end
  94. end
  95. return root