Exemplo n.º 1
0
        protected override ICommandResult ExecuteSelectCommand(SqlModelCommand <T> dataCommand, IDbCommand command)
        {
            if (TableMapFromDatabase)
            {
                var map = dataCommand.GetTableMap();
                if (dataCommand.SelectAll == false)
                {
                    if (!string.IsNullOrEmpty(map))
                    {
                        var columns = dataCommand.GetColumnAttributes();
                        foreach (var columnMap in columns)
                        {
                            bool addColumn = true;
                            if (dataCommand.TableMap == null)
                            {
                                addColumn = false;
                            }

                            var column = dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.ColumnName == columnMap.ColumnName);
                            if (column == null)
                            {
                                addColumn = false;
                            }


                            if (addColumn)
                            {
                                dataCommand.Column(columnMap.ColumnName);
                            }
                        }
                        dataCommand.SelectAll = false;
                    }
                }
            }


            dataCommand.BuildSqlCommand();
            dataCommand.BuildSqlParameters(command);
            command.CommandText = dataCommand.SqlCommandText;
            var            items  = new List <T>();
            ICommandResult result = null;

            try
            {
                int rowsIndex = 0;


                OnBeforeCommandExecute(this, new ModelCommandBeforeExecuteEventArgs(dataCommand, command, DataCommandType.Select));
                using (SqlDataReader r = command.ExecuteReader() as SqlDataReader)
                {
                    Type objectType = typeof(T);

                    if (objectType == typeof(DataTable))
                    {
                        result = new DataTableCommandResult();
                        var dt = new DataTable(dataCommand.TableName);
                        dt.Load(r);


                        ((DataTableCommandResult)result).Data = dt;
                        result.RecordsAffected = dt.Rows.Count;

                        foreach (var item in dataCommand.TableMap.ColumnMaps)
                        {
                            if (item.ColumnName != item.PropertyName)
                            {
                                foreach (System.Data.DataColumn column in dt.Columns)
                                {
                                    if (item.ColumnName == column.ColumnName)
                                    {
                                        if (!string.IsNullOrEmpty(item.PropertyName))
                                        {
                                            if (item.IsMapped)
                                            {
                                                column.ColumnName = item.PropertyName;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        return(result);
                    }
                    result = new ModelCommandResult <T>();

                    var tablename = dataCommand.GetTableAttribute();

                    var accessor = TypeAccessor.Create(objectType);

                    ConstructorInfo ctor = objectType.GetConstructor(Type.EmptyTypes);
                    TrinityActivator.ObjectActivator <T> createdActivator = TrinityActivator.GetActivator <T>(ctor);

                    while (r.Read())
                    {
                        bool userDataManager = false;

                        //https://vagifabilov.wordpress.com/2010/04/02/dont-use-activator-createinstance-or-constructorinfo-invoke-use-compiled-lambda-expressions/

                        var newObject = createdActivator();
                        //var newObject = (T)Activator.CreateInstance(objectType);
                        var dataManager = newObject as IObjectDataManager;


                        if (dataCommand.TableMap != null)
                        {
                            if (dataCommand.TableName != objectType.Name)
                            {
                                if (tablename == dataCommand.TableName)
                                {
                                    if (dataManager != null)
                                    {
                                        userDataManager = true;
                                    }
                                    else
                                    {
                                        userDataManager = false;
                                    }
                                }
                            }
                            else
                            {
                                if (dataManager != null)
                                {
                                    userDataManager = true;
                                }
                            }
                        }

                        if (userDataManager)
                        {
                            dataManager.SetData(r);
                        }
                        else
                        {
                            //TODO Change code to use Reflection.Emit
                            int counter = r.FieldCount;
                            for (int i = 0; i < counter; i++)
                            {
                                var name = r.GetName(i);
                                try
                                {
                                    var fieldType = r.GetFieldType(i);
                                    var value     = r.GetValue(i);
                                    if (dataCommand.TableMap != null)
                                    {
                                        var column =
                                            dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.ColumnName == name);

                                        if (column != null)
                                        {
                                            if (!string.IsNullOrEmpty(column.PropertyName))
                                            {
                                                try
                                                {
                                                    if (value != DBNull.Value)
                                                    {
                                                        accessor[newObject, column.PropertyName] = value;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    try
                                                    {
                                                        if (value != DBNull.Value)
                                                        {
                                                            var prop = objectType.GetProperty(column.PropertyName);
                                                            accessor[newObject, column.PropertyName] = value.ConvertValue(prop);
                                                        }
                                                    }
                                                    catch (Exception exception)
                                                    {
                                                        var prop = objectType.GetProperty(column.PropertyName);
                                                        if (prop != null)
                                                        {
                                                            if (value != DBNull.Value)
                                                            {
                                                                try
                                                                {
                                                                    prop.SetValue(newObject, value, null);
                                                                }
                                                                catch (Exception)
                                                                {
                                                                    prop.SetValue(newObject, value.ConvertValue(prop), null);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    if (value != DBNull.Value)
                                                    {
                                                        accessor[newObject, name] = value;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    TrySetValue(dataCommand, newObject, objectType, name, value);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            try
                                            {
                                                if (value != DBNull.Value)
                                                {
                                                    accessor[newObject, name] = value;
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                TrySetValue(dataCommand, newObject, objectType, name, value);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var prop = objectType.GetProperty(name);
                                        if (prop != null)
                                        {
                                            if (value != DBNull.Value)
                                            {
                                                prop.SetValue(newObject, value, null);
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    result.AddError(LogType.Error, string.Format("{1} {0} ", name, dataCommand.TableName), ex);
                                }
                            }
                        }
                        items.Add(newObject);
                        rowsIndex++;
                    }
                }
                result.RecordsAffected = rowsIndex;
            }
            catch (Exception exception)
            {
                result.AddError(LogType.Error, $"{dataCommand.TableName} {dataCommand.SqlCommandText} {typeof(T).FullName}", exception);
            }
            ((ModelCommandResult <T>)result).Data = items;
            result.AddMessage(string.Format("{0} executed with {1} rows affected", dataCommand.SqlCommandText, result.RecordsAffected));
            //TODO change class to use base type


            dataCommand.OnCommandExecuted(new ModelCommandExecutedEventArgs <T> {
                Result = (ModelCommandResult <T>)result
            });

            dataCommand.ResetCommand();
            result.DataCommand = dataCommand;
            return(result);
        }
Exemplo n.º 2
0
        protected override async Task <ICommandResult> ExecuteSelectCommandAsync(SqlModelCommand <T> dataCommand,
                                                                                 IDbCommand command)
        {
            if (TableMapFromDatabase)
            {
                dataCommand.GetTableMap();
            }

            dataCommand.BuildSqlCommand();
            dataCommand.BuildSqlParameters(command);
            command.CommandText = dataCommand.SqlCommandText;
            var            items  = new List <T>();
            ICommandResult result = null;

            try
            {
                int rowsIndex  = 0;
                var sqlCommand = command as SqlCommand;

                OnBeforeCommandExecute(this, new ModelCommandBeforeExecuteEventArgs(dataCommand, command, DataCommandType.Select));

                using (SqlDataReader r = await sqlCommand.ExecuteReaderAsync())
                {
                    Type objectType = typeof(T);

                    if (objectType == typeof(DataTable))
                    {
                        result = new DataTableCommandResult();
                        var dt = new DataTable(dataCommand.TableName);
                        dt.Load(r);
                        ((DataTableCommandResult)result).Data = dt;
                        result.RecordsAffected = dt.Rows.Count;

                        return(result);
                    }
                    result = new ModelCommandResult <T>();

                    var tablename = dataCommand.GetTableAttribute();
                    while (await r.ReadAsync())
                    {
                        bool userDataManager = false;
                        var  newObject       = (T)Activator.CreateInstance(objectType);
                        var  dataManager     = newObject as IObjectDataManager;

                        if (dataManager != null)
                        {
                            userDataManager = true;
                        }

                        if (dataCommand.TableMap != null)
                        {
                            if (dataCommand.TableName != objectType.Name)
                            {
                                if (tablename == dataCommand.TableName)
                                {
                                    if (dataManager != null)
                                    {
                                        userDataManager = true;
                                    }
                                    else
                                    {
                                        userDataManager = false;
                                    }
                                }
                            }
                        }

                        if (userDataManager)
                        {
                            dataManager.SetData(r);
                        }
                        else
                        {
                            //TODO Change code to use Reflection.Emit
                            int counter = r.FieldCount;
                            for (int i = 0; i < counter; i++)
                            {
                                var name = r.GetName(i);
                                try
                                {
                                    var fieldType = r.GetFieldType(i);
                                    var value     = r.GetValue(i);
                                    if (dataCommand.TableMap != null)
                                    {
                                        var column =
                                            dataCommand.TableMap.ColumnMaps.FirstOrDefault(m => m.ColumnName == name);

                                        if (column != null)
                                        {
                                            if (!string.IsNullOrEmpty(column.PropertyName))
                                            {
                                                var prop = objectType.GetProperty(column.PropertyName);
                                                if (prop != null)
                                                {
                                                    if (value != DBNull.Value)
                                                    {
                                                        prop.SetValue(newObject, value, null);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                TrySetValue(dataCommand, newObject, objectType, name, value);
                                            }
                                        }
                                        else
                                        {
                                            TrySetValue(dataCommand, newObject, objectType, name, value);
                                        }
                                    }
                                    else
                                    {
                                        var prop = objectType.GetProperty(name);
                                        if (prop != null)
                                        {
                                            if (value != DBNull.Value)
                                            {
                                                prop.SetValue(newObject, value, null);
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    result.AddError(LogType.Error,
                                                    string.Format("{1} {0} ", name, dataCommand.TableName), ex);
                                }
                            }
                        }
                        items.Add(newObject);
                        rowsIndex++;
                    }
                }
                result.RecordsAffected = rowsIndex;
            }
            catch (Exception exception)
            {
                result.AddError(LogType.Error, dataCommand.TableName + " " + typeof(T).FullName, exception);
            }

            ((ModelCommandResult <T>)result).Data = items;
            result.AddMessage(string.Format("{0} executed with {1} rows affected", dataCommand.SqlCommandText,
                                            result.RecordsAffected));
            //TODO change class to use base type
            dataCommand.OnCommandExecuted(new ModelCommandExecutedEventArgs <T> {
                Result = (ModelCommandResult <T>)result
            });
            dataCommand.ResetCommand();
            result.DataCommand = dataCommand;
            return(result);
        }