--检测配置 local M = {} local mysqldbx = require "mysqldbx" local tools = require "tools" local skynet = require "skynet" local mysql = require "skynet.db.mysql" local config = require "run_config" local cjson = require "cjson" local db local mysqldtaskbx = {} -- SELECT * FROM products WHERE JSON_CONTAINS(main_id_info, '111') LIMIT 100 function M.set_products_main_id(msg_body) local isok ,key = tools.checkData({"main_id_info","id"},msg_body) if not isok then return false,string.format("缺少字段: %s.", key) end local sql = string.format("UPDATE products SET main_id_info = '%s' WHERE id = %d ",cjson.encode(msg_body.main_id_info),msg_body.id) mysqldtaskbx.Singleton().query(sql) return true, {} end function M.search_products(msg_body) local isok ,key = tools.checkData({"page_size","page_number"},msg_body) if not isok then return false,string.format("缺少字段: %s.", key) end local page_size = msg_body.page_size local page_number = msg_body.page_number local offset = (page_number - 1) * page_size local sql = "SELECT * FROM products "..string.format(" LIMIT %d OFFSET %d ",page_size, offset) local list = mysqldtaskbx.Singleton().query(sql) sql = "SELECT COUNT(*) AS total FROM products " local total = mysqldtaskbx.Singleton().query(sql) return true,list,total[1].total end function mysqldtaskbx.start() local function on_connect(db) db:query("set charset utf8mb4"); end local conf = config.db_cnf.book_server.mysqldb_checks_cnf db = mysql.connect{ host=conf.ip, port=conf.port, database=conf.db, user=conf.user, password=conf.password, charset="utf8mb4", max_packet_size = 1024 * 1024, on_connect = on_connect } if not db then skynet.error("mysql connect fail") end end function mysqldtaskbx.Singleton() if db == nil then mysqldtaskbx.start() end return mysqldtaskbx end function mysqldtaskbx.query(sql) return db:query(sql) end return M