checks.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --检测配置
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. local mysql = require "skynet.db.mysql"
  7. local config = require "run_config"
  8. local cjson = require "cjson"
  9. local db
  10. local mysqldtaskbx = {}
  11. -- SELECT * FROM products WHERE JSON_CONTAINS(main_id_info, '111') LIMIT 100
  12. function M.set_products_main_id(msg_body)
  13. local isok ,key = tools.checkData({"main_id_info","id"},msg_body)
  14. if not isok then
  15. return false,string.format("缺少字段: %s.", key)
  16. end
  17. local sql = string.format("UPDATE products SET main_id_info = '%s' WHERE id = %d ",cjson.encode(msg_body.main_id_info),msg_body.id)
  18. mysqldtaskbx.Singleton().query(sql)
  19. return true, {}
  20. end
  21. function M.search_products(msg_body)
  22. local isok ,key = tools.checkData({"page_size","page_number"},msg_body)
  23. if not isok then
  24. return false,string.format("缺少字段: %s.", key)
  25. end
  26. local page_size = msg_body.page_size
  27. local page_number = msg_body.page_number
  28. local offset = (page_number - 1) * page_size
  29. local sql = "SELECT * FROM products "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  30. local list = mysqldtaskbx.Singleton().query(sql)
  31. sql = "SELECT COUNT(*) AS total FROM products "
  32. local total = mysqldtaskbx.Singleton().query(sql)
  33. return true,list,total[1].total
  34. end
  35. function mysqldtaskbx.start()
  36. local function on_connect(db)
  37. db:query("set charset utf8mb4");
  38. end
  39. local conf = config.db_cnf.book_server.mysqldb_checks_cnf
  40. db = mysql.connect{
  41. host=conf.ip,
  42. port=conf.port,
  43. database=conf.db,
  44. user=conf.user,
  45. password=conf.password,
  46. charset="utf8mb4",
  47. max_packet_size = 1024 * 1024,
  48. on_connect = on_connect
  49. }
  50. if not db then
  51. skynet.error("mysql connect fail")
  52. end
  53. end
  54. function mysqldtaskbx.Singleton()
  55. if db == nil then
  56. mysqldtaskbx.start()
  57. end
  58. return mysqldtaskbx
  59. end
  60. function mysqldtaskbx.query(sql)
  61. return db:query(sql)
  62. end
  63. return M