Skip to content

Commit

Permalink
chore: fix test and fix the logic around computing Value from SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Sep 9, 2023
1 parent a37100a commit 1adecc6
Show file tree
Hide file tree
Showing 24 changed files with 891 additions and 327 deletions.
8 changes: 4 additions & 4 deletions src/parser/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ pub fn get_sql_from_expr<'a>(
Expr::Assign(assign) => {
let right_expr = &assign.right;
get_sql_from_expr(sqls, var_decl_name, right_expr, span, import_alias);

let left_expr = &assign.left;
left_expr.as_expr().map(|expr| {
get_sql_from_expr(sqls, var_decl_name, &expr, span, import_alias)
});
left_expr
.as_expr()
.map(|expr| get_sql_from_expr(sqls, var_decl_name, &expr, span, import_alias));
}
Expr::Member(member) => {
let obj = &member.obj;
Expand Down
30 changes: 15 additions & 15 deletions src/ts_generator/sql_parser/expressions/translate_data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use crate::{
use sqlparser::ast::DataType;
use sqlparser::ast::Value;

pub fn translate_value(value: &Value) -> TsFieldType {
pub fn translate_value(value: &Value) -> Option<TsFieldType> {
match &value {
Value::Number(_, _) => TsFieldType::Number,
Value::SingleQuotedString(_) => TsFieldType::String,
Value::DollarQuotedString(_) => TsFieldType::String,
Value::EscapedStringLiteral(_) => TsFieldType::String,
Value::SingleQuotedByteStringLiteral(_) => TsFieldType::String,
Value::DoubleQuotedByteStringLiteral(_) => TsFieldType::String,
Value::RawStringLiteral(_) => TsFieldType::String,
Value::NationalStringLiteral(_) => TsFieldType::String,
Value::HexStringLiteral(_) => TsFieldType::String,
Value::DoubleQuotedString(_) => TsFieldType::String,
Value::Boolean(_) => TsFieldType::Boolean,
Value::Null => TsFieldType::Null,
Value::Placeholder(_) => TsFieldType::Any,
Value::UnQuotedString(_) => TsFieldType::String,
Value::Number(_, _) => Some(TsFieldType::Number),
Value::SingleQuotedString(_) => Some(TsFieldType::String),
Value::DollarQuotedString(_) => Some(TsFieldType::String),
Value::EscapedStringLiteral(_) => Some(TsFieldType::String),
Value::SingleQuotedByteStringLiteral(_) => Some(TsFieldType::String),
Value::DoubleQuotedByteStringLiteral(_) => Some(TsFieldType::String),
Value::RawStringLiteral(_) => Some(TsFieldType::String),
Value::NationalStringLiteral(_) => Some(TsFieldType::String),
Value::HexStringLiteral(_) => Some(TsFieldType::String),
Value::DoubleQuotedString(_) => Some(TsFieldType::String),
Value::Boolean(_) => Some(TsFieldType::Boolean),
Value::Null => Some(TsFieldType::Null),
Value::UnQuotedString(_) => Some(TsFieldType::String),
Value::Placeholder(_) => None,
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/ts_generator/sql_parser/expressions/translate_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,13 @@ pub fn translate_expr(
is_selection,
),
Expr::Value(placeholder) => {
if placeholder.to_string() == "?" {
ts_query.insert_param(&TsFieldType::Boolean, &Some("?".to_string()))
} else {
let ts_field_type = translate_value(&placeholder);
ts_query.insert_result(alias, &[ts_field_type], is_selection, expr_for_logging)
let ts_field_type = translate_value(&placeholder);

if let Some(ts_field_type) = ts_field_type {
return ts_query.insert_result(alias, &[ts_field_type], is_selection, expr_for_logging);
}
},
ts_query.insert_param(&TsFieldType::Boolean, &Some(placeholder.to_string()))
}
Expr::JsonAccess {
left: _,
operator: _,
Expand Down
16 changes: 16 additions & 0 deletions tests/demo/typescript/expression/array.snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


export type ArrayQueryParams = [];


export interface IArrayQueryResult {
id: number;
};


export interface IArrayQueryQuery {
params: ArrayQueryParams;
result: IArrayQueryResult;
};


48 changes: 48 additions & 0 deletions tests/demo/typescript/expression/arrow-function.snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@


export type PlainArrowFunctionParams = [];


export interface IPlainArrowFunctionResult {
id: number;
};


export interface IPlainArrowFunctionQuery {
params: PlainArrowFunctionParams;
result: IPlainArrowFunctionResult;
};




export type NameOverriddenArrowFuncParams = [];


export interface INameOverriddenArrowFuncResult {
id: number;
};


export interface INameOverriddenArrowFuncQuery {
params: NameOverriddenArrowFuncParams;
result: INameOverriddenArrowFuncResult;
};




export type ArrowFunctionParams = [];


export interface IArrowFunctionResult {
id: number;
};


export interface IArrowFunctionQuery {
params: ArrowFunctionParams;
result: IArrowFunctionResult;
};


256 changes: 256 additions & 0 deletions tests/demo/typescript/expression/assignment.snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@


export type AssignmentParams = [];


export interface IAssignmentResult {
id: number;
};


export interface IAssignmentQuery {
params: AssignmentParams;
result: IAssignmentResult;
};




export type PlusEqualQueryParams = [];


export interface IPlusEqualQueryResult {
id: number;
};


export interface IPlusEqualQueryQuery {
params: PlusEqualQueryParams;
result: IPlusEqualQueryResult;
};




export type MinusEqualQueryParams = [];


export interface IMinusEqualQueryResult {
id: number;
};


export interface IMinusEqualQueryQuery {
params: MinusEqualQueryParams;
result: IMinusEqualQueryResult;
};




export type MultiplyEqualQueryParams = [];


export interface IMultiplyEqualQueryResult {
id: number;
};


export interface IMultiplyEqualQueryQuery {
params: MultiplyEqualQueryParams;
result: IMultiplyEqualQueryResult;
};




export type MulMulEqualQueryParams = [];


export interface IMulMulEqualQueryResult {
id: number;
};


export interface IMulMulEqualQueryQuery {
params: MulMulEqualQueryParams;
result: IMulMulEqualQueryResult;
};




export type SlashEqualQueryParams = [];


export interface ISlashEqualQueryResult {
id: number;
};


export interface ISlashEqualQueryQuery {
params: SlashEqualQueryParams;
result: ISlashEqualQueryResult;
};




export type PercentEqualQueryParams = [];


export interface IPercentEqualQueryResult {
id: number;
};


export interface IPercentEqualQueryQuery {
params: PercentEqualQueryParams;
result: IPercentEqualQueryResult;
};




export type LessLessEqualQueryParams = [];


export interface ILessLessEqualQueryResult {
id: number;
};


export interface ILessLessEqualQueryQuery {
params: LessLessEqualQueryParams;
result: ILessLessEqualQueryResult;
};




export type GreaterGreaterEqualQueryParams = [];


export interface IGreaterGreaterEqualQueryResult {
id: number;
};


export interface IGreaterGreaterEqualQueryQuery {
params: GreaterGreaterEqualQueryParams;
result: IGreaterGreaterEqualQueryResult;
};




export type GreaterGreaterGreaterEqualQueryParams = [];


export interface IGreaterGreaterGreaterEqualQueryResult {
id: number;
};


export interface IGreaterGreaterGreaterEqualQueryQuery {
params: GreaterGreaterGreaterEqualQueryParams;
result: IGreaterGreaterGreaterEqualQueryResult;
};




export type AmpersandEqualQueryParams = [];


export interface IAmpersandEqualQueryResult {
id: number;
};


export interface IAmpersandEqualQueryQuery {
params: AmpersandEqualQueryParams;
result: IAmpersandEqualQueryResult;
};




export type AmpersandAmpersandEqualQueryParams = [];


export interface IAmpersandAmpersandEqualQueryResult {
id: number;
};


export interface IAmpersandAmpersandEqualQueryQuery {
params: AmpersandAmpersandEqualQueryParams;
result: IAmpersandAmpersandEqualQueryResult;
};




export type BarEqualQueryParams = [];


export interface IBarEqualQueryResult {
id: number;
};


export interface IBarEqualQueryQuery {
params: BarEqualQueryParams;
result: IBarEqualQueryResult;
};




export type BarBarEqualQueryParams = [];


export interface IBarBarEqualQueryResult {
id: number;
};


export interface IBarBarEqualQueryQuery {
params: BarBarEqualQueryParams;
result: IBarBarEqualQueryResult;
};




export type QuestionQuestionEqualQueryParams = [];


export interface IQuestionQuestionEqualQueryResult {
id: number;
};


export interface IQuestionQuestionEqualQueryQuery {
params: QuestionQuestionEqualQueryParams;
result: IQuestionQuestionEqualQueryResult;
};




export type CaretEqualQueryParams = [];


export interface ICaretEqualQueryResult {
id: number;
};


export interface ICaretEqualQueryQuery {
params: CaretEqualQueryParams;
result: ICaretEqualQueryResult;
};


Loading

0 comments on commit 1adecc6

Please sign in to comment.