/// <summary> /// /// </summary> /// <param name="dJson"></param> /// <param name="dba"></param> /// <returns></returns> protected string OnSave(dynamic dJson, DBAccessor dba, bool isDelete = false) { string json = string.Empty; DynamicJson dj = dJson as DynamicJson; if (dj.IsList) { json = "["; int i = 0; foreach (DynamicJson djItem in dj) { if (isDelete) { djItem.Set("IsDeleted", true); } json += DoSaveOne(dJson[i], dba) + ","; i++; } json.TrimEnd(','); json += "]"; } else if (dj.PropertyCount > 0) { if (isDelete) { dj.Set("IsDeleted", true); } json = DoSaveOne(dj, dba); } return(json); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dba"></param> /// <param name="dj"></param> /// <param name="primaryKey"></param> /// <param name="tableName"></param> /// <param name="seqName"></param> /// <returns></returns> protected string SaveOne(Type t, DBAccessor dba, DynamicJson dj, string primaryKey = "Id") { int rows = 0; string sql = string.Empty; string json = string.Empty; string tableName = TableName(t); IList <string> cols = new List <string>(); try { if (GetPrimaryKey(t).Count() > 0) { primaryKey = GetPrimaryKey(t).FirstOrDefault().Name; } if (dj.Get <int>(primaryKey) >= 0) { json = this.Update(t, dba, dj); return(json); } string seqName = SeqName(t); int id = dj.Get <int>(primaryKey); if (!seqName.IsNullOrWhiteSpace()) { id = dba.ExecuteScalar(string.Format(" select {0}.NEXTVAL from dual", seqName), false); dj.Set(primaryKey, id); } dj.Set("RowVersion", DateTime.Now); DbParameter[] paras = BuildParameter(t, dba, dj, ref cols); sql = string.Format(@" insert into {0} ( {1}) values ( {2} )", tableName, cols.ToString(" ,"), cols.Select(a => ":" + a).ToString(",")); rows = dba.ExecuteCommand(sql, paras, false); json = dba.GetReader(string.Format("select * from {0} where {1}=:{1}", tableName, primaryKey), dba.CreateDbParameter(primaryKey, dj.Get(primaryKey)), false) .ToList(t).FirstOrDefault().ToJson(); } catch (Exception ex) { return(Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName(), ex)); } return(json); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dba"></param> /// <param name="dj"></param> /// <param name="primaryKey"></param> /// <param name="tableName"></param> /// <returns></returns> protected string Update(Type t, DBAccessor dba, DynamicJson dj, string primaryKey = "Id") { int rows = 0; string sql = string.Empty; string json = string.Empty; if (GetPrimaryKey(t).Count() > 0) { primaryKey = GetPrimaryKey(t).FirstOrDefault().Name; } string tableName = TableName(t); IList <string> cols = new List <string>(); dj.Set("RowVersion", DateTime.Now); DbParameter[] paras = BuildParameter(t, dba, dj, ref cols); sql = string.Format(" update {0} set {1} where {2}=:{2}", tableName, cols.Select(a => a + "= :" + a).ToString(","), primaryKey); rows = dba.ExecuteCommand(sql, paras, false); json = dba.GetReader(string.Format("select * from {0} where {1}=:{1}", tableName, primaryKey), dba.CreateDbParameter(primaryKey, dj.Get(primaryKey)), false) .ToList(t).FirstOrDefault().ToJson(); return(json); }