From b0d07290cf73a4b12c0e5b32bc7c1bbfaeb6c736 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 10 Jan 2018 15:18:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=93=BE=E5=BC=8F=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=96=B9=E6=B3=95=E5=90=8D=E7=A7=B0=EF=BC=9ATable->Fr?= =?UTF-8?q?om,=20Condition->Where?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/database/gdb/gdb.go | 1 + g/database/gdb/gdb_linkop.go | 31 +++++++++++++---------- geg/frame/mvc/controller/demo/template.go | 1 - geg/frame/mvc/controller/demo/user.go | 1 + 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/g/database/gdb/gdb.go b/g/database/gdb/gdb.go index 379fa42c9..55391d811 100644 --- a/g/database/gdb/gdb.go +++ b/g/database/gdb/gdb.go @@ -69,6 +69,7 @@ type Link interface { Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error) Table(tables string) (*gLinkOp) + From(tables string) (*gLinkOp) } // 数据库链接对象 diff --git a/g/database/gdb/gdb_linkop.go b/g/database/gdb/gdb_linkop.go index 926874acb..92579a0dc 100644 --- a/g/database/gdb/gdb_linkop.go +++ b/g/database/gdb/gdb_linkop.go @@ -18,8 +18,8 @@ type gLinkOp struct { link Link // 数据库链接对象 tables string // 数据库操作表 fields string // 操作字段 - condition string // 操作条件 - conditionArgs []interface{} // 操作条件参数 + where string // 操作条件 + whereArgs []interface{} // 操作条件参数 groupby string // 分组语句 orderby string // 排序语句 start int // 分页开始 @@ -30,12 +30,17 @@ type gLinkOp struct { // 链式操作,数据表字段,可支持多个表,以半角逗号连接 func (l *dbLink) Table(tables string) (*gLinkOp) { - return &gLinkOp{ + return &gLinkOp { link : l.link, tables: tables, } } +// 链式操作,数据表字段,可支持多个表,以半角逗号连接 +func (l *dbLink) From(tables string) (*gLinkOp) { + return l.Table(tables) +} + // 链式操作,左联表 func (op *gLinkOp) LeftJoin(joinTable string, on string) (*gLinkOp) { op.tables += fmt.Sprintf(" LEFT JOIN %s ON (%s)", joinTable, on) @@ -61,9 +66,9 @@ func (op *gLinkOp) Fields(fields string) (*gLinkOp) { } // 链式操作,consition -func (op *gLinkOp) Condition(condition string, args...interface{}) (*gLinkOp) { - op.condition = condition - op.conditionArgs = args +func (op *gLinkOp) Where(where string, args...interface{}) (*gLinkOp) { + op.where = where + op.whereArgs = args return op } @@ -157,15 +162,15 @@ func (op *gLinkOp) Update() (sql.Result, error) { if op.data == nil { return nil, errors.New("updating table with empty data") } - return op.link.Update(op.tables, op.data, op.condition, op.conditionArgs ...) + return op.link.Update(op.tables, op.data, op.where, op.whereArgs ...) } // 链式操作, CURD - Delete func (op *gLinkOp) Delete() (sql.Result, error) { - if op.condition == "" { - return nil, errors.New("condition is required while deleting") + if op.where == "" { + return nil, errors.New("where is required while deleting") } - return op.link.Delete(op.tables, op.condition, op.conditionArgs...) + return op.link.Delete(op.tables, op.where, op.whereArgs...) } // 设置批处理的大小 @@ -180,8 +185,8 @@ func (op *gLinkOp) Select() (List, error) { op.fields = "*" } s := fmt.Sprintf("SELECT %s FROM %s", op.fields, op.tables) - if op.condition != "" { - s += " WHERE " + op.condition + if op.where != "" { + s += " WHERE " + op.where } if op.groupby != "" { s += " GROUP BY " + op.groupby @@ -192,7 +197,7 @@ func (op *gLinkOp) Select() (List, error) { if op.limit != 0 { s += fmt.Sprintf(" LIMIT %d, %d", op.start, op.limit) } - return op.link.GetAll(s, op.conditionArgs...) + return op.link.GetAll(s, op.whereArgs...) } // 链式操作,查询所有记录 diff --git a/geg/frame/mvc/controller/demo/template.go b/geg/frame/mvc/controller/demo/template.go index 825ccb29e..050cbbb1e 100644 --- a/geg/frame/mvc/controller/demo/template.go +++ b/geg/frame/mvc/controller/demo/template.go @@ -3,7 +3,6 @@ package demo import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gmvc" - "gitee.com/johng/gf/g/frame/gins" ) type ControllerTemplate struct { diff --git a/geg/frame/mvc/controller/demo/user.go b/geg/frame/mvc/controller/demo/user.go index d246d439f..3bc684956 100644 --- a/geg/frame/mvc/controller/demo/user.go +++ b/geg/frame/mvc/controller/demo/user.go @@ -3,6 +3,7 @@ package demo import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/frame/gmvc" + "fmt" ) // 定义业务相关的控制器对象,