2024-02-11 20:15:08 +00:00
|
|
|
{
|
|
|
|
package nosql
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
import "github.com/pikami/cosmium/parsers"
|
2024-02-11 20:15:08 +00:00
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt, error) {
|
|
|
|
selectStmt := parsers.SelectStmt{
|
|
|
|
Columns: columns.([]parsers.FieldPath),
|
|
|
|
Table: table.(parsers.Table),
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
if filters, ok := whereClause.(parsers.ComparisonExpression); ok {
|
2024-02-11 20:15:08 +00:00
|
|
|
selectStmt.Filters = filters
|
2024-02-11 21:14:30 +00:00
|
|
|
} else if filters, ok := whereClause.(parsers.LogicalExpression); ok {
|
2024-02-11 20:15:08 +00:00
|
|
|
selectStmt.Filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectStmt, nil
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parsers.FieldPath, error) {
|
2024-02-11 20:15:08 +00:00
|
|
|
ps := path.([]interface{})
|
|
|
|
|
|
|
|
paths := make([]string, 1)
|
|
|
|
paths[0] = name.(string)
|
|
|
|
for _, p := range ps {
|
2024-02-11 22:55:07 +00:00
|
|
|
paths = append(paths, p.(string))
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
fieldPath := parsers.FieldPath{Path: paths}
|
2024-02-11 20:15:08 +00:00
|
|
|
if aliasValue, ok := alias.(string); ok {
|
|
|
|
fieldPath.Alias = aliasValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldPath, nil
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.FieldPath, error) {
|
2024-02-11 20:15:08 +00:00
|
|
|
collsAsArray := other_columns.([]interface{})
|
2024-02-11 21:14:30 +00:00
|
|
|
columnList := make([]parsers.FieldPath, len(collsAsArray) + 1)
|
|
|
|
columnList[0] = column.(parsers.FieldPath)
|
2024-02-11 20:15:08 +00:00
|
|
|
|
|
|
|
for i, v := range collsAsArray {
|
2024-02-11 21:14:30 +00:00
|
|
|
if col, ok := v.(parsers.FieldPath); ok {
|
2024-02-11 20:15:08 +00:00
|
|
|
columnList[i+1] = col
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return columnList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinStrings(array []interface{}) string {
|
|
|
|
var stringsArray []string
|
|
|
|
for _, elem := range array {
|
|
|
|
str, ok := elem.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
stringsArray = append(stringsArray, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(stringsArray, "")
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
func combineExpressions(ex1 interface{}, exs interface{}, operation parsers.LogicalExpressionType) (interface{}, error) {
|
2024-02-11 20:15:08 +00:00
|
|
|
if exs == nil || len(exs.([]interface{})) < 1 {
|
|
|
|
return ex1, nil
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.LogicalExpression{
|
2024-02-11 20:15:08 +00:00
|
|
|
Expressions: append([]interface{}{ex1}, exs.([]interface{})...),
|
|
|
|
Operation: operation,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Input <- selectStmt:SelectStmt {
|
|
|
|
return selectStmt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectStmt <- Select ws columns:ColumnList ws
|
|
|
|
From ws table:TableName ws
|
|
|
|
whereClause:(ws Where ws condition:Condition { return condition, nil })? {
|
|
|
|
return makeSelectStmt(columns, table, whereClause)
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnList <- column:FieldPath other_columns:(ws "," ws coll:FieldPath {return coll, nil })* {
|
|
|
|
return makeColumnList(column, other_columns)
|
|
|
|
}
|
|
|
|
|
|
|
|
TableName <- key:Identifier {
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.Table{Value: key.(string)}, nil
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 22:55:07 +00:00
|
|
|
FieldPath <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)*
|
2024-02-11 20:15:08 +00:00
|
|
|
asClause:(ws "AS" ws alias:Identifier { return alias, nil })? {
|
|
|
|
return makeFieldPath(name, path, asClause)
|
|
|
|
}
|
|
|
|
|
2024-02-11 22:55:07 +00:00
|
|
|
DotFieldAccess <- "." id:Identifier {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2024-02-11 20:15:08 +00:00
|
|
|
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
|
|
|
|
return string(c.text), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Condition <- expression:OrExpression {
|
|
|
|
return expression, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
OrExpression <- ex1:AndExpression ex2:(ws "OR" ws ex:AndExpression { return ex, nil })* {
|
2024-02-11 21:14:30 +00:00
|
|
|
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeOr)
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpression { return ex, nil })* {
|
2024-02-11 21:14:30 +00:00
|
|
|
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd)
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ComparisonExpression <- left:(Literal / FieldPath) ws op:ComparisonOperator ws right:(Literal / FieldPath) {
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Select <- ("select" / "SELECT")
|
|
|
|
|
|
|
|
From <- ("from" / "FROM")
|
|
|
|
|
|
|
|
Where <- ("where" / "WHERE")
|
|
|
|
|
|
|
|
ComparisonOperator <- "=" / "!=" / "<" / "<=" / ">" / ">=" {
|
|
|
|
return string(c.text), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral
|
|
|
|
|
|
|
|
IntegerLiteral <- [0-9]+ {
|
|
|
|
intValue, _ := strconv.Atoi(string(c.text))
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.Constant{Type: parsers.ConstantTypeInteger, Value: intValue}, nil
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
StringLiteral <- "\"" chars:StringCharacter* "\"" {
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.Constant{Type: parsers.ConstantTypeString,Value: joinStrings(chars.([]interface{}))}, nil
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
FloatLiteral <- [0-9]+"."[0-9]+ {
|
|
|
|
floatValue, _ := strconv.ParseFloat(string(c.text), 64)
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.Constant{Type: parsers.ConstantTypeFloat, Value: floatValue}, nil
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
BooleanLiteral <- ("true" / "false") {
|
|
|
|
boolValue, _ := strconv.ParseBool(string(c.text))
|
2024-02-11 21:14:30 +00:00
|
|
|
return parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: boolValue}, nil
|
2024-02-11 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringCharacter <- !('"' / "\\") . { return string(c.text), nil }
|
|
|
|
/ "\\" seq:EscapeSequenceCharacter { return seq, nil }
|
|
|
|
|
|
|
|
EscapeSequenceCharacter <- char:EscapeCharacter
|
|
|
|
|
|
|
|
EscapeCharacter <- "'"
|
|
|
|
/ '"'
|
|
|
|
/ "\\"
|
|
|
|
/ "b" { return "\b", nil }
|
|
|
|
/ "f" { return "\f", nil }
|
|
|
|
/ "n" { return "\n", nil }
|
|
|
|
/ "r" { return "\r", nil }
|
|
|
|
/ "t" { return "\t", nil }
|
|
|
|
|
|
|
|
non_escape_character <- !(escape_character) char:.
|
|
|
|
{ return string(c.text), nil }
|
|
|
|
|
|
|
|
ws <- [ \t\n\r]*
|
|
|
|
|
|
|
|
EOF <- !.
|