fix: use keyword(like: group) as table name in sqlite (#2461)

This commit is contained in:
glennliao
2023-02-20 22:03:26 +08:00
committed by GitHub
parent ed858ebd4b
commit b4f76b8448
3 changed files with 48 additions and 11 deletions

View File

@ -152,7 +152,7 @@ func (d *Driver) TableFields(
if link, err = d.SlaveLink(usedSchema); err != nil {
return nil, err
}
result, err = d.DoSelect(ctx, link, fmt.Sprintf(`PRAGMA TABLE_INFO(%s)`, table))
result, err = d.DoSelect(ctx, link, fmt.Sprintf(`PRAGMA TABLE_INFO(%s)`, d.QuoteWord(table)))
if err != nil {
return nil, err
}

View File

@ -33,15 +33,16 @@ var (
)
const (
TableSize = 10
TableName = "user"
TestSchema1 = "test1"
TestSchema2 = "test2"
TableNamePrefix = "gf_"
CreateTime = "2018-10-24 10:00:00"
DBGroupTest = "test"
DBGroupPrefix = "prefix"
DBGroupInvalid = "invalid"
TableSize = 10
TableName = "user"
TableNameWhichIsKeyword = "group"
TestSchema1 = "test1"
TestSchema2 = "test2"
TableNamePrefix = "gf_"
CreateTime = "2018-10-24 10:00:00"
DBGroupTest = "test"
DBGroupPrefix = "prefix"
DBGroupInvalid = "invalid"
)
func init() {
@ -125,7 +126,7 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
nickname VARCHAR(45),
create_time DATETIME
);
`, name,
`, db.GetCore().QuoteWord(name),
)); err != nil {
gtest.Fatal(err)
}

View File

@ -1571,3 +1571,39 @@ func Test_TableFields(t *testing.T) {
gtest.AssertNE(err, nil)
})
}
func Test_TableNameIsKeyword(t *testing.T) {
table := createInitTable(TableNameWhichIsKeyword)
defer dropTable(table)
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := 1
result, err := db.Model(table).Fields("*").Where("id = ?", id).All()
if err != nil {
gtest.Fatal(err)
}
type t_user struct {
Id int
Passport string
Password string
NickName string
CreateTime string
}
t_users := make([]t_user, 0)
err = result.Structs(&t_users)
if err != nil {
gtest.Fatal(err)
}
resultIntMap := result.MapKeyInt("id")
t.Assert(t_users[0].Id, resultIntMap[id]["id"])
t.Assert(t_users[0].Passport, resultIntMap[id]["passport"])
t.Assert(t_users[0].Password, resultIntMap[id]["password"])
t.Assert(t_users[0].NickName, resultIntMap[id]["nickname"])
t.Assert(t_users[0].CreateTime, resultIntMap[id]["create_time"])
})
}