mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
expose insert option constants for package gdb
This commit is contained in:
parent
795ac4773e
commit
7e5301c845
@ -278,10 +278,6 @@ const (
|
|||||||
queryTypeCount = 1
|
queryTypeCount = 1
|
||||||
unionTypeNormal = 0
|
unionTypeNormal = 0
|
||||||
unionTypeAll = 1
|
unionTypeAll = 1
|
||||||
insertOptionDefault = 0
|
|
||||||
insertOptionReplace = 1
|
|
||||||
insertOptionSave = 2
|
|
||||||
insertOptionIgnore = 3
|
|
||||||
defaultBatchNumber = 10 // Per count for batch insert/replace/save.
|
defaultBatchNumber = 10 // Per count for batch insert/replace/save.
|
||||||
defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
|
defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
|
||||||
defaultMaxOpenConnCount = 0 // Max open connection count in pool. Default is no limit.
|
defaultMaxOpenConnCount = 0 // Max open connection count in pool. Default is no limit.
|
||||||
@ -294,6 +290,13 @@ const (
|
|||||||
dbRoleSlave = `slave`
|
dbRoleSlave = `slave`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
InsertOptionDefault = 0
|
||||||
|
InsertOptionReplace = 1
|
||||||
|
InsertOptionSave = 2
|
||||||
|
InsertOptionIgnore = 3
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SqlTypeBegin = "DB.Begin"
|
SqlTypeBegin = "DB.Begin"
|
||||||
SqlTypeTXCommit = "TX.Commit"
|
SqlTypeTXCommit = "TX.Commit"
|
||||||
|
@ -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
|
keysStr = charL + strings.Join(keys, charR+","+charL) + charR
|
||||||
operation = GetInsertOperationByOption(option.InsertOption)
|
operation = GetInsertOperationByOption(option.InsertOption)
|
||||||
)
|
)
|
||||||
if option.InsertOption == insertOptionSave {
|
if option.InsertOption == InsertOptionSave {
|
||||||
onDuplicateStr = c.formatOnDuplicate(keys, option)
|
onDuplicateStr = c.formatOnDuplicate(keys, option)
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
|
@ -295,10 +295,10 @@ ORDER BY a.id,a.colorder`,
|
|||||||
// DoInsert is not supported in mssql.
|
// 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) {
|
func (d *DriverMssql) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
|
||||||
switch option.InsertOption {
|
switch option.InsertOption {
|
||||||
case insertOptionSave:
|
case InsertOptionSave:
|
||||||
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by mssql driver`)
|
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`)
|
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by mssql driver`)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -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;
|
// 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) {
|
func (d *DriverOracle) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
|
||||||
switch option.InsertOption {
|
switch option.InsertOption {
|
||||||
case insertOptionSave:
|
case InsertOptionSave:
|
||||||
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by mssql driver`)
|
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`)
|
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by mssql driver`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,10 +195,10 @@ ORDER BY a.attnum`,
|
|||||||
// DoInsert is not supported in pgsql.
|
// 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) {
|
func (d *DriverPgsql) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
|
||||||
switch option.InsertOption {
|
switch option.InsertOption {
|
||||||
case insertOptionSave:
|
case InsertOptionSave:
|
||||||
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by pgsql driver`)
|
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`)
|
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by pgsql driver`)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -146,10 +146,10 @@ func (d *DriverSqlite) TableFields(ctx context.Context, table string, schema ...
|
|||||||
// DoInsert is not supported in sqlite.
|
// 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) {
|
func (d *DriverSqlite) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
|
||||||
switch option.InsertOption {
|
switch option.InsertOption {
|
||||||
case insertOptionSave:
|
case InsertOptionSave:
|
||||||
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by sqlite driver`)
|
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`)
|
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by sqlite driver`)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -147,9 +147,9 @@ func ListItemValuesUnique(list interface{}, key string, subKey ...interface{}) [
|
|||||||
func GetInsertOperationByOption(option int) string {
|
func GetInsertOperationByOption(option int) string {
|
||||||
var operator string
|
var operator string
|
||||||
switch option {
|
switch option {
|
||||||
case insertOptionReplace:
|
case InsertOptionReplace:
|
||||||
operator = "REPLACE"
|
operator = "REPLACE"
|
||||||
case insertOptionIgnore:
|
case InsertOptionIgnore:
|
||||||
operator = "INSERT IGNORE"
|
operator = "INSERT IGNORE"
|
||||||
default:
|
default:
|
||||||
operator = "INSERT"
|
operator = "INSERT"
|
||||||
|
@ -167,7 +167,7 @@ func (m *Model) Insert(data ...interface{}) (result sql.Result, err error) {
|
|||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
return m.Data(data...).Insert()
|
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.
|
// 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 {
|
if len(data) > 0 {
|
||||||
return m.Data(data...).InsertAndGetId()
|
return m.Data(data...).InsertAndGetId()
|
||||||
}
|
}
|
||||||
result, err := m.doInsertWithOption(insertOptionDefault)
|
result, err := m.doInsertWithOption(InsertOptionDefault)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -189,7 +189,7 @@ func (m *Model) InsertIgnore(data ...interface{}) (result sql.Result, err error)
|
|||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
return m.Data(data...).InsertIgnore()
|
return m.Data(data...).InsertIgnore()
|
||||||
}
|
}
|
||||||
return m.doInsertWithOption(insertOptionIgnore)
|
return m.doInsertWithOption(InsertOptionIgnore)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace does "REPLACE INTO ..." statement for the model.
|
// 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 {
|
if len(data) > 0 {
|
||||||
return m.Data(data...).Replace()
|
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.
|
// 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 {
|
if len(data) > 0 {
|
||||||
return m.Data(data...).Save()
|
return m.Data(data...).Save()
|
||||||
}
|
}
|
||||||
return m.doInsertWithOption(insertOptionSave)
|
return m.doInsertWithOption(InsertOptionSave)
|
||||||
}
|
}
|
||||||
|
|
||||||
// doInsertWithOption inserts data with option parameter.
|
// doInsertWithOption inserts data with option parameter.
|
||||||
@ -318,7 +318,7 @@ func (m *Model) formatDoInsertOption(insertOption int, columnNames []string) (op
|
|||||||
InsertOption: insertOption,
|
InsertOption: insertOption,
|
||||||
BatchCount: m.getBatch(),
|
BatchCount: m.getBatch(),
|
||||||
}
|
}
|
||||||
if insertOption == insertOptionSave {
|
if insertOption == InsertOptionSave {
|
||||||
onDuplicateExKeys, err := m.formatOnDuplicateExKeys(m.onDuplicateEx)
|
onDuplicateExKeys, err := m.formatOnDuplicateExKeys(m.onDuplicateEx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return option, err
|
return option, err
|
||||||
|
Loading…
Reference in New Issue
Block a user