Exemplo n.º 1
0
        protected virtual void VisitGroupBy(ExpressionVisitorContext context, GroupByClause clause)
        {
            if (clause == null || clause.Keys.Count == 0)
            {
                return;
            }

            if (context.Output.Length > 0)
            {
                context.WriteLine();
            }

            context.Write("GROUP BY ");

            int index = 0;

            foreach (var key in clause.Keys)
            {
                if (index++ > 0)
                {
                    context.Write(",");
                }

                context.Visit(key);
            }

            if (clause.Having != null)
            {
                context.WriteLine();
                context.Write("HAVING ");
                context.Visit(clause.Having);
            }
        }
Exemplo n.º 2
0
        private void VisitReturning(ExpressionVisitorContext context, ReturningClause returning)
        {
            if (returning == null)
            {
                return;
            }

            context.WriteLine();
            context.Write("RETURNING ");

            if (returning.Members == null || returning.Members.Count == 0)
            {
                context.Write("*");
            }
            else
            {
                int index = 0;

                foreach (var member in returning.Members)
                {
                    if (index++ > 0)
                    {
                        context.Write(",");
                    }

                    context.Visit(member.Field);
                }
            }

            if (returning.Table != null)
            {
                context.Write(" INTO ");
                context.Write(context.Dialect.GetIdentifier(returning.Table.Identifier()));
            }
        }
Exemplo n.º 3
0
        protected virtual void VisitOrderBy(ExpressionVisitorContext context, OrderByClause clause)
        {
            if (clause == null || clause.Members.Count == 0)
            {
                return;
            }

            if (context.Output.Length > 0)
            {
                context.WriteLine();
            }

            context.Write("ORDER BY ");

            int index = 0;

            foreach (var member in clause.Members)
            {
                if (index++ > 0)
                {
                    context.Write(",");
                }

                context.Visit(member.Field);

                if (member.Mode == SortingMode.Descending)
                {
                    context.Write(" DESC");
                }
            }
        }
Exemplo n.º 4
0
        protected virtual void VisitFields(ExpressionVisitorContext context, UpdateStatement statement, ICollection <FieldValue> fields)
        {
            var index = 0;

            foreach (var field in fields)
            {
                if (index++ > 0)
                {
                    context.WriteLine(",");
                }

                context.Visit(field.Field);
                context.Write("=");

                var parenthesisRequired = field.Value is IStatementBase;

                if (parenthesisRequired)
                {
                    context.Write("(");
                }

                context.Visit(field.Value);

                if (parenthesisRequired)
                {
                    context.Write(")");
                }
            }
        }
Exemplo n.º 5
0
        protected override void OnVisit(ExpressionVisitorContext context, UpdateStatement statement)
        {
            if (statement.Tables == null || statement.Tables.Count == 0)
            {
                throw new DataException("Missing required tables in the update statement.");
            }

            if (statement.Fields == null || statement.Fields.Count == 0)
            {
                throw new DataException("Missing required fields in the update statment.");
            }

            if (statement.Returning != null && statement.Returning.Table != null)
            {
                context.Visit(statement.Returning.Table);
            }

            context.Write("UPDATE ");
            this.VisitTables(context, statement, statement.Tables);

            context.WriteLine(" SET");
            this.VisitFields(context, statement, statement.Fields);

            this.VisitFrom(context, statement, statement.From);
            this.VisitWhere(context, statement, statement.Where);

            context.WriteLine(";");
        }
        public static void VisitJoin(this ExpressionVisitorContext context, JoinClause joining)
        {
            context.WriteLine();

            switch (joining.Type)
            {
            case JoinType.Inner:
                context.Write("INNER JOIN ");
                break;

            case JoinType.Left:
                context.Write("LEFT JOIN ");
                break;

            case JoinType.Right:
                context.Write("RIGHT JOIN ");
                break;

            case JoinType.Full:
                context.Write("FULL JOIN ");
                break;
            }

            switch (joining.Target)
            {
            case TableIdentifier table:
                context.Visit(table);

                if (string.IsNullOrEmpty(joining.Name))
                {
                    context.WriteLine(" ON");
                }
                else
                {
                    context.WriteLine(" ON /* " + joining.Name + " */");
                }

                break;

            case SelectStatement subquery:
                context.Write("(");

                //递归生成子查询语句
                context.Visit(subquery);

                if (string.IsNullOrEmpty(subquery.Alias))
                {
                    context.WriteLine(") ON");
                }
                else
                {
                    context.WriteLine(") AS " + subquery.Alias + " ON");
                }

                break;
            }

            context.Visit(joining.Conditions);
        }
