diff --git a/q/select.go b/q/select.go index 097294f..b630783 100644 --- a/q/select.go +++ b/q/select.go @@ -137,6 +137,14 @@ func ParseSelectStatement(sql string) Select { if !passedWHERE && token.Type == sqllexer.KEYWORD && strings.ToUpper(token.Value) == "WHERE" { passedWHERE = true continue + } else if !passedWHERE && token.Type == sqllexer.KEYWORD && strings.ToUpper(token.Value) != "WHERE" { + passedWHERE = true + } + + if passedWHERE && !passedConditionals { + if token.Type == sqllexer.KEYWORD && strings.ToUpper(token.Value) != "AND" && strings.ToUpper(token.Value) != "OR" && strings.ToUpper(token.Value) != "NOT" { + passedConditionals = true + } } if passedWHERE && !passedConditionals { @@ -146,20 +154,26 @@ func ParseSelectStatement(sql string) Select { workingConditional.Operator = token.Value } else if token.Type == sqllexer.BOOLEAN || token.Type == sqllexer.NULL || token.Type == sqllexer.STRING || token.Type == sqllexer.NUMBER { workingConditional.Value = token.Value + } else if token.Type == sqllexer.KEYWORD { + if strings.ToUpper(token.Value) == "AND" || strings.ToUpper(token.Value) == "OR" { + workingConditional.Extension = strings.ToUpper(token.Value) + } } if workingConditional.Key != "" && workingConditional.Operator != "" && workingConditional.Value != "" { query.Conditionals = append(query.Conditionals, workingConditional) workingConditional = Conditional{} - passedConditionals = true + } + if IsTokenEndOfStatement(token) { + break } continue } // Checking For ORDER BY - if !passedOrderByKeywords && token.Type == sqllexer.KEYWORD { + if passedConditionals && !passedOrderByKeywords && token.Type == sqllexer.KEYWORD { unshiftBuffer(&lookBehindBuffer, *token) if strings.ToUpper(lookBehindBuffer[1].Value) == "ORDER" && strings.ToUpper(lookBehindBuffer[0].Value) == "BY" { diff --git a/q/select_test.go b/q/select_test.go index 3545acd..1d30dc9 100644 --- a/q/select_test.go +++ b/q/select_test.go @@ -78,6 +78,37 @@ func TestParseSelectStatement(t *testing.T) { }, }, }, + { + input: "SELECT id, streetNumber, streetName, city, state FROM Addresses WHERE state = 'AL' AND zip > 9000 ORDER BY zip DESC, streetNumber", + expected: Select{ + Table: "Addresses", + IsWildcard: false, + Columns: []string{"id", "streetNumber", "streetName", "city", "state"}, + Conditionals: []Conditional{ + { + Key: "state", + Operator: "=", + Value: "'AL'", + }, + { + Key: "zip", + Operator: ">", + Value: "9000", + Extension: "AND", + }, + }, + OrderBys: []OrderBy{ + { + Key: "zip", + IsDescend: true, + }, + { + Key: "streetNumber", + IsDescend: false, + }, + }, + }, + }, } for _, sql := range testSqlStatements {