-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconnect.go
126 lines (96 loc) · 2.06 KB
/
connect.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
// Copyright 2019 The Go-CUBRID-Driver Authors. All rights reserved.
//
// Package cubrid provides a CUBRID driver for Go's database/sql package.
//
// The driver should be used via the database/sql package:
//
// import "database/sql"
// import _ "github.com/CUBRID/cubrid-go"
package cubrid
// #include <cas_cci.h>
import "C"
import (
"database/sql/driver"
"errors"
"time"
)
type cub_conn struct {
handle int
tr_start time.Time
closed bool
cancel bool
}
func (c *cub_conn) Commit() (error) {
var err error
var err_buf C.T_CCI_ERROR
if c.handle < 0 {
return driver.ErrBadConn
}
res := C.cci_end_tran(C.int(c.handle), 1, &err_buf)
if res < 0 {
err = errors.New(C.GoString(&err_buf.err_msg[0]))
return err
}
return nil
}
func (c *cub_conn) Rollback() (error) {
var err error
var err_buf C.T_CCI_ERROR
if c.handle < 0 {
return driver.ErrBadConn
}
res := C.cci_end_tran(C.int(c.handle), 2, &err_buf)
if res < 0 {
err = errors.New(C.GoString(&err_buf.err_msg[0]))
}
return err
}
func (c *cub_conn) Prepare(query string) (driver.Stmt, error) {
var err error
var flag C.char
var err_buf C.T_CCI_ERROR
var res C.int
err = nil
if c.handle < 0 {
return nil, driver.ErrBadConn
}
sql := C.CString(query)
res = C.cci_prepare(C.int(c.handle), sql, flag, &err_buf)
if res < 0 {
err := errors.New(C.GoString(&err_buf.err_msg[0]))
return nil, err
}
stmt := &cub_stmt {
conn: c,
handle: -1,
params: 0,
}
stmt.handle = int(res)
res = C.cci_get_bind_num(res)
if res < 0 {
err := errors.New("connect: cci_get_bind_num, bind param numbers error")
return stmt, err
}
stmt.params = int(res)
return stmt, err
}
func (c *cub_conn) Begin() (driver.Tx, error) {
if c.handle < 0 {
return c, driver.ErrBadConn
}
c.tr_start = time.Now()
return c, nil
}
func (c* cub_conn) Close() (error) {
var err_buf C.T_CCI_ERROR
if c.handle < 0 {
return driver.ErrBadConn
}
res := C.cci_disconnect(C.int(c.handle), &err_buf)
if res < 0 {
err := errors.New(C.GoString(&err_buf.err_msg[0]))
return err
}
c.closed = true
return nil
}