-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisc_builtin_mssql.go
105 lines (95 loc) · 3.11 KB
/
isc_builtin_mssql.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
package goscript
import (
"database/sql"
"fmt"
_ "github.com/microsoft/go-mssqldb"
)
type mssqlObject struct {
baseObject
db *sql.DB
}
func (r *Runtime) builtinMssql_close(call FunctionCall) Value {
thisObj := r.toObject(call.This)
mo, ok := thisObj.self.(*mssqlObject)
if !ok {
panic(r.NewTypeError("Method Mssql.prototype.close called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
_ = mo.db.Close()
return _undefined
}
func (r *Runtime) builtinMssql_exec(call FunctionCall) Value {
thisObj := r.toObject(call.This)
mo, ok := thisObj.self.(*mssqlObject)
if !ok {
panic(r.NewTypeError("Method Mssql.prototype.exec called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
_sql := call.Argument(0).toString().String()
_, err := mo.db.Exec(_sql)
return r.toBoolean(err == nil)
}
func (r *Runtime) builtinMssql_query(call FunctionCall) Value {
thisObj := r.toObject(call.This)
mo, ok := thisObj.self.(*mssqlObject)
if !ok {
panic(r.NewTypeError("Method Mssql.prototype.query called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
_sql := call.Argument(0).toString().String()
rows, err := DatabaseQuery(mo.db, _sql)
if err != nil {
return _null
} else {
return r.ToValue(rows)
}
}
func (r *Runtime) builtin_newMssql(args []Value, newTarget *Object) *Object {
if newTarget == nil {
panic(r.needNew("Mssql"))
}
if len(args) != 5 {
panic("number of arguments must be 5")
}
// 连接数据库
_host := args[0].toString().String()
_port := args[1].ToInteger()
_user := args[2].toString().String()
_password := args[3].toString().String()
_database := args[4].toString().String()
db, err := sql.Open("sqlserver", fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", _user, _password, _host, _port, _database))
if err != nil {
return nil
} else {
proto := r.getPrototypeFromCtor(newTarget, r.global.Mssql, r.global.MssqlPrototype)
o := &Object{runtime: r}
mo := &mssqlObject{
baseObject: baseObject{
class: classMssql,
val: o,
prototype: proto,
extensible: true,
values: nil,
},
db: db,
}
o.self = mo
mo.init()
return o
}
}
func (r *Runtime) createMssqlProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
o._putProp("constructor", r.global.Mssql, true, false, true)
o._putProp("close", r.newNativeFunc(r.builtinMssql_close, nil, "close", nil, 0), true, false, true)
o._putProp("exec", r.newNativeFunc(r.builtinMssql_exec, nil, "exec", nil, 1), true, false, true)
o._putProp("query", r.newNativeFunc(r.builtinMssql_query, nil, "query", nil, 1), true, false, true)
o._putSym(SymToStringTag, valueProp(asciiString(classMssql), false, false, true))
return o
}
func (r *Runtime) createMssql(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newMssql, r.global.MssqlPrototype, "Mssql", 5)
return o
}
func (r *Runtime) initMssql() {
r.global.MssqlPrototype = r.newLazyObject(r.createMssqlProto)
r.global.Mssql = r.newLazyObject(r.createMssql)
r.addToGlobal("Mssql", r.global.Mssql)
}