Skip to content

Commit c8094fe

Browse files
authored
Merge pull request #39 from sql2builder/feat/issue-30-tests
Feat/issue 30 tests
2 parents f54cab2 + 2a2ceaa commit c8094fe

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
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

tests/converter.test.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {Converter} from "../src/converter";
2-
import {test, expect} from "@jest/globals";
3-
import {complex_ast} from './ast';
4-
import {cross_join_ast} from './crossjoin';
5-
1+
const {Converter} = require('../src/converter');
2+
const {test, expect} = require('@jest/globals');
3+
const {complex_ast} = require('./ast');
4+
const {cross_join_ast} = require('./crossjoin');
65

76
function getQueryBuilder(ast) {
87
return (new Converter(ast[0].Query).run());
@@ -14,12 +13,12 @@ test('complex sql', () => {
1413
->leftJoin('comments','comments.post_id','=','posts.id')
1514
->rightJoin('users','user.id','=','posts.user_id')
1615
->leftJoin(DB::raw("DB::table('address')
17-
\t->select('*')") as a), function($join) {
18-
\t$join->on('user.aid','=','a.id');
16+
->select('*')") as a), function($join) {
17+
$join->on('user.aid','=','a.id');
1918
}
2019
->where(function ($query) {
21-
\t$query->where('a.name','=','bejing')
22-
\t\t->where('a.id','<',10);
20+
$query->where('a.name','=','bejing')
21+
->where('a.id','<',10);
2322
})
2423
->where('comments.conent','=','abc')
2524
->orderBy('comments.created_at','asc')
@@ -30,9 +29,9 @@ test('complex sql', () => {
3029
test('cross join', () => {
3130
expect(getQueryBuilder(cross_join_ast)).toBe(`DB::table('posts')
3231
->crossJoinSub(function ($query) {
33-
\t$query->from('posts')
34-
\t\t->select('count', DB::raw("'max'(created_date) as created_date"))
35-
\t\t->groupBy('count');
32+
$query->from('posts')
33+
->select('count', DB::raw("'max'(created_date) as created_date"))
34+
->groupBy('count');
3635
},'max_counts')
3736
->select('posts.*')
3837
->where('posts.count','=',DB::raw('max_counts.count'))

tests/union.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const wasm = require('sqlparser-rs-wasm');
2+
const Converter = require('../src/converter');
3+
4+
test('union all for users where null', () => {
5+
const sql = "(select * from `users` where `last_name` is null) union all (select * from `users` where `first_name` is null) order by id";
6+
const ast = wasm.parse_sql('--mysql', sql);
7+
const conv = new Converter(ast, null);
8+
const out = conv.run(true);
9+
10+
const expected = `DB::table('users')\n->whereNull('last_name')\n->unionAll(\n DB::table('users')\n ->whereNull('first_name')\n)\n->orderBy('id')\n->get();`;
11+
12+
expect(out.replace(/\s+$/g, '')).toBe(expected);
13+
});

0 commit comments

Comments
 (0)