{ package nosql import "github.com/pikami/cosmium/parsers" func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt, error) { selectStmt := parsers.SelectStmt{ Columns: columns.([]parsers.FieldPath), Table: table.(parsers.Table), } if filters, ok := whereClause.(parsers.ComparisonExpression); ok { selectStmt.Filters = filters } else if filters, ok := whereClause.(parsers.LogicalExpression); ok { selectStmt.Filters = filters } return selectStmt, nil } func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parsers.FieldPath, error) { ps := path.([]interface{}) paths := make([]string, 1) paths[0] = name.(string) for _, p := range ps { paths = append(paths, p.(string)) } fieldPath := parsers.FieldPath{Path: paths} if aliasValue, ok := alias.(string); ok { fieldPath.Alias = aliasValue } return fieldPath, nil } func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.FieldPath, error) { collsAsArray := other_columns.([]interface{}) columnList := make([]parsers.FieldPath, len(collsAsArray) + 1) columnList[0] = column.(parsers.FieldPath) for i, v := range collsAsArray { if col, ok := v.(parsers.FieldPath); ok { 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, "") } func combineExpressions(ex1 interface{}, exs interface{}, operation parsers.LogicalExpressionType) (interface{}, error) { if exs == nil || len(exs.([]interface{})) < 1 { return ex1, nil } return parsers.LogicalExpression{ 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 { return parsers.Table{Value: key.(string)}, nil } FieldPath <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* asClause:(ws "AS" ws alias:Identifier { return alias, nil })? { return makeFieldPath(name, path, asClause) } DotFieldAccess <- "." id:Identifier { return id, nil } ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil } 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 })* { return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeOr) } AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpression { return ex, nil })* { return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd) } ComparisonExpression <- left:(Literal / FieldPath) ws op:ComparisonOperator ws right:(Literal / FieldPath) { return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil } 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)) return parsers.Constant{Type: parsers.ConstantTypeInteger, Value: intValue}, nil } StringLiteral <- "\"" chars:StringCharacter* "\"" { return parsers.Constant{Type: parsers.ConstantTypeString,Value: joinStrings(chars.([]interface{}))}, nil } FloatLiteral <- [0-9]+"."[0-9]+ { floatValue, _ := strconv.ParseFloat(string(c.text), 64) return parsers.Constant{Type: parsers.ConstantTypeFloat, Value: floatValue}, nil } BooleanLiteral <- ("true" / "false") { boolValue, _ := strconv.ParseBool(string(c.text)) return parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: boolValue}, nil } 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 <- !.