refactor: 实现 DbType 类型,集中处理部分差异化的数据库操作

This commit is contained in:
kanzihuang
2023-10-15 10:36:01 +08:00
parent f04b82c933
commit ba82b5b516
8 changed files with 127 additions and 61 deletions

View File

@@ -9,46 +9,46 @@ import (
func Test_escapeSql(t *testing.T) {
tests := []struct {
name string
dbType string
dbType entity.DBType
sql string
want string
}{
{
dbType: entity.DbTypeMysql,
dbType: entity.DBTypeMysql{},
sql: "\\a\\b",
want: "'\\\\a\\\\b'",
},
{
dbType: entity.DbTypeMysql,
dbType: entity.DBTypeMysql{},
sql: "'a'",
want: "'''a'''",
},
{
name: "不间断空格",
dbType: entity.DbTypeMysql,
dbType: entity.DBTypeMysql{},
sql: "a\u00A0b",
want: "'a\u00A0b'",
},
{
dbType: entity.DbTypePostgres,
dbType: entity.DBTypePostgres{},
sql: "\\a\\b",
want: " E'\\\\a\\\\b'",
},
{
dbType: entity.DbTypePostgres,
dbType: entity.DBTypePostgres{},
sql: "'a'",
want: "'''a'''",
},
{
name: "不间断空格",
dbType: entity.DbTypePostgres,
dbType: entity.DBTypePostgres{},
sql: "a\u00A0b",
want: "'a\u00A0b'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := escapeSql(tt.dbType, tt.sql)
got := tt.dbType.QuoteLiteral(tt.sql)
require.Equal(t, tt.want, got)
})
}