public InputFragment FindInputFromProperties(PropertyFragment fragment) { Debug.Assert(fragment != null); PropertyFragment propertyFragment = fragment as PropertyFragment; Debug.Assert(propertyFragment != null); if (propertyFragment.Properties.Count >= 2) { for (int x = propertyFragment.Properties.Count - 2; x >= 0; x--) { string reference = propertyFragment.Properties[x]; InputFragment input = GetFragment(reference); if (input == null) { continue; } if (input.Scoped) { return(input); } if (input is SelectStatement) { return((input as SelectStatement).From); } continue; } } Debug.Fail("Should have found an input"); return(null); }
private void AddDefaultColumns(Scope scope) { if (columnHash == null) { columnHash = new Dictionary <string, ColumnFragment>(); } List <ColumnFragment> columns = GetDefaultColumnsForFragment(From); foreach (ColumnFragment column in columns) { // first we need to set the input for this column InputFragment input = scope.FindInputFromProperties(column.PropertyFragment); column.TableName = input.Name; // then we rename the column if necessary if (columnHash.ContainsKey(column.ColumnName)) { column.ColumnAlias = MakeColumnNameUnique(column.ColumnName); columnHash.Add(column.ColumnAlias, column); } else { columnHash.Add(column.ColumnName, column); } Columns.Add(column); } }
public void Remove(InputFragment fragment) { if (fragment == null) { return; } if (fragment.Name != null) { scopeTable.Remove(fragment.Name); } if (fragment is SelectStatement) { Remove((fragment as SelectStatement).From); } else if (fragment is JoinFragment) { JoinFragment j = fragment as JoinFragment; Remove(j.Left); Remove(j.Right); } else if (fragment is UnionFragment) { UnionFragment u = fragment as UnionFragment; Remove(u.Left); Remove(u.Right); } }
private InputFragment WrapJoinInputIfNecessary(InputFragment fragment, bool isRightPart) { if (fragment is SelectStatement || fragment is UnionFragment) { fragment.Wrap(scope); fragment.Scoped = true; } else if (fragment is JoinFragment && isRightPart) { SelectStatement select = new SelectStatement(); select.From = fragment; select.Name = fragment.Name; select.Wrap(scope); return(select); } return(fragment); }
public override SqlFragment Visit(DbPropertyExpression expression) { propertyLevel++; PropertyFragment fragment = expression.Instance.Accept(this) as PropertyFragment; fragment.Properties.Add(expression.Property.Name); propertyLevel--; // if we are not at the top level property then just return if (propertyLevel > 0) { return(fragment); } ColumnFragment column = new ColumnFragment(null, fragment.LastProperty); column.PropertyFragment = fragment; InputFragment input = scope.FindInputFromProperties(fragment); if (input != null) { column.TableName = input.Name; } // now we need to check if our column name was possibly renamed if (input is TableFragment) { return(column); } SelectStatement select = input as SelectStatement; UnionFragment union = input as UnionFragment; if (select != null) { select.HasDifferentNameForColumn(column); } else if (union != null) { union.HasDifferentNameForColumn(column); } // input is a table, selectstatement, or unionstatement return(column); }
private SelectStatement VisitInputExpressionEnsureSelect(DbExpression e, string name, TypeUsage type) { InputFragment fragment = VisitInputExpression(e, name, type); if (fragment is SelectStatement) { return(fragment as SelectStatement); } SelectStatement s = new SelectStatement(); // if the fragment is a union then it needs to be wrapped if (fragment is UnionFragment) { (fragment as UnionFragment).Wrap(scope); } s.From = fragment; return(s); }
private List <ColumnFragment> GetDefaultColumnsForFragment(InputFragment input) { List <ColumnFragment> columns = new List <ColumnFragment>(); if (input is TableFragment) { return(GetDefaultColumnsForTable(input as TableFragment)); } else if (input is JoinFragment || input is UnionFragment) { Debug.Assert(input.Left != null); columns = GetDefaultColumnsForFragment(input.Left); if (input is JoinFragment && input.Right != null) { List <ColumnFragment> right = GetDefaultColumnsForFragment(input.Right); columns.AddRange(right); } } else if (input is SelectStatement) { SelectStatement select = input as SelectStatement; foreach (ColumnFragment cf in select.Columns) { ColumnFragment newColumn = new ColumnFragment(cf.TableName, cf.ActualColumnName); newColumn.PushInput(cf.ActualColumnName); columns.Add(newColumn); } } else { throw new NotImplementedException(); } if (!String.IsNullOrEmpty(input.Name) && input.Name != From.Name) { foreach (ColumnFragment c in columns) { c.PushInput(input.Name); } } return(columns); }
protected InputFragment VisitInputExpression(DbExpression e, string name, TypeUsage type) { SqlFragment f = e.Accept(this); Debug.Assert(f is InputFragment); InputFragment inputFragment = f as InputFragment; inputFragment.Name = name; if (inputFragment is TableFragment && type != null) { (inputFragment as TableFragment).Type = type; } if (name != null) { scope.Add(name, inputFragment); } return(inputFragment); }
private List<ColumnFragment> GetDefaultColumnsForFragment(InputFragment input) { List<ColumnFragment> columns = new List<ColumnFragment>(); if (input is TableFragment) { return GetDefaultColumnsForTable(input as TableFragment); } else if (input is JoinFragment || input is UnionFragment) { Debug.Assert(input.Left != null); columns = GetDefaultColumnsForFragment(input.Left); if (input is JoinFragment && input.Right != null) { List<ColumnFragment> right = GetDefaultColumnsForFragment(input.Right); columns.AddRange(right); } } else if (input is SelectStatement) { SelectStatement select = input as SelectStatement; foreach (ColumnFragment cf in select.Columns) { ColumnFragment newColumn = new ColumnFragment(cf.TableName, cf.ActualColumnName); newColumn.PushInput(cf.ActualColumnName); columns.Add(newColumn); } } else throw new NotImplementedException(); if (!String.IsNullOrEmpty(input.Name) && input.Name != From.Name) foreach (ColumnFragment c in columns) c.PushInput(input.Name); return columns; }
public void Add(string name, InputFragment fragment) { scopeTable.Add(name, fragment); }
private InputFragment WrapJoinInputIfNecessary(InputFragment fragment, bool isRightPart) { if (fragment is SelectStatement || fragment is UnionFragment) { fragment.Wrap(scope); fragment.Scoped = true; } else if (fragment is JoinFragment && isRightPart) { SelectStatement select = new SelectStatement(); select.From = fragment; select.Name = fragment.Name; select.Wrap(scope); return select; } return fragment; }