forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_sp.go
183 lines (169 loc) · 4.83 KB
/
conn_sp.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package freetds
import (
"errors"
"fmt"
"unsafe"
)
/*
#cgo LDFLAGS: -lsybdb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
#include <sybfront.h>
#include <sybdb.h>
*/
import "C"
//Execute stored procedure by name and list of params.
//
//Example:
// conn.ExecSp("sp_help", "authors")
func (conn *Conn) ExecSp(spName string, params ...interface{}) (*SpResult, error) {
//hold references to data sent to the C code until the end of this function
//without this GC could remove something used later in C, and we will get SIGSEG
refHolder := make([]*[]byte, 0)
conn.clearMessages()
name := C.CString(spName)
defer C.free(unsafe.Pointer(name))
if C.dbrpcinit(conn.dbproc, name, 0) == C.FAIL {
return nil, conn.raiseError("dbrpcinit failed")
}
//input params
spParams, err := conn.getSpParams(spName)
if err != nil {
return nil, err
}
for i, spParam := range spParams {
//get datavalue for the suplied stored procedure parametar
var datavalue *C.BYTE
datalen := 0
if i < len(params) {
param := params[i]
if param != nil {
data, sqlDatalen, err := typeToSqlBuf(int(spParam.UserTypeId), param, conn.freetdsVersionGte095)
if err != nil {
return nil, err
}
if len(data) > 0 {
datalen = sqlDatalen
datavalue = (*C.BYTE)(unsafe.Pointer(&data[0]))
refHolder = append(refHolder, &data)
}
}
}
//set parametar valus, call dbrpcparam
if i < len(params) || spParam.IsOutput {
maxOutputSize := C.DBINT(-1)
status := C.BYTE(0)
if spParam.IsOutput {
status = C.DBRPCRETURN
maxOutputSize = C.DBINT(spParam.MaxLength)
if maxOutputSize == -1 {
maxOutputSize = 8000
}
}
paramname := C.CString(spParam.Name)
defer C.free(unsafe.Pointer(paramname))
if C.dbrpcparam(conn.dbproc, paramname, status,
C.int(spParam.UserTypeId), maxOutputSize, C.DBINT(datalen), datavalue) == C.FAIL {
return nil, errors.New("dbrpcparam failed")
}
}
}
//execute
if C.dbrpcsend(conn.dbproc) == C.FAIL {
return nil, conn.raiseError("dbrpcsend failed")
}
//results
result := NewSpResult()
result.results, err = conn.fetchResults()
if err != nil {
return nil, conn.raise(err)
}
//return status
if C.dbhasretstat(conn.dbproc) == C.TRUE {
result.status = int(C.dbretstatus(conn.dbproc))
}
//read output params
numOutParams := int(C.dbnumrets(conn.dbproc))
result.outputParams = make([]*SpOutputParam, numOutParams)
for i := 1; i <= numOutParams; i++ {
j := C.int(i)
len := C.dbretlen(conn.dbproc, j)
name := C.GoString(C.dbretname(conn.dbproc, j))
typ := int(C.dbrettype(conn.dbproc, j))
data := C.GoBytes(unsafe.Pointer(C.dbretdata(conn.dbproc, j)), len)
value := sqlBufToType(typ, data)
param := &SpOutputParam{Name: name, Value: value}
result.outputParams[i-1] = param
}
return result, nil
}
func (conn *Conn) raise(err error) error {
if len(conn.Error) != 0 {
return errors.New(fmt.Sprintf("%s\n%s", conn.Error, conn.Message))
} else {
return err
}
}
func (conn *Conn) raiseError(errMsg string) error {
return conn.raise(errors.New(errMsg))
}
// func toRpcParam(datatype int, value interface{}) (datalen C.DBINT, datavalue *C.BYTE, err error) {
// data, err := typeToSqlBuf(datatype, value)
// if err != nil {
// return
// }
// datalen = C.DBINT(len(data))
// if len(data) > 0 {
// datavalue = (*C.BYTE)(unsafe.Pointer(&data[0]))
// }
// //fmt.Printf("\ndatavalue: %v, datalen: %v, data: %v %s\n", datavalue, datalen, data, data)
// return
// }
//Stored procedure parameter definition
type spParam struct {
Name string
ParameterId int32
UserTypeId int32
IsOutput bool
MaxLength int16
Precision uint8
Scale uint8
}
//Read stored procedure parameters.
//Will cache params in connection or pool and reuse it.
func (conn *Conn) getSpParams(spName string) ([]*spParam, error) {
if spParams, ok := conn.spParamsCache[spName]; ok {
return spParams, nil
}
sql := fmt.Sprintf(`
select name, parameter_id, user_type_id, is_output, max_length, precision, scale
from sys.all_parameters
where object_id = (select object_id from sys.all_objects where object_id = object_id('%s'))
order by parameter_id
`, spName)
results, err := conn.exec(sql)
if err != nil {
return nil, err
}
r := results[0]
spParams := make([]*spParam, len(r.Rows))
for i := 0; r.Next(); i++ {
p := &spParam{}
err := r.Scan(&p.Name, &p.ParameterId, &p.UserTypeId, &p.IsOutput, &p.MaxLength, &p.Precision, &p.Scale)
//fixme: mapping uniqueidentifier, datetimeoffset, date, time, datetime2 to string
if p.UserTypeId == 0x24 || p.UserTypeId == 0x2B || p.UserTypeId == 0x28 || p.UserTypeId == 0x29 || p.UserTypeId == 0x2A {
p.UserTypeId = 0x27
}
if err != nil {
return nil, err
}
spParams[i] = p
}
conn.spParamsCache[spName] = spParams
return spParams, nil
}