Пример #1
0
        public static IDatabaseProvider GetDatabaseProvider(DatabaseConfig config)
        {
            ArgumentAssertion.IsNotNull(config, "config");
            var providerName = config.ProviderName;

            if (providerTypes.ContainsKey(providerName) == false)
            {
                lock (syncRoot)
                {
                    if (providerTypes.ContainsKey(providerName) == false)
                    {
                        var providerType = TypeExtension.GetMapType(providerName);
                        if (null == providerType)
                        {
                            throw new ArgumentOutOfRangeException("providerName", providerName
                                                                  , string.Format("没有定义数据库{0}的驱动:{1}", config.ConfigName, providerName));
                        }
                        else
                        {
                            providerTypes[providerName] = providerType;
                        }
                    }
                }
            }

            var dbPrproviderType = providerTypes[providerName];
            var dbProvider       = (IDatabaseProvider)Activator.CreateInstance(dbPrproviderType);

            dbProvider.ConnectionString = config.ConnectionString;
            return(dbProvider);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <param name="entity"></param>
        public static T Serialize <T>(this DataRow row, T entity)
        {
            ArgumentAssertion.IsNotNull(row, "row");
            ArgumentAssertion.IsNotNull(entity, "entity");

            if (entity is IEntity)
            {
                (entity as IEntity).PersistentState = PersistentState.Persistent;
            }

            var targetType       = entity.GetType();
            var targetProperties = targetType.GetPersistProperties();

            var propertyValues = new Dictionary <string, object>(targetProperties.Count);

            foreach (DataColumn column in row.Table.Columns)
            {
                foreach (var prop in targetProperties)
                {
                    if (prop.Value.IsPrimitiveType() && prop.Key.ToLower() == column.ColumnName.ToLower())
                    {
                        if (row[column.ColumnName] != null && row[column.ColumnName] != DBNull.Value)
                        {
                            propertyValues.Add(prop.Key, row[column.ColumnName]);
                        }
                        break;
                    }
                }
            }

            entity.SetPropertyValues(propertyValues);

            return(entity);
        }
Пример #3
0
        /// <summary>
        /// 通过指定SqlWrap,执行后返回自增值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameterValues"></param>
        /// <param name="partitionValues">分区字段值列表</param>
        /// <returns></returns>
        public static T ExecuteIdentity <T>(SqlWrap sql, IDictionary <string, object> parameterValues, string partitionValues)
        {
            ArgumentAssertion.IsNotNull(sql, "sql");

            var dbProvider = GetDatabaseProvider(sql, partitionValues);

            try
            {
                var identity = dbProvider.ExecuteIdentity <T>(sql, parameterValues);
                return(identity);
            }
            catch (Exception ex)
            {
                throw BuildSqlWrapException(ex, sql, parameterValues);
            }
        }
Пример #4
0
        /// <summary>
        /// 通过指定SqlWrap,执行后返回查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameterValues"></param>
        /// <param name="pagination"></param>
        /// <param name="partitionValues">分区字段值列表</param>
        /// <returns></returns>
        public static DataTable ExecutePaginationTable(SqlWrap sql, IDictionary <string, object> parameterValues, Pagination pagination, string partitionValues)
        {
            ArgumentAssertion.IsNotNull(sql, "sql");

            var dbProvider = GetDatabaseProvider(sql, partitionValues);

            try
            {
                var dataset = dbProvider.ExecutePaginationTable(sql, parameterValues, pagination);
                return(dataset);
            }
            catch (Exception ex)
            {
                throw BuildSqlWrapException(ex, sql, parameterValues);
            }
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameterValues"></param>
        /// <param name="partitionValues"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(SqlWrap sql, IDictionary <string, object> parameterValues, string partitionValues)
        {
            ArgumentAssertion.IsNotNull(sql, "sql");

            var dbProvider = GetDatabaseProvider(sql, partitionValues);

            try
            {
                var rowCount = dbProvider.ExecuteNonQuery(sql, parameterValues);
                return(rowCount);
            }
            catch (Exception ex)
            {
                throw BuildSqlWrapException(ex, sql, parameterValues);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        protected virtual IList <T> Convert(DataTable dataTable)
        {
            ArgumentAssertion.IsNotNull(dataTable, "dataTable");

            IList <T> list      = null;
            var       itemCount = dataTable.Rows.Count;

            if (itemCount > 0)
            {
                list = new List <T>(itemCount);
                for (var i = 0; i < itemCount; i++)
                {
                    var item = this.Convert(dataTable.Rows[i]);
                    list.Add(item);
                }
            }
            return(list);
        }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static IList <T> Convert <T>(this DataTable dataTable) where T : new()
        {
            ArgumentAssertion.IsNotNull(dataTable, "dataTable");

            IList <T> list      = null;
            var       itemCount = dataTable.Rows.Count;

            if (itemCount > 0)
            {
                list = new List <T>(itemCount);
                for (var i = 0; i < itemCount; i++)
                {
                    var item = dataTable.Rows[i].Serialize <T>();
                    list.Add(item);
                }
            }
            return(list);
        }
Пример #8
0
        /// <summary>
        /// 通过指定SQL名称执行SQL语句,获得DbDataReader后,使用委托Action[DbDataReader]处理数据流
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameterValues"></param>
        /// <param name="partitionValues"></param>
        /// <param name="action"></param>
        public static void ExecuteReader(SqlWrap sql, IDictionary <string, object> parameterValues, string partitionValues, Action <DbDataReader> action)
        {
            ArgumentAssertion.IsNotNull(sql, "sql");

            if (null == action)
            {
                throw new ArgumentNullException("action");
            }

            var dbProvider = GetDatabaseProvider(sql, partitionValues);

            try
            {
                using (var dbReader = dbProvider.ExecuteReader(sql, parameterValues))
                {
                    action(dbReader);
                }
            }
            catch (Exception ex)
            {
                throw BuildSqlWrapException(ex, sql, parameterValues);
            }
        }