Exemplo n.º 1
0
        /// <summary>
        /// Addds the default join.
        /// </summary>
        /// <param name="join">The join.</param>
        /// <param name="toTableName">Name of to table.</param>
        /// <param name="toTableAlias">To table alias.</param>
        /// <returns>The created join clause.</returns>
        public JoinClause AdddDefaultJoin(JoinType join, string toTableName, string toTableAlias)
        {
            var newJoin = new JoinClause(join, toTableName, toTableAlias);

            JoinClauses.Add(newJoin);

            return(newJoin);
        }
Exemplo n.º 2
0
        public JoinClause <TEntity> Join <TJoin>(JoinType joinType = JoinType.InnerJoin, string alias = null)
        {
            var joinClause = new JoinClause <TEntity>(_builder);

            joinClause.Init <TJoin>(joinType, alias);
            JoinClauses.Add(joinClause);
            return(joinClause);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds the join.
        /// </summary>
        /// <param name="join">The join.</param>
        /// <param name="toTableName">Name of to table.</param>
        /// <param name="toTableAlias">To table alias.</param>
        /// <param name="toColumnName">Name of to column.</param>
        /// <param name="operator">The operator.</param>
        /// <param name="fromTableAlias">From table alias.</param>
        /// <param name="fromColumnName">Name of from column.</param>
        /// <returns>The created join clause.</returns>
        public JoinClause AddJoin(JoinType join, string toTableName, string toTableAlias, string toColumnName, Comparison @operator, string fromTableAlias, string fromColumnName)
        {
            var newJoin = new JoinClause(join, toTableName, toTableAlias, toColumnName, @operator, fromTableAlias, fromColumnName);

            JoinClauses.Add(newJoin);

            return(newJoin);
        }
Exemplo n.º 4
0
        protected object VisitMember(MemberExpression m, bool columnOnly)
        {
            try
            {
                if (m.Expression != null && m.Expression.NodeType == ExpressionType.Constant && (_overridedNodeType == null))
                {
                    VisitConstantFixed(m.Expression as ConstantExpression, m.Member?.Name);
                    return(m);
                }
                else if (m.Expression != null && (m.Expression.NodeType == ExpressionType.Parameter || (m.ToString().EndsWith(".HasValue") && m.Expression.NodeType == ExpressionType.MemberAccess)) && (_overridedNodeType == null))
                {
                    var hasValueAttr = m.ToString().EndsWith(".HasValue");
                    _overridedNodeType = null;
                    var cl         = hasValueAttr ? (m.Expression as MemberExpression).Expression.Type : m.Expression.Type;
                    var prop       = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == (hasValueAttr ? (m.Expression as MemberExpression).Member.Name : m.Member.Name));
                    var name       = prop.GetPropertyName();
                    var table      = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name;
                    var columnName = string.Format("[{0}].[{1}]", table, name);
                    var dataEncode = prop.GetCustomAttribute <DataEncode>();
                    if (columnOnly)
                    {
                        return(columnName);
                    }

                    bool isNot = sb.ToString().EndsWith("NOT ");
                    if (prop.PropertyType.IsEnum && prop.ContainAttribute <StringFy>())
                    {
                        if (!SavedTypes.ContainsKey(prop.FullName))
                        {
                            SavedTypes.Add(prop.FullName, prop.PropertyType);
                        }
                        columnName += stringyFy.Replace("#", prop.FullName);
                    }
                    if (isNot)
                    {
                        if (!hasValueAttr)
                        {
                            columnName = "(case when " + columnName + " = 0 then 1 else 0 end)" + boolString.Replace("#", "0");
                        }
                        else
                        {
                            columnName = "(case when " + columnName + " IS NULL then 1 else 0 end)" + boolString.Replace("#", "0");
                        }
                    }
                    else if (hasValueAttr)
                    {
                        columnName = "(case when " + columnName + " IS NULL then 0 else 1 end)" + boolString.Replace("#", "1");
                    }
                    else if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(bool?))
                    {
                        columnName = columnName + boolString.Replace("#", "1");
                    }

                    if (dataEncode != null)
                    {
                        columnName = columnName + dataEncodeString.Replace("#", dataEncode.Key + "|" + ((int)dataEncode.KeySize).ToString());
                    }
                    sb.Append(columnName);
                    return(m);
                }
                else if (m.Expression != null && (m.Expression.NodeType == ExpressionType.MemberAccess))
                {
                    _overridedNodeType = null;
                    var key             = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1));
                    var cl              = m.Expression.Type;
                    var prop            = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name);
                    var name            = prop.GetPropertyName();
                    var table           = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name;
                    var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey();
                    var primaryId       = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName();
                    var columnName      = string.Format("[{0}].[{1}]", randomTableName, name);
                    if (columnOnly)
                    {
                        return(columnName);
                    }
                    sb.Append(columnName);
                    if (JoinClauses.ContainsKey(key))
                    {
                        return(m);
                    }
                    // Ok lets build inner join
                    var parentType  = (m.Expression as MemberExpression).Expression.Type;
                    var parentTable = parentType.GetCustomAttribute <Table>()?.Name ?? parentType.Name;
                    prop = DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl);
                    var v = "";
                    if (prop != null)
                    {
                        v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, primaryId, parentTable, prop.GetPropertyName());
                    }
                    else
                    {
                        prop = DeepCloner.GetFastDeepClonerProperties(cl).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == parentType);
                        if (prop != null)
                        {
                            v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, prop.GetPropertyName(), parentTable, primaryId);
                        }
                    }

                    if (string.IsNullOrEmpty(v))
                    {
                        sb = sb.Remove(sb.Length - columnName.Length, columnName.Length);
                        sb.Append(ValuetoSql(GetValue(m)));
                    }
                    else
                    {
                        JoinClauses.Add(key, new Tuple <string, string>(randomTableName, v));
                    }


                    return(m);
                }
                else if (m.Expression != null && _overridedNodeType == ExpressionType.MemberAccess)
                {
                    _overridedNodeType = null;
                    var key             = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1));
                    var cl              = m.Expression.Type;
                    var prop            = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name);
                    var table           = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name;
                    var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey();
                    var primaryId       = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName();
                    if (JoinClauses.ContainsKey(key))
                    {
                        return(m);
                    }
                    // Ok lets build inner join
                    var parentType  = (m as MemberExpression).Type.GetActualType();
                    var parentTable = parentType.GetCustomAttribute <Table>()?.Name ?? parentType.Name;
                    prop = DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl);
                    var v = "";
                    if (prop != null)
                    {
                        v += string.Format("INNER JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", parentTable, randomTableName, table, primaryId, randomTableName, prop.GetPropertyName());
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format("CLASS STRUCTURE IS NOT SUPPORTED MEMBER{0}", m.Member.Name));
                    }

                    if (!string.IsNullOrEmpty(v))
                    {
                        JoinClauses.Add(key, new Tuple <string, string>(randomTableName, v));
                    }
                    return(m);
                }
            }
            catch
            {
                throw new NotSupportedException(string.Format("Expression '{0}' is not supported", m.ToString()));
            }

            if (m.Type.IsInternalType() && m.Expression.NodeType == ExpressionType.Call)
            {
                sb.Append(ValuetoSql(Expression.Lambda(m).Compile().DynamicInvoke()));
                return(m);
            }
            throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name));
        }
