Write your own database: implement a small SQL interpreter (Part 2)

In this section we complete the last part of the SQL interpreter, which involves deleting and changing data. First we look at the parsing of the delete statement. Let’s first look at the syntax corresponding to delete:

Delete -> DELETE FROM ID (where Predicate)?

It can be seen from the grammar rules that the delete statement must start with the keywords DELETE and FROM, and then the following string must meet the definition of ID. Finally, it may be followed by the where keyword, and then enter the parsing of Predicate. Let's take a look at the code implementation. The Delete function in parser.go adds the following code:

func (p *SQLParser) Delete() interface{
    
    } {
    
    
	/*
		第一个关键字 delete,第二个关键字必须 from
	*/
	p.checkWordTag(lexer.DELETE)
	p.checkWordTag(lexer.FROM)
	p.checkWordTag(lexer.ID)
	tblName := p.sqlLexer.Lexeme
	pred := query.NewPredicate()
	if p.isMatchTag(lexer.WHERE) {
    
    
		pred = p.Predicate()
	}
	return NewDeleteData(tblName, pred)
}

Add a new delete_data.go file and add the following code:

package parser

import "query"

type DeleteData struct {
    
    
	tblName string
	pred    *query.Predicate
}

func NewDeleteData(tblName string, pred *query.Predicate) *DeleteData {
    
    
	return &DeleteData{
    
    
		tblName: tblName,
		pred:    pred,
	}
}

func (d *DeleteData) TableName() string {
    
    
	return d.tblName
}

func (d *DeleteData) Pred() *query.Predicate {
    
    
	return d.pred
}

Finally, add the following code to main.go:

func main() {
    
    
	sql := "DELETE FROM Customers WHERE CustomerName=\"Alfreds Futterkiste\""
	sqlParser := parser.NewSQLParser(sql)
	sqlParser.UpdateCmd()

}

For the debugging demonstration process of the above code, please search coding Disney on station B to view related videos. We have the last statement left, which is update. Let’s first look at the syntax corresponding to the update statement:

Modify -> UPDATE ID SET Field EQUAL Expression (WHERE Predicate)?

First, it must start with the keyword update, then the following string must meet the definition of ID, then follow the keyword SET, and the following series of strings must meet the definition of Field. In fact, Field here corresponds to the column name, followed by the equal sign. Keyword, followed by the equal sign is a calculation expression. At the end, we have to determine whether the where keyword is followed. If so, we also need to parse the corresponding expression after where. Let's take a look at the corresponding code implementation:

func (p *SQLParser) Modify() interface{
    
    } {
    
    
	p.checkWordTag(lexer.UPDATE)
	p.checkWordTag(lexer.ID)
	//获得表名
	tblName := p.sqlLexer.Lexeme
	p.checkWordTag(lexer.SET)
	_, fldName := p.Field()
	p.checkWordTag(lexer.ASSIGN_OPERATOR)
	newVal := p.Expression()
	pred := query.NewPredicate()
	if p.isMatchTag(lexer.WHERE) {
    
    
		pred = p.Predicate()
	}
	return NewModifyData(tblName, fldName, newVal, pred)
}

Next, add a file modify_data.go and add the following code:

package parser

import "query"

type ModifyData struct {
    
    
	tblName string
	fldName string
	newVal  *query.Expression
	pred    *query.Predicate
}

func NewModifyData(tblName string, fldName string, newVal *query.Expression, pred *query.Predicate) *ModifyData {
    
    
	return &ModifyData{
    
    
		tblName: tblName,
		fldName: fldName,
		newVal:  newVal,
		pred:    pred,
	}
}

func (m *ModifyData) TableName() string {
    
    
	return m.tblName
}

func (m *ModifyData) TargetField() string {
    
    
	return m.fldName
}

func (m *ModifyData) NewValue() *query.Expression {
    
    
	return m.newVal
}

func (m *ModifyData) Pred() *query.Predicate {
    
    
	return m.pred
}

At this point we have basically completed a small SQL interpreter. For more detailed debugging demonstrations and explanations, please refer to coding Disney at station B. Code download address:
https://github.com/wycl16514/SQL_PARSER_FINISH.git

Guess you like

Origin blog.csdn.net/tyler_download/article/details/132610585