0505: 输出表达式
修改数据结构
这一步把表达式应用到 select 和 update 上:
select a * 4 - b, d + c from t where d = 123;
update t set a = a - b, b = a, c = d + c where d = 123;StmtSelect 中的 cols 从 string 改成了 interface{}; StmtUpdate 也作类似修改,新增 ExprAssign:
type StmtSelect struct {
table string
// cols []string
cols []interface{} // ExprUnOp | ExprBinOp | string | *Cell
keys []NamedCell
}
type StmtUpdate struct {
table string
keys []NamedCell
// value []ExprEqual
value []ExprAssign
}
type ExprAssign struct {
column string
expr interface{} // ExprUnOp | ExprBinOp | string | *Cell
}修改语法解析
parseSelect()里的tryName()替换成parseExpr()。parseUpdate()里的parseEqual()替换成支持表达式的parseAssign()。
func (p *Parser) parseAssign(out *ExprAssign) (err error)修改语句执行
修改 select 和 update,调用 evalExpr() 输出结果:
func (db *DB) execSelect(stmt *StmtSelect) ([]Row, error)
func (db *DB) execUpdate(stmt *StmtUpdate) (count int, err error)您正在阅读免费版教程,从第4章起只有简单的指引,适合爱好挑战和自学的读者。
可以购买有详细指导+背景知识的完整版。