Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="node"></param>
        /// <param name="context"></param>
        /// <param name="batchScope"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual SqlResult Compile(Node node, IResolveFieldContext context, IEnumerable?batchScope = null)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // TODO: Should we validate the node?

            var selections         = new List <string>();
            var tables             = new List <string>();
            var wheres             = new List <WhereCondition>();
            var orders             = new List <string>();
            var sqlCompilerContext = new SqlCompilerContext(this);

            StringifySqlAst(null, node, Array.Empty <string>(), context, selections, tables, wheres, orders, batchScope ?? Enumerable.Empty <object>(),
                            sqlCompilerContext);

            var sb = new StringBuilder();

            sb.AppendLine("SELECT");
            sb.Append("  ");
            sb.AppendLine(string.Join(",\n  ", selections));
            sb.AppendLine(string.Join("\n", tables));

            if (wheres.Count > 0)
            {
                sb.Append("WHERE ");
                sb.AppendLine(_dialect.CompileConditions(wheres, sqlCompilerContext));
            }

            if (orders.Count > 0)
            {
                sb.Append("ORDER BY ");
                sb.AppendLine(string.Join(", ", orders));
            }

            return(new SqlResult(sb.ToString().Trim(), sqlCompilerContext.Parameters));
        }