コード例 #1
0
        private static void ReadOracle(Object item, DbDataReader dr, int id, PropertyModel info)
        {
            object value    = null;
            var    typeName = dr.GetDataTypeName(id);

            if (string.Compare(typeName, "clob", true) == 0 || string.Compare(typeName, "nclob", true) == 0)
            {
                var temp = BaseEmit.Invoke(dr, dr.GetType().GetMethod("GetOracleClob"), new object[] { id });
                if (temp != null)
                {
                    value = BaseEmit.Get(temp, "Value");
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Close"), null);
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Dispose"), null);
                }
            }
            else if (string.Compare(typeName, "blob", true) == 0)
            {
                var temp = BaseEmit.Invoke(dr, dr.GetType().GetMethod("GetOracleBlob"), new object[] { id });
                if (temp != null)
                {
                    value = BaseEmit.Get(temp, "Value");
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Close"), null);
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Dispose"), null);
                }
            }
            else
            {
                value = dr.GetValue(id);
            }

            if (!dr.IsDBNull(id))
            {
                BaseEmit.Set(item, info.Name, value);
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取datatabel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <returns></returns>
        public static DataTable GetTable <T>(DbCommand cmd, List <T> list)
        {
            var dt = new DataTable();

            cmd.CommandText = string.Format("select top 1 * from {0}", typeof(T).Name);
            dt.Load(cmd.ExecuteReader());
            dt.Clear();
            list.ForEach(p => {
                var row = dt.NewRow();
                PropertyCache.GetPropertyInfo <T>().ForEach(a => { row[a.Name] = BaseEmit.Get <T>(p, a.Name); });
                dt.Rows.Add(row);
            });
            return(dt);
        }
コード例 #3
0
        /// <summary>
        /// model 转 insert sql
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="model">实体</param>
        /// <param name="sql">sql</param>
        /// <param name="oracleParam">参数</param>
        /// <returns></returns>
        public static OptionModel InsertToSql <T>(T model, ConfigModel config)
        {
            var sbName  = new StringBuilder();
            var sbValue = new StringBuilder();
            var list    = new List <MemberInfo>();
            var result  = new OptionModel();

            try
            {
                sbName.AppendFormat("insert into {0} (", typeof(T).Name);
                sbValue.Append(" values (");

                PropertyCache.GetPropertyInfo <T>(config.IsPropertyCache).ForEach(p =>
                {
                    if (!list.Exists(a => a.Name == p.Name))
                    {
                        sbName.AppendFormat("{0},", p.Name);

                        sbValue.AppendFormat("{1}{0},", p.Name, config.Flag);

                        var itemValue      = BaseEmit.Get <T>(model, p.Name);
                        var temp           = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = p.Name;
                        temp.Value         = itemValue == null ? DBNull.Value : itemValue;
                        result.Param.Add(temp);
                    }
                });

                result.Sql = string.Format("{0}) {1})", sbName.ToString().Substring(0, sbName.ToString().Length - 1)
                                           , sbValue.ToString().Substring(0, sbValue.ToString().Length - 1));
                result.IsSuccess = true;
                return(result);
            }
            catch (Exception ex)
            {
                if (string.Compare(config.SqlErrorType, SqlErrorType.Db, true) == 0)
                {
                    DbLogTable.LogException(config, ex, "InsertToSql<T>", result.Sql);
                }
                else
                {
                    DbLog.LogException(config.IsOutError, config.DbType, ex, "InsertToSql<T>", result.Sql);
                }

                result.IsSuccess = false;
                return(result);
            }
        }
コード例 #4
0
        /// <summary>
        /// mysql
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string GetMySql <T>(List <T> list)
        {
            var sql = new StringBuilder();

            sql.AppendFormat("insert into {0}(", typeof(T).Name);
            PropertyCache.GetPropertyInfo <T>().ForEach(a => { sql.AppendFormat("{0},", a.Name); });
            sql.Append(")").Replace(",)", ")");
            list.ForEach(a => {
                sql.Append("(");
                PropertyCache.GetPropertyInfo <T>().ForEach(p => {
                    sql.AppendFormat("'{0}',", BaseEmit.Get <T>(a, p.Name));
                });
                sql.Append("),").Replace(",)", ")");
            });

            return(sql.ToStr().Substring(0, sql.ToStr().Length - 1));
        }
コード例 #5
0
        /// <summary>
        /// 获取datatabel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <returns></returns>
        public static DataTable GetTable(DbCommand cmd, List <object> list, System.Type type)
        {
            var dt = new DataTable();

            cmd.CommandText = string.Format("select top 1 * from {0}", type.Name);
            dt.Load(cmd.ExecuteReader());
            dt.Clear();

            list.ForEach(a =>
            {
                var row = dt.NewRow();
                PropertyCache.GetPropertyInfo(list[0]).ForEach(p =>
                {
                    row[p.Name] = BaseEmit.Get(a, p.Name);
                });
                dt.Rows.Add(row);
            });

            return(dt);
        }
コード例 #6
0
        public object Invoke(object proxy, MethodInfo method, object[] args)
        {
            var pModel = new PageModel();
            var param  = new List <DbParameter>();
            var config = DataConfig.GetConfig();
            var key    = string.Format("{0}.{1}", method.DeclaringType.FullName, method.Name);

            if (DbCache.Exists(config.CacheType, key))
            {
                var model = DbCache.Get <ServiceModel>(config.CacheType, key);

                if (model.isPage)
                {
                    pModel = (PageModel)args.ToList().Find(a => a.GetType() == typeof(PageModel));
                }

                config = DataConfig.GetConfig(model.dbKey);
                if (model.isSysType)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (args[i].GetType() == typeof(PageModel))
                        {
                            continue;
                        }

                        var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = model.param.GetValue(i.ToString()).ToStr();
                        temp.Value         = args[i];
                        param.Add(temp);
                    }
                }
                else if (model.isDic)
                {
                    if (!args.ToList().Exists(a => a.GetType() == typeof(Dictionary <string, object>)))
                    {
                        throw new Exception($"error: service {method.DeclaringType.Name} , method {method.Name} , param type {args[0].GetType().Name} is not support");
                    }

                    var dic     = (Dictionary <string, object>)args.ToList().Find(a => a.GetType() == typeof(Dictionary <string, object>));
                    var tempDic = new Dictionary <int, string>();

                    foreach (KeyValuePair <string, object> keyValue in dic)
                    {
                        key = string.Format("{0}{1}", config.Flag, keyValue.Key).ToLower();
                        if (model?.sql.IndexOf(key) > 0 || model.isXml)
                        {
                            tempDic.Add((int)(model?.sql.IndexOf(key)), keyValue.Key);
                        }
                    }
                    var list = tempDic.OrderBy(d => d.Key).ToList();
                    foreach (KeyValuePair <int, string> keyValue in list)
                    {
                        var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = keyValue.Value;
                        temp.Value         = dic.GetValue(keyValue.Value);
                        param.Add(temp);
                    }
                }
                else
                {
                    var data = args.ToList().Find(a => a.GetType() != typeof(PageModel));
                    var type = method.GetParameters().ToList().Find(a => a.ParameterType != typeof(PageModel)).ParameterType;
                    for (int i = 0; i < model.param.Count; i++)
                    {
                        var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = model.param.GetValue(i.ToString()).ToStr();
                        temp.Value         = BaseEmit.Get(data, temp.ParameterName);
                        temp.ParameterName = temp.ParameterName.ToLower();
                        param.Add(temp);
                    }
                }

                if (model.isXml)
                {
                    model.sql = MapXml.GetFastMapSql(method, config, ref param);
                }

                using (var db = new DataContext(config.Key))
                {
                    if (model.isWrite)
                    {
                        return(db.ExecuteSql(model.sql, param.ToArray(), Aop.AopType.FaseWrite).writeReturn);
                    }
                    else
                    {
                        return(db.FastReadAttribute(model, param, pModel));
                    }
                }
            }

            throw new Exception($"error: service {method.DeclaringType.Name} , method {method.Name} not exists");
        }
