refactor insert condition generation #1998

Merged
lunny merged 5 commits from lunny/refactor_insert_cond into master 2021-07-20 05:46:24 +00:00
21 changed files with 25 additions and 67 deletions
Showing only changes of commit 0cfaedd2dc - Show all commits

View File

@ -373,7 +373,6 @@ func asTime(src interface{}, dbLoc *time.Location, uiLoc *time.Location) (*time.
case *sql.NullInt64:
tm := time.Unix(t.Int64, 0).In(uiLoc)
return &tm, nil
}
return nil, fmt.Errorf("unsupported value %#v as time", src)
}
@ -751,7 +750,6 @@ func asKind(vv reflect.Value, tp reflect.Type) (interface{}, error) {
}
return v, nil
}
}
return nil, fmt.Errorf("unsupported primary key type: %v, %v", tp, vv)
}
@ -946,8 +944,10 @@ var (
_ sql.Scanner = &EmptyScanner{}
)
// EmptyScanner represents an empty scanner which will ignore the scan
type EmptyScanner struct{}
// Scan implements sql.Scanner
func (EmptyScanner) Scan(value interface{}) error {
return nil
}

View File

@ -10,6 +10,7 @@ import (
"time"
)
// Interface2Interface converts interface of pointer as interface of value
func Interface2Interface(userLocation *time.Location, v interface{}) (interface{}, error) {
if v == nil {
return nil, nil

View File

@ -45,5 +45,5 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
return &tm, nil
}
}
return nil, fmt.Errorf("unsupported convertion from %s to time", s)
return nil, fmt.Errorf("unsupported conversion from %s to time", s)
}

View File

@ -18,14 +18,9 @@ type ScanContext struct {
UserLocation *time.Location
}
type DriverFeatures struct {
SupportNullable bool
}
// Driver represents a database driver
type Driver interface {
Parse(string, string) (*URI, error)
Features() DriverFeatures
GenScanResult(string) (interface{}, error) // according given column type generating a suitable scan interface
Scan(*ScanContext, *core.Rows, []*sql.ColumnType, ...interface{}) error
}
@ -82,9 +77,3 @@ type baseDriver struct{}
func (b *baseDriver) Scan(ctx *ScanContext, rows *core.Rows, types []*sql.ColumnType, v ...interface{}) error {
return rows.Scan(v...)
}
func (b *baseDriver) Features() DriverFeatures {
return DriverFeatures{
SupportNullable: true,
}
}

View File

@ -667,8 +667,7 @@ func (p *odbcDriver) Parse(driverName, dataSourceName string) (*URI, error) {
for _, c := range kv {
vv := strings.Split(strings.TrimSpace(c), "=")
if len(vv) == 2 {
switch strings.ToLower(vv[0]) {
case "database":
if strings.ToLower(vv[0]) == "database" {
dbName = vv[1]
}
}

View File

@ -257,9 +257,6 @@ func (db *mysql) SetParams(params map[string]string) {
fallthrough
case "COMPRESSED":
db.rowFormat = t
break
default:
break
}
}
}
@ -699,14 +696,12 @@ func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*URI, error) {
for _, kv := range kvs {
splits := strings.Split(kv, "=")
if len(splits) == 2 {
switch splits[0] {
case "charset":
if splits[0] == "charset" {
uri.Charset = splits[1]
}
}
}
}
}
}
return uri, nil

View File

@ -854,7 +854,7 @@ type godrorDriver struct {
baseDriver
}
func (cfg *godrorDriver) Parse(driverName, dataSourceName string) (*URI, error) {
func (g *godrorDriver) Parse(driverName, dataSourceName string) (*URI, error) {
db := &URI{DBType: schemas.ORACLE}
dsnPattern := regexp.MustCompile(
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
@ -866,8 +866,7 @@ func (cfg *godrorDriver) Parse(driverName, dataSourceName string) (*URI, error)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "dbname":
if names[i] == "dbname" {
db.DBName = match
}
}
@ -877,7 +876,7 @@ func (cfg *godrorDriver) Parse(driverName, dataSourceName string) (*URI, error)
return db, nil
}
func (p *godrorDriver) GenScanResult(colType string) (interface{}, error) {
func (g *godrorDriver) GenScanResult(colType string) (interface{}, error) {
switch colType {
case "CHAR", "NCHAR", "VARCHAR", "VARCHAR2", "NVARCHAR2", "LONG", "CLOB", "NCLOB":
var s sql.NullString
@ -903,7 +902,7 @@ type oci8Driver struct {
// dataSourceName=user/password@ipv4:port/dbname
// dataSourceName=user/password@[ipv6]:port/dbname
func (p *oci8Driver) Parse(driverName, dataSourceName string) (*URI, error) {
func (o *oci8Driver) Parse(driverName, dataSourceName string) (*URI, error) {
db := &URI{DBType: schemas.ORACLE}
dsnPattern := regexp.MustCompile(
`^(?P<user>.*)\/(?P<password>.*)@` + // user:password@
@ -912,8 +911,7 @@ func (p *oci8Driver) Parse(driverName, dataSourceName string) (*URI, error) {
matches := dsnPattern.FindStringSubmatch(dataSourceName)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "dbname":
if names[i] == "dbname" {
db.DBName = match
}
}

View File

@ -1339,12 +1339,6 @@ type pqDriver struct {
baseDriver
}
func (b *pqDriver) Features() DriverFeatures {
return DriverFeatures{
SupportNullable: false,
}
}
type values map[string]string
func (vs values) Set(k, v string) {

View File

@ -76,9 +76,7 @@ func TestParsePgx(t *testing.T) {
} else if err == nil && !reflect.DeepEqual(test.expected, uri.DBName) {
t.Errorf("%q got: %#v want: %#v", test.in, uri.DBName, test.expected)
}
}
}
func TestGetIndexColName(t *testing.T) {

View File

@ -598,9 +598,3 @@ func (p *sqlite3Driver) GenScanResult(colType string) (interface{}, error) {
return &r, nil
}
}
func (b *sqlite3Driver) Features() DriverFeatures {
return DriverFeatures{
SupportNullable: false,
}
}

View File

@ -172,7 +172,6 @@ func TestDumpTables(t *testing.T) {
name := fmt.Sprintf("dump_%v-table.sql", tp)
t.Run(name, func(t *testing.T) {
assert.NoError(t, testEngine.(*xorm.Engine).DumpTablesToFile([]*schemas.Table{tb}, name, tp))
})
}

View File

@ -1313,7 +1313,6 @@ func TestUpdateIgnoreOnlyFromDBFields(t *testing.T) {
assert.EqualValues(t, true, has)
assert.EqualValues(t, "", record.OnlyFromDBField)
return &record
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(TestOnlyFromDBField))

View File

@ -781,7 +781,6 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
return pkField.Interface(), true, nil
}
return nil, false, nil
}
return nil, false, fmt.Errorf("not supported %v as %v", fieldValue.Interface(), table.PrimaryKeys)
}
@ -811,16 +810,14 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
return fieldValue.Bytes(), true, nil
}
return nil, false, nil
} else {
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, false, err
}
return bytes, true, nil
}
} else {
return nil, false, nil
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, false, err
}
return bytes, true, nil
}
return nil, false, nil
}
return fieldValue.Interface(), true, nil
}

