Esempio n. 1
0
        private SchemaModel.Table GetTable()
        {
            MemberExpression expression;

Label_0000:
            expression   = (MemberExpression)_memberInfos.Peek();
            _tableMember = expression.Member;
            Type fieldType = null;

            if (_tableMember.MemberType == MemberTypes.Field)
            {
                fieldType = ((FieldInfo)_tableMember).FieldType;
            }
            else
            {
                fieldType = ((PropertyInfo)_tableMember).PropertyType;
            }
            if (TypeHelper.IsCompilerGenerated(fieldType))
            {
                _memberInfos.Pop();
                goto Label_0000;
            }
            if (EntityConfigurationManager.IsEntity(fieldType))
            {
                _memberInfos.Pop();
                return(GetTable(fieldType));
            }
            return(null);
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            var type       = node.Type;
            var expression = _memberExpressions.LastOrDefault();

            if (!TypeHelper.IsValueType(expression.Type))
            {
                while (!EntityConfigurationManager.IsEntity(type))
                {
                    type = _memberExpressions.Pop().Type;
                }
                ParseEntityType(type);
                return(node);
            }
            var visitor = new MemberExpressionVisitor(Context);

            visitor.Visit(expression);
            Columns.Add(visitor.SelectedColumn);
            return(node);
        }
Esempio n. 3
0
        public TResult Execute <TResult>(Expression expression)
        {
            object obj2;
            var    base2  = ProviderFactory.CreateProvider(dc.dbconn(string.Empty).Reader.DatabaseType);
            var    result = ParseExpression(expression);
            var    type   = typeof(TResult);

            //SqlExecutorBase base3 = base2.CreateSqlExecutor();
            if ((expression.NodeType != ExpressionType.Call) || !type.IsValueType)
            {
                var flag = false;
                if (type.IsGenericType)
                {
                    var reader = ModelOpretion.SearchDataRetunDataTable(result.CommandText,
                                                                        result.SqlQueryParameters, null); //base3.ExecuteReader(result.CommandText, result.Parameters);
                    if (type.GetGenericTypeDefinition() == ReflectorConsts.IEnumerableType)
                    {
                        var   objectType = type.GetGenericArguments()[0];
                        IList list       = null;
                        try
                        {
                            list = EntityMapper.Map(objectType, reader, base2.ObjectPropertyConvertType);
                        }
                        catch
                        {
                            throw new Exception("转化成TResult失败");
                        }


                        return((TResult)list);
                    }
                    if (typeof(Nullable <>).IsAssignableFrom(type))
                    {
                        flag = true;
                    }
                    if (!flag)
                    {
                        throw new Exception();
                    }
                }
                if (type.IsValueType | flag)
                {
                    var obj = ModelOpretion.ScalarBache(result.CommandText, result.SqlQueryParameters, null);
                    if (obj == null)
                    {
                        return(default(TResult));
                    }


                    if (obj == DBNull.Value)
                    {
                        return(default(TResult));
                    }
                    return((TResult)Convert.ChangeType(obj, type));
                }
                if (!EntityConfigurationManager.IsEntity(type))
                {
                    throw new Exception();
                }
                var dataReader = ModelOpretion.SearchDataRetunDataTable(result.CommandText,
                                                                        result.SqlQueryParameters, null);
                IList list2 = null;
                try
                {
                    list2 = EntityMapper.Map(type, dataReader, base2.ObjectPropertyConvertType);
                }
                catch (Exception ex)
                {
                    throw new Exception("TResult:转化成实体失败.");
                }
                finally
                {
                }
                if (list2.Count <= 0)
                {
                    return(default(TResult));
                }
                var entity = list2[0];

                return((TResult)entity);
            }
            //var name = ((MethodCallExpression) expression).Method.Name;
            //if (name != "Any")
            //{
            //    if ((name != "Delete") && (name != "Update"))
            //    {
            //        if ((name != "Average") && (name != "Sum") && (name != "Count"))
            //        {
            //            throw new Exception();
            //        }
            //        obj2 = base3.ExecuteScalar(result.CommandText, result.Parameters);
            //        if (obj2 == DBNull.Value)
            //        {
            //            return default(TResult);
            //        }
            //        var converter1 = new BaseTypeConverter(obj2, type);
            //        converter1.Process();
            //        return (TResult) converter1.Result;
            //    }
            //}
            //else
            //{
            //    DataSet set = base3.ExecuteDataSet(result.CommandText, result.Parameters);
            //    obj2 = set.Tables.Count <= 0 ? 0 : (object) (set.Tables[0].Rows.Count > 0);
            //    return (TResult) obj2;
            //}

            obj2 = ModelOpretion.ScalarBache(result.CommandText,
                                             result.SqlQueryParameters, null);
            return((TResult)obj2);
        }
