sqlUtil.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. local root = {}
  2. local function formatColumnValue(v)
  3. if type(v) == "string" then
  4. return string.format("'%s'", v)
  5. end
  6. return v
  7. end
  8. function root.is_table_exist(database, tableName)
  9. return string.format(
  10. "SELECT table_name FROM information_schema.TABLES WHERE TABLE_SCHEMA='%s' and table_name ='%s'",
  11. database,
  12. tableName
  13. )
  14. end
  15. function root.create_table(tableName, columnNameOptions, primaryKey, secondaryKeys)
  16. local str = "CREATE TABLE " .. tableName .. " ("
  17. for key, value in pairs(columnNameOptions) do
  18. str = str .. key .. " " .. value .. ","
  19. end
  20. if secondaryKeys then
  21. str = str .. string.format("PRIMARY KEY (%s), %s)", primaryKey, secondaryKeys)
  22. else
  23. str = str .. string.format("PRIMARY KEY (%s))", primaryKey)
  24. end
  25. str = str .. "DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_unicode_ci;"
  26. return str
  27. end
  28. function root.get_able_column_options(database, tableName)
  29. return string.format(
  30. "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'",
  31. database,
  32. tableName
  33. )
  34. end
  35. function root.alter_column_type(tableName, columnName, ops)
  36. return string.format("alter table %s alter column %s %s;", tableName, columnName, ops)
  37. end
  38. function root.alter_table_comment(tableName, commment)
  39. return string.format("alter table %s comment %s;", tableName, commment)
  40. end
  41. function root.alter_table_add_column(tableName, columnName, ops)
  42. return string.format("alter table %s add %s %s;", tableName, columnName, ops)
  43. end
  44. function root.select_table(tableName)
  45. return string.format("select * from %s;", tableName)
  46. end
  47. function root.select_table_by_key(tableName, key, value)
  48. return string.format("select * from %s where %s = %s;", tableName, key, formatColumnValue(value))
  49. end
  50. function root.insert(tableName, row)
  51. local columns, values = {}, {}
  52. for k, v in pairs(row) do
  53. table.insert(columns, k)
  54. table.insert(values, formatColumnValue(v))
  55. end
  56. return string.format(
  57. "insert into %s(%s) values(%s);",
  58. tableName,
  59. table.concat(columns, ","),
  60. table.concat(values, ",")
  61. )
  62. end
  63. function root.insert_or_update_data(tableName, row, key)
  64. local sql = root.insert(tableName, row)
  65. if string.sub(sql, #sql) == ";" then
  66. sql = string.sub(sql, 1, #sql - 1)
  67. end
  68. local updates = {}
  69. for k, v in pairs(row) do
  70. table.insert(updates, string.format("%s=%s", k, formatColumnValue(v)))
  71. end
  72. return string.format("%s on duplicate key update %s;", sql, table.concat(updates, ","))
  73. end
  74. function root.delete_by_key(tableName, key, value)
  75. return string.format("delete from %s where %s = %s;", tableName, key, formatColumnValue(value))
  76. end
  77. function root.update_data(tableName, row, key, value)
  78. local body = {}
  79. for k, v in pairs(row) do
  80. table.insert(body, string.format("%s=%s", k, formatColumnValue(v)))
  81. end
  82. local setvalues = table.concat(body, ",")
  83. return string.format("update %s set %s where %s =%s;", tableName, setvalues, key, formatColumnValue(value))
  84. end
  85. function root.getMax(tableName, key)
  86. return string.format("select max(%s) as %s from %s", key, key, tableName)
  87. end
  88. function root.replace(tableName, row)
  89. local columns, values = {}, {}
  90. for k, v in pairs(row) do
  91. table.insert(columns, k)
  92. table.insert(values, formatColumnValue(v))
  93. end
  94. return string.format(
  95. "replace into %s (%s) values(%s)",
  96. tableName,
  97. table.concat(columns, ","),
  98. table.concat(values, ",")
  99. )
  100. end
  101. function root.escape_sql(sql)
  102. local result = ""
  103. for i = 1, string.len(sql) do
  104. local escape = nil
  105. local char_num = string.byte(string.sub(sql, i, i))
  106. if char_num == 0 then
  107. escape = "0"
  108. elseif char_num == string.byte("'") then
  109. escape = "'"
  110. elseif char_num == string.byte('"') then
  111. escape = '"'
  112. elseif char_num == string.byte("\b") then
  113. escape = "b"
  114. elseif char_num == string.byte("\n") then
  115. escape = "n"
  116. elseif char_num == string.byte("\r") then
  117. escape = "r"
  118. elseif char_num == string.byte("\t") then
  119. escape = "t"
  120. elseif char_num == 26 then
  121. escape = "Z"
  122. elseif char_num == string.byte("\\") then
  123. escape = "\\"
  124. end
  125. if escape then
  126. result = result .. "\\" .. escape
  127. else
  128. result = result .. string.sub(sql, i, i)
  129. end
  130. end
  131. return result
  132. end
  133. function root.is_database_exist(dbName)
  134. return string.format("show databases like '%s'", dbName)
  135. end
  136. function root.create_catabase(dbName)
  137. return string.format(
  138. "create database if not exists '%s' default character set utf8mb4 default collate utf8mb4_bin;",
  139. dbName
  140. )
  141. end
  142. return root