internal override SqlExpression VisitMultiset(SqlSubSelect sms)
		{
			// allow one big-join per query?
			if((this.options & SqlMultiplexerOptionType.EnableBigJoin) != 0 &&
			   !this.hasBigJoin && this.canJoin && this.isTopLevel && this.outerSelect != null
			   && !MultisetChecker.HasMultiset(sms.Select.Selection)
			   && BigJoinChecker.CanBigJoin(sms.Select))
			{

				sms.Select = this.VisitSelect(sms.Select);

				SqlAlias alias = new SqlAlias(sms.Select);
				SqlJoin join = new SqlJoin(SqlJoinType.OuterApply, this.outerSelect.From, alias, null, sms.SourceExpression);
				this.outerSelect.From = @join;
				this.outerSelect.OrderingType = SqlOrderingType.Always;

				// make joined expression
				SqlExpression expr = (SqlExpression)SqlDuplicator.Copy(sms.Select.Selection);

				// make count expression
				SqlSelect copySelect = (SqlSelect)SqlDuplicator.Copy(sms.Select);
				SqlAlias copyAlias = new SqlAlias(copySelect);
				SqlSelect countSelect = new SqlSelect(sql.Unary(SqlNodeType.Count, null, sms.SourceExpression), copyAlias, sms.SourceExpression);
				countSelect.OrderingType = SqlOrderingType.Never;
				SqlExpression count = sql.SubSelect(SqlNodeType.ScalarSubSelect, countSelect);

				// make joined collection
				SqlJoinedCollection jc = new SqlJoinedCollection(sms.ClrType, sms.SqlType, expr, count, sms.SourceExpression);
				this.hasBigJoin = true;
				return jc;
			}
			return QueryExtractor.Extract(sms, this.parentParameters);
		}
Exemple #2
0
 internal SqlClientQuery(SqlSubSelect subquery)
     : base(SqlNodeType.ClientQuery, subquery.ClrType, subquery.SqlType, subquery.SourceExpression)
 {
     this.query      = subquery;
     this.arguments  = new List <SqlExpression>();
     this.parameters = new List <SqlParameter>();
 }
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			SqlScope save = this.CurrentScope;
			this.CurrentScope = new SqlScope(null, this.CurrentScope);
			base.VisitSubSelect(ss);
			this.CurrentScope = save;
			return ss;
		}
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			List<SqlOrderExpression> save = this.orders;
			this.orders = new List<SqlOrderExpression>();
			base.VisitSubSelect(ss);
			this.orders = save;
			return ss;
		}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			base.VisitScalarSubSelect(ss);
			if(ss.Select.Row.Columns.Count > 0)
			{
				Diagnostics.Debug.Assert(ss != null && ss.Select != null && ss.Select.Row != null && ss.Select.Row.Columns.Count == 1);
				// make sure these scalar subselects don't get redundantly named
				ss.Select.Row.Columns[0].Name = "";
			}
			return ss;
		}
		internal static SqlClientQuery Extract(SqlSubSelect subquery, IEnumerable<SqlParameter> parentParameters)
		{
			SqlClientQuery cq = new SqlClientQuery(subquery);
			if(parentParameters != null)
			{
				cq.Parameters.AddRange(parentParameters);
			}
			SubSelectDuplicator v = new SubSelectDuplicator(cq.Arguments, cq.Parameters);
			cq.Query = (SqlSubSelect)v.Visit(subquery);
			return cq;
		}
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			bool saveIsTopLevel = _isTopLevel;
			try
			{
				return base.VisitSubSelect(ss);
			}
			finally
			{
				_isTopLevel = saveIsTopLevel;
			}
		}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			SqlSelect innerSelect = this.VisitSelect(ss.Select);
			if(!this.aggregateChecker.HasAggregates(innerSelect))
			{
				innerSelect.Top = this.sql.ValueFromObject(1, ss.SourceExpression);
			}
			innerSelect.OrderingType = SqlOrderingType.Blocked;
			SqlAlias alias = new SqlAlias(innerSelect);
			this.currentSelect.From = new SqlJoin(SqlJoinType.OuterApply, this.currentSelect.From, alias, null, ss.SourceExpression);
			return new SqlColumnRef(innerSelect.Row.Columns[0]);
		}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			bool saveIsTopLevel = this.isTopLevel;
			this.isTopLevel = false;
			bool saveCanJoin = this.canJoin;
			this.canJoin = false;
			try
			{
				return base.VisitScalarSubSelect(ss);
			}
			finally
			{
				this.isTopLevel = saveIsTopLevel;
				this.canJoin = saveCanJoin;
			}
		}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			bool saveIsTopLevel = _isTopLevel;
			_isTopLevel = false;
			bool saveForceReferenceAll = _forceReferenceAll;
			_forceReferenceAll = true;
			try
			{
				return base.VisitScalarSubSelect(ss);
			}
			finally
			{
				_isTopLevel = saveIsTopLevel;
				_forceReferenceAll = saveForceReferenceAll;
			}
		}
		internal override SqlExpression VisitMultiset(SqlSubSelect sms)
		{
			if(!_isDebugMode)
			{
				throw Error.InvalidFormatNode("Multiset");
			}
			int saveDepth = _depth;
			_depth++;
			_commandStringBuilder.Append("MULTISET(");
			this.NewLine();
			this.Visit(sms.Select);
			this.NewLine();
			_commandStringBuilder.Append(")");
			_depth = saveDepth;
			return sms;
		}
		internal override SqlExpression VisitElement(SqlSubSelect elem)
		{
			if(!_isDebugMode)
			{
				throw Error.InvalidFormatNode("Element");
			}
			int saveDepth = _depth;
			_depth++;
			_commandStringBuilder.Append("ELEMENT(");
			this.NewLine();
			this.Visit(elem.Select);
			this.NewLine();
			_commandStringBuilder.Append(")");
			_depth = saveDepth;
			return elem;
		}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			int saveDepth = _depth;
			_depth++;
			if(_isDebugMode)
			{
				_commandStringBuilder.Append("SCALAR");
			}
			_commandStringBuilder.Append("(");
			this.NewLine();
			this.Visit(ss.Select);
			this.NewLine();
			_commandStringBuilder.Append(")");
			_depth = saveDepth;
			return ss;
		}
		internal SqlClientQuery(SqlSubSelect subquery)
			: base(SqlNodeType.ClientQuery, subquery.ClrType, subquery.SqlType, subquery.SourceExpression) {
			this.query = subquery;
			this.arguments = new List<SqlExpression>();
			this.parameters = new List<SqlParameter>();
			}
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			return (SqlExpression)new SqlDuplicator().Duplicate(ss);
		}
			internal override SqlExpression VisitElement(SqlSubSelect elem)
			{
				return elem;
			}
			internal override SqlExpression VisitMultiset(SqlSubSelect sms)
			{
				return sms;
			}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			base.VisitScalarSubSelect(ss);
			ss.SetSqlType(ss.Select.Selection.SqlType);
			return ss;
		}
		internal override SqlExpression VisitExists(SqlSubSelect sqlExpr)
		{
			int saveDepth = _depth;
			_depth++;
			_commandStringBuilder.Append("EXISTS(");
			this.NewLine();
			this.Visit(sqlExpr.Select);
			this.NewLine();
			_commandStringBuilder.Append(")");
			_depth = saveDepth;
			return sqlExpr;
		}