Exemplo n.º 7
0
 protected virtual void VisitLiteral(ExpressionVisitorContext context, LiteralExpression literal)
 {
     //输出字面量文本
     if (!string.IsNullOrEmpty(literal.Text))
     {
         context.Write(literal.Text);
     }
 }
Exemplo n.º 8
0
 protected virtual void VisitComment(ExpressionVisitorContext context, CommentExpression comment)
 {
     //输出注释文本
     if (!string.IsNullOrEmpty(comment.Text))
     {
         context.Write("/* " + comment.Text + " */");
     }
 }
Exemplo n.º 9
0
        protected virtual void VisitTable(ExpressionVisitorContext context, TableIdentifier table)
        {
            context.Write(this.GetIdentifier(table));

            if (!string.IsNullOrEmpty(table.Alias) && !string.Equals(table.Name, table.Alias))
            {
                context.Write(" AS " + table.Alias);
            }
        }
Exemplo n.º 10
0
        protected override void OnVisiting(ExpressionVisitorContext context, SelectStatement statement)
        {
            if (!string.IsNullOrEmpty(statement.Alias))
            {
                context.WriteLine($"/* {statement.Alias} */");
            }

            //调用基类同名方法
            base.OnVisiting(context, statement);
        }
Exemplo n.º 11
0
        protected virtual void VisitInto(ExpressionVisitorContext context, IIdentifier into)
        {
            if (into == null)
            {
                return;
            }

            context.Write(" INTO ");
            context.Visit(into);
        }
Exemplo n.º 12
0
        public void Visit(ExpressionVisitorContext context, TStatement statement)
        {
            //通知当前语句开始访问
            this.OnVisiting(context, statement);

            //调用具体的访问方法
            this.OnVisit(context, statement);

            //通知当前语句访问完成
            this.OnVisited(context, statement);
        }
Exemplo n.º 13
0
 protected virtual void VisitVariable(ExpressionVisitorContext context, VariableIdentifier variable)
 {
     if (variable.IsGlobal)
     {
         context.Write("@@" + variable.Name);
     }
     else
     {
         context.Write("@" + variable.Name);
     }
 }
Exemplo n.º 14
0
        protected virtual void VisitTables(ExpressionVisitorContext context, UpdateStatement statement, IList <TableIdentifier> tables)
        {
            for (int i = 0; i < tables.Count; i++)
            {
                if (i > 0)
                {
                    context.Write(",");
                }

                context.Visit(tables[i]);
            }
        }
Exemplo n.º 15
0
        protected virtual void VisitField(ExpressionVisitorContext context, FieldDefinition field)
        {
            context.Write($"{field.Name} {this.Dialect.GetDbType(field.DbType, field.Length, field.Precision, field.Scale)}");

            if (field.Nullable)
            {
                context.Write(" NULL");
            }
            else
            {
                context.Write(" NOT NULL");
            }
        }
Exemplo n.º 16
0
        public static void VisitFrom(this ExpressionVisitorContext context, ICollection <ISource> sources, Action <ExpressionVisitorContext, JoinClause> join)
        {
            if (sources == null || sources.Count == 0)
            {
                return;
            }

            context.Write(" FROM ");

            foreach (var source in sources)
            {
                switch (source)
                {
                case TableIdentifier table:
                    context.Visit(table);

                    break;

                case SelectStatement subquery:
                    context.Write("(");

                    //递归生成子查询语句
                    context.Visit(subquery);

                    if (string.IsNullOrEmpty(subquery.Alias))
                    {
                        context.Write(")");
                    }
                    else
                    {
                        context.Write(") AS " + subquery.Alias);
                    }

                    break;

                case JoinClause joining:
                    if (join == null)
                    {
                        VisitJoin(context, joining);
                    }
                    else
                    {
                        join(context, joining);
                    }

                    break;
                }
            }
        }