コード例 #7
0
        /// <summary>
        /// model 转 delete sql
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="model"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static OptionModel DeleteToSql(DbCommand cmd, object model, ConfigModel config)
        {
            var result = new OptionModel();

            result.IsCache = config.IsPropertyCache;
            var where      = PrimaryKey(config, cmd, model.GetType());

            if (where.Count == 0)
            {
                result.Message   = string.Format("{0}没有主键", model.GetType().Name);
                result.IsSuccess = false;
                return(result);
            }

            try
            {
                result.Sql = string.Format("delete {0} ", model.GetType().Name);

                var count = 1;
                foreach (var item in where)
                {
                    var itemValue = BaseEmit.Get(model, item);

                    if (itemValue == null)
                    {
                        result.IsSuccess = false;
                        result.Message   = string.Format("主键{0}值为空", item);
                        return(result);
                    }

                    if (count == 1)
                    {
                        result.Sql = string.Format("{2} where {0}={1}{0} ", item, config.Flag, result.Sql);
                    }
                    else
                    {
                        result.Sql = string.Format("{2} and {0}={1}{0} ", item, config.Flag, result.Sql);
                    }

                    var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                    temp.ParameterName = item;
                    temp.Value         = itemValue == null ? DBNull.Value : itemValue;

                    result.Param.Add(temp);

                    count++;
                }

                result.IsSuccess = true;

                return(result);
            }
            catch (Exception ex)
            {
                if (config.SqlErrorType == SqlErrorType.Db)
                {
                    DbLogTable.LogException(config, ex, "UpdateToSql", result.Sql);
                }
                else
                {
                    DbLog.LogException(config.IsOutError, config.DbType, ex, "DeleteToSql", result.Sql);
                }
                result.IsSuccess = false;
                return(result);
            }
        }
