Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dba"></param>
        /// <param name="dynamicJson"></param>
        /// <param name="columns"></param>
        /// <returns></returns>
        public DbParameter[] BuildParameter(Type t, DBAccessor dba, DynamicJson dynamicJson, ref IList <string> columns)
        {
            IList <DbParameter> paras = new List <DbParameter>();

            PropertyInfo[] propertys = t.GetProperties().Where(a => a.GetAttribute <EdmScalarPropertyAttribute>() != null).ToArray();
            if (propertys.Count() == 0)
            {
                propertys = t.GetProperties().Where(a => a.GetAttribute <ColumnAttribute>() != null).ToArray();
            }

            foreach (var columnName in dynamicJson.Keys)
            {
                try
                {
                    bool isDate = false;
                    var  p      = propertys.FirstOrDefault(b => b.Name.ToUpper() == columnName.ToUpper());
                    if (p != null && p.PropertyType == typeof(DateTime))
                    {
                        isDate = true;
                    }
                    if (columnName.StartsWith("_") || propertys.Count(b => b.Name.ToUpper() == columnName.ToUpper()) == 0)
                    {
                        continue;
                    }
                    if (dynamicJson.ContainsKey(columnName))
                    {
                        object v = dynamicJson.Get(columnName);
                        if (isDate)
                        {
                            v = v.Convert <DateTime>();
                        }
                        else if (v.ToString().ToUpper() == "TRUE" || v.ToString().ToUpper() == "FALSE")
                        {
                            v = v.Convert <bool>() ? 1 : 0;
                        }
                        paras.Add(dba.CreateDbParameter(columnName, v));
                    }
                    columns.Add(columnName);
                }
                catch (Exception ex)
                {
                    Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName() + "\r\n列名->\r\n" + columnName, ex);
                }
            }
            return(paras.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dba"></param>
        /// <param name="dynamicJson">以下划线开头的key不会构建参数</param>
        /// <returns></returns>
        public DbParameter[] BuildParameter(DBAccessor dba, DynamicJson dynamicJson)
        {
            IList <DbParameter> paras = new List <DbParameter>();

            foreach (var columnName in dynamicJson.Keys)
            {
                try
                {
                    bool isDate = false;
                    if (dynamicJson.GetStr(columnName) != null && columnName.Contains("Date"))
                    {
                        isDate = true;
                    }
                    if (columnName.StartsWith("_"))
                    {
                        continue;
                    }

                    if (dynamicJson.ContainsKey(columnName))
                    {
                        var dp = dba.CreateDbParameter(columnName, isDate ? dynamicJson.Get(columnName).Convert <DateTime>() : dynamicJson.Get(columnName));
                        if (columnName == "rs_1" && dp.GetType().FullName.Contains("Oracle"))
                        {
                            (dp as OracleParameter).OracleDbType = OracleDbType.RefCursor;
                            dp.Direction = ParameterDirection.Output;
                        }
                        paras.Add(dp);
                    }
                }
                catch (Exception ex)
                {
                    Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName() + "\r\n列名->\r\n" + columnName, ex);
                }
            }
            return(paras.ToArray());
        }
Exemplo n.º 3
0
        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();
                }
Exemplo n.º 4
0
        /// <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);
        }
Exemplo n.º 5
0
        /// <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);
        }