go-mysql-server 新版本发布

go-mysql-server 是在原有的基础上进行可增强,2021 q1版本实现了好多新的特性
如果结合以前的版本,triger 是一个很不错的特性,优化点也是很多的,很值得学习下
同时记得以前有写过与pg fdw 集成的,使用go-mysql-server或者一个基于mysql 的db
引擎,实现一些特殊业务操作还是很不错的

参考pdw 集成

  • server 代码
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"
    "github.com/dolthub/go-mysql-server/sql/information_schema"
)
 
 
func main() {
    engine := sqle.NewDefault()
    engine.AddDatabase(createTestDatabase())
    engine.AddDatabase(information_schema.NewInformationSchemaDatabase(engine.Catalog))
 
    config := server.Config{
        Protocol: "tcp",
        Address:  "0.0.0.0:3306",
        Auth:     auth.NewNativeSingle("root", "dalong", auth.AllPermissions),
    }
 
    s, err := server.NewDefaultServer(config, engine)
    if err != nil {
        panic(err)
    }
 
    s.Start()
}
 
func createTestDatabase() *memory.Database {
    const (
        dbName    = "mydb"
        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()
    table.Insert(ctx, sql.NewRow("John Doe", "john@doe.com"))
    table.Insert(ctx, sql.NewRow("John Doe", "johnalt@doe.com"))
    table.Insert(ctx, sql.NewRow("Jane Doe", "jane@doe.com"))
    table.Insert(ctx, sql.NewRow("Evil Bob", "evilbob@gmail.com"))
    return db
}
 
  • pdw 命令
CREATE EXTENSION mysql_fdw;
 
CREATE SERVER mysql_server
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host '192.168.0.102', port '3306');
 
CREATE USER MAPPING FOR postgres
SERVER mysql_server
OPTIONS (username 'root', password 'dalong');
 
CREATE FOREIGN TABLE mydemo
(
    name text,
    email text
)
SERVER mysql_server
OPTIONS (dbname 'mydb', table_name 'mytable');
select name,count(*) from mydemo group by name;
 

trigger 使用

create table a (x int primary key)
create trigger a1 before insert on a for each row set new.x = new.x + 1

参考资料

https://github.com/dolthub/go-mysql-server/releases
https://www.cnblogs.com/rongfengliang/p/14021349.html
https://sourcegraph.com/github.com/dolthub/go-mysql-server/-/blob/enginetest/memory_engine_test.go#L119

posted on 2021-02-14 23:03  荣锋亮  阅读(233)  评论(0编辑  收藏  举报

导航