Exemplo n.º 5
0
        protected dynamic VisitMember(MemberExpression m, bool columnOnly)
        {
            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Constant && (_overridedNodeType == null))
            {
                VisitConstantFixed(m.Expression as ConstantExpression, m.Member?.Name);
                return(m);
            }
            else if (m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter && (_overridedNodeType == null))
            {
                _overridedNodeType = null;
                var cl         = m.Expression.Type;
                var prop       = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name);
                var name       = prop.GetPropertyName();
                var table      = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name;
                var columnName = string.Format("[{0}].[{1}]", table, name);
                if (columnOnly)
                {
                    return(columnName);
                }
                sb.Append(columnName);
                return(m);
            }
            else if (m.Expression != null && (m.Expression.NodeType == ExpressionType.MemberAccess))
            {
                _overridedNodeType = null;
                var key             = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1));
                var cl              = m.Expression.Type;
                var prop            = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name);
                var name            = prop.GetPropertyName();
                var table           = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name;
                var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey();
                var primaryId       = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName();
                var columnName      = string.Format("[{0}].[{1}]", randomTableName, name);
                if (columnOnly)
                {
                    return(columnName);
                }
                sb.Append(columnName);
                if (JoinClauses.ContainsKey(key))
                {
                    return(m);
                }
                // Ok lets build inner join
                var parentType  = (m.Expression as MemberExpression).Expression.Type;
                var parentTable = parentType.GetCustomAttribute <Table>()?.Name ?? parentType.Name;
                prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl);
                var v = "";
                if (prop != null)
                {
                    v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, primaryId, parentTable, prop.GetPropertyName());
                }
                else
                {
                    prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == parentType);
                    if (prop != null)
                    {
                        v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, prop.GetPropertyName(), parentTable, primaryId);
                    }
                }

                JoinClauses.Add(key, new Tuple <string, string>(randomTableName, v));
                return(m);
            }
            else if (m.Expression != null && _overridedNodeType == ExpressionType.MemberAccess)
            {
                _overridedNodeType = null;
                var key             = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1));
                var cl              = m.Expression.Type;
                var prop            = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name);
                var table           = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name;
                var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey();
                var primaryId       = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName();
                if (JoinClauses.ContainsKey(key))
                {
                    return(m);
                }
                // Ok lets build inner join
                var parentType  = (m as MemberExpression).Type.GetActualType();
                var parentTable = parentType.GetCustomAttribute <Table>()?.Name ?? parentType.Name;
                prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl);
                var v = "";
                if (prop != null)
                {
                    v += string.Format("INNER JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", parentTable, randomTableName, table, primaryId, randomTableName, prop.GetPropertyName());
                }
                else
                {
                    throw new NotSupportedException(string.Format("CLASS STRUCTURE IS NOT SUPPORTED MEMBER{0}", m.Member.Name));
                }

                if (!string.IsNullOrEmpty(v))
                {
                    JoinClauses.Add(key, new Tuple <string, string>(randomTableName, v));
                }
                return(m);
            }

            throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a table or view to the FROM list of the statement.
        /// </summary>
        public FromBuilder Join(JoinType joinType, String objectName, String onClause = null, params Object[] arguments)
        {
            JoinClauses.Add(new JoinClause(joinType, objectName, onClause != null ? String.Format(onClause, arguments) : null));

            return(this);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Adds the join.
 /// </summary>
 /// <param name="newJoin">The new join.</param>
 public void AddJoin(JoinClause newJoin)
 {
     JoinClauses.Add(newJoin);
 }
Exemplo n.º 8
0
 public Join On(JoinClause joinClause)
 {
     JoinClauses.Add(joinClause);
     return(this);
 }
        //public IAcceptsJoin Join(TableOrSubQuery source, string constraint)
        //{
        //    this.Accept(this.Source.Join(source, constraint));
        //    return this;
        //}

        //public IAcceptsJoin Join(string table, string constraint, string alias)
        //{
        //    this.Accept(this.Source.Join(table, constraint, alias));
        //    return this;
        //}

        //public IAcceptsGroupBy Where(string expression)
        //{
        //    this.Accept(new WhereClause(expression));
        //    return this;
        //}

        //public IAcceptsOrderBy GroupBy(params string[] termArgs)
        //{
        //    return GroupBy((IEnumerable<string>)termArgs);
        //}

        //public IAcceptsOrderBy GroupBy(IEnumerable<string> terms)
        //{
        //    this.Accept(new GroupByClause(terms));
        //    return this;
        //}

        //public IAcceptsLimit OrderBy(IEnumerable<string> terms)
        //{
        //    this.Accept(new OrderByClause(terms));
        //    return this;
        //}

        //public IAcceptsLimit OrderBy(params string[] termArgs)
        //{
        //    this.Accept(new OrderByClause(termArgs));
        //    return this;
        //}

        //public ISelectElement Limit(int limit, int offset)
        //{
        //    this.Accept(new LimitClause(limit, offset));
        //    return this;
        //}

        //public void Accept(ISelectElement parent)
        //{
        //    throw new NotSupportedException("select can't have parent");
        //}

        public void Accept(JoinClause joinClause)
        {
            JoinClauses.Add(joinClause);
        }