improve word quoting for function FieldsEx

This commit is contained in:
John
2020-11-04 19:53:50 +08:00
parent cb422f043e
commit c056fd2a06
3 changed files with 56 additions and 1 deletions

View File

@ -112,7 +112,7 @@ func (m *Model) getFieldsFiltered() string {
if len(newFields) > 0 {
newFields += ","
}
newFields += k
newFields += m.db.QuoteWord(k)
}
return newFields
}

View File

@ -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)

View 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;