Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby connect oceanbase #60

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4606b76
Create example.rb
zhs722 May 30, 2024
4f9480a
Create README-CN.md
zhs722 May 30, 2024
91bc0a6
Delete ruby/README-CN.md
zhs722 May 30, 2024
d573499
Create README-CN.md
zhs722 May 30, 2024
ff92707
Create example.rb
zhs722 May 30, 2024
40f117e
Delete ruby/example.rb
zhs722 May 30, 2024
776d0bc
Create README
zhs722 May 30, 2024
029a037
Rename README to README.md
zhs722 May 30, 2024
9fcb7ce
Update README.md
zhs722 May 30, 2024
1c261d5
Update README.md
zhs722 May 30, 2024
4462a9e
Merge branch 'oceanbase:master' into ruby
zhs722 May 30, 2024
e795765
Update ci.yml
zhs722 May 30, 2024
1460ea4
Create run.sh
zhs722 May 30, 2024
786a2ad
Create README.md
zhs722 May 30, 2024
a47b7c4
Update example.rb
zhs722 May 30, 2024
4924412
Create example.rb
zhs722 May 30, 2024
63513ee
Create README-CN.md
zhs722 May 30, 2024
b3a4698
Update README-CN.md
zhs722 May 30, 2024
d26b523
Update README-CN.md
zhs722 May 30, 2024
6ea2ab8
Update README-CN.md
zhs722 May 30, 2024
742cf37
Create README.md
zhs722 May 30, 2024
bcd2ba5
Update README.md
zhs722 May 30, 2024
e8df7ad
Update README-CN.md
zhs722 May 30, 2024
9769e55
Update README.md
zhs722 May 30, 2024
29f39ec
Update README-CN.md
zhs722 May 30, 2024
c4aba9c
Update README.md
zhs722 May 30, 2024
10af579
Create run.sh
zhs722 May 30, 2024
0e26f4a
Create example.rb
zhs722 May 30, 2024
9641361
Update example.rb
zhs722 May 30, 2024
da27d96
Create README-CN.md
zhs722 May 30, 2024
013b399
Update README.md
zhs722 May 30, 2024
36d3456
Update README.md
zhs722 May 30, 2024
0bb41ff
Create README.md
zhs722 May 30, 2024
2dd4d94
Create run.sh
zhs722 May 30, 2024
454a238
Update ci.yml
zhs722 May 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ jobs:
- name: 'springboot'
language: 'java'
with_oceanbase_container: true
- name: 'mysql2'
language: 'ruby'
with_oceanbase_container: true
- name: 'activerecord'
language: 'ruby'
with_oceanbase_container: true
- name: 'sequel'
language: 'ruby'
with_oceanbase_container: true
uses: ./.github/workflows/basic-workflow.yml
with:
module: ${{ matrix.module.name }}
Expand Down
3 changes: 3 additions & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Introduction

Please add RUBY samples in this directory.
59 changes: 59 additions & 0 deletions ruby/activerecord/README-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Ruby连接 OceanBase 指南(activerecord)

[English](README.md) | 简体中文

本文介绍如何通过activerecord连接 OceanBase 数据库。

## 快速开始

在开始之前,需要先确保 mysql2,activerecord已安装。

安装命令

```
gem install activerecord
gem install mysql2
```

以 [example.rb](example.rb) 为例。
```
require 'active_record'

# 数据库配置
db_config = {
adapter: 'mysql2',
host: '127.0.0.1',
port: 2881,
username: 'root',
password: '',
database: 'test'
}

begin
# 建立连接
ActiveRecord::Base.establish_connection(db_config)

# 测试连接是否成功
connection = ActiveRecord::Base.connection
result = connection.active? # 检查数据库连接是否有效

if result
puts "成功连接到OceanBase数据库"
else
puts "连接到OceanBase数据库失败"
end

rescue StandardError => e
puts "连接到OceanBase数据库时发生错误: #{e.message}"
ensure
ActiveRecord::Base.connection.close if ActiveRecord::Base.connected?
end
```

修改代码中的连接信息,之后你就可以直接使用命令行运行示例代码。

```bash
sh run.sh
```
ActiveRecord::Base.connection.close if ActiveRecord::Base.connected?
end
58 changes: 58 additions & 0 deletions ruby/activerecord/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Ruby Connection OceanBase Guide (activerecord)

English | [简体中文](README-CN.md)

This article introduces how to connect to OceanBase database through activerecord.

## Quick Start

Before starting, it is necessary to ensure that mysql2,activerecord is installed.

Installation command

```
gem install activerecord
gem install mysql2
```

Taking [example.rb](example.rb) as an example.

