资源网站都是在哪找的,帮别人做网站必须要开公司,做外贸 英文网站,郑州网站建设网站推广本节我们完成 SQL 解释器的最后一部分#xff0c;它涉及到数据的删除和更改#xff0c;首先我们看删除语句的解析。我们先看 delete 对应的语法#xff1a;
Delete - DELETE FROM ID (where Predicate)?从语法规则可以看出#xff0c;delete 语句必须以关键字 DELETE…本节我们完成 SQL 解释器的最后一部分它涉及到数据的删除和更改首先我们看删除语句的解析。我们先看 delete 对应的语法
Delete - DELETE FROM ID (where Predicate)?从语法规则可以看出delete 语句必须以关键字 DELETE , FROM 开始然后接着的字符串必须要满足 ID 的定义最后可能接着 where 关键字然后进入 Predicate 的解析我们看看代码实现,在 parser.go 中的 Delete 函数增加代码如下
func (p *SQLParser) Delete() interface{} {/*第一个关键字 delete,第二个关键字必须 from*/p.checkWordTag(lexer.DELETE)p.checkWordTag(lexer.FROM)p.checkWordTag(lexer.ID)tblName : p.sqlLexer.Lexemepred : query.NewPredicate()if p.isMatchTag(lexer.WHERE) {pred p.Predicate()}return NewDeleteData(tblName, pred)
}新增一个 delete_data.go 文件添加代码如下
package parserimport querytype DeleteData struct {tblName stringpred *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
}最后在 main.go 增加代码如下
func main() {sql : DELETE FROM Customers WHERE CustomerName\Alfreds Futterkiste\sqlParser : parser.NewSQLParser(sql)sqlParser.UpdateCmd()}以上代码的调试演示过程请在 B 站搜索 coding 迪斯尼查看相关视频。我们还剩下最后一个语句那就是 update,先看看 update 语句对应的语法
Modify - UPDATE ID SET Field EQUAL Expression (WHERE Predicate)?首先它必须以关键字 update 开头然后跟着的字符串必须满足 ID 的定义然后跟着关键字 SET, 后面跟着的一系列字符串要满足 Field 的定义其实这里 Field 对应列名下面跟着等号关键字等号后面则是一个计算表达式在最后我们还得判断是否接着 where 关键字如果有我们还要解析 where 后面对应的表达式我们看看对应代码实现
func (p *SQLParser) Modify() interface{} {p.checkWordTag(lexer.UPDATE)p.checkWordTag(lexer.ID)//获得表名tblName : p.sqlLexer.Lexemep.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)
}接下来增加一个文件 modify_data.go,添加如下代码
package parserimport querytype ModifyData struct {tblName stringfldName stringnewVal *query.Expressionpred *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
}
到这里我们就基本完成了一个小型 SQL 解释器更详细的调试演示和讲解请在 B 站参看 coding 迪斯尼代码下载地址 https://github.com/wycl16514/SQL_PARSER_FINISH.git 文章转载自: http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.brjq.cn.gov.cn.brjq.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.smry.cn.gov.cn.smry.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.zlgth.cn.gov.cn.zlgth.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.jmbgl.cn.gov.cn.jmbgl.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.pnntx.cn.gov.cn.pnntx.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.ndltr.cn.gov.cn.ndltr.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.fddfn.cn.gov.cn.fddfn.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.dcccl.cn.gov.cn.dcccl.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.lfpzs.cn.gov.cn.lfpzs.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.wnnfh.cn.gov.cn.wnnfh.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn