/// <summary> /// 执行写操作 /// </summary> public static WriteReturn Write(string name, DbParameter[] param, DataContext db = null, string key = null, bool isOutSql = false) { key = key == null?MapDb(name) : key; var config = db == null?DataConfig.Get(key) : db.config; if (config.IsUpdateCache) { InstanceMap(key); } if (DbCache.Exists(config.CacheType, name.ToLower())) { var sql = MapXml.GetMapSql(name, ref param, db, key); BaseAop.AopMapBefore(name, sql, param, config, AopType.Map_Write); isOutSql = isOutSql ? isOutSql : IsMapLog(name); var result = FastWrite.ExecuteSql(sql, param, db, key, isOutSql, false); BaseAop.AopMapAfter(name, sql, param, config, AopType.Map_Write, result); return(result); } else { BaseAop.AopMapBefore(name, "", param, config, AopType.Map_Write); var data = new WriteReturn(); BaseAop.AopMapAfter(name, "", param, config, AopType.Map_Write, data); return(data); } }
/// <summary> /// maq 执行分页 /// </summary> public static PageResult QueryPage(PageModel pModel, string name, DbParameter[] param, DataContext db = null, string key = null, bool isOutSql = false) { key = key == null?MapDb(name) : key; var config = db == null?DataConfig.Get(key) : db.config; if (config.IsUpdateCache) { InstanceMap(key); } if (DbCache.Exists(config.CacheType, name.ToLower())) { var sql = MapXml.GetMapSql(name, ref param, db, key); isOutSql = isOutSql ? isOutSql : IsMapLog(name); BaseAop.AopMapBefore(name, sql, param, config, AopType.Map_Page_Dic); var result = ExecuteSqlPage(pModel, sql, param, db, key, isOutSql); if (MapXml.MapIsForEach(name, config)) { if (db == null) { using (var tempDb = new DataContext(key)) { for (var i = 1; i <= MapXml.MapForEachCount(name, config); i++) { result.list = MapXml.MapForEach(result.list, name, tempDb, key, config, i); } } } else { result.list = MapXml.MapForEach(result.list, name, db, key, config); } } BaseAop.AopMapAfter(name, sql, param, config, AopType.Map_Page_Dic, result.list); return(result); } else { BaseAop.AopMapBefore(name, "", param, config, AopType.Map_Page_Dic); var data = new PageResult(); BaseAop.AopMapAfter(name, "", param, config, AopType.Map_Page_Dic, data.list); return(data); } }
/// <summary> /// maq 执行返回结果 /// </summary> public List <T> Query <T>(string name, DbParameter[] param, DataContext db = null, string key = null, bool isOutSql = false) where T : class, new() { key = key == null?MapDb(name) : key; var config = db == null?DataConfig.Get(key) : db.config; if (config.IsUpdateCache) { InstanceMap(key); } if (DbCache.Exists(config.CacheType, name.ToLower())) { var sql = MapXml.GetMapSql(name, ref param, db, key); isOutSql = isOutSql ? isOutSql : IsMapLog(name); BaseAop.AopMapBefore(name, sql, param, config, AopType.Map_List_Model); var result = FastRead.ExecuteSql <T>(sql, param, db, key, isOutSql, false); if (MapXml.MapIsForEach(name, config)) { if (db == null) { using (var tempDb = new DataContext(key)) { for (var i = 1; i <= MapXml.MapForEachCount(name, config); i++) { result = MapXml.MapForEach <T>(result, name, tempDb, config, i); } } } else { result = MapXml.MapForEach <T>(result, name, db, config); } } BaseAop.AopMapAfter(name, sql, param, config, AopType.Map_List_Model, result); return(result); } else { BaseAop.AopMapBefore(name, "", param, config, AopType.Map_List_Model); var data = new List <T>(); BaseAop.AopMapAfter(name, "", param, config, AopType.Map_List_Model, data); return(data); } }
/// <summary> /// maq 执行分页 /// </summary> public PageResult <T> QueryPage <T>(PageModel pModel, string name, DbParameter[] param, DataContext db = null, string key = null) where T : class, new() { key = key == null?MapDb(name) : key; var config = db == null?DataConfig.Get(key) : db.config; if (config.IsUpdateCache) { InstanceMap(key); } if (DbCache.Exists(config.CacheType, name.ToLower())) { var sql = MapXml.GetMapSql(name, ref param, db, key); var result = ExecuteSqlPage <T>(pModel, sql, param, db, key); if (MapXml.MapIsForEach(name, config)) { if (db == null) { using (var tempDb = new DataContext(key)) { for (var i = 1; i <= MapXml.MapForEachCount(name, config); i++) { result.list = MapXml.MapForEach <T>(result.list, name, tempDb, config, i); } } } else { result.list = MapXml.MapForEach <T>(result.list, name, db, config); } } return(result); } else { return(new PageResult <T>()); } }
/// <summary> /// maq 执行返回 List<Dictionary<string, object>> /// </summary> public List <Dictionary <string, object> > Query(string name, DbParameter[] param, DataContext db = null, string key = null) { key = key == null?MapDb(name) : key; var config = db == null?DataConfig.Get(key) : db.config; if (config.IsUpdateCache) { InstanceMap(key); } if (DbCache.Exists(config.CacheType, name.ToLower())) { var sql = MapXml.GetMapSql(name, ref param, db, key); var result = FastRead.ExecuteSql(sql, param, db, key); if (MapXml.MapIsForEach(name, config)) { if (db == null) { using (var tempDb = new DataContext(key)) { for (var i = 1; i <= MapXml.MapForEachCount(name, config); i++) { result = MapXml.MapForEach(result, name, tempDb, key, config, i); } } } else { result = MapXml.MapForEach(result, name, db, key, config); } } return(result); } else { return(new List <Dictionary <string, object> >()); } }
/// <summary> /// 执行写操作 /// </summary> public WriteReturn Write(string name, DbParameter[] param, DataContext db = null, string key = null) { key = key == null?MapDb(name) : key; var config = db == null?DataConfig.Get(key) : db.config; if (config.IsUpdateCache) { InstanceMap(key); } if (DbCache.Exists(config.CacheType, name.ToLower())) { var sql = MapXml.GetMapSql(name, ref param, db, key); return(FastWrite.ExecuteSql(sql, param, db, key)); } else { return(new WriteReturn()); } }
void BeginRequest(object sender, EventArgs e) { var success = true; var dic = new Dictionary <string, object>(); var param = new List <DbParameter>(); var context = ((HttpApplication)sender); var key = context.Request.CurrentExecutionFilePath; key = string.IsNullOrEmpty(key) ? "" : key.Substring(1, key.Length - 1); if (FastMap.IsExists(key)) { context.Response.StatusCode = 200; context.Response.ContentType = "application/Json"; var data = new List <Dictionary <string, object> >(); var pageInfo = new PageResult(); var dbKey = FastMap.MapDb(key).ToStr(); var type = FastMap.MapType(key).ToStr(); var config = FastMap.DbConfig(dbKey); var url = context.Request.Form; if (url.Count == 0) { url = context.Request.QueryString; } var pageSize = url.GetValues("pageSize").ToStr().ToInt(10); var pageId = url.GetValues("pageId").ToStr().ToInt(1); foreach (var item in url) { var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter(); temp.ParameterName = item.ToStr(); temp.Value = url.Get(item.ToStr()); param.Add(temp); var existsKey = FastMap.MapExists(key, temp.ParameterName); var checkKey = FastMap.MapCheck(key, temp.ParameterName); //required if (!string.IsNullOrEmpty(FastMap.MapRequired(key, temp.ParameterName))) { dic.Add("success", false); dic.Add("error", string.Format("{0}不能为空", item)); param.Remove(temp); break; } //max length if (FastMap.MapMaxlength(key, temp.ParameterName).ToInt(0) != 0) { if (!(FastMap.MapMaxlength(key, temp.ParameterName).ToInt(0) >= temp.Value.ToStr().Length)) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1},最大长度{2}", item, temp.Value.ToStr(), FastMap.MapMaxlength(key, temp.ParameterName))); param.Remove(temp); break; } } //exists if (!string.IsNullOrEmpty(existsKey)) { var existsListParam = new List <DbParameter>(); var existsParam = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter(); existsParam.ParameterName = temp.ParameterName; existsParam.Value = temp.Value; existsListParam.Add(existsParam); var checkData = FastMap.Query(existsKey, existsListParam.ToArray())?.First() ?? new Dictionary <string, object>(); if (checkData.GetValue("count").ToStr().ToInt(0) >= 1) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1}已存在", item, temp.Value)); param.Remove(temp); break; } } //check if (!string.IsNullOrEmpty(checkKey)) { var checkListParam = new List <DbParameter>(); var checkParam = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter(); checkParam.ParameterName = temp.ParameterName; checkParam.Value = temp.Value; checkListParam.Add(checkParam); var checkData = FastMap.Query(existsKey, checkListParam.ToArray())?.First() ?? new Dictionary <string, object>(); if (checkData.GetValue("count").ToStr().ToInt(0) < 1) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1}不存在", item, temp.Value)); param.Remove(temp); break; } } //date if (string.Compare(FastMap.MapDate(key, temp.ParameterName).ToStr(), "true", true) == 0) { if (!BaseRegular.IsDate(temp.Value.ToStr())) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1},不是日期类型", item, temp.Value)); param.Remove(temp); break; } temp.Value = temp.Value.ToDate(); temp.DbType = System.Data.DbType.DateTime; } } if (dic.Count > 0) { context.Response.StatusCode = 200; context.Response.ContentType = "application/Json"; context.Response.Write(BaseJson.ModelToJson(dic)); context.Response.End(); } else { using (var db = new DataContext(dbKey)) { var tempParam = param.ToArray(); var sql = MapXml.GetMapSql(key, ref tempParam, db, dbKey); if (string.Compare(type, AppConfig.PageAll, true) == 0 || string.Compare(type, AppConfig.Page, true) == 0) { success = true; var page = new PageModel(); page.PageSize = pageSize == 0 ? 10 : pageSize; page.PageId = pageId == 0 ? 1 : pageId; pageInfo = db.GetPageSql(page, sql, tempParam).PageResult; dic.Add("data", pageInfo.list); dic.Add("page", pageInfo.pModel); } else if (string.Compare(type, AppConfig.All, true) == 0) { success = true; data = db.ExecuteSqlList(sql, tempParam, false).DicList; dic.Add("data", data); } else if (string.Compare(type, AppConfig.Write, true) == 0 && param.Count > 0) { var result = db.ExecuteSqlList(sql, tempParam, false).writeReturn; if (result.IsSuccess) { success = true; } else { success = false; dic.Add("error", result.Message); } } else { if (param.Count > 0) { success = true; data = db.ExecuteSqlList(sql, tempParam, false).DicList; dic.Add("data", data); } else { success = false; } } } } //if (FastMap.MapView(key).ToStr() != "") //{ // context.Response.ContentType = "text/html;charset=utf-8"; //} //else { dic.Add("success", success); context.Response.Write(BaseJson.ModelToJson(dic)); context.Response.End(); } } }
public async Task ContentAsync(HttpContext context, IFastRepository IFast, bool IsResource, string dbFile = "db.json") { var name = context.Request.Path.Value.ToStr().Substring(1, context.Request.Path.Value.ToStr().Length - 1).ToLower(); var urlParam = HttpUtility.UrlDecode(GetUrlParam(context)); var success = true; var dic = new Dictionary <string, object>(); var data = new List <Dictionary <string, object> >(); var dbKey = IFast.MapDb(name).ToStr(); var pageInfo = new PageResult(); context.Response.StatusCode = 200; context.Response.ContentType = "application/Json"; if (!IFast.IsExists(name)) { dic.Add("success", false); dic.Add("error", "接口不存在"); await context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8).ConfigureAwait(false); } else if (dbKey == "") { dic.Add("success", false); dic.Add("error", string.Format("map id {0}的db没有配置", name)); await context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8).ConfigureAwait(false); } else if (IFast.IsExists(name)) { var param = new List <DbParameter>(); foreach (var item in IFast.MapParam(name)) { var checkKey = IFast.MapCheck(name, item); var existsKey = IFast.MapExists(name, item); var tempParam = DbProviderFactories.GetFactory(IFast.MapDb(name), IsResource, dbFile).CreateParameter(); tempParam.ParameterName = item; tempParam.Value = GetUrlParam(urlParam, item); param.Add(tempParam); if (!string.IsNullOrEmpty(IFast.MapRequired(name, item))) { if (!(string.Compare(IFast.MapRequired(name, item), "true", true) == 0 && !string.IsNullOrEmpty(tempParam.Value.ToStr()))) { dic.Add("success", false); dic.Add("error", string.Format("{0}不能为空", item)); param.Remove(tempParam); break; } } if (IFast.MapMaxlength(name, item).ToInt(0) != 0) { if (!(IFast.MapMaxlength(name, item).ToInt(0) >= tempParam.Value.ToStr().Length)) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1},最大长度{2}", item, tempParam.Value.ToStr(), IFast.MapMaxlength(name, item))); param.Remove(tempParam); break; } } if (!string.IsNullOrEmpty(existsKey)) { var existsListParam = new List <DbParameter>(); var existsParam = DbProviderFactories.GetFactory(IFast.MapDb(existsKey), IsResource, dbFile).CreateParameter(); existsParam.ParameterName = item; existsParam.Value = tempParam.Value; existsListParam.Add(existsParam); var checkData = IFast.Query(existsKey, existsListParam.ToArray())?.First() ?? new Dictionary <string, object>(); if (checkData.GetValue("count").ToStr().ToInt(0) >= 1) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1}已存在", item, tempParam.Value)); param.Remove(tempParam); break; } } if (!string.IsNullOrEmpty(checkKey)) { var checkListParam = new List <DbParameter>(); var checkParam = DbProviderFactories.GetFactory(IFast.MapDb(checkKey), IsResource, dbFile).CreateParameter(); checkParam.ParameterName = item; checkParam.Value = GetUrlParam(urlParam, item); checkListParam.Add(checkParam); var checkData = IFast.Query(existsKey, checkListParam.ToArray())?.First() ?? new Dictionary <string, object>(); if (checkData.GetValue("count").ToStr().ToInt(0) < 1) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1}不存在", item, tempParam.Value)); param.Remove(tempParam); break; } } if (string.Compare(IFast.MapDate(name, item).ToStr(), "true", true) == 0) { if (!BaseRegular.IsDate(tempParam.Value.ToStr())) { dic.Add("success", false); dic.Add("error", string.Format("{0}:{1},不是日期类型", item, tempParam.Value)); param.Remove(tempParam); break; } tempParam.Value = tempParam.Value.ToDate(); tempParam.DbType = System.Data.DbType.DateTime; } if (tempParam.Value.ToStr() == "") { param.Remove(tempParam); } } using (var db = new DataContext(dbKey)) { var tempParam = param.ToArray(); var sql = MapXml.GetMapSql(name, ref tempParam, db, dbKey); if (dic.Count > 0) { await context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8).ConfigureAwait(false); } else if (string.Compare(IFast.MapType(name).ToStr(), AppConfig.PageAll, true) == 0 || string.Compare(IFast.MapType(name).ToStr(), AppConfig.Page, true) == 0) { success = true; var pageSize = GetUrlParam(urlParam, "PageSize"); var pageId = GetUrlParam(urlParam, "PageId"); var page = new PageModel(); page.PageSize = pageSize.ToInt(0) == 0 ? 10 : pageSize.ToInt(0); page.PageId = pageId.ToInt(0) == 0 ? 1 : pageId.ToInt(0); pageInfo = db.GetPageSql(page, sql, tempParam).PageResult; if (IFast.MapView(name).ToStr() == "") { dic.Add("data", pageInfo.list); dic.Add("page", pageInfo.pModel); } } else if (string.Compare(IFast.MapType(name).ToStr(), AppConfig.All, true) == 0) { success = true; data = db.ExecuteSqlList(sql, tempParam, false).DicList; dic.Add("data", data); } else if (string.Compare(IFast.MapType(name).ToStr(), AppConfig.Write, true) == 0 && param.Count > 0) { var result = db.ExecuteSqlList(sql, tempParam, false).writeReturn; if (result.IsSuccess) { success = true; } else { success = false; dic.Add("error", result.Message); } } else { if (param.Count > 0) { success = true; data = db.ExecuteSqlList(sql, tempParam, false).DicList; dic.Add("data", data); } else { success = false; } } } dic.Add("success", success); await context.Response.WriteAsync(BaseJson.ModelToJson(dic), Encoding.UTF8).ConfigureAwait(false); } }