mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve word quoting for function FieldsEx
This commit is contained in:
@ -112,7 +112,7 @@ func (m *Model) getFieldsFiltered() string {
|
||||
if len(newFields) > 0 {
|
||||
newFields += ","
|
||||
}
|
||||
newFields += k
|
||||
newFields += m.db.QuoteWord(k)
|
||||
}
|
||||
return newFields
|
||||
}
|
||||
|
||||
@ -11,7 +11,9 @@ import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"testing"
|
||||
"time"
|
||||
@ -763,6 +765,26 @@ func Test_Model_Count(t *testing.T) {
|
||||
t.Assert(err, nil)
|
||||
t.Assert(count, SIZE)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.Table(table).FieldsEx("id").Where("id>8").Count()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(count, 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.Table(table).Fields("distinct id,nickname").Where("id>8").Count()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(count, 2)
|
||||
})
|
||||
//gtest.C(t, func(t *gtest.T) {
|
||||
// count, err := db.Table(table).Fields("id myid").Where("id>8").Count()
|
||||
// t.Assert(err, nil)
|
||||
// t.Assert(count, 2)
|
||||
//})
|
||||
//gtest.C(t, func(t *gtest.T) {
|
||||
// count, err := db.Table(table).As("u1").LeftJoin(table, "u2", "u2.id=u1.id").Fields("u2.id u2id").Where("u1.id>8").Count()
|
||||
// t.Assert(err, nil)
|
||||
// t.Assert(count, 2)
|
||||
//})
|
||||
}
|
||||
|
||||
func Test_Model_FindCount(t *testing.T) {
|
||||
@ -2093,6 +2115,19 @@ func Test_Model_FieldsEx(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FieldsEx_WithReservedWords(t *testing.T) {
|
||||
table := "fieldsex_test_table"
|
||||
sqlTpcPath := gdebug.TestDataPath("reservedwords_table_tpl.sql")
|
||||
if _, err := db.Exec(fmt.Sprintf(gfile.GetContents(sqlTpcPath), table)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Table(table).FieldsEx("content").One()
|
||||
t.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FieldsStr(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
20
database/gdb/testdata/reservedwords_table_tpl.sql
vendored
Normal file
20
database/gdb/testdata/reservedwords_table_tpl.sql
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
CREATE TABLE %s (
|
||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`key` varchar(45) DEFAULT NULL,
|
||||
`category_id` int(10) unsigned NOT NULL,
|
||||
`user_id` int(10) unsigned NOT NULL,
|
||||
`title` varchar(255) NOT NULL,
|
||||
`content` mediumtext NOT NULL,
|
||||
`sort` int(10) unsigned DEFAULT '0',
|
||||
`brief` varchar(255) DEFAULT NULL,
|
||||
`thumb` varchar(255) DEFAULT NULL,
|
||||
`tags` varchar(900) DEFAULT NULL,
|
||||
`referer` varchar(255) DEFAULT NULL,
|
||||
`status` smallint(5) unsigned DEFAULT '0',
|
||||
`view_count` int(10) unsigned DEFAULT '0',
|
||||
`zan_count` int(10) unsigned DEFAULT NULL,
|
||||
`cai_count` int(10) unsigned DEFAULT NULL,
|
||||
`created_at` datetime DEFAULT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
Reference in New Issue
Block a user