コード例 #8
0
        /// <summary>
        /// model 转 update list sql
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="model">实体</param>
        /// <param name="sql">sql</param>
        /// <param name="oracleParam">参数</param>
        /// <returns></returns>
        public static OptionModel UpdateListToSql <T>(DbCommand cmd, List <T> list, ConfigModel config, Expression <Func <T, object> > field = null)
        {
            var result = new OptionModel();

            result.IsCache = config.IsPropertyCache;
            var where      = PrimaryKey(config, cmd, typeof(T));

            if (where.Count == 0)
            {
                result.Message   = string.Format("{0}没有主键", typeof(T).Name);
                result.IsSuccess = false;
                return(result);
            }

            try
            {
                result.table = BaseExecute.ToDataTable <T>(cmd, config, where, field);

                result.Sql = string.Format("update {0} set", typeof(T).Name);
                var pInfo = PropertyCache.GetPropertyInfo <T>(config.IsPropertyCache);

                if (field == null)
                {
                    #region 属性
                    foreach (var item in pInfo)
                    {
                        if (where.Exists(a => a == item.Name))
                        {
                            continue;
                        }
                        result.Sql = string.Format("{2} {0}={1}{0},", item.Name, config.Flag, result.Sql);
                        var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = item.Name;
                        temp.SourceColumn  = item.Name;
                        result.Param.Add(temp);
                    }
                    #endregion
                }
                else
                {
                    #region lambda
                    foreach (var item in (field.Body as NewExpression).Members)
                    {
                        if (where.Exists(a => a == item.Name))
                        {
                            continue;
                        }
                        result.Sql = string.Format("{2} {0}={1}{0},", item.Name, config.Flag, result.Sql);
                        var temp = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = item.Name;
                        temp.SourceColumn  = item.Name;
                        result.Param.Add(temp);
                    }
                    #endregion
                }

                result.Sql = result.Sql.Substring(0, result.Sql.Length - 1);

                var count = 1;
                where.ForEach(a =>
                {
                    if (count == 1)
                    {
                        result.Sql = string.Format("{2} where {0}={1}{0} ", a, config.Flag, result.Sql);
                    }
                    else
                    {
                        result.Sql = string.Format("{2} and {0}={1}{0} ", a, config.Flag, result.Sql);
                    }

                    var temp           = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                    temp.ParameterName = a;
                    temp.SourceColumn  = a;
                    result.Param.Add(temp);
                    count++;
                });

                result.IsSuccess = true;

                list.ForEach(p =>
                {
                    var row = result.table.NewRow();
                    where.ForEach(a => { row[a] = BaseEmit.Get <T>(p, a); });
                    if (field == null)
                    {
                        PropertyCache.GetPropertyInfo <T>().ForEach(a => { row[a.Name] = BaseEmit.Get <T>(p, a.Name); });
                    }
                    else
                    {
                        (field.Body as NewExpression).Members.ToList().ForEach(a => { row[a.Name] = BaseEmit.Get <T>(p, a.Name); });
                    }
                    result.table.Rows.Add(row);
                });

                return(result);
            }
            catch (Exception ex)
            {
                if (config.SqlErrorType == SqlErrorType.Db)
                {
                    DbLogTable.LogException <T>(config, ex, "UpdateListToSql<T>", result.Sql);
                }
                else
                {
                    DbLog.LogException <T>(config.IsOutError, config.DbType, ex, "UpdateListToSql<T>", result.Sql);
                }

                result.IsSuccess = false;
                return(result);
            }
        }