View File

@ -23,7 +23,7 @@ var (
bigFloatType = reflect.TypeOf(big.Float{})
)
// Value2Interface convert a field value of a struct to interface for puting into database
// Value2Interface convert a field value of a struct to interface for putting into database
func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue reflect.Value) (interface{}, error) {
if fieldValue.CanAddr() {
if fieldConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok {

View File

@ -13,7 +13,7 @@ func IndexNoCase(s, sep string) int {
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
}
// SplitNoCase split a string by a seperator with no care of capitalize
// SplitNoCase split a string by a separator with no care of capitalize
func SplitNoCase(s, sep string) []string {
idx := IndexNoCase(s, sep)
if idx < 0 {
@ -22,7 +22,7 @@ func SplitNoCase(s, sep string) []string {
return strings.Split(s, s[idx:idx+len(sep)])
}
// SplitNNoCase split n by a seperator with no care of capitalize
// SplitNNoCase split n by a separator with no care of capitalize
func SplitNNoCase(s, sep string, n int) []string {
idx := IndexNoCase(s, sep)
if idx < 0 {

View File

@ -79,7 +79,7 @@ func (m SameMapper) Table2Obj(t string) string {
return t
}
// SnakeMapper implements IMapper and provides name transaltion between
// SnakeMapper implements IMapper and provides name translation between
// struct and database table
type SnakeMapper struct {
}

View File

@ -211,12 +211,8 @@ func (engine *Engine) scan(rows *core.Rows, fields []string, types []*sql.Column
scanResult = &sql.RawBytes{}
replaced = true
default:
var useNullable = true
if engine.driver.Features().SupportNullable {
nullable, ok := types[0].Nullable()
useNullable = ok && nullable
}
if useNullable {
nullable, ok := types[0].Nullable()
if !ok || nullable {
scanResult, replaced, err = genScanResultsByBeanNullable(v)
} else {
scanResult, replaced, err = genScanResultsByBean(v)

View File

@ -58,7 +58,6 @@ func TestGetColumnIdx(t *testing.T) {
func BenchmarkGetColumnWithToLower(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range testsGetColumn {
if _, ok := table.columnsMap[strings.ToLower(test.name)]; !ok {
b.Errorf("Column not found:%s", test.name)
}
@ -69,7 +68,6 @@ func BenchmarkGetColumnWithToLower(b *testing.B) {
func BenchmarkGetColumnIdxWithToLower(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range testsGetColumn {
if c, ok := table.columnsMap[strings.ToLower(test.name)]; ok {
if test.idx < len(c) {
continue

View File

@ -65,6 +65,7 @@ func (s *SQLType) IsTime() bool {
return s.IsType(TIME_TYPE)
}
// IsBool returns true if column is a boolean type
func (s *SQLType) IsBool() bool {
return s.IsType(BOOL_TYPE)
}

View File

@ -325,7 +325,6 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
copy(afterClosures, session.afterClosures)
session.afterInsertBeans[bean] = &afterClosures
}
} else {
if _, ok := interface{}(bean).(AfterInsertProcessor); ok {
session.afterInsertBeans[bean] = nil

View File

@ -124,6 +124,7 @@ func addIndex(indexName string, table *schemas.Table, col *schemas.Column, index
}
}
// ErrIgnoreField represents an error to ignore field
var ErrIgnoreField = errors.New("field will be ignored")
func (parser *Parser) parseFieldWithNoTag(fieldIndex int, field reflect.StructField, fieldValue reflect.Value) (*schemas.Column, error) {