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 中的 colsstring 改成了 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
}

修改语法解析

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章起只有简单的指引,适合爱好挑战和自学的读者。
可以购买有详细指导+背景知识的完整版