【REST2SQL】01RDB关系型数据库REST初设计
【REST2SQL】02 GO连接Oracle数据库
【REST2SQL】03 GO读取JSON文件
【REST2SQL】04 REST2SQL第一版Oracle版实现
信创要求用国产数据库,刚好有项目用的达梦,研究一下go如何操作达梦数据库
1 准备工作
1.1 安装达梦数据
登录 达梦 官网,有DM8开发版可以下载,我下载的是X86,Win64版的DM8开发版。下载成功后,安装配置等这里省略5217字,自己脑补。
创建测试表 guci
导入部分测试数据
1.2 达梦 go驱动安装
安装达梦后,在达梦的安装目录…\dmdbms\drivers\go下有go驱动包dm-go-driver.zip,解压到go开发环境dm目录即可,也可以在第三方下载。
达梦的go驱动还有安装如下两个依赖
github.com/golang/snappy v0.0.4 // indirect
golang.org/x/text v0.14.0 // indirect
众所周知的原因,可能同步失败,自己想办法翻墙或代理等一系列操作。
2 新建一个godm的项目
新建一下godm的项目用来测试go操作达梦数据库。这次也试试 go mod
2.1 初始化 go mod
go mod init godm
go mod tidy
自动创建了go.mod 和 go.sum
//go.mod
module godmgo 1.21.5require (github.com/golang/snappy v0.0.4 // indirectgolang.org/x/text v0.14.0 // indirect
)
// go.sum
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
2.2 go连接达梦数据库
1 引入相关包
import ("database/sql""database/sql/driver"_ "dm"
)
2 声明连接字符串
var ConnString string = "dm://BLMA:dameng5217@127.0.0.1:5236/BLMA"
3 连接数据库
// 连接dm数据库
func connDB(connStr string) *sql.DB {end := strings.Index(connStr, "://")if end < 0 {log.Println("连接字符串设置有误。")panic(nil)}driverName := connStr[:end] // dmDB, err := sql.Open(driverName, connStr)dieOnError("Can't open the driver:", err)if err = DB.Ping(); err != nil {return nil}// fmt.Printf("connect to \"%s\" succeed.\n", connStr)return DB
}
2.3 实现CRUD
CUD比较简单,都执行execSQL操作,只是sql语句不同。
代码如下:
/* 往表插入数据 */
func insertData(insertSql string) string {result, _ := execSQL(insertSql)rows, err := result.RowsAffected()dieOnError("Can't insert", err)ret := map[string]int{"Insert rowsAffected": int(rows),}jsonBytes, err := json.Marshal(ret)dieOnError("map 转 json失败:", err)return string(jsonBytes)
}/* 删除表数据 */
func deleteData(deleteSql string) string {result, _ := execSQL(deleteSql)rows, err := result.RowsAffected()dieOnError("Can't delete", err)ret := map[string]int{"Delete rowsAffected": int(rows),}jsonBytes, err := json.Marshal(ret)dieOnError("map 转 json失败:", err)return string(jsonBytes)
}/* 修改表数据 */
func updateData(updateSql string) string {result, _ := execSQL(updateSql)rows, err := result.RowsAffected()dieOnError("Can't update", err)ret := map[string]int{"Update rowsAffected": int(rows),}jsonBytes, err := json.Marshal(ret)dieOnError("map 转 json失败:", err)return string(jsonBytes)// var sql =// result, err := db.Exec(sql)// if err != nil {// return err// }// affectedRows, _ := result.RowsAffected()// fmt.Println("updateTable succeed Affected rows:", affectedRows)// return nil
}// 执行SQL, execute stmt (INSERT, UPDATE, DELETE, DML, PLSQL) and return driver.Result object
func execSQL(sqls string) (result driver.Result, err error) {//连接数据库DB := connDB(ConnString)//延迟关闭连接defer DB.Close()statement, err := DB.Prepare(sqls)if err != nil {fmt.Println("prepare statement failed:", err.Error())}defer statement.Close()//执行SQL, execute stmt (INSERT, UPDATE, DELETE, DML) and return driver.Result objectresult, err = statement.Exec()if err != nil {fmt.Println("Exec failed:", err.Error())}dieOnError("Can't execSql() ", err)return result, err
}
2.4 动态查询有点费劲
达梦提供了简单的查询驱动,貌似没有提供动态查询相关驱动,只好自己动手实现了,好在其它数据库也能用上。
总体思路是先查询获取 *sql.Rows对象,从这里通过rows.Columns()和 rows. ColumnTypes()再获取列名切片和列类型信息,第三步把列名和数据库数据类型组合在一个map[string]string里;第四步初始化列值接收变量;第五步 rows.Next() 逐行遍历返回结果集并根据数据库类型(目前只匹配的VARCHER2 和 NUMBER,遇到其它类型再匹配)转换为Go的数据类型,组成一个dataset数据集;第六步序列化并转json返回查询结果。
代码如下:
/* 查询表数据 */
func selectData(sqlSelect string) string {// 连接数据库DB := connDB(ConnString)//延迟关闭连接defer DB.Close()// 准备查询语句statement, err := DB.Prepare(sqlSelect)if err != nil {fmt.Println("prepare statement failed:", err.Error())return ""}defer statement.Close()
// 1查询rows, err := statement.Query()if err != nil {fmt.Println("query failed:", err.Error())}// 2查询的列名称切片columns, err := rows.Columns()if err != nil {fmt.Println(err.Error())return ""}//fmt.Println(columns)// 3数据库列类型cType, err := rows.ColumnTypes()if err != nil {log.Fatal(err)}//fmt.Println(cType[0].Name(), cType[0].DatabaseTypeName())// 4列名类型mapcoltyp := colType(cType)// 5初始化列值接收变量row := make([]sql.RawBytes, len(columns))scanArgs := make([]interface{}, len(row))for i := range row {scanArgs[i] = &row[i]}// 查询结果数据集var dataset []map[string]interface{} //元素为map的切片for rows.Next() {err := rows.Scan(scanArgs...)if err != nil {panic(err.Error())}// rowvar row1 map[string]interface{} = make(map[string]interface{})for pos, col := range row {//fmt.Println(columns[pos], ":", string(col))colname := columns[pos]svalue := string(col)//数据类型处理value := typeConv(colname, svalue, coltyp)row1[colname] = value}//fmt.Println("row1:", row1)dataset = append(dataset, row1)//fmt.Println()}if err != io.EOF {dieOnError("Can't Next", err)}//切片转jsonjsonBytes, err := json.Marshal(dataset)dieOnError("slice 转 json失败:", err)//fmt.Println(string(jsonBytes))return string(jsonBytes)
}// 生成列名和类型 map
func colType(cType []*sql.ColumnType) map[string]string {var colTyp map[string]string = make(map[string]string)for _, col := range cType {colTyp[col.Name()] = col.DatabaseTypeName()}//fmt.Println(colTyp)return colTyp
}// 字符根据数据库类型转为go数据类型
func typeConv(colname string, svalue string, ct map[string]string) interface{} {var ret interface{}switch ct[colname] {case "VARCHAR2":ret = svaluecase "NUMBER":if len(svalue) > 0 {flt, err := strconv.ParseFloat(svalue, 64)if err != nil {fmt.Println("转换失败:", colname, svalue, err)} else {ret = flt}} else { //空串处理ret = nil}default:ret = svalue}return ret
}
3 全部代码及运行结果截图
全部代码:
/*该例程实现了达梦数据库插入数据,修改数据,删除数据,数据查询等基本操作。*/
package main// 引入相关包
import ("database/sql""database/sql/driver"_ "dm""encoding/json""fmt""io""log""strconv""strings"
)var ConnString string = "dm://BLMA:dameng5217@127.0.0.1:5236/BLMA"// var ConnString string = config.Conf.ConnStringfunc main() {fmt.Println("\ngo 操作达梦数据库 dome")var (sqls string //sql语句result string //sql执行后返回的结果)// insert 插入一行数据sqls = `INSERT INTO guci(p_id,f_zh,f_gp,s_mc) VALUES(-109,'bailongma','005217','白龙马');`result = insertData(sqls)fmt.Println(result)// delete 删除数据sqls = "delete from guci where p_id = -108"result = deleteData(sqls)fmt.Println(result)// update 更新数据sqls = "UPDATE guci SET n_sul = 400 WHERE p_id = -100"result = updateData(sqls)fmt.Println(result)sqls = "select p_id,f_zh,f_gp,s_mc,n_sul from guci where rownum < 6"result = selectData(sqls)fmt.Println(result)}// 连接dm数据库
func connDB(connStr string) *sql.DB {end := strings.Index(connStr, "://")if end < 0 {log.Println("连接字符串设置有误。")panic(nil)}driverName := connStr[:end] // dmDB, err := sql.Open(driverName, connStr)dieOnError("Can't open the driver:", err)if err = DB.Ping(); err != nil {return nil}// fmt.Printf("connect to \"%s\" succeed.\n", connStr)return DB
}// 发生错误退出1
func dieOnError(msg string, err error) {if err != nil {log.Println(msg, err)//os.Exit(1)}
}/* 往表插入数据 */
func insertData(insertSql string) string {result, _ := execSQL(insertSql)rows, err := result.RowsAffected()dieOnError("Can't insert", err)ret := map[string]int{"Insert rowsAffected": int(rows),}jsonBytes, err := json.Marshal(ret)dieOnError("map 转 json失败:", err)return string(jsonBytes)
}/* 删除表数据 */
func deleteData(deleteSql string) string {result, _ := execSQL(deleteSql)rows, err := result.RowsAffected()dieOnError("Can't delete", err)ret := map[string]int{"Delete rowsAffected": int(rows),}jsonBytes, err := json.Marshal(ret)dieOnError("map 转 json失败:", err)return string(jsonBytes)
}/* 修改表数据 */
func updateData(updateSql string) string {result, _ := execSQL(updateSql)rows, err := result.RowsAffected()dieOnError("Can't update", err)ret := map[string]int{"Update rowsAffected": int(rows),}jsonBytes, err := json.Marshal(ret)dieOnError("map 转 json失败:", err)return string(jsonBytes)// var sql =// result, err := db.Exec(sql)// if err != nil {// return err// }// affectedRows, _ := result.RowsAffected()// fmt.Println("updateTable succeed Affected rows:", affectedRows)// return nil
}// 执行SQL, execute stmt (INSERT, UPDATE, DELETE, DML, PLSQL) and return driver.Result object
func execSQL(sqls string) (result driver.Result, err error) {//连接数据库DB := connDB(ConnString)//延迟关闭连接defer DB.Close()statement, err := DB.Prepare(sqls)if err != nil {fmt.Println("prepare statement failed:", err.Error())}defer statement.Close()//执行SQL, execute stmt (INSERT, UPDATE, DELETE, DML) and return driver.Result objectresult, err = statement.Exec()if err != nil {fmt.Println("Exec failed:", err.Error())}dieOnError("Can't execSql() ", err)return result, err
}/* 查询表数据 */
func selectData(sqlSelect string) string {// 连接数据库DB := connDB(ConnString)//延迟关闭连接defer DB.Close()// 准备查询语句statement, err := DB.Prepare(sqlSelect)if err != nil {fmt.Println("prepare statement failed:", err.Error())return ""}defer statement.Close()// 1查询rows, err := statement.Query()if err != nil {fmt.Println("query failed:", err.Error())}// 2查询的列名称切片columns, err := rows.Columns()if err != nil {fmt.Println(err.Error())return ""}//fmt.Println(columns)// 3数据库列类型cType, err := rows.ColumnTypes()if err != nil {log.Fatal(err)}//fmt.Println(cType[0].Name(), cType[0].DatabaseTypeName())// 4列名类型mapcoltyp := colType(cType)// 5初始化列值接收变量row := make([]sql.RawBytes, len(columns))scanArgs := make([]interface{}, len(row))for i := range row {scanArgs[i] = &row[i]}// 查询结果数据集var dataset []map[string]interface{} //元素为map的切片for rows.Next() {err := rows.Scan(scanArgs...)if err != nil {panic(err.Error())}// rowvar row1 map[string]interface{} = make(map[string]interface{})for pos, col := range row {//fmt.Println(columns[pos], ":", string(col))colname := columns[pos]svalue := string(col)//数据类型处理value := typeConv(colname, svalue, coltyp)row1[colname] = value}//fmt.Println("row1:", row1)dataset = append(dataset, row1)//fmt.Println()}if err != io.EOF {dieOnError("Can't Next", err)}//切片转jsonjsonBytes, err := json.Marshal(dataset)dieOnError("slice 转 json失败:", err)//fmt.Println(string(jsonBytes))return string(jsonBytes)
}// 生成列名和类型 map
func colType(cType []*sql.ColumnType) map[string]string {var colTyp map[string]string = make(map[string]string)for _, col := range cType {colTyp[col.Name()] = col.DatabaseTypeName()}//fmt.Println(colTyp)return colTyp
}// 字符根据数据库类型转为go数据类型
func typeConv(colname string, svalue string, ct map[string]string) interface{} {var ret interface{}switch ct[colname] {case "VARCHAR2":ret = svaluecase "NUMBER":flt, err := strconv.ParseFloat(svalue, 64)if err != nil {fmt.Println("转换失败")} else {ret = flt}default:ret = svalue}return ret
}
运行结果截图:
完