Skip to content

Commit

Permalink
delete query parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vahagz committed Feb 1, 2024
1 parent dc8a7aa commit c86e5f7
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 21 deletions.
47 changes: 32 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,47 @@ func main() {
// fmt.Println(qu.Where)
// return

// ParserService := parser.New()
// q, err := ParserService.ParseQuery([]byte(`
// INSERT_INTO testtable (firstname,lastname)
// VALUES
// ("Vahag","Zargaryan"),
// ("Ruben", "Manandyan"),
// ("Sergey", "Zargaryan"),
// ("Arman", "Sargsyan"),
// ("Mery", "Voskanyan"),
// ("David", "Harutyunyan"),
// ("Alexader", "Bakunc"),
// ("Hayk", "Vardanyan"),
// ("Serob", "Gevorgyan"),
// ("Gevorg", "Aznauryan");
// `))
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// qu := q.(*dml.QueryInsert)
// fmt.Println(qu.Type)
// fmt.Println(qu.Table)
// fmt.Println(qu.Columns)
// fmt.Println(qu.Values)
// return

ParserService := parser.New()
q, err := ParserService.ParseQuery([]byte(`
INSERT testtable (firstname,lastname)
VALUES
("Vahag","Zargaryan"),
("Ruben", "Manandyan"),
("Sergey", "Zargaryan"),
("Arman", "Sargsyan"),
("Mery", "Voskanyan"),
("David", "Harutyunyan"),
("Alexader", "Bakunc"),
("Hayk", "Vardanyan"),
("Serob", "Gevorgyan"),
("Gevorg", "Aznauryan");
DELETE_FROM testtable
WHERE_INDEX id_1 id >= 100 AND id < 800
WHERE id > 10 AND (firstname = "Vahag" OR lastname = "Sargsyan");
`))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
qu := q.(*dml.QueryInsert)
qu := q.(*dml.QueryDelete)
fmt.Println(qu.Type)
fmt.Println(qu.Table)
fmt.Println(qu.Columns)
fmt.Println(qu.Values)
fmt.Println(qu.WhereIndex)
fmt.Println(qu.Where)
return

pwd, _ := os.Getwd()
Expand Down
96 changes: 95 additions & 1 deletion services/parser/query/dml/delete.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dml

import (
"encoding/json"
"text/scanner"

"go-dbms/pkg/types"
"go-dbms/services/parser/query"
)

Expand All @@ -14,6 +16,98 @@ type QueryDelete struct {
WhereIndex *whereIndex `json:"where_index"`
}

func (qs *QueryDelete) Parse(s *scanner.Scanner) (err error) {
func (qd *QueryDelete) Parse(s *scanner.Scanner) (err error) {
defer func () {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
panic(r)
}
}
}()

qd.Type = query.DELETE

qd.parseFrom(s)

word := s.TokenText()
if word == "WHERE_INDEX" {
qd.parseWhereIndex(s)
}

word = s.TokenText()
if word == "WHERE" {
qd.parseWhere(s)
}

return nil
}

func (qd *QueryDelete) parseFrom(s *scanner.Scanner) {
tok := s.Scan()
word := s.TokenText()
_, isKW := keyWords[word]
if tok == scanner.EOF {
panic(ErrSyntax)
} else if isKW {
panic(ErrNoFrom)
}

qd.Table = word

tok = s.Scan()
word = s.TokenText()
_, idKW := keyWords[word]
if tok != scanner.EOF && !idKW {
panic(ErrSyntax)
}
}

func (qs *QueryDelete) parseWhereIndex(s *scanner.Scanner) {
tok := s.Scan()
word := s.TokenText()
_, isKW := keyWords[word]
if tok == scanner.EOF || isKW {
panic(ErrSyntax)
}

qs.WhereIndex = &whereIndex{}
qs.WhereIndex.Name = word
qs.WhereIndex.FilterStart = &indexFilter{}
col, op, val := parseWhereFilter(s, false)
var valInt interface{}
if err := json.Unmarshal([]byte(val), &valInt); err != nil {
panic(err)
}
qs.WhereIndex.FilterStart.Operator = op
qs.WhereIndex.FilterStart.Value = map[string]types.DataType{
col: types.ParseJSONValue(valInt),
}

tok = s.Scan()
word = s.TokenText()
_, isKW = keyWords[word]
if tok == scanner.EOF || isKW {
panic(ErrSyntax)
}

if word == "AND" {
qs.WhereIndex.FilterEnd = &indexFilter{}
col, op, val := parseWhereFilter(s, false)
var valInt interface{}
if err := json.Unmarshal([]byte(val), &valInt); err != nil {
panic(err)
}
qs.WhereIndex.FilterEnd.Operator = op
qs.WhereIndex.FilterEnd.Value = map[string]types.DataType{
col: types.ParseJSONValue(valInt),
}
}

s.Scan()
}

func (qs *QueryDelete) parseWhere(s *scanner.Scanner) {
qs.Where = (*where)(parseWhere(s))
}
2 changes: 1 addition & 1 deletion services/parser/query/dml/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Parse(s *scanner.Scanner, queryType query.QueryType) (query.Querier, error)
var q query.Querier

switch queryType {
// case query.DELETE: q = &QueryDelete{}
case query.DELETE: q = &QueryDelete{}
case query.INSERT: q = &QueryInsert{}
case query.SELECT: q = &QuerySelect{}
// case query.UPDATE: q = &QueryUpdate{}
Expand Down
6 changes: 4 additions & 2 deletions services/parser/query/dml/key_words.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ var keyWords = map[string]struct{}{
"ORDER_BY": {},
"LIMIT": {},

"INSERT": {},
"VALUES": {},
"INSERT_INTO": {},
"VALUES": {},

"DELETE_FROM": {},
}

var indexOperators = map[string]struct{}{
Expand Down
4 changes: 2 additions & 2 deletions services/parser/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import "text/scanner"
type QueryType string

const (
INSERT QueryType = "INSERT"
INSERT QueryType = "INSERT_INTO"
SELECT QueryType = "SELECT"
UPDATE QueryType = "UPDATE"
DELETE QueryType = "DELETE"
DELETE QueryType = "DELETE_FROM"
CREATE QueryType = "CREATE"
ALTER QueryType = "ALTER"
DROP QueryType = "DROP"
Expand Down

0 comments on commit c86e5f7

Please sign in to comment.