public override ISqlElement VisitUnary(UnaryExpression unaryExpression) { var needParens = NeedParens(unaryExpression); var previous = parentOperatorExpression; parentOperatorExpression = unaryExpression; builder.Append(needParens ? "(" : " "); if (unaryExpression.Operator == UnaryOperator.Not) { builder.Append("not "); } else if (unaryExpression.Operator == UnaryOperator.Negation) { builder.Append("-"); } else { const string messageFormat = "unexpected unary operator type [{0}]"; throw new InvalidOperationException(string.Format(messageFormat, unaryExpression.Operator)); } Visit(unaryExpression.Argument); if (needParens) { builder.Append(")"); } parentOperatorExpression = previous; return(unaryExpression); }
/// <summary> /// Renders an element to a string using default debug settings. /// </summary> /// <param name="element">The element to render.</param> /// <returns>String representing the specified element.</returns> internal static string RenderDebug(this ISqlElement element) { var debugContext = new RenderContext(new GenericDialect()); element.Render(debugContext); return(debugContext.CommandText); }
public static string Format(ISqlElement element) { var formatter = new SqlFormatter(); formatter.Visit(element); return(formatter.builder.ToString()); }
private static void AssertIsColumnReference(ISqlElement element, string name, ISqlElement source) { Assert.That(element, Is.TypeOf <ColumnReferenceExpression>()); var columnReference = (ColumnReferenceExpression)element; Assert.That(columnReference.Table, Is.EqualTo(source)); Assert.That(columnReference.Name, Is.EqualTo(name)); }
private static void NotSupported(ISqlElement element, params object[] args) { const string messageFormat = "element [{0}] can't be turned to sql, " + "must be rewritten to something else first"; var argsString = args.JoinStrings(","); throw new InvalidOperationException(string.Format(messageFormat, element.GetType().FormatName() + (argsString == "" ? "" : ":" + argsString))); }
/// <summary> /// Initializes a new condition instance. /// </summary> /// <param name="operationType">Type of the operation.</param> /// <param name="left">The left.</param> /// <param name="right">The right.</param> public Condition( OperationType operationType, ISqlElement left, ISqlElement?right) { OperationType = operationType; Left = left; Right = right; }
public override SubqueryClause VisitSubquery(SubqueryClause clause) { var previous = parentOperatorExpression; parentOperatorExpression = null; builder.Append("("); Visit(clause.Query); builder.Append(")"); parentOperatorExpression = previous; return(clause); }
public override ISqlElement Visit(ISqlElement element) { var sqlElement = base.Visit(element); var typedElement = element as T; if (typedElement != null) { return(visit(typedElement)); } return(sqlElement); }
/// <summary> /// Represents a CAST expression using a raw string type value as the /// target type. /// </summary> /// <param name="expression">The expression to be cast.</param> /// <param name="rawType">Type to be converted. Note that this value /// is vulnerable to injection attacks if untrusted input is used. If /// this value contains an apostrophe, an exception will be thrown as a /// is a precaution, but is insufficient to prevent all possible /// injection attacks.</param> public Cast(ISqlElement expression, string rawType) { if (rawType == null) { throw new ArgumentNullException("Type value must not be null.", nameof(rawType)); } if (String.IsNullOrWhiteSpace(rawType) || rawType.Contains("'")) { throw new ArgumentException("Invalid type value string.", nameof(rawType)); } Expression = expression ?? throw new ArgumentNullException("Expression must not be null.", nameof(expression)); AsRaw = rawType; }
private static int?GetOperatorPrecedence(ISqlElement element) { var binaryExpression = element as BinaryExpression; if (binaryExpression != null) { return(GetPrecedence(binaryExpression.Operator)); } var unaryExpression = element as UnaryExpression; if (unaryExpression != null) { return(GetPrecedence(unaryExpression.Operator)); } return(null); }
public override ISqlElement VisitBinary(BinaryExpression expression) { var needParens = NeedParens(expression); var previous = parentOperatorExpression; parentOperatorExpression = expression; if (needParens) { builder.Append("("); } Visit(expression.Left); builder.AppendFormat(" {0} ", GetOperatorText(expression.Operator)); Visit(expression.Right); if (needParens) { builder.Append(")"); } parentOperatorExpression = previous; return(expression); }
private void VisitCondition(ISqlElement sqlElement) { if (sqlElement == null) { return; } var isReference = sqlElement as IsReferenceExpression; if (isReference != null) { SetPropertyType(isReference.Argument, isReference.ObjectName); return; } var andExpression = sqlElement as AndExpression; if (andExpression != null) { VisitCondition(andExpression.Left); VisitCondition(andExpression.Right); } }
private Reference GetReference(ISqlElement expr) { var column = expr as ColumnReferenceExpression; if (column == null) { return(null); } var table = column.Table as TableDeclarationClause; if (table == null) { return(null); } var tableMapping = mappingSource.ResolveTableByDbNameOrNull(table.Name); if (tableMapping == null) { return(null); } var mapping = tableMapping.Properties.Where(x => x.UnionLayout != null) .SingleOrDefault(x => x.UnionLayout.ReferenceColumnName == column.Name); if (mapping == null) { mapping = tableMapping.Properties.Where(x => x.SingleLayout != null) .SingleOrDefault(x => x.SingleLayout.DbColumnName == column.Name); if (mapping == null || string.IsNullOrEmpty(mapping.SingleLayout.NestedTableName)) { return(null); } } return(new Reference { Property = mapping, Column = column, }); }
public void RewriteTables(ISqlElement element) { var rewrittenTables = new Dictionary <IColumnSource, IColumnSource>(); TableDeclarationVisitor.Visit(element, original => { var rewritten = RewriteTableIfNeeded(original); if (rewritten != original) { rewrittenTables.Add(original, rewritten); } return(rewritten); }); new ColumnReferenceVisitor(column => { IColumnSource generatedTable; if (rewrittenTables.TryGetValue(column.Table, out generatedTable)) { column.Table = generatedTable; } return(column); }).Visit(element); }
public static void Visit(ISqlElement element, Func <SubqueryTable, SubqueryTable> visit) { new SubqueryVisitor(visit).Visit(element); }
/// <summary> /// Creates a descending <see cref="Order" /> instance using the specified element. /// </summary> /// <param name="element">The element to order.</param> /// <returns>A new <see cref="Order"/> element.</returns> public static Order Desc(this ISqlElement element) => new Order(element, Direction.Descending);
/// <summary> /// Creates an alias for the specified element. /// </summary> /// <param name="element">The element to alias.</param> /// <param name="alias">The alias name.</param> /// <returns>A new <see cref="Alias"/> instance</returns> public static Alias As(this ISqlElement element, string alias) => new Alias(element, alias);
/// <summary> /// Represents a CAST expression using a DB type and optional type /// parameters. The specific value used will be /// </summary> /// <param name="expression">The expression to be cast.</param> /// <param name="type">The DB type to cast to.</param> /// <param name="typeParameters">Any parameters for the cast type, such as length of a char type.</param> public Cast(ISqlElement expression, DbType type, params object[] typeParameters) { Expression = expression ?? throw new ArgumentNullException("Expression must not be null.", nameof(expression)); As = type; AsParams = typeParameters; }
/// <summary> /// Initializes a new instance of the <see cref="JsonExpression"/> class. /// </summary> /// <param name="left">The left element for this expression. Must not be null.</param> /// <param name="symbol">The operator for this expression.</param> /// <param name="right">The right element for this expression. Must not be null.</param> public JsonExpression(ExpressionElement left, JsonExpressionOperator symbol, ExpressionElement right) { Left = left as ISqlElement ?? throw new ArgumentNullException(nameof(left)); Symbol = symbol; Right = right as ISqlElement ?? throw new ArgumentNullException(nameof(right)); }
/// <summary> /// SQL element to assign a target = value. /// </summary> /// <param name="target">The target to update.</param> /// <param name="value">The source of the value to be assigned.</param> public Assignment(ISqlElement target, ISqlElement value) { Target = target; Value = value; }
//TODO. this trash is only because of selectParts public virtual ISqlElement VisitWhere(ISqlElement filter) { return(Visit(filter)); }
//TODO. this trash is only because of selectParts public virtual ISqlElement VisitHaving(ISqlElement element) { return(Visit(element)); }
public virtual ISqlElement Visit(ISqlElement element) { return(element.Accept(this)); }
public static void Write(this ISqlWriter writer, ISqlElement element) { element.ToString(writer); }
/// <summary> /// Renders an element to a string using default debug settings. This /// method can be accessed by external libraries which need include /// ISqlElement implementations. /// </summary> /// <param name="element">The element to render.</param> /// <returns>String representing the specified element.</returns> public static string CreateDebugString(ISqlElement element) => element.RenderDebug();
public Value(ISqlElement value) { throw new InvalidCommandException("Attempted to pass an instance of an ISqlElement as a value in query, SQL elements should not be passed as values."); }
/// <summary> /// Initializes a new instance of the <see cref="Alias"/> class. /// </summary> /// <param name="element">The element being aliased.</param> /// <param name="alias">The alias name.</param> public Alias(ISqlElement element, string alias) { Element = element; Name = alias; }
public static void Visit(ISqlElement element, Func <TableDeclarationClause, ISqlElement> visit) { var visitor = new TableDeclarationVisitor(visit); visitor.Visit(element); }
private bool NeedParens(ISqlElement current) { return(GetOperatorPrecedence(parentOperatorExpression) > GetOperatorPrecedence(current)); }
public override ISqlElement VisitHaving(ISqlElement element) { WithCurrentPart(SelectPart.Other, () => base.VisitHaving(element)); return(element); }
/// <summary> /// Initializes a new instance of the <see cref="Drop"/> class. /// </summary> /// <param name="type">The type of drop statement.</param> /// <param name="target">The element to drop.</param> public Drop(DropType type, ISqlElement target) { Type = type; Target = target; }