4 Star 16 Fork 12

北京小程科技有限公司/数据库批量同步工具

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
update.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
shallot 提交于 2020-04-25 01:45 +08:00 . 添加SQLite的支持,增加测试样例。
package dbsync
import (
"errors"
"time"
)
// 插入数据时的配置信息
type UpdateOptions struct {
Columns []string // 列名称
TimeFields []string // time.Time时间类型的列名称列表
FixedFields map[string]interface{} // 固定的插入列
UniqueFields []string // 唯一键或主键的列名称列表
SqlType string // 数据库类型,目前支持MySQL和SQLite
}
func (m UpdateOptions) IsMySQL() bool {
return m.SqlType == SqlTypeMySQL
}
func (m UpdateOptions) IsSqlite() bool {
return m.SqlType == SqlTypeSqlite
}
// 通用插入数据
func update(
db SQLCommon,
tableName string,
data [][]interface{},
options UpdateOptions,
) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
// 按数据库类型处理
if options.IsMySQL() {
err = updateMySQL(db, tableName, data, options)
} else if options.IsSqlite() {
for _, dataItem := range data {
if err = updateSqlite(db, tableName, dataItem, options); err != nil {
return
}
}
} else {
err = errors.New("sql type is not supported")
}
return
}
// 类型转换方法
func convertTimeType(data interface{}) interface{} {
if data != nil {
return time.Unix(int64(data.(float64)), 0)
} else {
return nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiaochengtech/dbsync.git
git@gitee.com:xiaochengtech/dbsync.git
xiaochengtech
dbsync
数据库批量同步工具
master

搜索帮助