-
Notifications
You must be signed in to change notification settings - Fork 142
/
stmt.go
108 lines (99 loc) · 2.02 KB
/
stmt.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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package odbc
import (
"database/sql/driver"
"errors"
"sync"
"github.com/alexbrainman/odbc/api"
)
type Stmt struct {
c *Conn
query string
os *ODBCStmt
mu sync.Mutex
}
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
if c.bad {
return nil, driver.ErrBadConn
}
os, err := c.PrepareODBCStmt(query)
if err != nil {
return nil, err
}
return &Stmt{c: c, os: os, query: query}, nil
}
func (s *Stmt) NumInput() int {
if s.os == nil {
return -1
}
return len(s.os.Parameters)
}
func (s *Stmt) Close() error {
if s.os == nil {
return errors.New("Stmt is already closed")
}
ret := s.os.closeByStmt()
s.os = nil
return ret
}
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
if s.os == nil {
return nil, errors.New("Stmt is closed")
}
s.mu.Lock()
defer s.mu.Unlock()
if s.os.usedByRows {
s.os.closeByStmt()
s.os = nil
os, err := s.c.PrepareODBCStmt(s.query)
if err != nil {
return nil, err
}
s.os = os
}
err := s.os.Exec(args, s.c)
if err != nil {
return nil, err
}
var sumRowCount int64
for {
var c api.SQLLEN
ret := api.SQLRowCount(s.os.h, &c)
if IsError(ret) {
return nil, NewError("SQLRowCount", s.os.h)
}
sumRowCount += int64(c)
if ret = api.SQLMoreResults(s.os.h); ret == api.SQL_NO_DATA {
break
}
}
return &Result{rowCount: sumRowCount}, nil
}
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
if s.os == nil {
return nil, errors.New("Stmt is closed")
}
s.mu.Lock()
defer s.mu.Unlock()
if s.os.usedByRows {
s.os.closeByStmt()
s.os = nil
os, err := s.c.PrepareODBCStmt(s.query)
if err != nil {
return nil, err
}
s.os = os
}
err := s.os.Exec(args, s.c)
if err != nil {
return nil, err
}
err = s.os.BindColumns()
if err != nil {
return nil, err
}
s.os.usedByRows = true // now both Stmt and Rows refer to it
return &Rows{os: s.os}, nil
}