Exemple #20
0
 internal virtual SqlExpression VisitScalarSubSelect(SqlSubSelect ss) {
     ss.Select = this.VisitSequence(ss.Select);
     return ss;
 }
Exemple #21
0
 internal virtual SqlExpression VisitElement(SqlSubSelect elem) {
     elem.Select = this.VisitSequence(elem.Select);
     return elem;
 }
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			// block where clauses from being lifted out of a sub-query
			Scope save = this.current;
			this.current = null;
			SqlExpression result = base.VisitSubSelect(ss);
			this.current = save;
			return result;
		}
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			// pass control back to parent
			return this.parent.VisitSubSelect(ss);
		}
Exemple #24
0
 internal virtual SqlExpression VisitSubSelect(SqlSubSelect ss) {
     switch(ss.NodeType) {
        case SqlNodeType.ScalarSubSelect: return this.VisitScalarSubSelect(ss);
        case SqlNodeType.Multiset: return this.VisitMultiset(ss);
        case SqlNodeType.Element: return this.VisitElement(ss);
        case SqlNodeType.Exists: return this.VisitExists(ss);
     }
     throw Error.UnexpectedNode(ss.NodeType);
 }
		internal override SqlExpression VisitElement(SqlSubSelect elem)
		{
			return this.VisitMultiset(elem);
		}
Exemple #26
0
 internal virtual SqlExpression VisitMultiset(SqlSubSelect sms) {
     sms.Select = this.VisitSequence(sms.Select);
     return sms;
 }
		internal override SqlExpression VisitExists(SqlSubSelect sqlExpr)
		{
			return new SqlSubSelect(sqlExpr.NodeType, sqlExpr.ClrType, sqlExpr.SqlType, (SqlSelect)this.Visit(sqlExpr.Select));
		}
Exemple #28
0
 internal virtual SqlExpression VisitExists(SqlSubSelect sqlExpr) {
     sqlExpr.Select = this.VisitSequence(sqlExpr.Select);
     return sqlExpr;
 }
		internal override SqlExpression VisitMultiset(SqlSubSelect sms)
		{
			return new SqlSubSelect(sms.NodeType, sms.ClrType, sms.SqlType, (SqlSelect)this.Visit(sms.Select));
		}
		// ignore subquery in selection
		internal override SqlExpression VisitSubSelect(SqlSubSelect ss)
		{
			return ss;
		}
		internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss)
		{
			return new SqlSubSelect(SqlNodeType.ScalarSubSelect, ss.ClrType, ss.SqlType, this.VisitSequence(ss.Select));
		}