public QuerySubQueryContainer(BaseStatement fNode, CriteriaOperator fAggregateProperty, Aggregate fAggregateType) { Node = fNode; AggregateProperty = fAggregateProperty; AggregateType = fAggregateType; }
protected override Task <Func <object> > DoInsertAsync(string executableSql, Dict executableParams, bool ignoreIfDuplicate) { InsertStatement executableStatement = new InsertStatement(this.database, executableSql); var memoryTable = executableStatement.StatementFromRefs[0].table as MemoryTable; var insertRefs = executableStatement.GetInsertRefs(executableParams); var fieldValues = BaseStatement.RemapStatementParamsToFieldValues(executableParams, insertRefs); var dataRow = memoryTable.DataTable.NewRow(); foreach (var nameValuePair in fieldValues) { dataRow[nameValuePair.Key] = nameValuePair.Value; } memoryTable.DataTable.Rows.Add(dataRow); this.changedTables.Add(memoryTable); if (memoryTable.AutoIncrementFieldName == null) { return(Task.FromResult <Func <object> >(null)); } else { return(Task.FromResult <Func <object> >(() => dataRow[memoryTable.AutoIncrementFieldName])); } }
[NotNull] private JToken SerializeStatement([NotNull] BaseStatement stmt) { switch (stmt) { case Goto g: return(new JObject { ["type"] = "statement::goto", ["expression"] = SerializeExpression(g.Destination) }); case If i: return(new JObject { ["type"] = "statement::if", ["condition"] = SerializeExpression(i.Condition), ["body"] = SerializeStatementList(i.TrueBranch), ["else_body"] = SerializeStatementList(i.FalseBranch) }); case Assignment a: return(SerializeAssignment(a)); case ExpressionWrapper e: return(new JObject { ["type"] = "statement::expression", ["expression"] = SerializeExpression(e.Expression) }); case StatementList _: throw new NotSupportedException(); default: throw new NotSupportedException($"Cannot serialize statement type `{stmt.GetType().Name}`"); } }
private DataStoreEntry GetDataStoreEntry(BaseStatement statement, DataStoreMode mode) { if (entries.Count == 1) { return(entries[0]); } var tables = GetTables(statement); var dataStores = tables.Select(GetDataStoreEntry).Distinct().ToArray(); if (dataStores.Length == 1) { var result = dataStores[0]; if (result.Mode < mode) { throw new InvalidOperationException($"Mode {mode} not allowed on data store {result.DataStore}"); } return(result); } string joinedNames = string.Join(", ", tables.Select(t => t.Name)); if (dataStores.Length == 0) { throw new InvalidOperationException($"No data stores found for tables: {joinedNames}"); } throw new InvalidOperationException($"Multiple data stores found in sinlge statement. Tables: {joinedNames}"); }
[NotNull] public TResult Visit([NotNull] BaseStatement statement) { switch (statement) { case Conditional a: return(Visit(a)); case TypedAssignment a: return(Visit(a)); case ErrorStatement a: return(Visit(a)); case CompoundAssignment a: return(Visit(a)); case Assignment a: return(Visit(a)); case ExpressionWrapper a: return(Visit(a)); case Goto a: return(Visit(a)); case If a: return(Visit(a)); case StatementList a: return(Visit(a)); case EmptyStatement a: return(Visit(a)); } return(VisitUnknown(statement)); }
/// <summary> /// IceKori 解释器对象。 /// </summary> /// <param name="commonVariables">公共变量列表</param> /// <param name="commonCommands">公共指令列表</param> /// <param name="globalVariables">全局变量列表</param> /// <param name="globalCommands">全局指令列表</param> /// <param name="commands">指令列表</param> public Interpreter(Dictionary <string, BaseExpression> commonVariables, Dictionary <string, List <BaseStatement> > commonCommands, Dictionary <string, IceKoriBaseType> globalVariables, Dictionary <string, List <BaseStatement> > globalCommands, List <BaseStatement> commands) { ErrorHandling = new ErrorHandling(); Env = new Enviroment(this, commonVariables, commonCommands, globalVariables, globalCommands); Statement = new Sequence(commands); _DefaultDefine(); State = InterpreterState.Pending; }
public static string GetTableNamesFromStatements(BaseStatement[] baseStatements) { var profiler = MiniProfiler.Current; if (profiler != null) { string[] tableNamesArray = new string[baseStatements.Length]; for (int i = 0; i < baseStatements.Length; i++) { tableNamesArray[i] = baseStatements[i].TableName; } return String.Join<String>(", ", tableNamesArray); } return null; }
public TResult Visit(BaseStatement statement) { return(statement switch { Conditional a => Visit(a), TypedAssignment a => Visit(a), ErrorStatement a => Visit(a), CompoundAssignment a => Visit(a), Assignment a => Visit(a), ExpressionWrapper a => Visit(a), Goto a => Visit(a), If a => Visit(a), StatementList a => Visit(a), EmptyStatement a => Visit(a), _ => VisitUnknown(statement) });
private JToken SerializeStatement(BaseStatement stmt) { return(stmt switch { Goto g => new JObject { ["type"] = "statement::goto", ["expression"] = SerializeExpression(g.Destination) }, If i => new JObject { ["type"] = "statement::if", ["condition"] = SerializeExpression(i.Condition), ["body"] = SerializeStatementList(i.TrueBranch), ["else_body"] = SerializeStatementList(i.FalseBranch) }, Assignment a => SerializeAssignment(a), ExpressionWrapper e => new JObject { ["type"] = "statement::expression", ["expression"] = SerializeExpression(e.Expression) }, StatementList _ => throw new NotSupportedException(), _ => throw new NotSupportedException($"Cannot serialize statement type `{stmt.GetType().Name}`") });
private void _Reduce() { if (IsDebug) { Debug.Log(IceKori.PrettifyPrint(Statement.ToString())); } if (Statement.GetType() != typeof(DoNothing)) { var reduceValue = Statement.Reduce(Env, ErrorHandling); Statement = (BaseStatement)reduceValue[0]; Env = (Enviroment)reduceValue[1]; ErrorHandling = (ErrorHandling)reduceValue[2]; } else { State = InterpreterState.End; } }
public void When_Statement_Has_ObjectType_Condtion() { var selectStatement = new SelectStatement { Condition = new BinaryOperator { LeftOperand = new QueryOperand("ObjectType", "N0", DBColumnType.Int32), RightOperand = new ParameterValue { Value = 14 } } }; var baseStatements = new BaseStatement[]{selectStatement}; var objectMerger = new ObjectMerger(baseStatements); objectMerger.Merge(14, 2); Assert.AreEqual("N0.{ObjectType,Int32} = 2 Or N0.{ObjectType,Int32} = 14", baseStatements[0].Condition.ToString()); }
private static void Assert(Model model, ITypeAssignments types, BaseStatement stmt) { switch (stmt) { default: throw new ArgumentOutOfRangeException(stmt.GetType().Name); case ExpressionWrapper _: case CompoundAssignment _: case If _: throw new NotSupportedException(stmt.GetType().Name); case EmptyStatement _: return; case Conditional conditional: Assert(model, conditional); return; case ErrorStatement errorStatement: Assert(model, errorStatement); return; case Assignment assignment: Assert(model, types, assignment); return; case Goto @goto: Assert(model, @goto); return; case StatementList statementList: foreach (var sub in statementList.Statements) { Assert(model, types, sub); } return; } }
protected override Task <int> DoUpdateAsync(string executableSql, Dict executableParams) { UpdateStatement executableStatement = new UpdateStatement(this.database, executableSql); if (!(executableStatement.StatementFromRefs[0].table is MemoryTable memoryTable)) { throw new Exception("Table is not a MemoryTable"); } (var whereIndex, var setRefs, var whereRefs) = executableStatement.GetWhereIndexSetRefsAndWhereRefs(this.database, executableParams); var fieldValues = BaseStatement.RemapStatementParamsToFieldValues(executableParams, setRefs); string evaluatedWhereClause = MemoryDatabase.EvaluateWhereClause(executableStatement.whereClause, executableParams, executableStatement.StatementFromRefs); var dataRows = memoryTable.DataTable.Select(evaluatedWhereClause); int count = 0; foreach (var dataRow in dataRows) { bool changed = false; foreach ((string name, object value) in fieldValues) { //object value = executableParams[setRef.paramName]; if (dataRow[name] != value) { dataRow[name] = value; changed = true; } } if (changed) { count++; } } this.changedTables.Add(memoryTable); return(Task.FromResult(count)); }
public static BaseStatement OnError(this BaseStatement stmt, Action <Exception> handler) { stmt.AddErrorHandler(new ActionErrorHandlerBuilder(handler)); return(stmt); }
public async Task HandlesErrors( [CombinatorialValues( "select CAST('a' as int)/Conversion failed when converting the varchar value 'a' to data type int.", "select hrkljush/Invalid column name 'hrkljush'.", "select 1 as a, 1%0 as b for json path/Divide by zero error encountered.", "select JSON_VALUE('a', '$.test')/JSON text is not properly formatted. Unexpected character 'a' is found at position 0.", "select * from UnknownTable FOR JSON PATH/Invalid object name 'UnknownTable'.", "select UnknownColumn from sys.objects FOR JSON PATH/Invalid column name 'UnknownColumn'.", "select g= geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0) from sys.objects FOR JSON PATH/FOR JSON cannot serialize CLR objects. Cast CLR types explicitly into one of the supported types in FOR JSON queries.")] string query_error, [CombinatorialValues(false, true)] bool async, [CombinatorialValues("query", "mapper", "command")] string client, [CombinatorialValues(true, false)] bool useCommandAsPipe, [CombinatorialValues("?", "N/A", null, "")] string defaultValue, [CombinatorialValues("<s>", "{", "<!--", null, "")] string prefix, [CombinatorialValues(null, "</s>", "}", "", "-->")] string suffix, [CombinatorialValues(true, false)] bool executeCallbackOnError, [CombinatorialValues(true, false)] bool provideOnErrorHandler) { // Arrange var pair = query_error.Split('/'); var query = pair[0]; var error = pair[1]; bool exceptionThrown = false; BaseStatement sut = null; switch (client) { case "query": sut = (BaseStatement) new Belgrade.SqlClient.SqlDb.QueryPipe(Util.Settings.MasterConnectionString) .Sql(query); break; case "mapper": sut = (BaseStatement) new Belgrade.SqlClient.SqlDb.QueryMapper(Util.Settings.MasterConnectionString) .Sql(query); break; case "command": sut = (BaseStatement) new Belgrade.SqlClient.SqlDb.Command(Util.Settings.MasterConnectionString) .Sql(query); break; } if (provideOnErrorHandler) { sut.OnError(ex => { Assert.True(ex.GetType().Name == "SqlException", "The thrown excpetion should be 'SqlException'!"); Assert.Equal(error, ex.Message); exceptionThrown = true; }); } using (MemoryStream ms = new MemoryStream()) { Task t = null; switch (client) { case "query": t = ((IQueryPipe)sut) .Stream(ms, new Options() { Prefix = prefix, DefaultOutput = defaultValue, Suffix = suffix }); break; case "mapper": if (executeCallbackOnError) { t = ((IQuery)sut) .Map((r, ex) => { Assert.Null(r); Assert.NotNull(ex); Assert.True(ex.GetType().Name == "SqlException"); Assert.Equal(error, ex.Message); exceptionThrown = true; }); } else { t = ((IQuery)sut) .Map(r => { throw new Exception("Should not execute callback!"); }); } break; case "command": if (useCommandAsPipe) { t = ((ICommand)sut) .Stream(ms, new Options() { Prefix = prefix, DefaultOutput = defaultValue, Suffix = suffix }); } else { t = ((ICommand)sut) .Exec(); } break; } try { // Action if (async) { await t; } else { t.Wait(); } if (!provideOnErrorHandler) { Assert.True(false, "Exception should be thrown before this statement"); } else { Assert.True(exceptionThrown, "Exception handler is not invoked!"); if (client == "query" || client == "command" && useCommandAsPipe) { ms.Position = 0; var text = new StreamReader(ms).ReadToEnd(); Assert.Equal(prefix + defaultValue + suffix, text);// "NEW: Exception is not thrown so returned value shoudl be prefix+default+suffix"); } } } catch (Exception ex) { if (provideOnErrorHandler) { Assert.True(false, "Exception should not be thrown if error handler is provided." + ex.ToString()); } } } }
public ObjectMerger(BaseStatement[] selectStatements) { _selectStatements = selectStatements; }
public QuerySubQueryContainer(BaseStatement fNode, CriteriaOperator fAggregateProperty) : this(fNode, fAggregateProperty, Aggregate.Sum) { }
/// <summary> /// Возвращает запрос модификации данных /// </summary> /// <param name="table">Таблица, данные которой модифицируются</param> /// <param name="node">Выражение модификации данных</param> /// <returns>Запрос модификации данных</returns> public Query GenerateSql(DBTableEx table, BaseStatement node) { this.table = table; return GenerateSql(node); }
private void Process(BaseStatement statement) { if (statement == null) return; CollectTables(statement); Process((JoinNode)statement); Process(statement.Operands); }
/// <summary> /// Выполнить обработку алиасов в указанном выражении /// </summary> /// <param name="statement">Выражение выборки или изменения данных</param> /// <param name="dataStore">Объект, выполняющий операции с данными</param> /// <param name="customAliases">Список алиасов таблиц с настраиваемым управлением данными</param> public static void Execute(BaseStatement statement, IDataStore dataStore, out StringCollection customAliases) { CustomPersistentSelectProcessor processor = new CustomPersistentSelectProcessor(dataStore); processor.Process(statement); customAliases = new StringCollection(); if (processor.tables.Count > 0) customAliases.AddRange(processor.tables.Keys.ToArray()); }
/// <summary> /// Выполнить обработку алиасов в указанном выражении /// </summary> /// <param name="statement">Выражение БД</param> /// <param name="process">Процедура обработки алиасов</param> public static void Execute(BaseStatement statement, AliasProcess process) { AliasProcessor processor = new AliasProcessor(process); processor.Process(statement); }
public int PrepareStatement <TResult>(BaseStatement <TResult> statement) where TResult : BaseResult { int stmtId = Interlocked.Increment(ref _stmtId); switch (statement.GetType().Name) { case nameof(FindStatement): FindStatement fs = statement as FindStatement; Debug.Assert(fs != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Find, fs.Target.Schema.Name, fs.Target.Name, false, fs.FilterData, fs.findParams); break; case nameof(TableSelectStatement): TableSelectStatement ss = statement as TableSelectStatement; Debug.Assert(ss != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Find, ss.Target.Schema.Name, ss.Target.Name, true, ss.FilterData, ss.findParams); break; case nameof(ModifyStatement): ModifyStatement ms = statement as ModifyStatement; Debug.Assert(ms != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Update, ms.Target.Schema.Name, ms.Target.Name, false, ms.FilterData, null, ms.Updates); break; case nameof(TableUpdateStatement): TableUpdateStatement us = statement as TableUpdateStatement; Debug.Assert(us != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Update, us.Target.Schema.Name, us.Target.Name, true, us.FilterData, null, us.updates); break; case nameof(RemoveStatement): RemoveStatement rs = statement as RemoveStatement; Debug.Assert(rs != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Delete, rs.Target.Schema.Name, rs.Target.Name, false, rs.FilterData, null); break; case nameof(TableDeleteStatement): TableDeleteStatement ds = statement as TableDeleteStatement; Debug.Assert(ds != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Delete, ds.Target.Schema.Name, ds.Target.Name, true, ds.FilterData, null); break; case nameof(TableInsertStatement): TableInsertStatement insert = statement as TableInsertStatement; Debug.Assert(insert != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.Insert, insert.Target.Schema.Name, insert.Target.Name, true, null, null, null, insert.values.ToArray(), insert.fields, false); break; case nameof(SqlStatement): SqlStatement sqlStatement = statement as SqlStatement; Debug.Assert(sqlStatement != null); protocol.SendPrepareStatement( (uint)stmtId, DataAccess.PreparedStatementType.SqlStatement, null, null, true, null, null, null, sqlStatement.parameters.ToArray(), null, false, sqlStatement.SQL); break; default: throw new NotSupportedException(statement.GetType().Name); } _preparedStatements.Add(stmtId); return(stmtId); }
public override CypherStatementModifier Tokenize(string cypherStatement) { CurrentVariables.Clear(); base.Tokenize(cypherStatement); if (Tokens.Count == 0) { return(this); } foreach (string item in Tokens.Keys) { CurrentVariables.Add(item, new List <Tuple <string, string> >()); } int i = 0; string prevKey = null; foreach (string currKey in Tokens.Keys) { foreach (Match m in Regex.Matches(Tokens[currKey].Value.Substring(4), @"(?:(\*)|(?:[\w_\(\)\*]+\s+AS\s+([\w_\(\)\*]+))|([\w_\(\)\*]+))", RegexOptions.IgnoreCase)) { if (!string.IsNullOrEmpty(m.Groups[1].Value)) { List <Tuple <string, string> > querySymbols = new List <Tuple <string, string> >(); foreach (Match mq in Regex.Matches(BaseStatement.Substring( prevKey == null && Tokens[currKey].Index != 0 ? 0 : Tokens[prevKey].Index + Tokens[prevKey].Length, prevKey == null && Tokens[currKey].Index != 0 ? Tokens[currKey].Index : Tokens[currKey].Index - (Tokens[prevKey].Index + Tokens[prevKey].Length) ), @"[\(\[]\s*([\w_]+)(?:\s*|:)", RegexOptions.IgnoreCase)) { if (!querySymbols.Any(p => p.Item1 == mq.Groups[1].Value)) { querySymbols.Add(new Tuple <string, string>(mq.Groups[1].Value, null)); } } if (prevKey != null) { CurrentVariables[currKey].AddRange(CurrentVariables[prevKey].Select(p => new Tuple <string, string>(p.Item1, null))); } CurrentVariables[currKey].AddRange(querySymbols); } else if (!string.IsNullOrEmpty(m.Groups[2].Value)) { CurrentVariables[currKey].Add(new Tuple <string, string>(m.Groups[2].Value, m.Value)); } else if (!string.IsNullOrEmpty(m.Groups[3].Value)) { CurrentVariables[currKey].Add(new Tuple <string, string>(m.Groups[3].Value, null)); } else { throw new Exception("Statement parsing error"); } } prevKey = currKey; i++; } return(this); }
public override bool Equals(BaseStatement other) { return(other is Conditional a && a.Equals(this)); }
protected override BaseStatement VisitUnknown(BaseStatement statement) { return(statement); }
public override bool Equals(BaseStatement other) { return(other is ErrorStatement a && a.Equals(this)); }
[NotNull] protected virtual TResult VisitUnknown(BaseStatement statement) { throw new InvalidOperationException($"`Visit` invalid for statement type `{statement.GetType().FullName}`"); }
public void Add(BaseStatement stmt) { _statements.Add(stmt); }