From 7e5301c8458bf02f89d81ac346745fba04aa4c46 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 21 Jan 2022 17:31:48 +0800 Subject: [PATCH] expose insert option constants for package gdb --- database/gdb/gdb.go | 11 +++++++---- database/gdb/gdb_core.go | 2 +- database/gdb/gdb_driver_mssql.go | 4 ++-- database/gdb/gdb_driver_oracle.go | 4 ++-- database/gdb/gdb_driver_pgsql.go | 4 ++-- database/gdb/gdb_driver_sqlite.go | 4 ++-- database/gdb/gdb_func.go | 4 ++-- database/gdb/gdb_model_insert.go | 12 ++++++------ 8 files changed, 24 insertions(+), 21 deletions(-) diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index f99367a18..3ce4c6ded 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -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" diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 9921f40a1..c92e4f9a4 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -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 ( diff --git a/database/gdb/gdb_driver_mssql.go b/database/gdb/gdb_driver_mssql.go index 7f9983e44..c2acb21a5 100644 --- a/database/gdb/gdb_driver_mssql.go +++ b/database/gdb/gdb_driver_mssql.go @@ -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: diff --git a/database/gdb/gdb_driver_oracle.go b/database/gdb/gdb_driver_oracle.go index a853378c5..68976006d 100644 --- a/database/gdb/gdb_driver_oracle.go +++ b/database/gdb/gdb_driver_oracle.go @@ -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`) } diff --git a/database/gdb/gdb_driver_pgsql.go b/database/gdb/gdb_driver_pgsql.go index 77cf3db4c..f000ea84d 100644 --- a/database/gdb/gdb_driver_pgsql.go +++ b/database/gdb/gdb_driver_pgsql.go @@ -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: diff --git a/database/gdb/gdb_driver_sqlite.go b/database/gdb/gdb_driver_sqlite.go index ed9aa0ef1..68a2bea99 100644 --- a/database/gdb/gdb_driver_sqlite.go +++ b/database/gdb/gdb_driver_sqlite.go @@ -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: diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index b9055028a..0b6047ec2 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -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" diff --git a/database/gdb/gdb_model_insert.go b/database/gdb/gdb_model_insert.go index e598d5eb4..09787ec93 100644 --- a/database/gdb/gdb_model_insert.go +++ b/database/gdb/gdb_model_insert.go @@ -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