- 新增 dialect/sqlite3/vtab 包,提供干净的虚拟表接口(Module/Table/Cursor) - 适配层自动将 BestIndex 约束与 Filter 值绑定(ConstraintInfo.Value),无需手动编解码 IdxStr - 支持 OpLIMIT/OpOFFSET 约束下推 - 新增 SupportsDistinct 方言选项,控制 SELECT 级和表达式级 DISTINCT 生成 - sqlite3 方言注册 IF() 函数支持
219 lines
5.8 KiB
Go
219 lines
5.8 KiB
Go
package sqlite3_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"git.fsdpf.net/go/db"
|
|
"git.fsdpf.net/go/db/engine"
|
|
"git.fsdpf.net/go/db/schema"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var (
|
|
tableName = "entry"
|
|
|
|
dropTable = "DROP TABLE IF EXISTS `entry`;"
|
|
|
|
createTable = "CREATE TABLE IF NOT EXISTS `entry` (" +
|
|
"`id` INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
"`int` INT NOT NULL ," +
|
|
"`float` FLOAT NOT NULL ," +
|
|
"`string` VARCHAR(255) NOT NULL ," +
|
|
"`time` DATETIME NOT NULL ," +
|
|
"`bool` TINYINT NOT NULL ," +
|
|
"`bytes` BLOB NOT NULL" +
|
|
");"
|
|
)
|
|
|
|
type sqlite3Test struct {
|
|
suite.Suite
|
|
schema schema.Schema
|
|
}
|
|
|
|
func TestSqlite3Suite(t *testing.T) {
|
|
suite.Run(t, new(sqlite3Test))
|
|
}
|
|
|
|
func (t *sqlite3Test) SetupSuite() {
|
|
db := engine.Open(map[string]engine.DBConfig{
|
|
"test-sqlite3": engine.NewDBConfig("sqlite3",
|
|
engine.WithSQLiteFile(os.Getenv("DB_FILE")),
|
|
),
|
|
}).Connection("test-sqlite3")
|
|
|
|
t.schema = schema.GetSchemaDialect(db)
|
|
|
|
// db.Exec(dropTable)
|
|
if _, err := db.Exec(createTable); err != nil {
|
|
t.Require().NoError(err)
|
|
}
|
|
}
|
|
|
|
func (t *sqlite3Test) TestTableExists() {
|
|
result, err := t.schema.TableExists(tableName)
|
|
t.Require().NoError(err)
|
|
t.Require().Equal(result, true)
|
|
}
|
|
|
|
func (t *sqlite3Test) TestGetColumnListing() {
|
|
result, err := t.schema.GetColumnListing(tableName)
|
|
t.Require().NoError(err)
|
|
t.Require().ElementsMatch([]string{"bool", "bytes", "float", "id", "int", "string", "time"}, result)
|
|
}
|
|
|
|
// 创建列表
|
|
func (t *sqlite3Test) TestCompileCreate() {
|
|
bp := schema.NewBlueprint("users")
|
|
bp.Create()
|
|
bp.Charset = "utf8mb4"
|
|
|
|
bp.BigIncrements("id").AutoIncrement().Comment("ID")
|
|
bp.Boolean("enabled").Default("1").Comment("是否有效")
|
|
bp.Char("created_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("创建者")
|
|
bp.Char("owned_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("拥有者")
|
|
bp.Timestamp("created_at").UseCurrent().Comment("创建时间")
|
|
bp.Timestamp("updated_at").UseCurrent().Default(db.L("ON UPDATE CURRENT_TIMESTAMP")).Comment("更新时间")
|
|
bp.DateTime("deleted_at").Nullable().Comment("删除时间")
|
|
|
|
sql := t.schema.CompileCreate(bp)
|
|
|
|
t.Equal([]string{
|
|
"CREATE TABLE IF NOT EXISTS `users` (\n" +
|
|
"`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, -- ID\n" +
|
|
"`enabled` tinyint(1) NOT NULL DEFAULT '1', -- 是否有效\n" +
|
|
"`created_user` char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000', -- 创建者\n" +
|
|
"`owned_user` char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000', -- 拥有者\n" +
|
|
"`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间\n" +
|
|
"`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间\n" +
|
|
"`deleted_at` datetime NULL -- 删除时间\n" +
|
|
")",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 添加字段
|
|
func (t *sqlite3Test) TestCompileAdd() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.String("name", 50).Comment("用户名")
|
|
bp.SmallInteger("age").Default("18").Comment("年龄")
|
|
bp.Enum("sex", []string{"0", "1"}).Default("0").Comment("性别")
|
|
|
|
sql := t.schema.CompileAdd(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE `users` ADD COLUMN `name` varchar(50) NOT NULL",
|
|
"ALTER TABLE `users` ADD COLUMN `age` smallint(4) NOT NULL DEFAULT '18'",
|
|
"ALTER TABLE `users` ADD COLUMN `sex` varchar check('sex' in('0', '1')) NOT NULL DEFAULT '0'",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 修改字段
|
|
func (t *sqlite3Test) TestCompileChange() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.String("name", 50).Change("username")
|
|
bp.SmallInteger("age").Default("19").Comment("年龄").Change()
|
|
|
|
sql := t.schema.CompileChange(bp)
|
|
t.Equal(len([]string{
|
|
"ALTER TABLE `users` ADD COLUMN `name_1742738970482105700` varchar(50) NOT NULL",
|
|
"UPDATE `users` SET `name_1742738970482105700`=`name`",
|
|
"ALTER TABLE `users` DROP COLUMN `name`",
|
|
"ALTER TABLE `users` RENAME COLUMN `name_1742738970482105700` TO `username`",
|
|
"ALTER TABLE `users` ADD COLUMN `age_1742738970482151800` smallint(4) NOT NULL DEFAULT '19'",
|
|
"UPDATE `users` SET `age_1742738970482151800`=`age`",
|
|
"ALTER TABLE `users` DROP COLUMN `age`",
|
|
"ALTER TABLE `users` RENAME COLUMN `age_1742738970482151800` TO `age`",
|
|
}), len(sql))
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 删除字段
|
|
func (t *sqlite3Test) TestCompileDropColumn() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.DropColumn("age", "sex")
|
|
|
|
sql := t.schema.CompileDropColumn(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE `users` DROP COLUMN `age`",
|
|
"ALTER TABLE `users` DROP COLUMN `sex`",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 删除表
|
|
func (t *sqlite3Test) TestCompileDrop() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Drop()
|
|
|
|
sql := t.schema.CompileDrop(bp)
|
|
|
|
t.Equal([]string{"DROP TABLE `users`"}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 删除表
|
|
func (t *sqlite3Test) TestCompileDropIfExists() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Drop()
|
|
|
|
sql := t.schema.CompileDropIfExists(bp)
|
|
|
|
t.Equal([]string{"DROP TABLE IF EXISTS `users`"}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// HIDDEN 列(虚拟表参数列)
|
|
func (t *sqlite3Test) TestCompileCreate_HiddenColumn() {
|
|
bp := schema.NewBlueprint("api_users")
|
|
bp.Create()
|
|
|
|
bp.BigIncrements("id").AutoIncrement()
|
|
bp.String("name", 100).Nullable()
|
|
bp.Integer("age")
|
|
bp.String("token", 255).Hidden().Nullable()
|
|
bp.Integer("page_size").Hidden().Nullable()
|
|
|
|
sql := t.schema.CompileCreate(bp)
|
|
|
|
t.Equal([]string{
|
|
"CREATE TABLE IF NOT EXISTS `api_users` (\n" +
|
|
"`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT\n" +
|
|
"`name` varchar(100) NULL\n" +
|
|
"`age` INTEGER(0) NOT NULL\n" +
|
|
"`token` varchar(255) HIDDEN\n" +
|
|
"`page_size` INTEGER(0) HIDDEN\n" +
|
|
")",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 表重命名
|
|
func (t *sqlite3Test) TestCompileRename() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Rename("user")
|
|
|
|
sql := t.schema.CompileRename(bp)
|
|
|
|
t.Equal([]string{"RENAME TABLE `users` TO `user`"}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|