golang适配国产数据库

随着国产化的推进,越来越多的企业选择国产数据库替代mysql或者Oracle。

本篇主要介绍golang如何适配国产数据库

主流的国产数据库有达梦,金仓,神通,优炫,南大通用数据库等

前提环境

unixODBC安装

bash 复制代码
yum -y install unixODBC unixODBC-devel

配置 gbase8s-odbc-driver

bash 复制代码
wget https://gbasedbt.com/dl/odbc/GBase8s_3.0.0_1-Linux64-ODBC-Driver.tar.gz --no-check-certificate
tar -zxf GBase8s_3.0.0_1-Linux64-ODBC-Driver.tar.gz

配置 sqlhosts

bash 复制代码
gbase01  onsoctcp        ip  9088    g=db_group
cm_update       group   -       -       i=2,c=0
w1      onsoctcp        ip  18888   g=cm_update
cm_read         group   -       -       i=3,c=0
r1      onsoctcp        ip  19999   g=cm_read

配置环境变量 (使用 root) vim /etc/profile

bash 复制代码
export GBASEDBTDIR=/home/gbase8s-odbc-driver
export GBASEDBTSERVER=gbase01
export PATH=$GBASEDBTDIR/bin:$PATH
export GBASEDBTSQLHOSTS=$GBASEDBTDIR/etc/sqlhosts
export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/esql:$GBASEDBTDIR/lib/cli:$LD_LIBRARY_PATH
export ODBCINI=/home/gbase8s-odbc-driver/odbc.ini

配置 驱动 (使用 root) vim /etc/odbcinst.ini

bash 复制代码
[com.gbasedbt.jdbc.Driver]
Driver=/home/gbase8s-odbc-driver/lib/cli/iclit09b.so
Setup=/home/gbase8s-odbc-driver/lib/cli/iclit09b.so
APILevel=1
ConnectFunctions=YYY
DriverODBCVer=03.51
FileUsage=0
SQLLevel=1
smProcessPerConnect=Y

配置 odbc.ini vim /home/linkdood/gbase8s-odbc-driver/odbc.ini

bash 复制代码
[ODBC]
UNICODE=UCS-2

测试驱动连接

bash 复制代码
isql -v -k "DRIVER={com.gbasedbt.jdbc.Driver};DB=testdataserver;UID=gbasedbt;pwd=GBase123;Server=gbase01;CLIENT_LOCALE=zh_CN.57372;DB_LOCALE=zh_CN.57372;"

golang通过odbc连接国产数据库

func init() {

file, err := os.Open("config.xml")

if err != nil {

Log.Info("error:%v", err)

return

}

defer file.Close()

config, err := ioutil.ReadAll(file)

if err != nil {

Log.Info("error:%v", err)

return

}

configValue := SConfig{}

err = xml.Unmarshal(config, &configValue)

if err != nil {

Log.Error("error:%v", err)

return

}

strKey := configValue.Server.Aid + configValue.Server.Elogo

var strPassword string

var strUsername string

strPassword = configValue.DataBase.Password

strUsername = configValue.DataBase.User

fmt.Printf("strPassword:%v\n", strPassword)

fmt.Printf("strUsername:%v\n", strUsername)

ServerPort = configValue.Server.Port

var dbDSN = "" //dataSourceName

var driverName = "" //驱动名

var DbServer = configValue.DataBase.Host + ":" + configValue.DataBase.Port

if configValue.DataBase.DbType == "mysql" {

fmt.Printf("mysql database\n")

driverName = "mysql"

dbDSN = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s", strUsername, strPassword, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, configValue.DataBase.Charset)

} else if configValue.DataBase.DbType == "shentong" {

fmt.Printf("shentongsql database\n")

/driverName = "odbc"
dbDSN = fmt.Sprintf("driver={%s};server=%s;port=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)
/

driverName = "aci"

dbDSN = fmt.Sprintf("%s/%s@%s:%s/%s", strUsername, strPassword, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName)

} else if configValue.DataBase.DbType == "dameng" {

strPassword = CheckAndModifyString(strPassword)

fmt.Printf("dmsql database\n")

driverName = "dm"

//dbDSN = fmt.Sprintf("driver={%s};server=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, DbServer, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)

dbDSN = fmt.Sprintf("%s://%s:%s@%s?schema=%s", driverName, strUsername, strPassword, DbServer, configValue.DataBase.DBName)

} else if configValue.DataBase.DbType == "kingbase" {

fmt.Printf("kingbase database\n")

driverName = "odbc"

dbDSN = fmt.Sprintf("driver={%s};server=%s;port=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)

} else if configValue.DataBase.DbType == "GBase8s" {

fmt.Printf("GBase8s database\n")

driverName = "odbc"

dbDSN = fmt.Sprintf("driver={%s};server=%s;port=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)

}

dbType = configValue.DataBase.DbType

// 打开连接失败

MysqlDb, MysqlDbErr = sql.Open(driverName, dbDSN)

//defer MysqlDb.Close();

if MysqlDbErr != nil {

panic("数据源配置不正确: " + MysqlDbErr.Error())

}

// 最大连接数

MysqlDb.SetMaxOpenConns(100)

// 闲置连接数

MysqlDb.SetMaxIdleConns(20)

// 最大连接周期

MysqlDb.SetConnMaxLifetime(100 * time.Second)

if MysqlDbErr = MysqlDb.Ping(); nil != MysqlDbErr {

panic("数据库链接失败: " + MysqlDbErr.Error())

}

}

相关推荐
Narutolxy4 分钟前
深入探讨 Go 中的高级表单验证与翻译:Gin 与 Validator 的实践之道20241223
开发语言·golang·gin
Hello.Reader11 分钟前
全面解析 Golang Gin 框架
开发语言·golang·gin
Mr.1322 分钟前
数据库的三范式是什么?
数据库
禁默22 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood29 分钟前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑32 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
程序猿进阶35 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
Python之栈36 分钟前
【无标题】
数据库·python·mysql
qq_4336184437 分钟前
shell 编程(二)
开发语言·bash·shell
风_流沙1 小时前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch