protected override void Dispose(bool disposing) { if (_defaultDatabaseName != null) { _defaultDatabaseName.Dispose(); _defaultDatabaseName = null; } base.Dispose(disposing); }
private void LoadQueryWithDerivedTableAndCte() { UnionSubQuery unionSubQuery = _query.QueryRoot.FirstSelect(); // Derived Table SQLFromQuery fq = new SQLFromQuery(_query.SQLContext) { Alias = new SQLAliasObjectAlias(_query.SQLContext) { Alias = _query.QueryRoot.CreateUniqueSubQueryName() }, SubQuery = new SQLSubSelectStatement(_query.SQLContext) }; var sqse = new SQLSubQuerySelectExpression(_query.SQLContext); fq.SubQuery.Add(sqse); sqse.SelectItems = new SQLSelectItems(_query.SQLContext); sqse.From = new SQLFromClause(_query.SQLContext); var dataSourceQuery = (DataSourceQuery)_query.AddObject(unionSubQuery, fq, typeof(DataSourceQuery)); var usc = dataSourceQuery.SubQuery.FirstSelect(); var dsDerivedTable = _query.AddObject(usc, "MyDB.MySchema.SalesOrderHeader"); var ciDerivedTable1 = usc.QueryColumnList.AddField(dsDerivedTable, "OrderDate"); ciDerivedTable1.Selected = true; var ciDerivedTable2 = usc.QueryColumnList.AddField(dsDerivedTable, "SalesOrderID"); ciDerivedTable2.ConditionStrings[0] = "> 25"; // CTE var qn = new SQLQualifiedName(_query.SQLContext); if (!unionSubQuery.QueryRoot.SQLContext.SyntaxProvider.IsSupportCTE()) { return; } DataSource dataSourceCte; try { var withClauseItemName = _query.QueryRoot.CreateUniqueCTEName("CTE"); qn.Add(withClauseItemName); var parentSubQuery = unionSubQuery.ParentSubQuery ?? unionSubQuery.QueryRoot; if (parentSubQuery.IsMainQuery) { _query.QueryRoot.AddNewCTE(null, withClauseItemName); } else { if (parentSubQuery.IsSubQueryCTE()) { var index = parentSubQuery.GetSubQueryCTEIndex(); parentSubQuery.InsertNewCTE(index, null, withClauseItemName); } else { parentSubQuery.AddNewCTE(null, withClauseItemName); } } if (_query.IsUniqueAlias(unionSubQuery, withClauseItemName)) { dataSourceCte = _query.AddObject(unionSubQuery, qn, null); } else { var withClauseItemNameStr = withClauseItemName.GetSQL(new SQLGenerationOptions()); using (var alias = _query.CreateUniqueAlias(unionSubQuery, withClauseItemNameStr)) dataSourceCte = _query.AddObject(unionSubQuery, qn, alias); } } finally { qn.Dispose(); } var cte = _query.QueryRoot.GetSubQueryCTEList().FirstOrDefault(); if (cte == null) { return; } var unionSubQueryCte = cte.FirstSelect(); var ds1 = _query.AddObject(unionSubQueryCte, "MyDB.MySchema.Customers"); // create output column QueryColumnListItem ci1 = unionSubQueryCte.QueryColumnList.AddField(ds1, "CustomerName"); ci1.Selected = true; // create output column QueryColumnListItem ci2 = unionSubQuery.QueryColumnList.AddField(dataSourceCte, "CustomerName"); ci2.Selected = true; }