Skip to content

Commit

Permalink
[Feat]: Add a demo of golang gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
yyt030 committed May 25, 2024
1 parent 3125416 commit 2128fc1
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
28 changes: 28 additions & 0 deletions golang/gorm/README-CN.md
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;
```
28 changes: 28 additions & 0 deletions golang/gorm/README.md
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;
```
67 changes: 67 additions & 0 deletions golang/gorm/example.go
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)
}
14 changes: 14 additions & 0 deletions golang/gorm/go.mod
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
)
3 changes: 3 additions & 0 deletions golang/gorm/run.sh
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

0 comments on commit 2128fc1

Please sign in to comment.