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

ydb #53

Merged
merged 3 commits into from
Feb 3, 2024
Merged

ydb #53

Changes from 1 commit
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
Next Next commit
ydb
xushiwei committed Feb 2, 2024
commit bc1b1767d1342869006e9cfe9d23c3202611eb03
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ module github.com/goplus/yap
go 1.18

require (
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/goplus/gop v1.2.0
github.com/qiniu/x v1.13.3
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7 h1:rzpGloMVgWexXti9yRXMH8aAdjNy8rZrlAQdKvzZeMg=
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/goplus/gop v1.2.0 h1:1EOUKhr4OitJs0BtVBVVbejuUkLbXMiFparS1VW7Fhg=
github.com/goplus/gop v1.2.0/go.mod h1:F4xOWRMTPCKzNBaF1gZC/JsEKtYW1+Ldwp7tyFoaOWo=
github.com/qiniu/x v1.13.3 h1:NER9aJnVzjH0XapzIWrWNAn2SPwck0xGMyIIlfCMm84=
github.com/qiniu/x v1.13.3/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
3 changes: 3 additions & 0 deletions gop.mod
Original file line number Diff line number Diff line change
@@ -9,3 +9,6 @@ import github.com/goplus/yap/ytest/auth/jwt
project _ytest.gox App github.com/goplus/yap/ytest
class _ytest.gox Case
import github.com/goplus/yap/ytest/auth/jwt

project _ydb.gox AppGen github.com/goplus/yap/ydb
class _ydb.gox Sql github.com/goplus/yap/ydb
44 changes: 44 additions & 0 deletions ydb/class.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ydb

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

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

type Class struct {
}

func newClass(name string) *Class {
return &Class{}
}

func (p *Class) Use(table string) {
}

func (p *Class) Api(name string, creator func(), fnlit ...*ast.FuncLit) {
}

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

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

// -----------------------------------------------------------------------------
101 changes: 101 additions & 0 deletions ydb/classfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ydb

import (
"strings"
)

const (
GopPackage = true
)

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

type dbTable = Table
type dbClass = Class

type Sql struct {
driver string
tables map[string]*Table
classes map[string]*Class
*dbTable
*dbClass
}

func (p *Sql) initSql() {
p.tables = make(map[string]*Table)
p.classes = make(map[string]*Class)
}

// Engine sets engine name of a sql driver.
func (p *Sql) Engine(name string) {
p.driver = name
}

// Table creates a new table.
func (p *Sql) Table(nameVer string, creator func()) {
pos := strings.IndexByte(nameVer, ' ') // user v0.1.0
if pos < 0 {
panic("table name should have a version: eg. `user v0.1.0`")
}
name, ver := nameVer[:pos], strings.TrimLeft(nameVer[pos+1:], " \t")
p.dbTable = newTable(name, ver)
p.tables[name] = p.dbTable
creator()
p.dbTable = nil
}

// Class creates a new class.
func (p *Sql) Class(name string, creator func()) {
p.dbClass = newClass(name)
p.classes[name] = p.dbClass
creator()
p.dbClass = nil
}

// Ret checks a query or call result.
func (p *Sql) Ret(args ...any) {
if tbl := p.dbTable; tbl != nil {
tbl.Ret(args...)
} else if cls := p.dbClass; cls != nil {
cls.Ret(args...)
} else {
panic("pelase use ret after query or call")
}
}

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

type AppGen struct {
}

func (p *AppGen) initApp() {
}

func Gopt_AppGen_Main(app interface{ initApp() }, workers ...interface{ initSql() }) {
app.initApp()
if me, ok := app.(interface{ MainEntry() }); ok {
me.MainEntry()
}
for _, worker := range workers {
worker.initSql()
worker.(interface{ Main() }).Main()
}
}

// -----------------------------------------------------------------------------
80 changes: 80 additions & 0 deletions ydb/demo/foo/foo_ydb.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import "time"

engine "mysql"

table "user v0.1.0", => {
col [32]byte, "id"
col string, "spwd"
col string, "salt"
col string, "nickname"
col string, "email"
col string, "tel"
col date, "born"
col [6]datetime, "ctime" // 1000-01-01 00:00:00.000000

unique "id"
index "email"
index "tel"
index "born"
index "ctime"
}

class "Users", => {
use "user"

api "register", func(id, pwd, nickname, email, tel string, ctime time.Time) error {
if email == "" && tel == "" {
return ErrNoEmailAndTel
}
limit 3, "email={email}" // one email can't register >3 accounts
limit 3, "tel={tel}"

salt := rand
spwd := hmac(pwd, salt)
insert "id", id, "spwd", spwd, "salt", salt, "nickname", nickname,
"email", email, "tel", tel, "ctime", ctime
// ErrDuplicated
return nil
}
call "user", "pwd", "nickname", "", "", time.now
ret ErrNoEmailAndTel
call "user", "pwd", "nickname", "[email protected]", "", time.now
ret nil
call "user", "pwd", "nickname", "[email protected]", "13500000000", time.now
ret ErrDuplicated

api "login", func(id, pwd string) bool {
var spwd, salt string
query "id={id}"
ret "salt", &salt, "spwd", &spwd
return hmac(pwd, salt) == spwd
}
call "", ""
ret false
call "user", "pwd"
ret true
}

table "article v0.1.0", => {
col string, "id"
col string, "author", "name@user"
col string, "title"
col blob, "body"

unique "id"
index "author"

from "oldart v0.9.1", => {
// migrate from oldart table because it's an incompatible change
// ...
}
}

table "tag v0.1.0", => {
col string, "id", "id@article"
col string, "tag"
unique "id", "tag"
}

class "Articles", => {
}
150 changes: 150 additions & 0 deletions ydb/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ydb

import (
"fmt"
"reflect"
"strconv"
"time"
)

type (
blob []byte
float float64

date time.Time
datetime time.Time
timestamp time.Time
)

type basetype interface {
string | int | bool | blob | date | datetime | timestamp | float
}

func colBaseType(v any) string {
switch v.(type) {
case *string:
return "TEXT"
case *int:
return "INT"
case *bool:
return "BOOL"
case *blob:
return "BLOB"
case *date:
return "DATE"
case *datetime:
return "DATETIME"
case *timestamp:
return "TIMESTAMP"
case *float:
return "DOUBLE"
default:
panic("unknown column type: " + reflect.TypeOf(v).Elem().String())
}
}

/*
type baseelem interface {
byte | int | blob | datetime | timestamp | float
}
*/

func colArrType(v any, n int) string {
switch v.(type) {
case byte:
if n <= 65535 {
return "TEXT(" + strconv.Itoa(n) + ")"
}
if n <= 16777215 {
return "MEDIUMTEXT"
}
return "LONGTEXT"
case int:
return "BIGINT(" + strconv.Itoa(n) + ")"
case blob:
if n <= 16777215 {
return "MEDIUMBLOB"
}
return "LONGBLOB"
case datetime:
return "DATETIME(" + strconv.Itoa(n) + ")"
case timestamp:
return "TIMESTAMP(" + strconv.Itoa(n) + ")"
case float:
return "FLOAT(" + strconv.Itoa(n) + ")"
default:
panic(fmt.Sprintf("unknown column type: [%d]%T", n, v))
}
}

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

type Table struct {
}

func newTable(name, ver string) *Table {
return &Table{}
}

func (p *Table) Unique(name ...string) {
}

func (p *Table) Index(name ...string) {
}

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

func (p *Table) Insert(kvPair ...any) {
}

func (p *Table) Ret(kvPair ...any) {
}

func (p *Table) Query(query string) {
}

func (p *Table) Limit__0(n int) {
}

func (p *Table) Limit__1(n int, query string) {
}

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

func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) {
tcol := colBaseType((*T)(nil))
_ = tcol
}

func Gopt_Table_Gopx_Col__1[Array any](p *Table, name string) {
varr := (*Array)(nil)
tarr := reflect.TypeOf(varr).Elem()
if tarr.Kind() != reflect.Array {
panic(fmt.Sprintf("unknown column type: %T", varr))
}
n := tarr.Len()
elem := tarr.Elem()
v := reflect.Zero(reflect.PointerTo(elem)).Interface()
tcol := colArrType(v, n)
_ = tcol
}

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