Skip to content

Commit

Permalink
[mysql] implement withRecursive
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-ptr committed Oct 6, 2023
1 parent 14ea114 commit e62c43e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions drizzle-orm/src/mysql-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ export class MySqlDatabase<
}
}

$withRecursive<TAlias extends string>(alias: TAlias) {
return {
as<TSelection extends ColumnsSelection>(
qb: TypedQueryBuilder<TSelection> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection>),
): WithSubqueryWithSelection<TSelection, TAlias, 'mysql'> {
if (typeof qb === 'function') {
qb = qb(new QueryBuilder());
}

return new Proxy(
new WithSubquery(qb.getSQL(), qb.getSelectedFields() as SelectedFields, alias, true, true),
new SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),
) as WithSubqueryWithSelection<TSelection, TAlias, 'mysql'>;
},
};
}

$with<TAlias extends string>(alias: TAlias) {
return {
as<TSelection extends ColumnsSelection>(
Expand Down
4 changes: 4 additions & 0 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ export class MySqlDialect {
let withSql: SQL | undefined;
if (withList?.length) {
const withSqlChunks = [sql`with `];
if (withList[0] && withList[0][SubqueryConfig].isRecursive) {
withSqlChunks.push(sql`recursive `)
}

for (const [i, w] of withList.entries()) {
withSqlChunks.push(sql`${sql.identifier(w[SubqueryConfig].alias)} as (${w[SubqueryConfig].sql})`);
if (i < withList.length - 1) {
Expand Down

0 comments on commit e62c43e

Please sign in to comment.