From 76bc9bd385c4e62d0d81680d662506728f313367 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 16 Dec 2019 21:00:16 +0800 Subject: [PATCH] improve gdb.Model --- .../frame/mvc/app/model/article/article_model.go | 13 ++++++++++--- database/gdb/gdb_func.go | 5 +++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.example/frame/mvc/app/model/article/article_model.go b/.example/frame/mvc/app/model/article/article_model.go index 04398997f..7b5c6246d 100644 --- a/.example/frame/mvc/app/model/article/article_model.go +++ b/.example/frame/mvc/app/model/article/article_model.go @@ -26,19 +26,26 @@ var ( ) // FindOne retrieves and returns a single Entity by a primary key or where conditions by -// Model.Where. +// Model.Where. The optional parameter is like follows: +// 123, []int{1, 2, 3}, "john", []string{"john", "smith"} +// g.Map{"id": g.Slice{1,2,3}}, g.Map{"id": 1, "name": "john"}, etc. +// +// Note that it differs with Mode.One is that any single where condition parameter will +// be treated as the value of the primary key in FindOne, but a string condition in +// Model.One. That is, if primary key is "id" and given parameter as "123", the +// FindOne function treats it as "id=123", but Model.One treats it as "123", just a string. func FindOne(where ...interface{}) (*Entity, error) { return Model.One(gdb.GetPrimaryKeyCondition(Primary, where...)...) } // FindAll retrieves and returns multiple Entity by a primary key or where conditions by -// Model.Where. +// Model.Where. Also see FindOne. func FindAll(where ...interface{}) ([]*Entity, error) { return Model.All(gdb.GetPrimaryKeyCondition(Primary, where...)...) } // FindValue retrieves and returns single field value by a primary key or where conditions by -// Model.Where. +// Model.Where. Also see FindOne. func FindValue(fieldsAndWhere ...interface{}) (gdb.Value, error) { if len(fieldsAndWhere) == 2 { return Model.Value(append(fieldsAndWhere[0:1], gdb.GetPrimaryKeyCondition(Primary, fieldsAndWhere[1:]...)...)...) diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index fdaddbbab..89ed4eed2 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -126,8 +126,9 @@ func GetPrimaryKey(pointer interface{}) string { } // GetPrimaryKeyCondition returns a new where condition by primary field name. -// The parameter is like as follows: -// 123, []int{1,2,3}, "john", []string{"john","smith"} +// The optional parameter is like follows: +// 123, []int{1, 2, 3}, "john", []string{"john", "smith"} +// g.Map{"id": g.Slice{1,2,3}}, g.Map{"id": 1, "name": "john"}, etc. // // Note that it returns the given parameter directly if there's the is empty. func GetPrimaryKeyCondition(primary string, where ...interface{}) (newWhereCondition []interface{}) {