コード例 #9
0
        /// <summary>
        /// model 转 update sql
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="model">实体</param>
        /// <param name="sql">sql</param>
        /// <param name="oracleParam">参数</param>
        /// <returns></returns>
        public static OptionModel UpdateToSql <T>(T model, ConfigModel config, Expression <Func <T, object> > field = null, DbCommand cmd = null)
        {
            var result = new OptionModel();

            result.IsCache = config.IsPropertyCache;
            var where      = PrimaryKey(config, cmd, typeof(T));

            try
            {
                result.Sql = string.Format("update {0} set", typeof(T).Name);
                if (field == null)
                {
                    #region 属性
                    PropertyCache.GetPropertyInfo <T>(config.IsPropertyCache).ForEach(a =>
                    {
                        result.Sql         = string.Format("{2} {0}={1}{0},", a.Name, config.Flag, result.Sql);
                        var itemValue      = BaseEmit.Get <T>(model, a.Name);
                        var temp           = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = a.Name;
                        temp.Value         = itemValue == null ? DBNull.Value : itemValue;
                        result.Param.Add(temp);
                    });
                    #endregion
                }
                else
                {
                    #region lambda
                    (field.Body as NewExpression).Members.ToList().ForEach(a =>
                    {
                        result.Sql         = string.Format("{2} {0}={1}{0},", a.Name, config.Flag, result.Sql);
                        var itemValue      = BaseEmit.Get <T>(model, a.Name);
                        var temp           = DbProviderFactories.GetFactory(config.ProviderName).CreateParameter();
                        temp.ParameterName = a.Name;
                        temp.Value         = itemValue == null ? DBNull.Value : itemValue;
                        result.Param.Add(temp);
                    });
                    #endregion
                }

                foreach (var item in where)
                {
                    if (result.Param.Exists(a => a.ParameterName == item))
                    {
                        var itemValue = BaseEmit.Get <T>(model, item);
                        if (itemValue == null)
                        {
                            result.IsSuccess = false;
                            result.Message   = string.Format("主键{0}值为空", item);
                            return(result);
                        }
                    }
                }

                result.Sql       = result.Sql.Substring(0, result.Sql.Length - 1);
                result.IsSuccess = true;

                return(result);
            }
            catch (Exception ex)
            {
                if (string.Compare(config.SqlErrorType, SqlErrorType.Db, true) == 0)
                {
                    DbLogTable.LogException(config, ex, "UpdateToSql<T>", result.Sql);
                }
                else
                {
                    DbLog.LogException(config.IsOutError, config.DbType, ex, "UpdateToSql<T>", result.Sql);
                }

                result.IsSuccess = false;
                return(result);
            }
        }
