Skip to content

Commit

Permalink
env YDB_MYSQL_TEST as mysql default datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Feb 7, 2024
1 parent 6e42ffb commit 1a773e3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
11 changes: 8 additions & 3 deletions ydb/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion ydb/demo/foo/foo_ydb.gox
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import (
"time"

_ "github.com/goplus/yap/ydb/mysql"
_ "github.com/goplus/yap/ydb/sqlite3"
)

Expand Down Expand Up @@ -32,7 +33,7 @@ type Tag struct {
Article string
}

engine "sqlite3"
engine "mysql"

table User, "user v0.1.0"

Expand Down
95 changes: 48 additions & 47 deletions ydb/demo/foo/gop_autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
}

Expand All @@ -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", "[email protected]", "", 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", "[email protected]", "", time.Now())
//line ydb/demo/foo/foo_ydb.gox:67:1
this.Call__1("user", "pwd", "nickname", "[email protected]", "13500000000", time.Now())
this.Ret__0(nil)
//line ydb/demo/foo/foo_ydb.gox:68:1
this.Call__1("user", "pwd", "nickname", "[email protected]", "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
})
})
Expand Down
13 changes: 13 additions & 0 deletions ydb/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package mysql

import (
"log"
"os"

_ "github.com/go-sql-driver/mysql"
"github.com/goplus/yap/ydb"
)
Expand All @@ -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
})
}

0 comments on commit 1a773e3

Please sign in to comment.