/// <summary> /// 禁止外部直接new /// </summary> /// <param name="p_isTransactional"></param> internal DataProvider(bool p_isTransactional) { // 根据方法的 Transaction 标签确定是否自动启动事务 // 整个Api调用结束后自动提交或回滚事务、关闭连接 _db = new MySqlAccess(false); if (p_isTransactional) { _db.BeginTrans().Wait(); } }
/// <summary> /// 缓存当前服务的所有Sql语句,表名xxx_sql /// </summary> public static void LoadCacheSql() { try { var ls = new MySqlAccess().Each($"select id,`sql` from {Kit.SvcName}_sql").Result; foreach (Row item in ls) { _sqlDict[item.Str("id")] = item.Str("sql"); } Log.Information("缓存Sql成功"); } catch (Exception e) { Log.Fatal(e, "缓存Sql失败!"); throw; } }
/// <summary> /// 一个事务内执行多个Sql /// </summary> /// <param name="p_dts">参数列表,每个Dict中包含两个键:text,params,text为sql语句params类型为Dict或List{Dict}</param> /// <returns>返回执行后影响的行数</returns> public async Task <int> BatchExec(List <Dict> p_dts) { if (p_dts == null || p_dts.Count == 0) { return(0); } MySqlAccess db = new MySqlAccess(); try { int cnt = 0; await db.BeginTrans(); foreach (Dict dt in p_dts) { string sql = (string)dt["text"]; if (dt["params"] is List <Dict> ls) { foreach (var par in ls) { cnt += await db.Exec(sql, par); } } else if (dt["params"] is Dict par) { cnt += await db.Exec(sql, par); } } await db.CommitTrans(); return(cnt); } catch { await db.RollbackTrans(); throw; } }