public virtual string Remove() { DynamicJson dj = GetParam(); string resJson = string.Empty; string primaryKey = "Id"; try { JMI jmi = GetJMI(dj); using (DBAccessor dba = DBAccessor.Instance(Dbs.Get(jmi.DbsName))) { string condition = string.Empty; string sql = string.Empty; string tableName = TableName(jmi.DtoType); IList <string> cols = new List <string>(); if (GetPrimaryKey(jmi.DtoType).Count() > 0) { primaryKey = GetPrimaryKey(jmi.DtoType).FirstOrDefault().Name; } DbParameter[] paras = BuildParameter(jmi.DtoType, dba, dj, ref cols); int rows = dba.ExecuteCommand(string.Format("delete from {0} where {1}=:{1}", tableName, primaryKey), dba.CreateDbParameter(primaryKey, dj.Get(primaryKey)), false); if (rows > 0) { resJson = new { result = 1 } } .ToJson(); }
/// <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); }