```
require 'active_record'

# 数据库配置
db_config = {
adapter: 'mysql2',
host: '127.0.0.1',
port: 2881,
username: 'root',
password: '',
database: 'test'
}

begin
# 建立连接
ActiveRecord::Base.establish_connection(db_config)

# 测试连接是否成功
connection = ActiveRecord::Base.connection
result = connection.active? # 检查数据库连接是否有效

if result
puts "成功连接到OceanBase数据库"
else
puts "连接到OceanBase数据库失败"
end

rescue StandardError => e
puts "连接到OceanBase数据库时发生错误: #{e.message}"
ensure
ActiveRecord::Base.connection.close if ActiveRecord::Base.connected?
end
```

Modify the connection information in the code, and then you can directly run the example code using the command line.

```bash
sh run.sh
```
31 changes: 31 additions & 0 deletions ruby/activerecord/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'active_record'

# 数据库配置
db_config = {
adapter: 'mysql2',
host: '127.0.0.1',
port: 2881,
username: 'root',
password: '',
database: 'test'
}

begin
# 建立连接
ActiveRecord::Base.establish_connection(db_config)

# 测试连接是否成功
connection = ActiveRecord::Base.connection
result = connection.active? # 检查数据库连接是否有效

if result
puts "成功连接到OceanBase数据库"
else
puts "连接到OceanBase数据库失败"
end

rescue StandardError => e
puts "连接到OceanBase数据库时发生错误: #{e.message}"
ensure
ActiveRecord::Base.connection.close if ActiveRecord::Base.connected?
end
3 changes: 3 additions & 0 deletions ruby/activerecord/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gem install activerecord
gem install mysql2
ruby example.rb
68 changes: 68 additions & 0 deletions ruby/mysql2/README-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Ruby连接 OceanBase 指南(mysql2)

[English](README.md) | 简体中文

本文介绍如何通过 mysql2驱动连接 OceanBase 数据库。

## 快速开始

在开始之前,需要先确保 mysql2已安装。

安装命令

```
gem install mysql2
```

以 [example.rb](example.rb) 为例。

```
require 'mysql2'

def connect_to_oceanbase(host, port, username, password, database)
begin
# 创建数据库连接
client = Mysql2::Client.new(
host: host,
port: port,
username: username,
password: password,
database: database
)

puts "连接到OceanBase数据库成功"

# 执行一个简单的查询
results = client.query("SELECT DATABASE();")

results.each do |row|
puts "连接到的数据库: #{row['DATABASE()']}"
end

# 你可以在这里执行其他的SQL查询或操作

client.close
puts "MySQL连接已关闭"

rescue Mysql2::Error => e
puts "连接到OceanBase数据库失败: #{e.error}"
end
end

if __FILE__ == $0
connect_to_oceanbase(
'127.0.0.1',
2881,
'root',
'',
'test'
)
end

```

修改代码中的连接信息,之后你就可以直接使用命令行运行示例代码。

```bash
sh run.sh
```
68 changes: 68 additions & 0 deletions ruby/mysql2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Ruby Connection OceanBase Guide (Mysql2)

English | [简体中文](README-CN.md)

This article introduces how to connect to OceanBase database through mysql2 driver.

## Quick Start

Before starting, it is necessary to ensure that mysql2 is installed.

Installation command

```
gem install mysql2
```

Taking [example.rb](example.rb) as an example.

```python
require 'mysql2'

def connect_to_oceanbase(host, port, username, password, database)
begin
# 创建数据库连接
client = Mysql2::Client.new(
host: host,
port: port,
username: username,
password: password,
database: database
)

puts "连接到OceanBase数据库成功"

# 执行一个简单的查询
results = client.query("SELECT DATABASE();")

results.each do |row|
puts "连接到的数据库: #{row['DATABASE()']}"
end

# 你可以在这里执行其他的SQL查询或操作

client.close
puts "MySQL连接已关闭"

rescue Mysql2::Error => e
puts "连接到OceanBase数据库失败: #{e.error}"
end
end

if __FILE__ == $0
connect_to_oceanbase(
'127.0.0.1',
2881,
'root',
'',
'test'
)
end

```

Modify the connection information in the code, and then you can directly run the example code using the command line.

```bash
sh run.sh
```
41 changes: 41 additions & 0 deletions ruby/mysql2/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'mysql2'

def connect_to_oceanbase(host, port, username, password, database)
begin
# 创建数据库连接
client = Mysql2::Client.new(
host: host,
port: port,
username: username,
password: password,
database: database
)

puts "连接到OceanBase数据库成功"

# 执行一个简单的查询
results = client.query("SELECT DATABASE();")

results.each do |row|
puts "连接到的数据库: #{row['DATABASE()']}"
end

# 你可以在这里执行其他的SQL查询或操作

client.close
puts "MySQL连接已关闭"

rescue Mysql2::Error => e
puts "连接到OceanBase数据库失败: #{e.error}"
end
end

if __FILE__ == $0
connect_to_oceanbase(
'127.0.0.1',
2881,
'root',
'',
'test'
)
end
2 changes: 2 additions & 0 deletions ruby/mysql2/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gem install mysql2
ruby example.rb
Loading
Loading