mysql_fdw 集成go-mysql-server 开发的mysql server
早期go-mysql-server golang 包对于mysql 的支持有限,但是dolthub团队扩展之后我们可以集成了
以下是一个简单的demo测试
环境准备
- docker-compose
mysql_fdw 环境
version: "3"
services:
pg:
image: dalongrong/pgspider:mysql
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
- mysql server
go.mod
module mysql-app
go 1.15
require /dolthub/go-mysql-server v0.6.1-0.20201113020219-f934e2bcb07f
main.go
package main
import (
sqle "/dolthub/go-mysql-server"
"/dolthub/go-mysql-server/auth"
"/dolthub/go-mysql-server/memory"
"/dolthub/go-mysql-server/server"
"/dolthub/go-mysql-server/sql"
)
func main() {
driver := sqle.NewDefault()
driver.AddDatabase(createTestDatabase())
config := server.Config{
Protocol: "tcp",
Address: "0.0.0.0:3306",
Auth: auth.NewNativeSingle("root", "dalong", auth.AllPermissions),
}
s, err := server.NewDefaultServer(config, driver)
if err != nil {
panic(err)
}
s.Start()
}
func createTestDatabase() *memory.Database {
const (
dbName = "test"
tableName = "mytable"
)
db := memory.NewDatabase(dbName)
table := memory.NewTable(tableName, sql.Schema{
{Name: "name", Type: sql.Text, Nullable: false, Source: tableName},
{Name: "email", Type: sql.Text, Nullable: false, Source: tableName},
})
db.AddTable(tableName, table)
ctx := sql.NewEmptyContext()
rows := []sql.Row{
sql.NewRow("John Doe", "john@"),
sql.NewRow("John Doe", "johnalt@"),
sql.NewRow("Jane Doe", "jane@"),
sql.NewRow("Evil Bob", "evilbob@"),
}
for _, row := range rows {
table.Insert(ctx, row)
}
return db
}
运行
- 启动服务
docker-compose up -d
go run main.go
- 效果
pg
mysql
说明
以上只是一个简单的demo,实际上dolthub 团队扩展的go-mysql-server 支持的特性还是比较多的