Skip to content

Commit

Permalink
add select fields and fix Select fetch slice
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed Jan 15, 2019
1 parent 8f3cd89 commit 0b42729
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 16 deletions.
5 changes: 5 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (b *Builder) Where(str string, args ...interface{}) *Builder {
return b
}

func (b *Builder) Select(fields string) *Builder {
b.fields = fields
return b
}

//Limit
func (b *Builder) Limit(i int) *Builder {
b.limit = strconv.Itoa(i)
Expand Down
30 changes: 23 additions & 7 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ func TestBuilder_All(t *testing.T) {
})
}

func TestBuilder_Select(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)

user := make([]*Users, 0)
err := Model(&user).Select("id,name,email").All()

if err != nil {
t.Error(err)
}

fmt.Println(jsonEncode(user))
})
}

func TestBuilder_InAll(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
Expand All @@ -236,7 +252,7 @@ func TestBuilder_InAll(t *testing.T) {
insert(5)

user := make([]*Users, 0)
err := Model(&user).Where("status = ? and id in(?)",1,[]int{1,3,4}).All()
err := Model(&user).Where("status = ? and id in(?)", 1, []int{1, 3, 4}).All()

if err != nil {
t.Error(err)
Expand Down Expand Up @@ -550,11 +566,11 @@ func TestBuilder_NullString(t *testing.T) {

func TestBuilder_Relation1(t *testing.T) {
moment := &MomentList{}
err := Model(moment).Relation("User" , func(b *Builder) {
err := Model(moment).Relation("User", func(b *Builder) {
b.Where("gender = 1")
}).Where("status = 1 and id = ?",14).Get()
}).Where("status = 1 and id = ?", 14).Get()

b , _ :=json.MarshalIndent(moment,""," ")
b, _ := json.MarshalIndent(moment, "", " ")
fmt.Println(string(b), err)

if err != nil {
Expand All @@ -564,14 +580,14 @@ func TestBuilder_Relation1(t *testing.T) {

func TestBuilder_Relation2(t *testing.T) {
var moments = make([]*MomentList, 0)
err := Model(&moments).Relation("User" , func(b *Builder) {
err := Model(&moments).Relation("User", func(b *Builder) {
b.Where("gender = 0")
}).Where("status = 1").Limit(10).All()

b , _ :=json.MarshalIndent(moments,""," ")
b, _ := json.MarshalIndent(moments, "", " ")
fmt.Println(string(b), err)

if err != nil {
t.Fatal(err)
}
}
}
7 changes: 6 additions & 1 deletion sql_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type SQLBuilder struct {
fields string
table string
where string
order string
Expand Down Expand Up @@ -39,7 +40,11 @@ func (s *SQLBuilder) orderFormat() string {

//queryString Assemble the query statement
func (s *SQLBuilder) queryString() string {
query := fmt.Sprintf("SELECT %s * FROM `%s` %s %s %s %s", s.hint, s.table, s.where, s.orderFormat(), s.limitFormat(), s.offsetFormat())
if s.fields == "" {
s.fields = "*"
}

query := fmt.Sprintf("SELECT %s %s FROM `%s` %s %s %s %s", s.hint,s.fields, s.table, s.where, s.orderFormat(), s.limitFormat(), s.offsetFormat())
query = strings.TrimRight(query, " ")
query = query + ";"

Expand Down
10 changes: 5 additions & 5 deletions sql_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestSQLBuilder_queryString(t *testing.T) {

b.Where("id = ?", 1)

if b.queryString() != "SELECT * FROM users WHERE (id = ?) ORDER BY id desc LIMIT 0 OFFSET 10;" {
if b.queryString() != "SELECT * FROM `users` WHERE (id = ?) ORDER BY id desc LIMIT 0 OFFSET 10;" {
t.Error("sql builder query error", b.queryString())
}
fmt.Println(b.queryString())
Expand All @@ -36,7 +36,7 @@ func TestSQLBuilder_insertString(t *testing.T) {
"updated_at": "2018-07-11 11:58:21",
})

if query != "INSERT INTO users (`created_at`,`email`,`id`,`name`,`updated_at`) VALUES(?,?,?,?,?);" {
if query != "INSERT INTO `users` (`created_at`,`email`,`id`,`name`,`updated_at`) VALUES(?,?,?,?,?);" {
t.Error("sql builder insert error", query)
}
}
Expand All @@ -53,7 +53,7 @@ func TestSQLBuilder_updateString(t *testing.T) {
"email": "[email protected]",
})

if query != "UPDATE users SET `email`=?,`name`=? WHERE (id = ?);" {
if query != "UPDATE `users` SET `email`=?,`name`=? WHERE (id = ?);" {
t.Error("sql builder update error", query)
}

Expand All @@ -70,7 +70,7 @@ func TestSQLBuilder_deleteString(t *testing.T) {

query := b.deleteString()

if query != "DELETE FROM users WHERE (id = ?);" {
if query != "DELETE FROM `users` WHERE (id = ?);" {
t.Error("sql builder delete error", query)
}
}
Expand All @@ -84,7 +84,7 @@ func TestSQLBuilder_countString(t *testing.T) {

query := b.countString()

if query != "SELECT count(*) FROM users WHERE (id = ?);" {
if query != "SELECT count(*) FROM `users` WHERE (id = ?);" {
t.Error("sql builder count error", query)
}
}
23 changes: 20 additions & 3 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func (w *Wrapper) Get(dest interface{}, query string, args ...interface{}) (err
return err
}


if reflect.Indirect(refVal).Kind() == reflect.Struct {
// relation data fill
err = RelationOne(dest , w.RelationMap)
Expand All @@ -162,6 +161,13 @@ func (w *Wrapper) Get(dest interface{}, query string, args ...interface{}) (err
return nil
}

func indirectType(v reflect.Type) reflect.Type {
if v.Kind() != reflect.Ptr {
return v
}
return v.Elem()
}

//Select wrapper sqlx.Select
func (w *Wrapper) Select(dest interface{}, query string, args ...interface{}) (err error) {
defer func(start time.Time) {
Expand All @@ -184,8 +190,19 @@ func (w *Wrapper) Select(dest interface{}, query string, args ...interface{}) (e
return err
}

// relation data fill
return RelationAll(dest , w.RelationMap)
t := indirectType(reflect.TypeOf(dest))
if t.Kind() == reflect.Slice {
if indirectType(t.Elem()).Kind() == reflect.Struct {
// relation data fill
err = RelationAll(dest , w.RelationMap)
}
}

if err != nil {
return err
}

return nil
}

//Txx the transaction with context
Expand Down
33 changes: 33 additions & 0 deletions wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ func TestGet(t *testing.T) {
})
}

func TestGetSingle(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
db := Use("default")
{
var name string
err := db.Get(&name, "select name from users where id = ?", 1)

if err != nil {
t.Error(err)
}

fmt.Println(name)
}
})
}

func TestSelectSlice(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
var users []string
err := Select(&users, "select name from users")

if err != nil {
t.Error(err)
}

fmt.Println(jsonEncode(users))
})
}


func TestSelect(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
Expand Down

0 comments on commit 0b42729

Please sign in to comment.