Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
12fcb95b9d | ||
7eee72b053 | |||
183beb1286 | |||
![]() |
cc8c502ec0 | ||
9cd64d91d0 |
73
db.go
73
db.go
@ -46,6 +46,9 @@ func init() {
|
|||||||
Desc: "db api by github.com/ssgo/db",
|
Desc: "db api by github.com/ssgo/db",
|
||||||
TsCode: dbTS,
|
TsCode: dbTS,
|
||||||
Example: dbMD,
|
Example: dbMD,
|
||||||
|
SetSSKey: func(key, iv []byte) {
|
||||||
|
db.SetEncryptKeys(key, iv)
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +70,20 @@ func makeDBObject(conn *db.DB, tx *db.Tx) map[string]any {
|
|||||||
panic(vm.NewGoError(r.Error))
|
panic(vm.NewGoError(r.Error))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"querya": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
|
args, conn, tx, _ := initDBArgs(argsIn, vm, 1)
|
||||||
|
var r *db.QueryResult
|
||||||
|
if tx != nil {
|
||||||
|
r = tx.Query(args.Str(0), args.Array(1)...)
|
||||||
|
} else {
|
||||||
|
r = conn.Query(args.Str(0), args.Array(1)...)
|
||||||
|
}
|
||||||
|
if r.Error == nil {
|
||||||
|
return vm.ToValue(makeQueryResult(r, r.SliceResults()))
|
||||||
|
} else {
|
||||||
|
panic(vm.NewGoError(r.Error))
|
||||||
|
}
|
||||||
|
},
|
||||||
"query1": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"query1": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args, conn, tx, _ := initDBArgs(argsIn, vm, 1)
|
args, conn, tx, _ := initDBArgs(argsIn, vm, 1)
|
||||||
var r *db.QueryResult
|
var r *db.QueryResult
|
||||||
@ -81,6 +98,28 @@ func makeDBObject(conn *db.DB, tx *db.Tx) map[string]any {
|
|||||||
panic(vm.NewGoError(r.Error))
|
panic(vm.NewGoError(r.Error))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"query1a": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
|
args, conn, tx, _ := initDBArgs(argsIn, vm, 1)
|
||||||
|
var r *db.QueryResult
|
||||||
|
if tx != nil {
|
||||||
|
r = tx.Query(args.Str(0), args.Array(1)...)
|
||||||
|
} else {
|
||||||
|
r = conn.Query(args.Str(0), args.Array(1)...)
|
||||||
|
}
|
||||||
|
if r.Error == nil {
|
||||||
|
a := r.SliceResults()
|
||||||
|
if len(a) > 0 && len(a[0]) > 0 {
|
||||||
|
list := make([]any, len(a))
|
||||||
|
for i := 0; i < len(a); i++ {
|
||||||
|
list[i] = a[i][0]
|
||||||
|
}
|
||||||
|
return vm.ToValue(makeQueryResult(r, list))
|
||||||
|
}
|
||||||
|
return vm.ToValue(makeQueryResult(r, nil))
|
||||||
|
} else {
|
||||||
|
panic(vm.NewGoError(r.Error))
|
||||||
|
}
|
||||||
|
},
|
||||||
"query11": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"query11": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args, conn, tx, _ := initDBArgs(argsIn, vm, 1)
|
args, conn, tx, _ := initDBArgs(argsIn, vm, 1)
|
||||||
var r *db.QueryResult
|
var r *db.QueryResult
|
||||||
@ -94,7 +133,7 @@ func makeDBObject(conn *db.DB, tx *db.Tx) map[string]any {
|
|||||||
if len(a) > 0 && len(a[0]) > 0 {
|
if len(a) > 0 && len(a[0]) > 0 {
|
||||||
return vm.ToValue(makeQueryResult(r, a[0][0]))
|
return vm.ToValue(makeQueryResult(r, a[0][0]))
|
||||||
}
|
}
|
||||||
return vm.ToValue(nil)
|
return vm.ToValue(makeQueryResult(r, nil))
|
||||||
} else {
|
} else {
|
||||||
panic(vm.NewGoError(r.Error))
|
panic(vm.NewGoError(r.Error))
|
||||||
}
|
}
|
||||||
@ -169,23 +208,47 @@ func makeDBObject(conn *db.DB, tx *db.Tx) map[string]any {
|
|||||||
panic(vm.NewGoError(r.Error))
|
panic(vm.NewGoError(r.Error))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"makeInKeys": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
|
return vm.ToValue(db.InKeys(args.Int(0)))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
obj["make"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
obj["make"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args, conn, _, logger := initDBArgs(argsIn, vm, 1)
|
args, conn, _, logger := initDBArgs(argsIn, vm, 1)
|
||||||
arg0 := args.Str(0)
|
erDesc := args.Str(0)
|
||||||
tryFile := gojs.FindPath(vm, arg0)
|
tryFile := gojs.FindPath(vm, erDesc)
|
||||||
if u.FileExists(tryFile) {
|
if u.FileExists(tryFile) {
|
||||||
arg0 = u.ReadFileN(tryFile)
|
erDesc = u.ReadFileN(tryFile)
|
||||||
}
|
}
|
||||||
if err := dao.MakeDBFromDesc(conn, args.Str(0), logger); err == nil {
|
if err := dao.MakeDBFromDesc(conn, erDesc+`
|
||||||
|
|
||||||
|
_deleted
|
||||||
|
table v30 PK
|
||||||
|
id v30 PK
|
||||||
|
time dt ct
|
||||||
|
owner v30 I
|
||||||
|
data t
|
||||||
|
`, logger); err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
panic(vm.NewGoError(err))
|
panic(vm.NewGoError(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj["makeER"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
|
args, _, _, logger := initDBArgs(argsIn, vm, 1)
|
||||||
|
outputFile := args.Str(0)
|
||||||
|
erDesc := args.Str(1)
|
||||||
|
tryFile := gojs.FindPath(vm, erDesc)
|
||||||
|
if u.FileExists(tryFile) {
|
||||||
|
erDesc = u.ReadFileN(tryFile)
|
||||||
|
}
|
||||||
|
dao.MakeERFile(erDesc, "", outputFile, logger)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
obj["destroy"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
obj["destroy"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
_, conn, _, _ := initDBArgs(argsIn, vm, 0)
|
_, conn, _, _ := initDBArgs(argsIn, vm, 0)
|
||||||
if err := conn.Destroy(); err == nil {
|
if err := conn.Destroy(); err == nil {
|
||||||
|
37
db.ts
37
db.ts
@ -4,8 +4,10 @@ export default {
|
|||||||
get,
|
get,
|
||||||
setDefault,
|
setDefault,
|
||||||
make,
|
make,
|
||||||
|
makeER,
|
||||||
query,
|
query,
|
||||||
query1,
|
query1,
|
||||||
|
query1a,
|
||||||
query11,
|
query11,
|
||||||
exec,
|
exec,
|
||||||
insert,
|
insert,
|
||||||
@ -14,13 +16,17 @@ export default {
|
|||||||
delete: delete_,
|
delete: delete_,
|
||||||
destroy,
|
destroy,
|
||||||
begin,
|
begin,
|
||||||
|
makeInKeys,
|
||||||
}
|
}
|
||||||
|
|
||||||
function get(dbName: string): DB { return null as any }
|
function get(dbName: string): DB { return null as any }
|
||||||
function setDefault(dbName: string): void { }
|
function setDefault(dbName: string): void { }
|
||||||
function make(descFileOrContent: string): Array<Object>{return null as any}
|
function make(descFileOrContent: string): Object[] { return null as any }
|
||||||
|
function makeER(outputFile: string, descFileOrContent: string): void { }
|
||||||
function query(sql: string, ...args: any): QueryResult { return null as any }
|
function query(sql: string, ...args: any): QueryResult { return null as any }
|
||||||
|
function querya(sql: string, ...args: any): QueryResultA { return null as any }
|
||||||
function query1(sql: string, ...args: any): QueryResult1 { return null as any }
|
function query1(sql: string, ...args: any): QueryResult1 { return null as any }
|
||||||
|
function query1a(sql: string, ...args: any): QueryResult1A { return null as any }
|
||||||
function query11(sql: string, ...args: any): QueryResult11 { return null as any }
|
function query11(sql: string, ...args: any): QueryResult11 { return null as any }
|
||||||
function exec(sql: string, ...args: any): ExecResult { return null as any }
|
function exec(sql: string, ...args: any): ExecResult { return null as any }
|
||||||
function insert(table: string, data: Object): ExecResult { return null as any }
|
function insert(table: string, data: Object): ExecResult { return null as any }
|
||||||
@ -29,11 +35,13 @@ function update(table: string, data:Object, where: string, ...args:any): ExecRes
|
|||||||
function delete_(table: string, where: string, ...args: any): ExecResult { return null as any }
|
function delete_(table: string, where: string, ...args: any): ExecResult { return null as any }
|
||||||
function destroy(): void { }
|
function destroy(): void { }
|
||||||
function begin(): Tx { return null as any }
|
function begin(): Tx { return null as any }
|
||||||
|
function makeInKeys(numKeys: number): string { return '' }
|
||||||
|
|
||||||
interface DB {
|
interface DB {
|
||||||
make(descFileOrContent: string): Array<Object>
|
make(descFileOrContent: string): Object[]
|
||||||
query(sql: string, ...args: any): QueryResult
|
query(sql: string, ...args: any): QueryResult
|
||||||
query1(sql: string, ...args: any): QueryResult1
|
query1(sql: string, ...args: any): QueryResult1
|
||||||
|
query1a(sql: string, ...args: any): QueryResult1A
|
||||||
query11(sql: string, ...args: any): QueryResult11
|
query11(sql: string, ...args: any): QueryResult11
|
||||||
exec(sql: string, ...args: any): ExecResult
|
exec(sql: string, ...args: any): ExecResult
|
||||||
insert(table: string, data: Object): ExecResult
|
insert(table: string, data: Object): ExecResult
|
||||||
@ -42,11 +50,13 @@ interface DB {
|
|||||||
delete(table: string, where: string, ...args: any): ExecResult
|
delete(table: string, where: string, ...args: any): ExecResult
|
||||||
destroy(): void
|
destroy(): void
|
||||||
begin(): Tx
|
begin(): Tx
|
||||||
|
makeInKeys(numKeys: number): string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Tx {
|
interface Tx {
|
||||||
query(sql: string, ...args: any): QueryResult
|
query(sql: string, ...args: any): QueryResult
|
||||||
query1(sql: string, ...args: any): QueryResult1
|
query1(sql: string, ...args: any): QueryResult1
|
||||||
|
query1a(sql: string, ...args: any): QueryResult1A
|
||||||
query11(sql: string, ...args: any): QueryResult11
|
query11(sql: string, ...args: any): QueryResult11
|
||||||
exec(sql: string, ...args: any): ExecResult
|
exec(sql: string, ...args: any): ExecResult
|
||||||
insert(table: string, data: Object): ExecResult
|
insert(table: string, data: Object): ExecResult
|
||||||
@ -54,29 +64,42 @@ interface Tx {
|
|||||||
update(table: string, data: Object, where: string, ...args: any): ExecResult
|
update(table: string, data: Object, where: string, ...args: any): ExecResult
|
||||||
delete(table: string, where: string, ...args: any): ExecResult
|
delete(table: string, where: string, ...args: any): ExecResult
|
||||||
end(ok: boolean): void
|
end(ok: boolean): void
|
||||||
|
makeInKeys(numKeys: number): string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueryResult {
|
interface QueryResult {
|
||||||
sql: string
|
sql: string
|
||||||
args: Array<any>
|
args: any[]
|
||||||
result: Array<Object>
|
result: Object[]
|
||||||
|
}
|
||||||
|
|
||||||
|
interface QueryResultA {
|
||||||
|
sql: string
|
||||||
|
args: any[]
|
||||||
|
result: any[][]
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueryResult1 {
|
interface QueryResult1 {
|
||||||
sql: string
|
sql: string
|
||||||
args: Array<any>
|
args: any[]
|
||||||
result: Object
|
result: Object
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface QueryResult1A {
|
||||||
|
sql: string
|
||||||
|
args: any[]
|
||||||
|
result: any[]
|
||||||
|
}
|
||||||
|
|
||||||
interface QueryResult11 {
|
interface QueryResult11 {
|
||||||
sql: string
|
sql: string
|
||||||
args: Array<any>
|
args: any[]
|
||||||
result: any
|
result: any
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ExecResult {
|
interface ExecResult {
|
||||||
sql: string
|
sql: string
|
||||||
args: Array<any>
|
args: any[]
|
||||||
id: number
|
id: number
|
||||||
changes: number
|
changes: number
|
||||||
}
|
}
|
||||||
|
10
db_test.js
10
db_test.js
@ -3,9 +3,15 @@ import db from 'apigo.cc/gojs/db'
|
|||||||
function main(testUserName) {
|
function main(testUserName) {
|
||||||
db.setDefault('sqlite://test.db')
|
db.setDefault('sqlite://test.db')
|
||||||
db.make(`
|
db.make(`
|
||||||
User
|
User // {api} {select:isValid=1}
|
||||||
id i AI
|
id i AI
|
||||||
name t
|
name v100 // [i]
|
||||||
|
password v100 // [p] {-}
|
||||||
|
age i // [n]
|
||||||
|
sex v10 // [s:Female,Male]
|
||||||
|
memo t // [t]
|
||||||
|
isValid b // [c]
|
||||||
|
version ubi //
|
||||||
`)
|
`)
|
||||||
db.insert('User', { name: testUserName })
|
db.insert('User', { name: testUserName })
|
||||||
let user = db.query1('SELECT * FROM User').result
|
let user = db.query1('SELECT * FROM User').result
|
||||||
|
20
go.mod
20
go.mod
@ -1,21 +1,21 @@
|
|||||||
module apigo.cc/gojs/db
|
module apigo.cc/gojs/db
|
||||||
|
|
||||||
go 1.18
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
apigo.cc/gojs v0.0.4
|
apigo.cc/gojs v0.0.12
|
||||||
github.com/ssgo/dao v0.1.5
|
github.com/ssgo/dao v0.1.7
|
||||||
github.com/ssgo/db v1.7.11
|
github.com/ssgo/db v1.7.11
|
||||||
github.com/ssgo/log v1.7.7
|
github.com/ssgo/log v1.7.7
|
||||||
github.com/ssgo/u v1.7.9
|
github.com/ssgo/u v1.7.13
|
||||||
modernc.org/sqlite v1.33.1
|
modernc.org/sqlite v1.34.4
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
filippo.io/edwards25519 v1.1.0 // indirect
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/dlclark/regexp2 v1.11.4 // indirect
|
github.com/dlclark/regexp2 v1.11.4 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||||
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // indirect
|
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // indirect
|
||||||
@ -25,11 +25,11 @@ require (
|
|||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/ssgo/config v1.7.8 // indirect
|
github.com/ssgo/config v1.7.9 // indirect
|
||||||
github.com/ssgo/standard v1.7.7 // indirect
|
github.com/ssgo/standard v1.7.7 // indirect
|
||||||
github.com/ssgo/tool v0.4.27 // indirect
|
github.com/ssgo/tool v0.4.28 // indirect
|
||||||
golang.org/x/sys v0.26.0 // indirect
|
golang.org/x/sys v0.28.0 // indirect
|
||||||
golang.org/x/text v0.19.0 // indirect
|
golang.org/x/text v0.21.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
|
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
|
||||||
modernc.org/libc v1.55.3 // indirect
|
modernc.org/libc v1.55.3 // indirect
|
||||||
|
Loading…
Reference in New Issue
Block a user