diff --git a/golang/gorm/README-CN.md b/golang/gorm/README-CN.md new file mode 100644 index 0000000..7f4e961 --- /dev/null +++ b/golang/gorm/README-CN.md @@ -0,0 +1,28 @@ +# Golang 连接 OceanBase 指南(使用 go-sql-driver/mysql) + +[English](README.md) | 简体中文 + +本文介绍如何通过 `gorm.io/driver/mysql` 连接 OceanBase 数据库。 + +关于 `gorm.io/driver/mysql` 的详细信息,您可参考 [gorm.io/driver/mysql](https://gorm.io/driver/mysql)。 + +## 快速开始 + +您需要使用 `conf` 来创建数据库连接,详细信息请参考 [gorm.io/docs/](https://gorm.io/docs)。 + +以 [example.go](example.go) 代码为例 + + +修改代码中的连接信息,之后你就可以直接使用 run.sh 运行示例代码。 + +```bash +sh run.sh +``` + +### 使用 PreparedStatement + +使用 root 用户登录 OceanBase,运行如下命令: + +```bash +alter system set _ob_enable_prepared_statement = true; +``` diff --git a/golang/gorm/README.md b/golang/gorm/README.md new file mode 100644 index 0000000..52cb524 --- /dev/null +++ b/golang/gorm/README.md @@ -0,0 +1,28 @@ +# Connect OceanBase with Golang (gorm.io/driver/mysql) + +English | [简体中文](README-CN.md) + +This article describes how to connect to the OceanBase database through ``. + +For details about `gorm.io/driver/mysql`, you can refer to [gorm.io/driver/mysql](https://gorm.io/driver/mysql). + +## Quick Start + +You can use `conf` to create a database connection, please refer to [gorm.io/docs/](https://gorm.io/docs) for details. + +Take [example.go](example.go) code as an example. + + +Modify the connection info in code, and use `run.sh` to run the example code. + +```bash +sh run.sh +``` + +### Use PreparedStatement + +Log in to OceanBase with a root user and run the following command: + +```bash +alter system set _ob_enable_prepared_statement = true; +``` diff --git a/golang/gorm/example.go b/golang/gorm/example.go new file mode 100644 index 0000000..ae06c65 --- /dev/null +++ b/golang/gorm/example.go @@ -0,0 +1,67 @@ +package main + +import ( + "flag" + "fmt" + + "gorm.io/driver/mysql" + "gorm.io/gorm" + + stdmysql "github.com/go-sql-driver/mysql" +) + +var ( + host string + port int + username string + password string + database string +) + +func init() { + flag.StringVar(&host, "host", "", "host") + flag.IntVar(&port, "port", 0, "port") + flag.StringVar(&username, "username", "", "username") + flag.StringVar(&password, "password", "", "password") + flag.StringVar(&database, "database", "", "database") + + flag.Parse() +} + +type Product struct { + ID uint `gorm:"primaryKey;default:auto_random()"` + Code string + Price uint +} + +func main() { + //dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, database) + conf := stdmysql.Config{ + Addr: fmt.Sprintf("%s:%d", host, port), + User: username, + Passwd: password, + DBName: database, + // parameters + } + dial := mysql.New(mysql.Config{DSNConfig: &conf}) + db, err := gorm.Open(dial, &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } + + if err := db.AutoMigrate(&Product{}); err != nil { + panic(err) + } + + insertProduct := &Product{Code: "D42", Price: 100} + + db.Create(insertProduct) + fmt.Printf("insert ID: %d, Code: %s, Price: %d\n", + insertProduct.ID, insertProduct.Code, insertProduct.Price) + + readProduct := &Product{} + db.First(&readProduct, "code = ?", "D42") // find product with code D42 + + fmt.Printf("read ID: %d, Code: %s, Price: %d\n", + readProduct.ID, readProduct.Code, readProduct.Price) +} diff --git a/golang/gorm/go.mod b/golang/gorm/go.mod new file mode 100644 index 0000000..fd5a64a --- /dev/null +++ b/golang/gorm/go.mod @@ -0,0 +1,14 @@ +module github.com/oceanbase/ob-samples/gorm + +go 1.19 + +require ( + gorm.io/driver/mysql v1.5.6 + gorm.io/gorm v1.25.10 +) + +require ( + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect +) diff --git a/golang/gorm/run.sh b/golang/gorm/run.sh new file mode 100644 index 0000000..1bc0b77 --- /dev/null +++ b/golang/gorm/run.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cd `dirname $0` +CGO_ENABLED=0 GO111MODULE=on go run example.go