Skip to content

Commit c5dfd41

Browse files
authored
process object var declaration and var declarations without a name (#61)
* wip * chore: skip adding sqls from var decl if we already found new sqls * test: update snapshot test * chore: snapshot * chore: update versions to 0.3.0 * chore: remove unused comment
1 parent bc9620d commit c5dfd41

File tree

9 files changed

+191
-12
lines changed

9 files changed

+191
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlx-ts"
3-
version = "0.2.2"
3+
version = "0.3.0"
44
edition = "2021"
55
homepage = "https://github.com/JasonShin/sqlx-ts"
66
authors = ['Jason Shin <[email protected]>']

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlx-ts",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "sqlx-ts ensures your raw SQLs are compile-time checked",
55
"main": "dist/index.js",
66
"maintainers": [

src/parser/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,21 @@ fn recurse_and_find_sql(mut sqls: &mut Vec<SQL>, stmt: &Stmt, import_alias: &Str
151151
Decl::Var(var) => {
152152
for var_decl in &var.decls {
153153
let span: MultiSpan = var.span.into();
154-
let new_sqls = get_sql_from_var_decl(var_decl, span, import_alias);
154+
let new_sqls = get_sql_from_var_decl(var_decl, &span, import_alias);
155+
let num_new_sqls = new_sqls.len();
155156
sqls.extend(new_sqls);
157+
158+
// We've already found the sqls based on the variable name, we should skip processing further
159+
if num_new_sqls > 0 {
160+
continue;
161+
}
162+
163+
// this is when the variable name is not found due to syntax like
164+
// const [rows, i] = await connection.execute....
165+
if let Some(init) = &var_decl.init {
166+
let expr = *init.clone();
167+
get_sql_from_expr(&mut sqls, &None, &expr, &span, import_alias);
168+
}
156169
}
157170
}
158171
Decl::TsInterface(_) => {}

src/parser/tag.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ pub fn process_block_stmt_as_expr(
1717
if let Some(body) = block_stmt {
1818
for stmt in &body.stmts {
1919
let expr = stmt.as_expr();
20-
let decl = stmt.as_decl();
2120
if let Some(expr) = expr {
2221
let expr = &expr.expr;
2322
get_sql_from_expr(sqls, var_decl_name, expr, span, import_alias);
2423
}
25-
if let Some(decl) = decl {
26-
recurse_and_find_sql(sqls, stmt, import_alias);
27-
}
24+
25+
let _ = recurse_and_find_sql(sqls, stmt, import_alias);
2826
}
2927
}
3028
}
@@ -179,7 +177,6 @@ pub fn get_sql_from_expr<'a>(
179177
Expr::Arrow(arrow) => {
180178
let expr = &arrow.clone().body.expr();
181179
let block_stmt = &arrow.clone().body.block_stmt();
182-
183180
process_block_stmt_as_expr(&block_stmt, sqls, var_decl_name, span, import_alias);
184181

185182
if let Some(expr) = expr {
@@ -262,7 +259,7 @@ pub fn get_sql_from_expr<'a>(
262259

263260
/// you would normally pass in any var declarator such as
264261
/// const sql = sql`SELECT * FROM xxx;`
265-
pub fn get_sql_from_var_decl(var_declarator: &VarDeclarator, span: MultiSpan, import_alias: &String) -> Vec<SQL> {
262+
pub fn get_sql_from_var_decl(var_declarator: &VarDeclarator, span: &MultiSpan, import_alias: &String) -> Vec<SQL> {
266263
let mut bag_of_sqls: Vec<SQL> = vec![];
267264
let var_decl_name = get_var_decl_name(var_declarator);
268265

src/ts_generator/generator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub fn get_query_name(sql: &SQL) -> Result<String> {
4444
}
4545

4646
let var_decl_name = var_decl_name.clone();
47-
4847
if let Some(var_decl_name) = var_decl_name {
4948
return Ok(var_decl_name.to_case(Case::Pascal));
5049
}

tests/demo/ts-syntax/ts-syntax.queries.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11

22

3+
export type TestQueryWithoutVariableDeclarationParams = [];
4+
5+
6+
export interface ITestQueryWithoutVariableDeclarationResult {
7+
food_type: string;
8+
id: number;
9+
points: number;
10+
table_id: number;
11+
time_takes_to_cook: number;
12+
};
13+
14+
15+
export interface ITestQueryWithoutVariableDeclarationQuery {
16+
params: TestQueryWithoutVariableDeclarationParams;
17+
result: ITestQueryWithoutVariableDeclarationResult;
18+
};
19+
20+
21+
22+
323
export type TestQuery1Params = [];
424

525

@@ -750,6 +770,46 @@ export interface ITestAwaitQueryQuery {
750770

751771

752772

773+
export type TestAwaitQueryParams = [];
774+
775+
776+
export interface ITestAwaitQueryResult {
777+
food_type: string;
778+
id: number;
779+
points: number;
780+
table_id: number;
781+
time_takes_to_cook: number;
782+
};
783+
784+
785+
export interface ITestAwaitQueryQuery {
786+
params: TestAwaitQueryParams;
787+
result: ITestAwaitQueryResult;
788+
};
789+
790+
791+
792+
793+
export type TestAwaitQuery2Params = [];
794+
795+
796+
export interface ITestAwaitQuery2Result {
797+
food_type: string;
798+
id: number;
799+
points: number;
800+
table_id: number;
801+
time_takes_to_cook: number;
802+
};
803+
804+
805+
export interface ITestAwaitQuery2Query {
806+
params: TestAwaitQuery2Params;
807+
result: ITestAwaitQuery2Result;
808+
};
809+
810+
811+
812+
753813
export type TestAwaitQuery2Params = [];
754814

755815

@@ -787,3 +847,23 @@ export interface IAwaitClientQueryQuery {
787847
result: IAwaitClientQueryResult;
788848
};
789849

850+
851+
852+
853+
export type GetItemsWithRowsParams = [];
854+
855+
856+
export interface IGetItemsWithRowsResult {
857+
food_type: string;
858+
id: number;
859+
points: number;
860+
table_id: number;
861+
time_takes_to_cook: number;
862+
};
863+
864+
865+
export interface IGetItemsWithRowsQuery {
866+
params: GetItemsWithRowsParams;
867+
result: IGetItemsWithRowsResult;
868+
};
869+

tests/demo/ts-syntax/ts-syntax.snapshot.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11

22

3+
export type TestQueryWithoutVariableDeclarationParams = [];
4+
5+
6+
export interface ITestQueryWithoutVariableDeclarationResult {
7+
food_type: string;
8+
id: number;
9+
points: number;
10+
table_id: number;
11+
time_takes_to_cook: number;
12+
};
13+
14+
15+
export interface ITestQueryWithoutVariableDeclarationQuery {
16+
params: TestQueryWithoutVariableDeclarationParams;
17+
result: ITestQueryWithoutVariableDeclarationResult;
18+
};
19+
20+
21+
22+
323
export type TestQuery1Params = [];
424

525

@@ -750,6 +770,46 @@ export interface ITestAwaitQueryQuery {
750770

751771

752772

773+
export type TestAwaitQueryParams = [];
774+
775+
776+
export interface ITestAwaitQueryResult {
777+
food_type: string;
778+
id: number;
779+
points: number;
780+
table_id: number;
781+
time_takes_to_cook: number;
782+
};
783+
784+
785+
export interface ITestAwaitQueryQuery {
786+
params: TestAwaitQueryParams;
787+
result: ITestAwaitQueryResult;
788+
};
789+
790+
791+
792+
793+
export type TestAwaitQuery2Params = [];
794+
795+
796+
export interface ITestAwaitQuery2Result {
797+
food_type: string;
798+
id: number;
799+
points: number;
800+
table_id: number;
801+
time_takes_to_cook: number;
802+
};
803+
804+
805+
export interface ITestAwaitQuery2Query {
806+
params: TestAwaitQuery2Params;
807+
result: ITestAwaitQuery2Result;
808+
};
809+
810+
811+
812+
753813
export type TestAwaitQuery2Params = [];
754814

755815

@@ -788,3 +848,23 @@ export interface IAwaitClientQueryQuery {
788848
};
789849

790850

851+
852+
853+
export type GetItemsWithRowsParams = [];
854+
855+
856+
export interface IGetItemsWithRowsResult {
857+
food_type: string;
858+
id: number;
859+
points: number;
860+
table_id: number;
861+
time_takes_to_cook: number;
862+
};
863+
864+
865+
export interface IGetItemsWithRowsQuery {
866+
params: GetItemsWithRowsParams;
867+
result: IGetItemsWithRowsResult;
868+
};
869+
870+

tests/demo/ts-syntax/ts-syntax.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { sql } from 'sqlx-ts'
22
import { QueryTypes, Sequelize } from 'sequelize'
33

44
// Array expression with sql, it should skip generating the type as we cannot figure out the name to use
5-
const [] = sql`SELECT * FROM items`
5+
const [] = sql`
6+
-- @name: testQueryWithoutVariableDeclaration
7+
SELECT * FROM items
8+
`
69

710
// Expression without variable declaration
811
sql`
@@ -289,7 +292,14 @@ function *yieldMethod() {
289292
-- @name: testAwaitQuery2
290293
SELECT * FROM items
291294
`
295+
292296
const awaitClientQuery = await client.query(sql`
293297
SELECT * FROM items;
294298
`)
299+
300+
const [rows, i] = await connection.execute<Rows<IGetItems2Result>>(sql`
301+
-- @name: getItemsWithRows
302+
SELECT * FROM items
303+
`)
304+
295305
})();

0 commit comments

Comments
 (0)