Skip to content

Commit 13c1d89

Browse files
authored
Add MySQL null typing option for insertion parameters (#255)
Add `null` typing option for insertion parameters fixes #254
1 parent f273e38 commit 13c1d89

5 files changed

Lines changed: 22 additions & 9 deletions

File tree

src/ts_generator/sql_parser/translate_insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub async fn translate_insert(
7070

7171
if value.to_string() == "?" {
7272
// If the placeholder is `'?'`, we can process it using insert_value_params and generate nested params type
73-
ts_query.insert_value_params(&field.field_type, &(row, column), &placeholder);
73+
ts_query.insert_value_params(&field.field_type, &(row, column), field.is_nullable, &placeholder);
7474
} else {
7575
ts_query.insert_param(&field.field_type, &field.is_nullable, &placeholder)?;
7676
}

src/ts_generator/types/ts_query.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ pub struct TsQuery {
208208
pub annotated_params: BTreeMap<usize, TsFieldType>,
209209

210210
// We use BTreeMap here as it's a collection that's already sorted
211-
pub insert_params: BTreeMap<usize, BTreeMap<usize, TsFieldType>>,
211+
pub insert_params: BTreeMap<usize, BTreeMap<usize, Vec<TsFieldType>>>,
212212
// Holds any annotated @param and perform replacement when generated TS types
213-
pub annotated_insert_params: BTreeMap<usize, BTreeMap<usize, TsFieldType>>,
213+
pub annotated_insert_params: BTreeMap<usize, BTreeMap<usize, Vec<TsFieldType>>>,
214214

215215
pub result: HashMap<String, Vec<TsFieldType>>,
216216
// Holds any annotated @result and perform replacement when generating TS types
@@ -319,7 +319,13 @@ impl TsQuery {
319319
///
320320
/// e.g.
321321
/// [ [number, string], [number, string] ]
322-
pub fn insert_value_params(&mut self, value: &TsFieldType, point: &(usize, usize), _placeholder: &Option<String>) {
322+
pub fn insert_value_params(
323+
&mut self,
324+
value: &TsFieldType,
325+
point: &(usize, usize),
326+
is_nullable: bool,
327+
_placeholder: &Option<String>,
328+
) {
323329
let (row, column) = point;
324330
let annotated_insert_param = self.annotated_insert_params.get(row);
325331

@@ -334,7 +340,11 @@ impl TsQuery {
334340
row_params = self.insert_params.get_mut(row);
335341
}
336342

337-
row_params.unwrap().insert(*column, value.to_owned());
343+
let mut types = vec![value.to_owned()];
344+
if is_nullable {
345+
types.push(TsFieldType::Null);
346+
}
347+
row_params.unwrap().insert(*column, types);
338348
}
339349
}
340350

@@ -397,7 +407,10 @@ impl TsQuery {
397407
// Process each row and produce Number, String, Boolean
398408
row
399409
.values()
400-
.map(|col| col.to_string())
410+
.map(|col| {
411+
let type_strings = col.iter().map(|t| t.to_string()).collect::<Vec<_>>();
412+
type_strings.join(" | ").to_string()
413+
})
401414
.collect::<Vec<String>>()
402415
.join(", ")
403416
})

tests/demo/typescript/demo.queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface IGetItemsWithRowsQuery {
7373
result: IGetItemsWithRowsResult;
7474
}
7575

76-
export type TestInsertParams = [[number, string, string]];
76+
export type TestInsertParams = [[number, string, string | null]];
7777

7878
export interface ITestInsertResult {
7979

tests/demo/typescript/demo.snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface IGetItemsWithRowsQuery {
7373
result: IGetItemsWithRowsResult;
7474
}
7575

76-
export type TestInsertParams = [[number, string, string]];
76+
export type TestInsertParams = [[number, string, string | null]];
7777

7878
export interface ITestInsertResult {
7979

tests/mysql_insert_query_parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ VALUES
5454

5555
//// Generated TS interfaces ////
5656
r#"
57-
export type SomeInputQueryParams = [[number, string], [string, string]];
57+
export type SomeInputQueryParams = [[number, string], [string | null, string | null]];
5858
5959
export interface ISomeInputQueryResult {
6060

0 commit comments

Comments
 (0)