-
Notifications
You must be signed in to change notification settings - Fork 9
/
classfile.go
157 lines (134 loc) · 3.69 KB
/
classfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* 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 (
"context"
"database/sql"
"flag"
"log"
"reflect"
"strings"
)
const (
GopPackage = "github.com/goplus/yap/test"
)
var (
debugExec bool
)
// -----------------------------------------------------------------------------
type Sql struct {
driver *Engine
wrap func(string, error) error
tables map[string]*Table
classes map[string]*Class
db *sql.DB
autodrop bool
}
func (p *Sql) initSql() {
p.tables = make(map[string]*Table)
p.classes = make(map[string]*Class)
}
// Engine initializes database by specified engine name.
func (p *Sql) Engine__0(name string) {
driver, ok := engines[name]
if !ok {
log.Panicf("engine `%s` not found: please call ydb.Register first\n", name)
}
defaultDataSource := driver.TestSource
dataSource, ok := defaultDataSource.(string)
if !ok {
dataSource = defaultDataSource.(func() string)()
}
const (
autodropParam = "autodrop"
)
if strings.HasSuffix(dataSource, autodropParam) {
dataSource = dataSource[:len(dataSource)-len(autodropParam)-1]
p.autodrop = true
}
db, err := sql.Open(name, dataSource)
if err != nil {
log.Panicln("sql.Open:", err)
}
p.db = db
p.driver = driver
p.wrap = driver.WrapErr
}
// Engine returns engine name of the database.
func (p *Sql) Engine__1() string {
return p.driver.Name
}
func (p *Sql) defineTable(nameVer string, zeroSchema any) {
var name, ver string
pos := strings.IndexByte(nameVer, ' ') // user v0.1.0
if pos < 0 {
ver = nameVer
} else {
name, ver = nameVer[:pos], strings.TrimLeft(nameVer[pos+1:], " \t")
}
schema := reflect.TypeOf(zeroSchema).Elem()
if name == "" {
name = dbName(schema.Name())
}
if _, ok := p.tables[name]; ok {
log.Panicf("table `%s` exists\n", name)
}
tbl := newTable(name, ver, schema)
p.tables[name] = tbl
tbl.create(context.TODO(), p)
}
func dbName(fldName string) string {
c := fldName[0]
if c >= 'A' && c <= 'Z' {
c += ('a' - 'A')
}
return string(c) + fldName[1:]
}
// Table creates a new table by specified Schema.
func Gopt_Sql_Gopx_Table[Schema any](sql interface{ defineTable(string, any) }, nameVer string) {
sql.defineTable(nameVer, (*Schema)(nil))
}
// -----------------------------------------------------------------------------
type Engine struct {
Name string
TestSource any // can be a `string` or a `func() string` object.
WrapErr func(prompt string, err error) error
}
var (
engines = make(map[string]*Engine) // engineName => engine
)
// Register registers an engine.
func Register(e *Engine) {
engines[e.Name] = e
}
// -----------------------------------------------------------------------------
type AppGen struct {
}
func (p *AppGen) initApp() {
}
func Gopt_AppGen_Main(app interface{ initApp() }, workers ...interface{ initClass(self any) }) {
flag.BoolVar(&debugExec, "v", false, "verbose infromation")
flag.Parse()
app.initApp()
if me, ok := app.(interface{ MainEntry() }); ok {
me.MainEntry()
}
for _, worker := range workers {
worker.initClass(worker)
worker.(interface{ Main() }).Main()
}
}
// -----------------------------------------------------------------------------