From 1a773e3c653b01cfa80c2dc798b9e78292b209d8 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 7 Feb 2024 08:11:11 +0800 Subject: [PATCH] env YDB_MYSQL_TEST as mysql default datasource --- ydb/classfile.go | 11 +++-- ydb/demo/foo/foo_ydb.gox | 3 +- ydb/demo/foo/gop_autogen.go | 95 +++++++++++++++++++------------------ ydb/mysql/mysql.go | 13 +++++ 4 files changed, 71 insertions(+), 51 deletions(-) diff --git a/ydb/classfile.go b/ydb/classfile.go index e008054..2c79795 100644 --- a/ydb/classfile.go +++ b/ydb/classfile.go @@ -55,7 +55,11 @@ func (p *Sql) Engine__0(name string, src ...ast.Node) { if !ok { log.Panicf("engine `%s` not found: please call ydb.Register first\n", name) } - db, err := sql.Open(name, defaultDataSource) + dataSource, ok := defaultDataSource.(string) + if !ok { + dataSource = defaultDataSource.(func() string)() + } + db, err := sql.Open(name, dataSource) if err != nil { log.Panicln("sql.Open:", err) } @@ -122,11 +126,12 @@ func (p *Sql) Class(name string, spec func(), src ...ast.Node) { // ----------------------------------------------------------------------------- var ( - engineDataSource = make(map[string]string) // engineName => defaultDataSource + engineDataSource = make(map[string]any) // engineName => defaultDataSource ) // Register registers a engine and its default data source. -func Register(name, defaultDataSource string) { +// defaultDataSource can be a `string` or a `func() string` object. +func Register(name string, defaultDataSource any) { engineDataSource[name] = defaultDataSource } diff --git a/ydb/demo/foo/foo_ydb.gox b/ydb/demo/foo/foo_ydb.gox index 8bcecdb..a551195 100644 --- a/ydb/demo/foo/foo_ydb.gox +++ b/ydb/demo/foo/foo_ydb.gox @@ -1,6 +1,7 @@ import ( "time" + _ "github.com/goplus/yap/ydb/mysql" _ "github.com/goplus/yap/ydb/sqlite3" ) @@ -32,7 +33,7 @@ type Tag struct { Article string } -engine "sqlite3" +engine "mysql" table User, "user v0.1.0" diff --git a/ydb/demo/foo/gop_autogen.go b/ydb/demo/foo/gop_autogen.go index 2079843..4a46b52 100644 --- a/ydb/demo/foo/gop_autogen.go +++ b/ydb/demo/foo/gop_autogen.go @@ -3,6 +3,7 @@ package main import ( "errors" "github.com/goplus/yap/ydb" + _ "github.com/goplus/yap/ydb/mysql" _ "github.com/goplus/yap/ydb/sqlite3" "time" ) @@ -38,7 +39,7 @@ type foo struct { } func main() { -//line ydb/demo/foo/foo_ydb.gox:82:1 +//line ydb/demo/foo/foo_ydb.gox:83:1 ydb.Gopt_AppGen_Main(new(ydb.AppGen), new(foo)) } @@ -53,101 +54,101 @@ func Hmac(pwd string, salt string) string { //line ydb/demo/foo/foo.gop:14:1 return "" } -//line ydb/demo/foo/foo_ydb.gox:35 +//line ydb/demo/foo/foo_ydb.gox:36 func (this *foo) Main() { -//line ydb/demo/foo/foo_ydb.gox:35:1 - this.Engine__0("sqlite3") -//line ydb/demo/foo/foo_ydb.gox:37:1 +//line ydb/demo/foo/foo_ydb.gox:36:1 + this.Engine__0("mysql") +//line ydb/demo/foo/foo_ydb.gox:38:1 ydb.Gopt_Sql_Gopx_Table[User](this, "user v0.1.0") -//line ydb/demo/foo/foo_ydb.gox:39:1 - ydb.Gopt_Sql_Gopx_Table[Article](this, "v0.1.0") //line ydb/demo/foo/foo_ydb.gox:40:1 + ydb.Gopt_Sql_Gopx_Table[Article](this, "v0.1.0") +//line ydb/demo/foo/foo_ydb.gox:41:1 this.From("oldart v0.9.1", func() { }) -//line ydb/demo/foo/foo_ydb.gox:45:1 +//line ydb/demo/foo/foo_ydb.gox:46:1 ydb.Gopt_Sql_Gopx_Table[Tag](this, "v0.1.0") -//line ydb/demo/foo/foo_ydb.gox:47:1 - this.Class("Users", func() { //line ydb/demo/foo/foo_ydb.gox:48:1 + this.Class("Users", func() { +//line ydb/demo/foo/foo_ydb.gox:49:1 this.Use("user") -//line ydb/demo/foo/foo_ydb.gox:50:1 - this.Api("register", func(id string, pwd string, nickname string, email string, tel string, ctime time.Time) error { //line ydb/demo/foo/foo_ydb.gox:51:1 - if email == "" && tel == "" { + this.Api("register", func(id string, pwd string, nickname string, email string, tel string, ctime time.Time) error { //line ydb/demo/foo/foo_ydb.gox:52:1 + if email == "" && tel == "" { +//line ydb/demo/foo/foo_ydb.gox:53:1 return ErrNoEmailAndTel } -//line ydb/demo/foo/foo_ydb.gox:54:1 - this.Limit__2(3, "email=?", email) //line ydb/demo/foo/foo_ydb.gox:55:1 + this.Limit__2(3, "email=?", email) +//line ydb/demo/foo/foo_ydb.gox:56:1 this.Limit__2(3, "tel=?", tel) -//line ydb/demo/foo/foo_ydb.gox:57:1 - salt := Rand() //line ydb/demo/foo/foo_ydb.gox:58:1 - spwd := Hmac(pwd, salt) + salt := Rand() //line ydb/demo/foo/foo_ydb.gox:59:1 + spwd := Hmac(pwd, salt) +//line ydb/demo/foo/foo_ydb.gox:60:1 this.Insert__1(&User{Id: id, Spwd: spwd, Salt: salt, Nickname: nickname, Email: email, Tel: tel, Ctime: ctime}) -//line ydb/demo/foo/foo_ydb.gox:61:1 +//line ydb/demo/foo/foo_ydb.gox:62:1 return nil }) -//line ydb/demo/foo/foo_ydb.gox:63:1 - this.Call__1("user", "pwd", "nickname", "", "", time.Now()) //line ydb/demo/foo/foo_ydb.gox:64:1 - this.Ret__1(ErrNoEmailAndTel) + this.Call__1("user", "pwd", "nickname", "", "", time.Now()) //line ydb/demo/foo/foo_ydb.gox:65:1 - this.Call__1("user", "pwd", "nickname", "user@foo.com", "", time.Now()) + this.Ret__1(ErrNoEmailAndTel) //line ydb/demo/foo/foo_ydb.gox:66:1 - this.Ret__0(nil) + this.Call__1("user", "pwd", "nickname", "user@foo.com", "", time.Now()) //line ydb/demo/foo/foo_ydb.gox:67:1 - this.Call__1("user", "pwd", "nickname", "user@foo.com", "13500000000", time.Now()) + this.Ret__0(nil) //line ydb/demo/foo/foo_ydb.gox:68:1 + this.Call__1("user", "pwd", "nickname", "user@foo.com", "13500000000", time.Now()) +//line ydb/demo/foo/foo_ydb.gox:69:1 this.Ret__1(ydb.ErrDuplicated) -//line ydb/demo/foo/foo_ydb.gox:70:1 - this.Api("login", func(id string, pwd string) bool { //line ydb/demo/foo/foo_ydb.gox:71:1 - var spwd, salt string + this.Api("login", func(id string, pwd string) bool { //line ydb/demo/foo/foo_ydb.gox:72:1 - this.Query__1("id=?", id) + var spwd, salt string //line ydb/demo/foo/foo_ydb.gox:73:1 - this.Ret__1("salt", &salt, "spwd", &spwd) + this.Query__1("id=?", id) //line ydb/demo/foo/foo_ydb.gox:74:1 + this.Ret__1("salt", &salt, "spwd", &spwd) +//line ydb/demo/foo/foo_ydb.gox:75:1 return Hmac(pwd, salt) == spwd }) -//line ydb/demo/foo/foo_ydb.gox:76:1 - this.Call__1("", "") //line ydb/demo/foo/foo_ydb.gox:77:1 - this.Ret__1(false) + this.Call__1("", "") //line ydb/demo/foo/foo_ydb.gox:78:1 - this.Call__1("user", "pwd") + this.Ret__1(false) //line ydb/demo/foo/foo_ydb.gox:79:1 + this.Call__1("user", "pwd") +//line ydb/demo/foo/foo_ydb.gox:80:1 this.Ret__1(true) }) -//line ydb/demo/foo/foo_ydb.gox:82:1 - this.Class("Articles", func() { //line ydb/demo/foo/foo_ydb.gox:83:1 + this.Class("Articles", func() { +//line ydb/demo/foo/foo_ydb.gox:84:1 this.Use("article") -//line ydb/demo/foo/foo_ydb.gox:85:1 - this.Api("listByTag", func(tag string) (result []ArticleEntry) { //line ydb/demo/foo/foo_ydb.gox:86:1 - var ids []string + this.Api("listByTag", func(tag string) (result []ArticleEntry) { //line ydb/demo/foo/foo_ydb.gox:87:1 - this.Query__1("tag.name=?", tag) + var ids []string //line ydb/demo/foo/foo_ydb.gox:88:1 + this.Query__1("tag.name=?", tag) +//line ydb/demo/foo/foo_ydb.gox:89:1 this.Ret__1("tag.article", &ids) -//line ydb/demo/foo/foo_ydb.gox:90:1 - this.Query__1("id=?", ids) //line ydb/demo/foo/foo_ydb.gox:91:1 - this.Ret__1(&result) + this.Query__1("id=?", ids) //line ydb/demo/foo/foo_ydb.gox:92:1 + this.Ret__1(&result) +//line ydb/demo/foo/foo_ydb.gox:93:1 return }) -//line ydb/demo/foo/foo_ydb.gox:95:1 - this.Api("listByAuthor", func(author string) (result []ArticleEntry) { //line ydb/demo/foo/foo_ydb.gox:96:1 - this.Query__1("author=?", author) + this.Api("listByAuthor", func(author string) (result []ArticleEntry) { //line ydb/demo/foo/foo_ydb.gox:97:1 - this.Ret__1(&result) + this.Query__1("author=?", author) //line ydb/demo/foo/foo_ydb.gox:98:1 + this.Ret__1(&result) +//line ydb/demo/foo/foo_ydb.gox:99:1 return }) }) diff --git a/ydb/mysql/mysql.go b/ydb/mysql/mysql.go index 1a1af80..fe63ebf 100644 --- a/ydb/mysql/mysql.go +++ b/ydb/mysql/mysql.go @@ -17,6 +17,9 @@ package mysql import ( + "log" + "os" + _ "github.com/go-sql-driver/mysql" "github.com/goplus/yap/ydb" ) @@ -25,3 +28,13 @@ import ( func Register(defaultDataSource string) { ydb.Register("mysql", defaultDataSource) } + +func init() { + ydb.Register("mysql", func() string { + dataSource := os.Getenv("YDB_MYSQL_TEST") + if dataSource == "" { + log.Panicln("env `YDB_MYSQL_TEST` not found, please set it before running") + } + return dataSource + }) +}