forked from weigj/go-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
156 lines (128 loc) · 2.46 KB
/
sql.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
// Copyright (c) 2012, Wei guangjing <vcc.163@gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package driver
import (
"database/sql"
"database/sql/driver"
"errors"
"io"
"github.com/weigj/odbc"
)
func init() {
d := &Driver{}
sql.Register("odbc", d)
}
type Driver struct {
}
func (d *Driver) Open(dsn string) (driver.Conn, error) {
c, err := odbc.Connect(dsn)
if err != nil {
return nil, err
}
conn := &conn{c: c}
return conn, nil
}
func (d *Driver) Close() error {
return nil
}
type conn struct {
c *odbc.Connection
t *tx
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
st, err := c.c.Prepare(query)
if err != nil {
return nil, err
}
stmt := &stmt{st: st}
return stmt, nil
}
func (c *conn) Begin() (driver.Tx, error) {
if err := c.c.AutoCommit(false); err != nil {
return nil, err
}
return &tx{c: c}, nil
}
func (c *conn) Close() error {
if c.c != nil {
return c.c.Close()
}
return nil
}
type tx struct {
c *conn
}
func (t *tx) Commit() error {
err := t.c.c.Commit()
return err
}
func (t *tx) Rollback() error {
err := t.c.c.Rollback()
return err
}
type stmt struct {
st *odbc.Statement
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
if err := s.st.Execute2(args); err != nil {
return nil, err
}
rowsAffected, err := s.st.RowsAffected()
r := &result{rowsAffected: int64(rowsAffected)}
return r, err
}
func (s *stmt) NumInput() int {
return s.st.NumParams()
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
if err := s.st.Execute2(args); err != nil {
return nil, err
}
rows := &rows{s: s}
return rows, nil
}
func (s *stmt) Close() error {
s.st.Close()
return nil
}
type result struct {
rowsAffected int64
}
func (r *result) LastInsertId() (int64, error) {
return 0, errors.New("not supported")
}
func (r *result) RowsAffected() (int64, error) {
return r.rowsAffected, nil
}
type rows struct {
s *stmt
}
func (r *rows) Columns() []string {
c, err := r.s.st.NumFields()
if err != nil {
return nil
}
columns := make([]string, c)
for i, _ := range columns {
f, err := r.s.st.FieldMetadata(i + 1)
if err != nil {
return nil
}
columns[i] = f.Name
}
return columns
}
func (r *rows) Close() error {
return r.s.Close()
}
func (r *rows) Next(dest []driver.Value) error {
eof, err := r.s.st.FetchOne2(dest)
if err != nil {
return err
}
if eof {
return io.EOF
}
return nil
}