private SqlFragment HandleFunction(DbFunctionAggregate fa, SqlFragment arg) { Debug.Assert(fa.Arguments.Count == 1); if (fa.Function.NamespaceName != "Edm") { throw new NotSupportedException(); } FunctionFragment fragment = new FunctionFragment(); fragment.Name = fa.Function.Name; if (fa.Function.Name == "BigCount") { fragment.Name = "COUNT"; } else { fragment.Name = fa.Function.Name.ToUpperInvariant(); } fragment.Distinct = fa.Distinct; fragment.Argument = arg; return(fragment); //return new CastExpression(aggregate, GetDbType(functionAggregate.ResultType.EdmType)); }
public void Add(string name, SqlFragment fragment) { Debug.Assert(!Symbols.ContainsKey(name)); if (fragment.Name == null) fragment.Name = name; Symbols.Add(name, fragment); }
private SqlFragment GenericFunction(Dictionary <string, string> funcs, DbFunctionExpression e) { SqlFragment[] frags = new SqlFragment[e.Arguments.Count]; for (int i = 0; i < e.Arguments.Count; i++) { frags[i] = e.Arguments[i].Accept(callingGenerator); } string sql; switch (e.Function.Name) { case "Round": // Special handling for Round as it has more than one signature. sql = HandleFunctionRound(e); break; default: sql = String.Format(funcs[e.Function.Name], frags); break; } return(new LiteralFragment(sql)); }
public void AddGroupBy(SqlFragment f) { if (GroupBy == null) { GroupBy = new List <SqlFragment>(); } GroupBy.Add(f); }
public void Add(string name, SqlFragment fragment) { Debug.Assert(!Symbols.ContainsKey(name)); if (fragment.Name == null) { fragment.Name = name; } Symbols.Add(name, fragment); }
/// <summary> /// If input sqlFragment is a group by structure, is flattened to remove some nested correlation queries. /// </summary> /// <param name="input"></param> /// <returns></returns> private SqlFragment TryFlatteningGroupBy(SqlFragment input) { SelectStatement select = null; SelectStatement tmpFrag = null, tmpFrag2 = null, tmpFrag3 = null; InputFragment table = null; string objName = null, colName = null, queryName = null; // First part assert is a kind-of structure we are looking for tmpFrag = input as SelectStatement; if (tmpFrag == null) goto NoChanges; tmpFrag = ( tmpFrag ).From as SelectStatement; if (tmpFrag == null) goto NoChanges; queryName = tmpFrag.Name; if (tmpFrag.Columns.Count < 2) goto NoChanges; if (!(tmpFrag.Columns[0] is ColumnFragment)) goto NoChanges; colName = objName = (tmpFrag.Columns[0] as ColumnFragment).ActualColumnName; tmpFrag2 = tmpFrag.From as SelectStatement; if (tmpFrag2 == null) goto NoChanges; if (tmpFrag2.Columns.Count < 1 || !(tmpFrag2.Columns[0] is ColumnFragment)) goto NoChanges; if (string.CompareOrdinal(objName, tmpFrag2.Columns[0].ActualColumnName) != 0) goto NoChanges; if (tmpFrag.Columns[1].Literal == null ) goto NoChanges; tmpFrag2 = tmpFrag.Columns[1].Literal as SelectStatement; if (tmpFrag2 == null) goto NoChanges; if ((tmpFrag2.Columns.Count != 1) || !(tmpFrag2.Columns[0].Literal != null)) goto NoChanges; tmpFrag3 = tmpFrag2.From as SelectStatement; if (tmpFrag3 == null) goto NoChanges; table = tmpFrag3.From as InputFragment; if (table == null) goto NoChanges; FunctionFragment func = tmpFrag2.Columns[0].Literal as FunctionFragment; if( tmpFrag3.Columns.Count != 1 || !( tmpFrag3.Columns[ 0 ] is ColumnFragment )) goto NoChanges; if (func == null) goto NoChanges; // Yes it is the kind-of type we like, then optimize it select = new SelectStatement(this); table.Name = null; string tableName = null; TableFragment t = tmpFrag3.From as TableFragment; if (t == null) tableName = tmpFrag3.Columns[0].TableName; else tableName = t.Table; select.From = table; select.Columns.Add(new ColumnFragment(tableName, colName)); select.Columns.Add(new ColumnFragment(tableName, "C0") { ColumnAlias = "C1", Literal = new FunctionFragment() { Argument = new ColumnFragment(tableName, tmpFrag3.Columns[0].ActualColumnName), Distinct = tmpFrag3.IsDistinct, Name = func.Name } }); select.Wrap(null); select.Name = queryName; select.AddGroupBy(select.Columns[0]); (input as SelectStatement).From = select; NoChanges: return input; }
private SqlFragment GenericFunction(Dictionary <string, string> funcs, DbFunctionExpression e) { SqlFragment[] frags = new SqlFragment[e.Arguments.Count]; for (int i = 0; i < e.Arguments.Count; i++) { frags[i] = e.Arguments[i].Accept(callingGenerator); } string sql = String.Format(funcs[e.Function.Name], frags); return(new LiteralFragment(sql)); }
private void VisitAndReplaceTableName(SqlFragment sf, string oldTable, string newTable) { BinaryFragment bf = sf as BinaryFragment; ColumnFragment cf = sf as ColumnFragment; if (bf != null) { VisitAndReplaceTableName(bf.Left, oldTable, newTable); VisitAndReplaceTableName(bf.Right, oldTable, newTable); } else if ((cf != null) && (cf.TableName == oldTable)) { cf.TableName = newTable; } }
public override SqlFragment Visit(DbConstantExpression expression) { SqlFragment value = null; if (_onReturningSelect && values.TryGetValue(_columnsVisited.Peek(), out value)) { if (value is LiteralFragment) { MySqlParameter par = Parameters.Find(p => p.ParameterName == (value as LiteralFragment).Literal); if (par != null) { return(new LiteralFragment(par.ParameterName)); } } } return(base.Visit(expression)); }
public override string GenerateSQL(DbCommandTree tree) { DbQueryCommandTree commandTree = tree as DbQueryCommandTree; SqlFragment fragment = null; DbExpression e = commandTree.Query; switch (commandTree.Query.ExpressionKind) { case DbExpressionKind.Project: fragment = e.Accept(this); Debug.Assert(fragment is SelectStatement); break; } return(fragment.ToString()); }
public override string GenerateSQL(DbCommandTree tree) { DbQueryCommandTree commandTree = tree as DbQueryCommandTree; SqlFragment fragment = null; DbExpression e = commandTree.Query; switch (commandTree.Query.ExpressionKind) { case DbExpressionKind.Project: fragment = e.Accept(this); Debug.Assert(fragment is SelectStatement); break; } // Apply post-optimizations here: fragment = TryFlatteningGroupBy(fragment); return(fragment.ToString()); }
public override string GenerateSQL(DbCommandTree tree) { DbInsertCommandTree commandTree = tree as DbInsertCommandTree; InsertStatement statement = new InsertStatement(); DbExpressionBinding e = commandTree.Target; statement.Target = (InputFragment)e.Expression.Accept(this); scope.Add("target", statement.Target); foreach (DbSetClause setClause in commandTree.SetClauses) { statement.Sets.Add(setClause.Property.Accept(this)); } if (values == null) { values = new Dictionary <EdmMember, SqlFragment>(); } foreach (DbSetClause setClause in commandTree.SetClauses) { DbExpression value = setClause.Value; SqlFragment valueFragment = value.Accept(this); statement.Values.Add(valueFragment); if (value.ExpressionKind != DbExpressionKind.Null) { EdmMember property = ((DbPropertyExpression)setClause.Property).Property; values.Add(property, valueFragment); } } if (commandTree.Returning != null) { statement.ReturningSelect = GenerateReturningSql(commandTree, commandTree.Returning); } return(statement.ToString()); }
public override string GenerateSQL(DbCommandTree tree) { DbUpdateCommandTree commandTree = tree as DbUpdateCommandTree; UpdateStatement statement = new UpdateStatement(); _onReturningSelect = false; statement.Target = commandTree.Target.Expression.Accept(this); scope.Add("target", statement.Target as InputFragment); if (values == null) { values = new Dictionary <EdmMember, SqlFragment>(); } foreach (DbSetClause setClause in commandTree.SetClauses) { statement.Properties.Add(setClause.Property.Accept(this)); DbExpression value = setClause.Value; SqlFragment valueFragment = value.Accept(this); statement.Values.Add(valueFragment); if (value.ExpressionKind != DbExpressionKind.Null) { EdmMember property = ((DbPropertyExpression)setClause.Property).Property; values.Add(property, valueFragment); } } statement.Where = commandTree.Predicate.Accept(this); _onReturningSelect = true; if (commandTree.Returning != null) { statement.ReturningSelect = GenerateReturningSql(commandTree, commandTree.Returning); } return(statement.ToString()); }
public void Append(SqlFragment s) { Fragments.Add(s); }
public ExistsFragment(SqlFragment f) { Argument = f; }
private SqlFragment GenericFunction(Dictionary<string, string> funcs, DbFunctionExpression e) { SqlFragment[] frags = new SqlFragment[e.Arguments.Count]; for (int i = 0; i < e.Arguments.Count; i++) frags[i] = e.Arguments[i].Accept(callingGenerator); string sql; switch (e.Function.Name) { case "Round": // Special handling for Round as it has more than one signature. sql = HandleFunctionRound(e); break; default: sql = String.Format(funcs[e.Function.Name], frags); break; } return new LiteralFragment(sql); }
private SqlFragment HandleFunction(DbFunctionAggregate fa, SqlFragment arg) { Debug.Assert(fa.Arguments.Count == 1); if (fa.Function.NamespaceName != "Edm") throw new NotSupportedException(); FunctionFragment fragment = new FunctionFragment(); fragment.Name = fa.Function.Name; if (fa.Function.Name == "BigCount") fragment.Name = "Count"; fragment.Distinct = fa.Distinct; fragment.Argmument = arg; return fragment; //return new CastExpression(aggregate, GetDbType(functionAggregate.ResultType.EdmType)); }
public void IndexFragment(SqlFragment fragment, string name) { Debug.Assert(!Variables.ContainsKey(name)); Variables.Add(name, fragment); }
public SortFragment(SqlFragment column, bool ascending) { Column = column; Ascending = ascending; }
public void Visit(SqlFragment f) { }
private SqlFragment GenericFunction(Dictionary<string,string> funcs, DbFunctionExpression e) { SqlFragment[] frags = new SqlFragment[e.Arguments.Count]; for (int i=0; i < e.Arguments.Count; i++) frags[i] = e.Arguments[i].Accept(callingGenerator); string sql = String.Format(funcs[e.Function.Name], frags); return new LiteralFragment(sql); }
/// <summary> /// If input sqlFragment is a group by structure, is flattened to remove some nested correlation queries. /// </summary> /// <param name="input"></param> /// <returns></returns> private SqlFragment TryFlatteningGroupBy(SqlFragment input) { SelectStatement select = null; SelectStatement tmpFrag = null, tmpFrag2 = null, tmpFrag3 = null; InputFragment table = null; string objName = null, colName = null, queryName = null; // First part assert is a kind-of structure we are looking for tmpFrag = input as SelectStatement; if (tmpFrag == null) { goto NoChanges; } tmpFrag = (tmpFrag).From as SelectStatement; if (tmpFrag == null) { goto NoChanges; } queryName = tmpFrag.Name; if (tmpFrag.Columns.Count < 2) { goto NoChanges; } if (!(tmpFrag.Columns[0] is ColumnFragment)) { goto NoChanges; } colName = objName = (tmpFrag.Columns[0] as ColumnFragment).ActualColumnName; tmpFrag2 = tmpFrag.From as SelectStatement; if (tmpFrag2 == null) { goto NoChanges; } if (tmpFrag2.Columns.Count < 1 || !(tmpFrag2.Columns[0] is ColumnFragment)) { goto NoChanges; } if (string.CompareOrdinal(objName, tmpFrag2.Columns[0].ActualColumnName) != 0) { goto NoChanges; } if (tmpFrag.Columns[1].Literal == null) { goto NoChanges; } tmpFrag2 = tmpFrag.Columns[1].Literal as SelectStatement; if (tmpFrag2 == null) { goto NoChanges; } if ((tmpFrag2.Columns.Count != 1) || !(tmpFrag2.Columns[0].Literal != null)) { goto NoChanges; } tmpFrag3 = tmpFrag2.From as SelectStatement; if (tmpFrag3 == null) { goto NoChanges; } table = tmpFrag3.From as InputFragment; if (table == null) { goto NoChanges; } FunctionFragment func = tmpFrag2.Columns[0].Literal as FunctionFragment; if (tmpFrag3.Columns.Count != 1 || !(tmpFrag3.Columns[0] is ColumnFragment)) { goto NoChanges; } if (func == null) { goto NoChanges; } // Yes it is the kind-of type we like, then optimize it select = new SelectStatement(this); table.Name = null; string tableName = null; TableFragment t = tmpFrag3.From as TableFragment; if (t == null) { tableName = tmpFrag3.Columns[0].TableName; } else { tableName = t.Table; } select.From = table; select.Columns.Add(new ColumnFragment(tableName, colName)); select.Columns.Add(new ColumnFragment(tableName, "C0") { ColumnAlias = "C1", Literal = new FunctionFragment() { Argument = new ColumnFragment(tableName, tmpFrag3.Columns[0].ActualColumnName), Distinct = tmpFrag3.IsDistinct, Name = func.Name } }); select.Wrap(null); select.Name = queryName; select.AddGroupBy(select.Columns[0]); (input as SelectStatement).From = select; NoChanges: return(input); }
public void AddGroupBy(SqlFragment f) { if (GroupBy == null) GroupBy = new List<SqlFragment>(); GroupBy.Add(f); }