expose insert option constants for package gdb

This commit is contained in:
John Guo 2022-01-21 17:31:48 +08:00
parent 795ac4773e
commit 7e5301c845
8 changed files with 24 additions and 21 deletions

View File

@ -278,10 +278,6 @@ const (
queryTypeCount = 1
unionTypeNormal = 0
unionTypeAll = 1
insertOptionDefault = 0
insertOptionReplace = 1
insertOptionSave = 2
insertOptionIgnore = 3
defaultBatchNumber = 10 // Per count for batch insert/replace/save.
defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
defaultMaxOpenConnCount = 0 // Max open connection count in pool. Default is no limit.
@ -294,6 +290,13 @@ const (
dbRoleSlave = `slave`
)
const (
InsertOptionDefault = 0
InsertOptionReplace = 1
InsertOptionSave = 2
InsertOptionIgnore = 3
)
const (
SqlTypeBegin = "DB.Begin"
SqlTypeTXCommit = "TX.Commit"

View File

@ -415,7 +415,7 @@ func (c *Core) DoInsert(ctx context.Context, link Link, table string, list List,
keysStr = charL + strings.Join(keys, charR+","+charL) + charR
operation = GetInsertOperationByOption(option.InsertOption)
)
if option.InsertOption == insertOptionSave {
if option.InsertOption == InsertOptionSave {
onDuplicateStr = c.formatOnDuplicate(keys, option)
}
var (

View File

@ -295,10 +295,10 @@ ORDER BY a.id,a.colorder`,
// DoInsert is not supported in mssql.
func (d *DriverMssql) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
switch option.InsertOption {
case insertOptionSave:
case InsertOptionSave:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by mssql driver`)
case insertOptionReplace:
case InsertOptionReplace:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by mssql driver`)
default:

View File

@ -255,10 +255,10 @@ FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '%s' ORDER BY COLUMN_ID`,
// 3: ignore: if there's unique/primary key in the data, it ignores the inserting;
func (d *DriverOracle) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
switch option.InsertOption {
case insertOptionSave:
case InsertOptionSave:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by mssql driver`)
case insertOptionReplace:
case InsertOptionReplace:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by mssql driver`)
}

View File

@ -195,10 +195,10 @@ ORDER BY a.attnum`,
// DoInsert is not supported in pgsql.
func (d *DriverPgsql) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
switch option.InsertOption {
case insertOptionSave:
case InsertOptionSave:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by pgsql driver`)
case insertOptionReplace:
case InsertOptionReplace:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by pgsql driver`)
default:

View File

@ -146,10 +146,10 @@ func (d *DriverSqlite) TableFields(ctx context.Context, table string, schema ...
// DoInsert is not supported in sqlite.
func (d *DriverSqlite) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
switch option.InsertOption {
case insertOptionSave:
case InsertOptionSave:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by sqlite driver`)
case insertOptionReplace:
case InsertOptionReplace:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by sqlite driver`)
default:

View File

@ -147,9 +147,9 @@ func ListItemValuesUnique(list interface{}, key string, subKey ...interface{}) [
func GetInsertOperationByOption(option int) string {
var operator string
switch option {
case insertOptionReplace:
case InsertOptionReplace:
operator = "REPLACE"
case insertOptionIgnore:
case InsertOptionIgnore:
operator = "INSERT IGNORE"
default:
operator = "INSERT"

View File

@ -167,7 +167,7 @@ func (m *Model) Insert(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Insert()
}
return m.doInsertWithOption(insertOptionDefault)
return m.doInsertWithOption(InsertOptionDefault)
}
// InsertAndGetId performs action Insert and returns the last insert id that automatically generated.
@ -175,7 +175,7 @@ func (m *Model) InsertAndGetId(data ...interface{}) (lastInsertId int64, err err
if len(data) > 0 {
return m.Data(data...).InsertAndGetId()
}
result, err := m.doInsertWithOption(insertOptionDefault)
result, err := m.doInsertWithOption(InsertOptionDefault)
if err != nil {
return 0, err
}
@ -189,7 +189,7 @@ func (m *Model) InsertIgnore(data ...interface{}) (result sql.Result, err error)
if len(data) > 0 {
return m.Data(data...).InsertIgnore()
}
return m.doInsertWithOption(insertOptionIgnore)
return m.doInsertWithOption(InsertOptionIgnore)
}
// Replace does "REPLACE INTO ..." statement for the model.
@ -199,7 +199,7 @@ func (m *Model) Replace(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Replace()
}
return m.doInsertWithOption(insertOptionReplace)
return m.doInsertWithOption(InsertOptionReplace)
}
// Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the model.
@ -212,7 +212,7 @@ func (m *Model) Save(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Save()
}
return m.doInsertWithOption(insertOptionSave)
return m.doInsertWithOption(InsertOptionSave)
}
// doInsertWithOption inserts data with option parameter.
@ -318,7 +318,7 @@ func (m *Model) formatDoInsertOption(insertOption int, columnNames []string) (op
InsertOption: insertOption,
BatchCount: m.getBatch(),
}
if insertOption == insertOptionSave {
if insertOption == InsertOptionSave {
onDuplicateExKeys, err := m.formatOnDuplicateExKeys(m.onDuplicateEx)
if err != nil {
return option, err