mirror of
https://github.com/pikami/cosmium.git
synced 2024-11-28 08:27:37 +00:00
Extract parser models
This commit is contained in:
parent
50b672a367
commit
12510ea3fa
48
parsers/models.go
Normal file
48
parsers/models.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package parsers
|
||||||
|
|
||||||
|
type LogicalExpressionType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
LogicalExpressionTypeOr LogicalExpressionType = iota
|
||||||
|
LogicalExpressionTypeAnd
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConstantType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ConstantTypeString ConstantType = iota
|
||||||
|
ConstantTypeInteger
|
||||||
|
ConstantTypeFloat
|
||||||
|
ConstantTypeBoolean
|
||||||
|
)
|
||||||
|
|
||||||
|
type SelectStmt struct {
|
||||||
|
Columns []FieldPath
|
||||||
|
Table Table
|
||||||
|
Filters interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Table struct {
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
type FieldPath struct {
|
||||||
|
Alias string
|
||||||
|
Path []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogicalExpression struct {
|
||||||
|
Expressions []interface{}
|
||||||
|
Operation LogicalExpressionType
|
||||||
|
}
|
||||||
|
|
||||||
|
type ComparisonExpression struct {
|
||||||
|
Left interface{}
|
||||||
|
Right interface{}
|
||||||
|
Operation string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Constant struct {
|
||||||
|
Type ConstantType
|
||||||
|
Value interface{}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -1,69 +1,24 @@
|
|||||||
{
|
{
|
||||||
package nosql
|
package nosql
|
||||||
|
|
||||||
type LogicalExpressionType int
|
import "github.com/pikami/cosmium/parsers"
|
||||||
|
|
||||||
const (
|
func makeSelectStmt(columns, table, whereClause interface{}) (parsers.SelectStmt, error) {
|
||||||
LogicalExpressionTypeOr LogicalExpressionType = iota
|
selectStmt := parsers.SelectStmt{
|
||||||
LogicalExpressionTypeAnd
|
Columns: columns.([]parsers.FieldPath),
|
||||||
)
|
Table: table.(parsers.Table),
|
||||||
|
|
||||||
type ConstantType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
ConstantTypeString ConstantType = iota
|
|
||||||
ConstantTypeInteger
|
|
||||||
ConstantTypeFloat
|
|
||||||
ConstantTypeBoolean
|
|
||||||
)
|
|
||||||
|
|
||||||
type SelectStmt struct{
|
|
||||||
Columns []FieldPath
|
|
||||||
Table Table
|
|
||||||
Filters interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Table struct{
|
if filters, ok := whereClause.(parsers.ComparisonExpression); ok {
|
||||||
Value string
|
|
||||||
}
|
|
||||||
|
|
||||||
type FieldPath struct {
|
|
||||||
Alias string
|
|
||||||
Path []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogicalExpression struct {
|
|
||||||
Expressions []interface{}
|
|
||||||
Operation LogicalExpressionType
|
|
||||||
}
|
|
||||||
|
|
||||||
type ComparisonExpression struct {
|
|
||||||
Left interface{}
|
|
||||||
Right interface{}
|
|
||||||
Operation string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Constant struct {
|
|
||||||
Type ConstantType
|
|
||||||
Value interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeSelectStmt(columns, table, whereClause interface{}) (SelectStmt, error) {
|
|
||||||
selectStmt := SelectStmt{
|
|
||||||
Columns: columns.([]FieldPath),
|
|
||||||
Table: table.(Table),
|
|
||||||
}
|
|
||||||
|
|
||||||
if filters, ok := whereClause.(ComparisonExpression); ok {
|
|
||||||
selectStmt.Filters = filters
|
selectStmt.Filters = filters
|
||||||
} else if filters, ok := whereClause.(LogicalExpression); ok {
|
} else if filters, ok := whereClause.(parsers.LogicalExpression); ok {
|
||||||
selectStmt.Filters = filters
|
selectStmt.Filters = filters
|
||||||
}
|
}
|
||||||
|
|
||||||
return selectStmt, nil
|
return selectStmt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeFieldPath(name interface{}, path interface{}, alias interface{}) (FieldPath, error) {
|
func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parsers.FieldPath, error) {
|
||||||
ps := path.([]interface{})
|
ps := path.([]interface{})
|
||||||
|
|
||||||
paths := make([]string, 1)
|
paths := make([]string, 1)
|
||||||
@ -76,7 +31,7 @@ func makeFieldPath(name interface{}, path interface{}, alias interface{}) (Field
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldPath := FieldPath{Path: paths}
|
fieldPath := parsers.FieldPath{Path: paths}
|
||||||
if aliasValue, ok := alias.(string); ok {
|
if aliasValue, ok := alias.(string); ok {
|
||||||
fieldPath.Alias = aliasValue
|
fieldPath.Alias = aliasValue
|
||||||
}
|
}
|
||||||
@ -84,13 +39,13 @@ func makeFieldPath(name interface{}, path interface{}, alias interface{}) (Field
|
|||||||
return fieldPath, nil
|
return fieldPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeColumnList(column interface{}, other_columns interface{}) ([]FieldPath, error) {
|
func makeColumnList(column interface{}, other_columns interface{}) ([]parsers.FieldPath, error) {
|
||||||
collsAsArray := other_columns.([]interface{})
|
collsAsArray := other_columns.([]interface{})
|
||||||
columnList := make([]FieldPath, len(collsAsArray) + 1)
|
columnList := make([]parsers.FieldPath, len(collsAsArray) + 1)
|
||||||
columnList[0] = column.(FieldPath)
|
columnList[0] = column.(parsers.FieldPath)
|
||||||
|
|
||||||
for i, v := range collsAsArray {
|
for i, v := range collsAsArray {
|
||||||
if col, ok := v.(FieldPath); ok {
|
if col, ok := v.(parsers.FieldPath); ok {
|
||||||
columnList[i+1] = col
|
columnList[i+1] = col
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,12 +66,12 @@ func joinStrings(array []interface{}) string {
|
|||||||
return strings.Join(stringsArray, "")
|
return strings.Join(stringsArray, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func combineExpressions(ex1 interface{}, exs interface{}, operation LogicalExpressionType) (interface{}, error) {
|
func combineExpressions(ex1 interface{}, exs interface{}, operation parsers.LogicalExpressionType) (interface{}, error) {
|
||||||
if exs == nil || len(exs.([]interface{})) < 1 {
|
if exs == nil || len(exs.([]interface{})) < 1 {
|
||||||
return ex1, nil
|
return ex1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return LogicalExpression{
|
return parsers.LogicalExpression{
|
||||||
Expressions: append([]interface{}{ex1}, exs.([]interface{})...),
|
Expressions: append([]interface{}{ex1}, exs.([]interface{})...),
|
||||||
Operation: operation,
|
Operation: operation,
|
||||||
}, nil
|
}, nil
|
||||||
@ -139,7 +94,7 @@ ColumnList <- column:FieldPath other_columns:(ws "," ws coll:FieldPath {return c
|
|||||||
}
|
}
|
||||||
|
|
||||||
TableName <- key:Identifier {
|
TableName <- key:Identifier {
|
||||||
return Table{Value: key.(string)}, nil
|
return parsers.Table{Value: key.(string)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
FieldPath <- name:Identifier path:("." Identifier)*
|
FieldPath <- name:Identifier path:("." Identifier)*
|
||||||
@ -156,15 +111,15 @@ Condition <- expression:OrExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
OrExpression <- ex1:AndExpression ex2:(ws "OR" ws ex:AndExpression { return ex, nil })* {
|
OrExpression <- ex1:AndExpression ex2:(ws "OR" ws ex:AndExpression { return ex, nil })* {
|
||||||
return combineExpressions(ex1, ex2, LogicalExpressionTypeOr)
|
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeOr)
|
||||||
}
|
}
|
||||||
|
|
||||||
AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpression { return ex, nil })* {
|
AndExpression <- ex1:ComparisonExpression ex2:(ws "AND" ws ex:ComparisonExpression { return ex, nil })* {
|
||||||
return combineExpressions(ex1, ex2, LogicalExpressionTypeAnd)
|
return combineExpressions(ex1, ex2, parsers.LogicalExpressionTypeAnd)
|
||||||
}
|
}
|
||||||
|
|
||||||
ComparisonExpression <- left:(Literal / FieldPath) ws op:ComparisonOperator ws right:(Literal / FieldPath) {
|
ComparisonExpression <- left:(Literal / FieldPath) ws op:ComparisonOperator ws right:(Literal / FieldPath) {
|
||||||
return ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil
|
return parsers.ComparisonExpression{Left:left,Right:right,Operation:string(op.([]uint8))}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
Select <- ("select" / "SELECT")
|
Select <- ("select" / "SELECT")
|
||||||
@ -181,18 +136,18 @@ Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral
|
|||||||
|
|
||||||
IntegerLiteral <- [0-9]+ {
|
IntegerLiteral <- [0-9]+ {
|
||||||
intValue, _ := strconv.Atoi(string(c.text))
|
intValue, _ := strconv.Atoi(string(c.text))
|
||||||
return Constant{Type: ConstantTypeInteger, Value: intValue}, nil
|
return parsers.Constant{Type: parsers.ConstantTypeInteger, Value: intValue}, nil
|
||||||
}
|
}
|
||||||
StringLiteral <- "\"" chars:StringCharacter* "\"" {
|
StringLiteral <- "\"" chars:StringCharacter* "\"" {
|
||||||
return Constant{Type: ConstantTypeString,Value: joinStrings(chars.([]interface{}))}, nil
|
return parsers.Constant{Type: parsers.ConstantTypeString,Value: joinStrings(chars.([]interface{}))}, nil
|
||||||
}
|
}
|
||||||
FloatLiteral <- [0-9]+"."[0-9]+ {
|
FloatLiteral <- [0-9]+"."[0-9]+ {
|
||||||
floatValue, _ := strconv.ParseFloat(string(c.text), 64)
|
floatValue, _ := strconv.ParseFloat(string(c.text), 64)
|
||||||
return Constant{Type: ConstantTypeFloat, Value: floatValue}, nil
|
return parsers.Constant{Type: parsers.ConstantTypeFloat, Value: floatValue}, nil
|
||||||
}
|
}
|
||||||
BooleanLiteral <- ("true" / "false") {
|
BooleanLiteral <- ("true" / "false") {
|
||||||
boolValue, _ := strconv.ParseBool(string(c.text))
|
boolValue, _ := strconv.ParseBool(string(c.text))
|
||||||
return Constant{Type: ConstantTypeBoolean, Value: boolValue}, nil
|
return parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: boolValue}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
StringCharacter <- !('"' / "\\") . { return string(c.text), nil }
|
StringCharacter <- !('"' / "\\") . { return string(c.text), nil }
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pikami/cosmium/parsers"
|
||||||
"github.com/pikami/cosmium/parsers/nosql"
|
"github.com/pikami/cosmium/parsers/nosql"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ import (
|
|||||||
// fmt.Printf("output:\n%v\n", string(result))
|
// fmt.Printf("output:\n%v\n", string(result))
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func testQueryParse(t *testing.T, query string, expectedQuery nosql.SelectStmt) {
|
func testQueryParse(t *testing.T, query string, expectedQuery parsers.SelectStmt) {
|
||||||
parsedQuery, err := nosql.Parse("", []byte(query))
|
parsedQuery, err := nosql.Parse("", []byte(query))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -41,12 +42,12 @@ func Test_Parse(t *testing.T) {
|
|||||||
testQueryParse(
|
testQueryParse(
|
||||||
t,
|
t,
|
||||||
`SELECT c.id, c.pk FROM c`,
|
`SELECT c.id, c.pk FROM c`,
|
||||||
nosql.SelectStmt{
|
parsers.SelectStmt{
|
||||||
Columns: []nosql.FieldPath{
|
Columns: []parsers.FieldPath{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: nosql.Table{Value: "c"},
|
Table: parsers.Table{Value: "c"},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -57,15 +58,15 @@ func Test_Parse(t *testing.T) {
|
|||||||
`select c.id
|
`select c.id
|
||||||
FROM c
|
FROM c
|
||||||
WHERE c.pk=true`,
|
WHERE c.pk=true`,
|
||||||
nosql.SelectStmt{
|
parsers.SelectStmt{
|
||||||
Columns: []nosql.FieldPath{
|
Columns: []parsers.FieldPath{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: nosql.Table{Value: "c"},
|
Table: parsers.Table{Value: "c"},
|
||||||
Filters: nosql.ComparisonExpression{
|
Filters: parsers.ComparisonExpression{
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "pk"}},
|
Left: parsers.FieldPath{Path: []string{"c", "pk"}},
|
||||||
Right: nosql.Constant{Type: nosql.ConstantTypeBoolean, Value: true},
|
Right: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -77,26 +78,26 @@ func Test_Parse(t *testing.T) {
|
|||||||
`select c.id, c._self AS self, c._rid, c._ts
|
`select c.id, c._self AS self, c._rid, c._ts
|
||||||
FROM c
|
FROM c
|
||||||
WHERE c.id="12345" OR c.pk=123`,
|
WHERE c.id="12345" OR c.pk=123`,
|
||||||
nosql.SelectStmt{
|
parsers.SelectStmt{
|
||||||
Columns: []nosql.FieldPath{
|
Columns: []parsers.FieldPath{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "_self"}, Alias: "self"},
|
{Path: []string{"c", "_self"}, Alias: "self"},
|
||||||
{Path: []string{"c", "_rid"}},
|
{Path: []string{"c", "_rid"}},
|
||||||
{Path: []string{"c", "_ts"}},
|
{Path: []string{"c", "_ts"}},
|
||||||
},
|
},
|
||||||
Table: nosql.Table{Value: "c"},
|
Table: parsers.Table{Value: "c"},
|
||||||
Filters: nosql.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Operation: nosql.LogicalExpressionTypeOr,
|
Operation: parsers.LogicalExpressionTypeOr,
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
nosql.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "id"}},
|
Left: parsers.FieldPath{Path: []string{"c", "id"}},
|
||||||
Right: nosql.Constant{Type: nosql.ConstantTypeString, Value: "12345"},
|
Right: parsers.Constant{Type: parsers.ConstantTypeString, Value: "12345"},
|
||||||
},
|
},
|
||||||
nosql.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "pk"}},
|
Left: parsers.FieldPath{Path: []string{"c", "pk"}},
|
||||||
Right: nosql.Constant{Type: nosql.ConstantTypeInteger, Value: 123},
|
Right: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 123},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -113,33 +114,33 @@ func Test_Parse(t *testing.T) {
|
|||||||
AND c.integer=1
|
AND c.integer=1
|
||||||
AND c.float=6.9
|
AND c.float=6.9
|
||||||
AND c.string="hello"`,
|
AND c.string="hello"`,
|
||||||
nosql.SelectStmt{
|
parsers.SelectStmt{
|
||||||
Columns: []nosql.FieldPath{{Path: []string{"c", "id"}, Alias: ""}},
|
Columns: []parsers.FieldPath{{Path: []string{"c", "id"}, Alias: ""}},
|
||||||
Table: nosql.Table{Value: "c"},
|
Table: parsers.Table{Value: "c"},
|
||||||
Filters: nosql.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
nosql.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "boolean"}},
|
Left: parsers.FieldPath{Path: []string{"c", "boolean"}},
|
||||||
Right: nosql.Constant{Type: 3, Value: true},
|
Right: parsers.Constant{Type: 3, Value: true},
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
},
|
},
|
||||||
nosql.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "integer"}},
|
Left: parsers.FieldPath{Path: []string{"c", "integer"}},
|
||||||
Right: nosql.Constant{Type: 1, Value: 1},
|
Right: parsers.Constant{Type: 1, Value: 1},
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
},
|
},
|
||||||
nosql.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "float"}},
|
Left: parsers.FieldPath{Path: []string{"c", "float"}},
|
||||||
Right: nosql.Constant{Type: 2, Value: 6.9},
|
Right: parsers.Constant{Type: 2, Value: 6.9},
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
},
|
},
|
||||||
nosql.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
Left: nosql.FieldPath{Path: []string{"c", "string"}},
|
Left: parsers.FieldPath{Path: []string{"c", "string"}},
|
||||||
Right: nosql.Constant{Type: 0, Value: "hello"},
|
Right: parsers.Constant{Type: 0, Value: "hello"},
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Operation: nosql.LogicalExpressionTypeAnd,
|
Operation: parsers.LogicalExpressionTypeAnd,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user