go-mysql-server 新版本发布
go-mysql-server 是在原有的基础上进行可增强,2021 q1版本实现了好多新的特性
如果结合以前的版本,triger 是一个很不错的特性,优化点也是很多的,很值得学习下
同时记得以前有写过与pg fdw 集成的,使用go-mysql-server或者一个基于mysql 的db引擎,实现一些特殊业务操作还是很不错的
参考pdw 集成
- server 代码
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"
"/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@"))
table.Insert(ctx, sql.NewRow("John Doe", "johnalt@"))
table.Insert(ctx, sql.NewRow("Jane Doe", "jane@"))
table.Insert(ctx, sql.NewRow("Evil Bob", "evilbob@"))
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