-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b9c067
commit d1846b5
Showing
3 changed files
with
102 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use model_migration::WithQuery; | ||
use sea_orm::sea_query::{ | ||
Alias, CommonTableExpression, Expr, Query, SelectStatement, UnionType, WithClause, | ||
}; | ||
use sea_orm::{ColumnTrait, JoinType, Order}; | ||
|
||
use crate::model_v2::object_dependency; | ||
use crate::model_v2::prelude::*; | ||
|
||
/// This function will construct a query using recursive cte to find all objects that are used by the given object. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use risingwave_meta::controller::utils::construct_obj_dependency_query; | ||
/// use sea_orm::sea_query::*; | ||
/// use sea_orm::*; | ||
/// | ||
/// let query = construct_obj_dependency_query(1, "used_by"); | ||
/// | ||
/// assert_eq!( | ||
/// query.to_string(MysqlQueryBuilder), | ||
/// r#"WITH RECURSIVE `used_by_object_ids` (`used_by`) AS (SELECT `used_by` FROM `object_dependency` WHERE `object_dependency`.`oid` = 1 UNION ALL (SELECT `object_dependency`.`used_by` FROM `object_dependency` INNER JOIN `used_by_object_ids` ON `used_by_object_ids`.`used_by` = `oid`)) SELECT DISTINCT `used_by` FROM `used_by_object_ids` ORDER BY `used_by` DESC"# | ||
/// ); | ||
/// assert_eq!( | ||
/// query.to_string(PostgresQueryBuilder), | ||
/// r#"WITH RECURSIVE "used_by_object_ids" ("used_by") AS (SELECT "used_by" FROM "object_dependency" WHERE "object_dependency"."oid" = 1 UNION ALL (SELECT "object_dependency"."used_by" FROM "object_dependency" INNER JOIN "used_by_object_ids" ON "used_by_object_ids"."used_by" = "oid")) SELECT DISTINCT "used_by" FROM "used_by_object_ids" ORDER BY "used_by" DESC"# | ||
/// ); | ||
/// assert_eq!( | ||
/// query.to_string(SqliteQueryBuilder), | ||
/// r#"WITH RECURSIVE "used_by_object_ids" ("used_by") AS (SELECT "used_by" FROM "object_dependency" WHERE "object_dependency"."oid" = 1 UNION ALL SELECT "object_dependency"."used_by" FROM "object_dependency" INNER JOIN "used_by_object_ids" ON "used_by_object_ids"."used_by" = "oid") SELECT DISTINCT "used_by" FROM "used_by_object_ids" ORDER BY "used_by" DESC"# | ||
/// ); | ||
/// ``` | ||
pub fn construct_obj_dependency_query(obj_id: i32, column: &str) -> WithQuery { | ||
let cte_alias = Alias::new("used_by_object_ids"); | ||
let cte_return_alias = Alias::new(column); | ||
|
||
let base_query = SelectStatement::new() | ||
.column(object_dependency::Column::UsedBy) | ||
.from(ObjectDependency) | ||
.and_where(object_dependency::Column::Oid.eq(obj_id)) | ||
.to_owned(); | ||
|
||
let cte_referencing = Query::select() | ||
.column((ObjectDependency, object_dependency::Column::UsedBy)) | ||
.from(ObjectDependency) | ||
.join( | ||
JoinType::InnerJoin, | ||
cte_alias.clone(), | ||
Expr::col((cte_alias.clone(), cte_return_alias.clone())) | ||
.equals(object_dependency::Column::Oid), | ||
) | ||
.to_owned(); | ||
|
||
let common_table_expr = CommonTableExpression::new() | ||
.query( | ||
base_query | ||
.clone() | ||
.union(UnionType::All, cte_referencing) | ||
.to_owned(), | ||
) | ||
.column(cte_return_alias.clone()) | ||
.table_name(cte_alias.clone()) | ||
.to_owned(); | ||
|
||
SelectStatement::new() | ||
.distinct() | ||
.column(cte_return_alias.clone()) | ||
.from(cte_alias) | ||
.order_by(cte_return_alias.clone(), Order::Desc) | ||
.to_owned() | ||
.with( | ||
WithClause::new() | ||
.recursive(true) | ||
.cte(common_table_expr) | ||
.to_owned(), | ||
) | ||
.to_owned() | ||
} |