Esempio n. 4
0
        protected override Expression VisitParameter(ParameterExpression node)
        {
            var column = new Column();

            if (!EntityConfigurationManager.IsEntity(node.Type))
            {
                var        table2 = GetTable();
                MemberInfo member = null;
                var        key    = string.Empty;
                Table      table3 = null;
                if (table2 != null)
                {
                    key    = _tableMember.Name;
                    member = ((MemberExpression)_memberInfos.Pop()).Member;
                    if (_joins != null)
                    {
                        if (_joins.ContainsKey(key))
                        {
                            key = _joins[key].Right.Table.Alias;
                        }
                    }
                    else
                    {
                        key = table2.Name;
                    }
                    table3 = CreateTable(key, table2.DataBase, table2.Name, table2.Type);
                }
                else
                {
                    member = ((MemberExpression)_memberInfos.Pop()).Member;
                    table3 = _columns[member.Name].Table;
                    key    = table3.Alias;
                    table2 = GetTable(table3.Type);
                }
                var propertyType = ((PropertyInfo)member).PropertyType;
                var name         = string.Empty;
                var column2      = table2.Columns[member.Name];
                if (column2 != null)
                {
                    name = column2.Name;
                }
                else
                {
                    name = Context.Columns[member.Name].Name;
                }
                column = new Column
                {
                    DataType   = propertyType,
                    Name       = name,
                    Table      = table3,
                    MemberInfo = member
                };
                while (_memberInfos.Count > 0)
                {
                    var expression2 = _memberInfos.Pop();
                    if (expression2.NodeType != ExpressionType.MemberAccess)
                    {
                        throw new Exception();
                    }
                    var memberInfo = ((MemberExpression)expression2).Member;
                    column.Converters.Push(new ColumnConverter(memberInfo, new List <object>()));
                }
            }
            else
            {
                var table = GetTable(node.Type);
                var info  = ((MemberExpression)_memberInfos.Pop()).Member;
                column.DataType   = ((PropertyInfo)info).PropertyType;
                column.Name       = table.Columns[info.Name].Name;
                column.MemberInfo = info;
                var alias = node.Name;
                if (_joins != null)
                {
                    if (_joins.ContainsKey(alias))
                    {
                        alias = _joins[alias].Right.Table.Alias;
                    }
                }
                else
                {
                    alias = table.Name;
                }
                column.Table = CreateTable(alias, table.DataBase, table.Name, table.Type);
                while (_memberInfos.Count > 0)
                {
                    var expression = _memberInfos.Pop();
                    if (expression.NodeType != ExpressionType.MemberAccess)
                    {
                        throw new Exception();
                    }
                    var info2 = ((MemberExpression)expression).Member;
                    column.Converters.Push(new ColumnConverter(info2, new List <object>()));
                }
            }
            Token = Token.Create(column);
            return(node);
        }
            protected override Expression VisitParameter(ParameterExpression node)
            {
                if (EntityConfigurationManager.IsEntity(node.Type))
                {
                    var table = GetTable(node.Type);
                    var info  = _memberInfoStack.Pop();
                    if (table.Columns[info.Name] == null)
                    {
                        SelectedColumn = null;
                        return(node);
                    }
                    SelectedColumn.DataType   = ((PropertyInfo)info).PropertyType;
                    SelectedColumn.Name       = table.Columns[info.Name].Name;
                    SelectedColumn.MemberInfo = info;
                    var key = node.Name;
                    if (_Joins != null)
                    {
                        if (_Joins.ContainsKey(key))
                        {
                            key = _Joins[key].Right.Table.Alias;
                        }
                    }
                    else
                    {
                        key = table.Name;
                    }
                    SelectedColumn.Table = CreateTable(key, table.DataBase, table.Name, table.Type);
                    while (_memberInfoStack.Count > 0)
                    {
                        var memberInfo = _memberInfoStack.Pop();
                        SelectedColumn.Converters.Push(new ColumnConverter(memberInfo, new List <object>()));
                    }
                    return(node);
                }
                var table2 = GetTable();
                var info3  = _memberInfoStack.Pop();

                if (table2.Columns[info3.Name] == null)
                {
                    SelectedColumn = null;
                    return(node);
                }
                var propertyType = ((PropertyInfo)info3).PropertyType;
                var name         = _tableMember.Name;

                if (_Joins != null)
                {
                    if (_Joins.ContainsKey(name))
                    {
                        name = _Joins[name].Right.Table.Alias;
                    }
                }
                else
                {
                    name = table2.Name;
                }
                var table3  = CreateTable(name, table2.DataBase, table2.Name, table2.Type);
                var column1 = new Column
                {
                    DataType   = propertyType,
                    Name       = table2.Columns[info3.Name].Name,
                    Table      = table3,
                    MemberInfo = info3
                };

                SelectedColumn = column1;
                while (_memberInfoStack.Count > 0)
                {
                    var info4 = _memberInfoStack.Pop();
                    SelectedColumn.Converters.Push(new ColumnConverter(info4, new List <object>()));
                }
                return(node);
            }