@@ -10,28 +10,32 @@ export const parseQuery = (query: string): ParsedSqlType => {
1010 filters : null ,
1111 } ;
1212
13- const [ command , ...rest ] = query . toLocaleLowerCase ( ) . split ( " " ) ;
13+ const [ command , ...rest ] = query . split ( " " ) ;
1414
15- if ( command !== "select" ) {
15+ const lowerCaseCommand = command . toLowerCase ( ) ;
16+ if ( lowerCaseCommand !== "select" ) {
1617 throw new Error ( "Only select queries are supported" ) ;
1718 }
18- parsedQuery . command = command ;
19+ // parsedQuery.command = command;
1920
20- const fromIndex = rest . indexOf ( "from" ) ;
21+ const fromIndex = rest . findIndex ( ( word ) => word . toLowerCase ( ) === "from" ) ;
2122 if ( fromIndex === - 1 ) {
2223 throw new Error ( "Invalid query, missing FROM keyword" ) ;
2324 }
2425 parsedQuery . table = rest [ fromIndex + 1 ] ;
2526 parsedQuery . columns = rest . slice ( 0 , fromIndex ) ;
2627
27- const whereIndex = rest . indexOf ( "where" ) ;
28+ const whereIndex = rest . findIndex ( ( word ) => word . toLowerCase ( ) === "where" ) ;
2829 if ( whereIndex !== - 1 ) {
2930 parsedQuery . filters = [
3031 {
3132 column : rest [ whereIndex + 1 ] ,
32- // FIXME: you know what to do here!
3333 operator : rest [ whereIndex + 2 ] as "=" ,
34- value : rest [ whereIndex + 3 ] ,
34+ // to handle string and number values
35+ value :
36+ Number ( rest [ whereIndex + 3 ] ) ||
37+ // remove quotes from string values
38+ String ( rest [ whereIndex + 3 ] ) . replace ( / ^ ' ( .* ) ' $ / , "$1" ) ,
3539 } ,
3640 ] ;
3741 }
0 commit comments