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 github.com/dolthub/go-mysql-server v0.6.1-0.20201113020219-f934e2bcb07f

main.go

package main
import (
    sqle "github.com/dolthub/go-mysql-server"
    "github.com/dolthub/go-mysql-server/auth"
    "github.com/dolthub/go-mysql-server/memory"
    "github.com/dolthub/go-mysql-server/server"
    "github.com/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@doe.com"),
        sql.NewRow("John Doe", "johnalt@doe.com"),
        sql.NewRow("Jane Doe", "jane@doe.com"),
        sql.NewRow("Evil Bob", "evilbob@gmail.com"),
    }
    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 支持的特性还是比较多的

参考资料

https://github.com/dolthub/go-mysql-server
https://github.com/EnterpriseDB/mysql_fdw
https://github.com/pgspider/pgspider
https://github.com/rongfengliang/pgspider-docker
https://hub.docker.com/repository/docker/dalongrong/pgspider
https://github.com/rongfengliang/go-mysql-server-mysql-fdw

posted on 2020-11-22 20:57  荣锋亮  阅读(318)  评论(0编辑  收藏  举报

导航