コード例 #10
0
        /// <summary>
        /// mysql
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string GetMySql(List <object> list)
        {
            var sql = new StringBuilder();

            sql.AppendFormat("insert into {0}(", list[0].GetType().Name);
            //var dyn = new Property.DynamicGet(list[0]);

            PropertyCache.GetPropertyInfo(list[0]).ForEach(a => { sql.AppendFormat("{0},", a.Name); });

            sql.Append(")").Replace(",)", ")");

            list.ForEach(a =>
            {
                sql.Append("(");
                PropertyCache.GetPropertyInfo(list[0]).ForEach(b => { sql.AppendFormat("'{0}',", BaseEmit.Get(a, b.Name)); }); // dyn.GetValue(a, b.Name)
                sql.Append("),").Replace(",)", ")");
            });

            return(sql.ToStr().Substring(0, sql.ToStr().Length - 1));
        }
コード例 #11
0
        /// <summary>
        /// model 转 update sql
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="model">实体</param>
        /// <param name="sql">sql</param>
        /// <param name="oracleParam">参数</param>
        /// <returns></returns>
        public static OptionModel UpdateToSql <T>(DbCommand cmd, T model, ConfigModel config, Expression <Func <T, object> > field = null)
        {
            var result = new OptionModel();

            result.IsCache = config.IsPropertyCache;
            var where      = PrimaryKey(config, cmd, typeof(T));

            if (where.Count == 0)
            {
                result.Message   = string.Format("{0}没有主键", typeof(T).Name);
                result.IsSuccess = false;
                return(result);
            }

            try
            {
                result.Sql = string.Format("update {0} set", typeof(T).Name);
                var pInfo = PropertyCache.GetPropertyInfo <T>(config.IsPropertyCache);

                if (field == null)
                {
                    #region 属性
                    foreach (var item in pInfo)
                    {
                        if (where.Exists(a => a == item.Name))
                        {
                            continue;
                        }

                        result.Sql = string.Format("{2} {0}={1}{0},", item.Name, config.Flag, result.Sql);

                        var itemValue = BaseEmit.Get <T>(model, item.Name);
                        var temp      = DbProviderFactories.GetFactory(config).CreateParameter();
                        temp.ParameterName = item.Name;
                        temp.Value         = itemValue == null ? DBNull.Value : itemValue;
                        result.Param.Add(temp);
                    }
                    #endregion
                }
                else
                {
                    #region lambda
                    var list = (field.Body as NewExpression).Members;
                    foreach (var item in list)
                    {
                        if (where.Exists(a => a == item.Name))
                        {
                            continue;
                        }

                        result.Sql = string.Format("{2} {0}={1}{0},", item.Name, config.Flag, result.Sql);

                        var itemValue = BaseEmit.Get <T>(model, item.Name);
                        var temp      = DbProviderFactories.GetFactory(config).CreateParameter();
                        temp.ParameterName = item.Name;
                        temp.Value         = itemValue == null ? DBNull.Value : itemValue;
                        result.Param.Add(temp);
                    }
                    #endregion
                }

                result.Sql = result.Sql.Substring(0, result.Sql.Length - 1);

                var count = 1;
                foreach (var item in where)
                {
                    var itemValue = BaseEmit.Get <T>(model, item);

                    if (itemValue == null)
                    {
                        result.IsSuccess = false;
                        result.Message   = string.Format("主键{0}值为空", item);
                        return(result);
                    }

                    if (count == 1)
                    {
                        result.Sql = string.Format("{2} where {0}={1}{0} ", item, config.Flag, result.Sql);
                    }
                    else
                    {
                        result.Sql = string.Format("{2} and {0}={1}{0} ", item, config.Flag, result.Sql);
                    }

                    var temp = DbProviderFactories.GetFactory(config).CreateParameter();
                    temp.ParameterName = item;
                    temp.Value         = itemValue == null ? DBNull.Value : itemValue;

                    result.Param.Add(temp);

                    count++;
                }

                result.IsSuccess = true;

                return(result);
            }
            catch (Exception ex)
            {
                if (config.SqlErrorType == SqlErrorType.Db)
                {
                    DbLogTable.LogException <T>(config, ex, "UpdateToSql<T>", result.Sql);
                }
                else
                {
                    DbLog.LogException <T>(config.IsOutError, config.DbType, ex, "UpdateToSql<T>", result.Sql);
                }
                result.IsSuccess = false;
                return(result);
            }
        }