-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
cd `dirname $0` | ||
CGO_ENABLED=0 GO111MODULE=on go run example.go |