Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all gen-api support optional (src ast.Node) #56

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions ydb/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package ydb

import (
"context"
"log"
"reflect"

Expand All @@ -28,58 +29,77 @@ import (
type Class struct {
name string
tbl string
apis map[string]*dbApi
api *dbApi
apis map[string]*api
api *api
result []reflect.Value
ret func(args ...any)
}

func newClass(name string) *Class {
apis := make(map[string]*dbApi)
apis := make(map[string]*api)
return &Class{name: name, apis: apis}
}

func (p *Class) create(ctx context.Context, sql *Sql) {
}

// Ret checks a query or call result.
func (p *Class) Ret__0(src ast.Node, args ...any) {
p.ret(args...)
}

// Ret checks a query or call result.
func (p *Class) Ret(args ...any) {
func (p *Class) Ret__1(args ...any) {
p.ret(args...)
}

// -----------------------------------------------------------------------------

func (p *Class) Use(table string) {
// Use sets the default table used in following sql operations.
func (p *Class) Use(table string, src ...ast.Node) {
p.tbl = table
}

func (p *Class) Insert(kvPair ...any) {
// Insert inserts a new row.
func (p *Class) Insert__0(src ast.Node, kvPair ...any) {
}

// Insert inserts a new row.
func (p *Class) Insert__1(kvPair ...any) {
}

func (p *Class) queryRet(kvPair ...any) {
}

func (p *Class) Query(query string) {
// Query creates a new query.
func (p *Class) Query(query string, src ...ast.Node) {
p.ret = p.queryRet
}

func (p *Class) Limit__0(n int) {
// Limit sets query result rows limit.
func (p *Class) Limit__0(n int, src ...ast.Node) {
}

func (p *Class) Limit__1(n int, query string) {
// Limit checks if query result rows is < n or not.
func (p *Class) Limit__1(n int, query string, src ...ast.Node) {
}

// -----------------------------------------------------------------------------

type dbApi struct {
type api struct {
name string
spec any
}

func (p *Class) Api(name string, spec any, fnlit ...*ast.FuncLit) {
api := &dbApi{name: name, spec: spec}
// Api creates a new api by a spec.
func (p *Class) Api(name string, spec any, src ...*ast.FuncLit) {
api := &api{name: name, spec: spec}
p.api = api
p.apis[name] = api
}

func (p *Class) Call(args ...any) {
// Call calls an api with specified args.
func (p *Class) Call__0(src ast.Node, args ...any) {
if p.api == nil {
log.Panicln("please call after an api definition")
}
Expand All @@ -91,6 +111,11 @@ func (p *Class) Call(args ...any) {
p.ret = p.callRet
}

// Call calls an api with specified args.
func (p *Class) Call__1(args ...any) {
p.Call__0(nil, args...)
}

func (p *Class) callRet(args ...any) {
}

Expand Down
18 changes: 11 additions & 7 deletions ydb/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"database/sql"
"log"
"strings"

"github.com/goplus/gop/ast"
)

const (
Expand All @@ -47,7 +49,7 @@ func (p *Sql) initSql() {
}

// Engine initializes database by specified engine name.
func (p *Sql) Engine__0(name string) {
func (p *Sql) Engine__0(name string, src ...ast.Node) {
if defaultDataSource, ok := engineDataSource[name]; ok {
db, err := sql.Open(name, defaultDataSource)
if err != nil {
Expand All @@ -63,8 +65,8 @@ func (p *Sql) Engine__1() string {
return p.driver
}

// Table creates a new table.
func (p *Sql) Table(nameVer string, spec func()) {
// Table creates a new table by a spec.
func (p *Sql) Table(nameVer string, spec func(), src ...ast.Node) {
pos := strings.IndexByte(nameVer, ' ') // user v0.1.0
if pos < 0 {
log.Panicln("table name should have a version: eg. `user v0.1.0`")
Expand All @@ -78,11 +80,13 @@ func (p *Sql) Table(nameVer string, spec func()) {
p.dbTable = nil
}

// Class creates a new class.
func (p *Sql) Class(name string, spec func()) {
p.dbClass = newClass(name)
p.classes[name] = p.dbClass
// Class creates a new class by a spec.
func (p *Sql) Class(name string, spec func(), src ...ast.Node) {
cls := newClass(name)
p.dbClass = cls
p.classes[name] = cls
spec()
cls.create(context.TODO(), p)
p.dbClass = nil
}

Expand Down
2 changes: 1 addition & 1 deletion ydb/demo/foo/foo_ydb.gox
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ table "tag v0.1.0", => {
col String, "id", "id@article"
col String, "tag"

unique "id", "tag"
unique ["id", "tag"]
}

class "Articles", => {
Expand Down
48 changes: 24 additions & 24 deletions ydb/demo/foo/gop_autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (this *foo) Main() {
//line ydb/demo/foo/foo_ydb.gox:7:1
this.Table("user v0.1.0", func() {
//line ydb/demo/foo/foo_ydb.gox:8:1
ydb.Gopt_Table_Gopx_Col__1[[32]byte](this, "id")
ydb.Gopt_Table_Gopx_Col__2[[32]byte](this, "id")
//line ydb/demo/foo/foo_ydb.gox:9:1
ydb.Gopt_Table_Gopx_Col__0[string](this, "spwd")
//line ydb/demo/foo/foo_ydb.gox:10:1
Expand All @@ -49,17 +49,17 @@ func (this *foo) Main() {
//line ydb/demo/foo/foo_ydb.gox:14:1
ydb.Gopt_Table_Gopx_Col__0[ydb.Date](this, "born")
//line ydb/demo/foo/foo_ydb.gox:15:1
ydb.Gopt_Table_Gopx_Col__1[[6]ydb.DateTime](this, "ctime")
ydb.Gopt_Table_Gopx_Col__2[[6]ydb.DateTime](this, "ctime")
//line ydb/demo/foo/foo_ydb.gox:17:1
this.Unique("id")
this.Unique__0("id")
//line ydb/demo/foo/foo_ydb.gox:18:1
this.Index("email")
this.Index__0("email")
//line ydb/demo/foo/foo_ydb.gox:19:1
this.Index("tel")
this.Index__0("tel")
//line ydb/demo/foo/foo_ydb.gox:20:1
this.Index("born")
this.Index__0("born")
//line ydb/demo/foo/foo_ydb.gox:21:1
this.Index("ctime")
this.Index__0("ctime")
})
//line ydb/demo/foo/foo_ydb.gox:24:1
this.Class("Users", func() {
Expand All @@ -81,68 +81,68 @@ func (this *foo) Main() {
//line ydb/demo/foo/foo_ydb.gox:35:1
spwd := Hmac(pwd, salt)
//line ydb/demo/foo/foo_ydb.gox:36:1
this.Insert("id", id, "spwd", spwd, "salt", salt, "nickname", nickname, "email", email, "tel", tel, "ctime", ctime)
this.Insert__1("id", id, "spwd", spwd, "salt", salt, "nickname", nickname, "email", email, "tel", tel, "ctime", ctime)
//line ydb/demo/foo/foo_ydb.gox:39:1
return nil
})
//line ydb/demo/foo/foo_ydb.gox:41:1
this.Call("user", "pwd", "nickname", "", "", time.Now())
this.Call__1("user", "pwd", "nickname", "", "", time.Now())
//line ydb/demo/foo/foo_ydb.gox:42:1
this.Ret(ErrNoEmailAndTel)
this.Ret__1(ErrNoEmailAndTel)
//line ydb/demo/foo/foo_ydb.gox:43:1
this.Call("user", "pwd", "nickname", "[email protected]", "", time.Now())
this.Call__1("user", "pwd", "nickname", "[email protected]", "", time.Now())
//line ydb/demo/foo/foo_ydb.gox:44:1
this.Ret(nil)
this.Ret__0(nil)
//line ydb/demo/foo/foo_ydb.gox:45:1
this.Call("user", "pwd", "nickname", "[email protected]", "13500000000", time.Now())
this.Call__1("user", "pwd", "nickname", "[email protected]", "13500000000", time.Now())
//line ydb/demo/foo/foo_ydb.gox:46:1
this.Ret(ydb.ErrDuplicated)
this.Ret__1(ydb.ErrDuplicated)
//line ydb/demo/foo/foo_ydb.gox:48:1
this.Api("login", func(id string, pwd string) bool {
//line ydb/demo/foo/foo_ydb.gox:49:1
var spwd, salt string
//line ydb/demo/foo/foo_ydb.gox:50:1
this.Query("id={id}")
//line ydb/demo/foo/foo_ydb.gox:51:1
this.Ret("salt", &salt, "spwd", &spwd)
this.Ret__1("salt", &salt, "spwd", &spwd)
//line ydb/demo/foo/foo_ydb.gox:52:1
return Hmac(pwd, salt) == spwd
})
//line ydb/demo/foo/foo_ydb.gox:54:1
this.Call("", "")
this.Call__1("", "")
//line ydb/demo/foo/foo_ydb.gox:55:1
this.Ret(false)
this.Ret__1(false)
//line ydb/demo/foo/foo_ydb.gox:56:1
this.Call("user", "pwd")
this.Call__1("user", "pwd")
//line ydb/demo/foo/foo_ydb.gox:57:1
this.Ret(true)
this.Ret__1(true)
})
//line ydb/demo/foo/foo_ydb.gox:60:1
this.Table("article v0.1.0", func() {
//line ydb/demo/foo/foo_ydb.gox:61:1
ydb.Gopt_Table_Gopx_Col__0[string](this, "id")
//line ydb/demo/foo/foo_ydb.gox:62:1
ydb.Gopt_Table_Gopx_Col__0[string](this, "author", "name@user")
ydb.Gopt_Table_Gopx_Col__1[string](this, "author", "name@user")
//line ydb/demo/foo/foo_ydb.gox:63:1
ydb.Gopt_Table_Gopx_Col__0[string](this, "title")
//line ydb/demo/foo/foo_ydb.gox:64:1
ydb.Gopt_Table_Gopx_Col__0[ydb.Blob](this, "body")
//line ydb/demo/foo/foo_ydb.gox:66:1
this.Unique("id")
this.Unique__0("id")
//line ydb/demo/foo/foo_ydb.gox:67:1
this.Index("author")
this.Index__0("author")
//line ydb/demo/foo/foo_ydb.gox:69:1
this.From("oldart v0.9.1", func() {
})
})
//line ydb/demo/foo/foo_ydb.gox:75:1
this.Table("tag v0.1.0", func() {
//line ydb/demo/foo/foo_ydb.gox:76:1
ydb.Gopt_Table_Gopx_Col__0[string](this, "id", "id@article")
ydb.Gopt_Table_Gopx_Col__1[string](this, "id", "id@article")
//line ydb/demo/foo/foo_ydb.gox:77:1
ydb.Gopt_Table_Gopx_Col__0[string](this, "tag")
//line ydb/demo/foo/foo_ydb.gox:79:1
this.Unique("id", "tag")
this.Unique__1([]string{"id", "tag"})
})
//line ydb/demo/foo/foo_ydb.gox:82:1
this.Class("Articles", func() {
Expand Down
43 changes: 27 additions & 16 deletions ydb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"strconv"
"strings"
"time"

"github.com/goplus/gop/ast"
)

var (
Expand Down Expand Up @@ -122,17 +124,25 @@ func newTable(name, ver string) *Table {
}

// From migrates from old table because it's an incompatible change
func (p *Table) From(old string, migrate func()) {
func (p *Table) From(old string, migrate func(), src ...ast.Node) {
}

// -----------------------------------------------------------------------------

func (p *Table) Unique(name ...string) {
p.uniqs = append(p.uniqs, name)
func (p *Table) Unique__0(name string, src ...ast.Node) {
p.uniqs = append(p.uniqs, []string{name})
}

func (p *Table) Unique__1(names []string, src ...ast.Node) {
p.uniqs = append(p.uniqs, names)
}

func (p *Table) Index(name ...string) {
p.idxs = append(p.idxs, name)
func (p *Table) Index__0(name string, src ...ast.Node) {
p.idxs = append(p.idxs, []string{name})
}

func (p *Table) Index__1(names []string, src ...ast.Node) {
p.idxs = append(p.idxs, names)
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -205,18 +215,26 @@ func (p *Table) defineCol(c *column) {
p.cols = append(p.cols, c)
}

func Gopt_Table_Gopx_Col__0[T basetype](tbl interface{ defineCol(c *column) }, name string, link ...string) {
func Gopt_Table_Gopx_Col__0[T basetype](tbl interface{ defineCol(c *column) }, name string, src ...ast.Node) {
Gopt_Table_Gopx_Col__1[T](tbl, name, "", src...)
}

func Gopt_Table_Gopx_Col__1[T basetype](tbl interface{ defineCol(c *column) }, name string, link string, src ...ast.Node) {
vcol := (*T)(nil)
tcol := colBaseType(vcol)
tbl.defineCol(&column{
typ: tcol,
name: name,
link: optString(link),
link: link,
zero: vcol,
})
}

func Gopt_Table_Gopx_Col__1[Array any](tbl interface{ defineCol(c *column) }, name string, link ...string) {
func Gopt_Table_Gopx_Col__2[Array any](tbl interface{ defineCol(c *column) }, name string, src ...ast.Node) {
Gopt_Table_Gopx_Col__3[Array](tbl, name, "", src...)
}

func Gopt_Table_Gopx_Col__3[Array any](tbl interface{ defineCol(c *column) }, name string, link string, src ...ast.Node) {
varr := (*Array)(nil)
tarr := reflect.TypeOf(varr).Elem()
if tarr.Kind() != reflect.Array {
Expand All @@ -229,17 +247,10 @@ func Gopt_Table_Gopx_Col__1[Array any](tbl interface{ defineCol(c *column) }, na
tbl.defineCol(&column{
typ: tcol,
name: name,
link: optString(link),
link: link,
zero: velem,
n: n,
})
}

func optString(v []string) string {
if v != nil {
return v[0]
}
return ""
}

// -----------------------------------------------------------------------------