Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ts_generator/sql_parser/translate_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub async fn translate_insert(

if value.to_string() == "?" {
// If the placeholder is `'?'`, we can process it using insert_value_params and generate nested params type
ts_query.insert_value_params(&field.field_type, &(row, column), &placeholder);
ts_query.insert_value_params(&field.field_type, &(row, column), field.is_nullable, &placeholder);
} else {
ts_query.insert_param(&field.field_type, &field.is_nullable, &placeholder)?;
}
Expand Down
23 changes: 18 additions & 5 deletions src/ts_generator/types/ts_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ pub struct TsQuery {
pub annotated_params: BTreeMap<usize, TsFieldType>,

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

pub result: HashMap<String, Vec<TsFieldType>>,
// Holds any annotated @result and perform replacement when generating TS types
Expand Down Expand Up @@ -319,7 +319,13 @@ impl TsQuery {
///
/// e.g.
/// [ [number, string], [number, string] ]
pub fn insert_value_params(&mut self, value: &TsFieldType, point: &(usize, usize), _placeholder: &Option<String>) {
pub fn insert_value_params(
&mut self,
value: &TsFieldType,
point: &(usize, usize),
is_nullable: bool,
_placeholder: &Option<String>,
) {
let (row, column) = point;
let annotated_insert_param = self.annotated_insert_params.get(row);

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

row_params.unwrap().insert(*column, value.to_owned());
let mut types = vec![value.to_owned()];
if is_nullable {
types.push(TsFieldType::Null);
}
row_params.unwrap().insert(*column, types);
}
}

Expand Down Expand Up @@ -397,7 +407,10 @@ impl TsQuery {
// Process each row and produce Number, String, Boolean
row
.values()
.map(|col| col.to_string())
.map(|col| {
let type_strings = col.iter().map(|t| t.to_string()).collect::<Vec<_>>();
type_strings.join(" | ").to_string()
})
.collect::<Vec<String>>()
.join(", ")
})
Expand Down
2 changes: 1 addition & 1 deletion tests/demo/typescript/demo.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface IGetItemsWithRowsQuery {
result: IGetItemsWithRowsResult;
}

export type TestInsertParams = [[number, string, string]];
export type TestInsertParams = [[number, string, string | null]];

export interface ITestInsertResult {

Expand Down
2 changes: 1 addition & 1 deletion tests/demo/typescript/demo.snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface IGetItemsWithRowsQuery {
result: IGetItemsWithRowsResult;
}

export type TestInsertParams = [[number, string, string]];
export type TestInsertParams = [[number, string, string | null]];

export interface ITestInsertResult {

Expand Down
2 changes: 1 addition & 1 deletion tests/mysql_insert_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ VALUES

//// Generated TS interfaces ////
r#"
export type SomeInputQueryParams = [[number, string], [string, string]];
export type SomeInputQueryParams = [[number, string], [string | null, string | null]];

export interface ISomeInputQueryResult {

Expand Down
Loading