Skip to content

Commit 18f88e9

Browse files
author
爪子
committed
fix: pass right side as DB::table(...) block to union/unionAll and append orderBy correctly
1 parent 7d4cd60 commit 18f88e9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/converter.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ export class Converter
4848
// left_sql currently may start with "DB::table('...')" or "->from('...')" when nested; ensure proper prefix
4949
let base = (new Converter(left.Query || left, this).run(false));
5050

51-
// Build union call: pass the right query builder expression as a DB::raw of the SQL from right_converter without trailing get();
52-
// Prefer passing DB::table(...) closure to preserve query builder: use function($query) { $query->from(...)->... }
53-
let right_closure = 'function ($query) {\n\t' + right_sql.replace('DB::table', '$query->from').split('\n').join('\n\t') + ';\n}';
51+
// Build union call: pass the right query builder expression directly (as DB::table(...)->...)
52+
let right_block = right_sql;
5453

55-
let union_section = base + '\n->' + union_method + '(' + right_closure + ')';
54+
// Indent right block and wrap in parentheses as argument to union/unionAll
55+
let union_section = base + '\n->' + union_method + '(\n' + addTabToEveryLine(right_block, 1) + '\n)';
5656

5757
// If top-level has order_by / limit / offset, append them
5858
if (propertyExistsInObjectAndNotNull(this.ast, 'order_by') && this.ast.order_by.length > 0) {
5959
union_section = union_section + '\n->' + (new Converter(this.ast, this).resolveOrderBySection());
60-
} else if (propertyExistsInObjectAndNotNull(this.ast, 'order_by') && this.ast.order_by.length === 0 && propertyExistsInObjectAndNotNull(this.ast.body, 'order_by') && this.ast.body.order_by.length > 0) {
60+
} else if (propertyExistsInObjectAndNotNull(this.ast.body, 'order_by') && this.ast.body.order_by.length > 0) {
6161
union_section = union_section + '\n->' + (new Converter(this.ast.body, this).resolveOrderBySection());
6262
}
6363

0 commit comments

Comments
 (0)