Exemplo n.º 17
0
        public static void VisitWhere(this ExpressionVisitorContext context, IExpression where)
        {
            if (where == null)
            {
                return;
            }

            if (context.Output.Length > 0)
            {
                context.WriteLine();
            }

            context.Write("WHERE ");
            context.Visit(where);
        }
Exemplo n.º 18
0
        protected override void OnVisit(ExpressionVisitorContext context, DeleteStatement statement)
        {
            if (statement.Returning != null && statement.Returning.Table != null)
            {
                context.Visit(statement.Returning.Table);
            }

            context.Write("DELETE ");

            this.VisitTables(context, statement, statement.Tables);
            this.VisitFrom(context, statement, statement.From);
            this.VisitWhere(context, statement, statement.Where);

            context.WriteLine(";");
        }
Exemplo n.º 19
0
        protected virtual void VisitConstant(ExpressionVisitorContext context, ConstantExpression constant)
        {
            if (constant.Value == null)
            {
                context.Write("NULL");
                return;
            }

            if (constant.ValueType == typeof(bool) || Zongsoft.Common.TypeExtension.IsNumeric(constant.ValueType))
            {
                context.Write(constant.Value.ToString());
            }
            else
            {
                context.Write("'" + constant.Value.ToString() + "'");
            }
        }
Exemplo n.º 20
0
        protected override void OnVisited(ExpressionVisitorContext context, SelectStatement statement)
        {
            if (context.Depth == 0)
            {
                context.WriteLine(";");
            }

            if (statement.Paging != null && statement.Paging.Enabled)
            {
                /*
                 * 注意:有分组子句的分页和没有分组子句的分页查询是不同的。
                 */

                if (statement.GroupBy == null)
                {
                    context.WriteLine("SELECT COUNT(*)");
                    this.VisitFrom(context, statement.From);
                    this.VisitWhere(context, statement.Where);
                }
                else
                {
                    context.WriteLine("SELECT COUNT(0) FROM (");
                    context.WriteLine("SELECT ");

                    int index = 0;

                    foreach (var key in statement.GroupBy.Keys)
                    {
                        if (index++ > 0)
                        {
                            context.Write(",");
                        }

                        context.Visit(key);
                    }

                    this.VisitFrom(context, statement.From);
                    this.VisitWhere(context, statement.Where);
                    this.VisitGroupBy(context, statement.GroupBy);
                    context.WriteLine(") AS __wrapping__");
                }
            }

            //调用基类同名方法
            base.OnVisited(context, statement);
        }
Exemplo n.º 21
0
        protected virtual void VisitFields(ExpressionVisitorContext context, InsertStatement statement, ICollection <FieldIdentifier> fields)
        {
            int index = 0;

            context.Write(" (");

            foreach (var field in fields)
            {
                if (index++ > 0)
                {
                    context.Write(",");
                }

                context.Visit(field);
            }

            context.Write(")");
        }
Exemplo n.º 22
0
        protected virtual void VisitTables(ExpressionVisitorContext context, DeleteStatement statement, IList <TableIdentifier> tables)
        {
            for (int i = 0; i < tables.Count; i++)
            {
                if (i > 0)
                {
                    context.Write(",");
                }

                if (string.IsNullOrEmpty(tables[i].Alias))
                {
                    context.Write(tables[i].Name);
                }
                else
                {
                    context.Write(tables[i].Alias);
                }
            }
        }
Exemplo n.º 23
0
        protected virtual void VisitBlock(ExpressionVisitorContext context, BlockExpression block)
        {
            if (block.Count == 0)
            {
                return;
            }

            int index = 0;

            foreach (var item in block)
            {
                //添加分割符以区隔各块元素
                if (index++ > 0 && block.Delimiter != BlockExpressionDelimiter.None)
                {
                    context.Write(this.GetDelimiter(block.Delimiter));
                }

                this.OnVisit(context, item);
            }
        }