-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stmt.go
67 lines (55 loc) · 1.21 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
package elasticsearch
import (
"database/sql/driver"
"fmt"
"strings"
)
//Stmt is a prepared statement
type Stmt struct {
dsn string
query string
}
//Close closes the statement
func (s *Stmt) Close() error {
if debug {
fmt.Println("Stmt Close")
}
s.query = ""
if s.dsn == "" {
return ErrClosedConnection
}
s.dsn = ""
return nil
}
//NumInput returns the number of placeholder parameters
func (s *Stmt) NumInput() int {
if debug {
fmt.Println("Stmt NumInput")
}
return strings.Count(strings.ReplaceAll(s.query, "%%", ""), "%")
}
//Exec is not support at this time
func (*Stmt) Exec(args []driver.Value) (driver.Result, error) {
if debug {
fmt.Println("Stmt Exec:", args)
}
return &Result{}, ErrNotSupportedFunction
}
//Query executes a query that may return rows, such as a SELECT
func (s *Stmt) Query(args []driver.Value) (result driver.Rows, errResult error) {
if debug {
fmt.Println("Stmt Query:", args)
}
defer func() {
if err := recover(); err != nil {
result = nil
errResult = err.(error)
}
}()
var newArgs []interface{}
for _, arg := range args {
newArgs = append(newArgs, arg.(interface{}))
}
newQusery := fmt.Sprintf(s.query, newArgs...)
return newRows(s.dsn, newQusery)
}