private object?Substitute(CompileState state, object?operand) { if (operand == null) { return(null); } Type type = operand.GetType(); if (type.IsSubclassOfOrSelf(typeof(Result))) { return(operand); } else if (type.IsSubclassOfOrSelf(typeof(QueryCondition))) { return(operand); } else if (type.IsSubclassOfOrSelf(typeof(Parameter))) { return(operand); } else { TypeMapping mapping = state.TypeMappings.FirstOrDefault(item => item.ReturnType == type); if (mapping == null) { return(operand); } return(Parameter.Constant(operand, type)); } }
private void CompileOperand(CompileState state, object?operand) { if (operand is null) { state.Text.Append("NULL"); return; } else { Type type = operand.GetType(); if (type.IsSubclassOfOrSelf(typeof(Result))) { ((Result)operand).Compile(state); } else if (type.IsSubclassOfOrSelf(typeof(QueryCondition))) { ((QueryCondition)operand).Compile(state); } else if (type.IsSubclassOfOrSelf(typeof(Parameter))) { ((Parameter)operand).Compile(state); } else { state.Errors.Add($"The type {type!.Name} is not supported for compilation."); state.Text.Append(operand.ToString()); } } }
internal CompiledQuery(CompileState state, AsResult[] resultColumns) { QueryText = state.Text.ToString(); Parameters = new List <Parameter>(state.Parameters); ConstantValues = new List <Parameter>(state.Values); ResultColumns = resultColumns; ResultColumnTypeByName = resultColumns.ToDictionary(key => key.GetFieldName() !, item => item.GetResultType()); Errors = new List <string>(state.Errors); }
internal void Compile(CompileState state) { //find the root Node root = this; while (root.FromRelationship != null) { root = root.FromRelationship.FromNode; } Node?current = root; do { GetDirection(current, state.Text); if (!(current.NodeAlias is null)) { if (current.NodeAlias.AliasName == null) { current.NodeAlias.AliasName = string.Format("n{0}", state.patternSeq++); } if (current.IsReference || current.Neo4jLabel == null) { state.Text.AppendFormat("({0})", current.NodeAlias.AliasName); } else { state.Text.AppendFormat("({0}:{1})", current.NodeAlias.AliasName, current.Neo4jLabel); } } else { if (current.Neo4jLabel == null) { state.Text.AppendFormat("()"); } else { state.Text.AppendFormat("(:{0})", current.Neo4jLabel); } } if (current.ToRelationship != null) { current.ToRelationship.Compile(state); current = current.ToRelationship.ToNode; if (current is null) { break; } } else { break; } } while (true);
internal CompiledQuery(CompileState state, AsResult[] resultColumns) { QueryText = state.Text.ToString(); Parameters = state.Parameters.ToList(); DefaultValues = state.Values.Where(item => !item.IsConstant).ToList(); ConstantValues = state.Values.Where(item => item.IsConstant).ToList(); ResultColumns = resultColumns; ResultColumnTypeByName = resultColumns.ToDictionary(key => key.GetFieldName() !, item => item.GetResultType()); CompiledResultColumns = resultColumns .Where(item => item.GetFieldName() is not null) .Select(item => new FieldInfo(Transaction.RunningTransaction, item)) .ToList(); Errors = new List <string>(state.Errors); }
internal void Compile(CompileState state) { string repeatPattern = (repeat is null) ? string.Empty : $"*{repeat.MinHops}..{repeat.MaxHops}"; GetDirection(this, state.Text); if (RelationshipAlias is not null) { RelationshipAlias.AliasName = $"r{state.patternSeq++}"; state.Text.Append($"[{RelationshipAlias.AliasName}:{NEO4J_TYPE}{repeatPattern}]"); } else { state.Text.Append($"[:{NEO4J_TYPE}{repeatPattern}]"); } }
public ICompiled Compile() { SetType(PartType.Compiled); var state = new CompileState(PersistenceProvider.SupportedTypeMappings, PersistenceProvider.Translator); Query[] parts = GetParts(); ForEach(parts, state.Text, "\r\n", item => item?.Compile(state)); CompiledQuery = new CompiledQuery(state, parts.Last(item => item.Last).AsResults ?? new AsResult[0]); if (CompiledQuery.Errors.Count > 0) { throw new QueryException(CompiledQuery); } return(this); }
internal string Preview(Action <CompileState> compile, CompileState?state = null) { string compiled; if (state is null) { CompileState tempState = new CompileState(TypeMappings, Translator); compile.Invoke(tempState); compiled = tempState.Text.ToString(); } else { StringBuilder old = state.Text; state.Text = new StringBuilder(); compile.Invoke(state); compiled = state.Text.ToString(); state.Text = old; } return(compiled); }
internal void Compile(CompileState state, bool add) { state.Translator.Compile(this, state, add); }
internal virtual void Compile(CompileState state) { Compile(state, false); }
internal static void Compile(this Operator op, CompileState state, bool rightIsNull) { switch (op) { case Operator.And: state.Text.Append(state.Translator.OpAnd); break; case Operator.Or: state.Text.Append(state.Translator.OpOr); break; case Operator.Equals: if (rightIsNull) { state.Text.Append(state.Translator.OpIs); } else { state.Text.Append(state.Translator.OpEqual); } break; case Operator.NotEquals: if (rightIsNull) { state.Text.Append(state.Translator.OpIsNot); } else { state.Text.Append(state.Translator.OpNotEqual); } break; case Operator.Less: state.Text.Append(state.Translator.OpLessThan); break; case Operator.LessOrEqual: state.Text.Append(state.Translator.OpLessThanOrEqual); break; case Operator.Greater: state.Text.Append(state.Translator.OpGreaterThan); break; case Operator.GreaterOrEqual: state.Text.Append(state.Translator.OpGreaterThanOrEqual); break; case Operator.StartsWith: state.Text.Append(state.Translator.OpStartsWith); break; case Operator.EndsWith: state.Text.Append(state.Translator.OpEndsWith); break; case Operator.Contains: state.Text.Append(state.Translator.OpContains); break; case Operator.Match: state.Text.Append(state.Translator.OpMatch); break; case Operator.HasLabel: state.Text.Append(state.Translator.OpHasLabel); break; case Operator.In: state.Text.Append(state.Translator.OpIn); break; case Operator.Pattern: case Operator.NotPattern: case Operator.Not: break; default: throw new NotSupportedException($"The operator {op.ToString()} is not supported yet."); } }
internal override void Compile(CompileState state) { state.Translator.Compile(this, state); }
internal void Compile(CompileState state) { Left = Substitute(state, Left); Right = Substitute(state, Right); if (Operator == Operator.Boolean) { state.Text.Append("("); ((BooleanResult)Left !).Compile(state); state.Text.Append(")"); return; } Type?leftType = GetOperandType(Left); Type?rightType = GetOperandType(Right); if (leftType != null && rightType != null) { if (leftType != rightType) { if (Operator == Operator.In) { if (rightType.GetInterface(nameof(IEnumerable)) == null) { state.Errors.Add($"The types of the fields {state.Preview(s => CompileOperand(s, Right))} should be a collection."); } rightType = GetEnumeratedType(rightType); } if (GetConversionGroup(leftType, state.TypeMappings) != GetConversionGroup(rightType, state.TypeMappings)) { state.Errors.Add($"The types of the fields {state.Preview(s => CompileOperand(s, Left))} and {state.Preview(s => CompileOperand(s, Right))} are not compatible."); } } } state.Text.Append("("); if (Operator != Operator.Not) { CompileOperand(state, Left); } else { state.Text.Append("NOT("); } if (Right is Parameter) { Parameter rightParameter = (Parameter)Right; if (rightParameter.IsConstant && rightParameter.Value == null) { Operator.Compile(state, true); CompileOperand(state, null); } else { Operator.Compile(state, false); CompileOperand(state, Right); } } else { Operator.Compile(state, Right == null); CompileOperand(state, Right); } if (Operator == Operator.Not) { state.Text.Append(")"); } state.Text.Append(")"); }
private void Compile(CompileState state) { state.Translator.Compile(this, state); }
internal void Compile(CompileState state, bool suppressAliases) { state.Translator.Compile(this, state, suppressAliases); }
internal protected abstract void Compile(CompileState state);