-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
57 lines (52 loc) · 2.02 KB
/
query.go
File metadata and controls
57 lines (52 loc) · 2.02 KB
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
package pgbalancer
import (
"context"
"database/sql"
"time"
)
// Query executes a query that returns rows, typically a SELECT.
// The args are for any placeholder parameters in the query.
// Query uses a free node as the physical db.
func (c *Cluster) Query(query string, args ...interface{}) (*sql.Rows, error) {
node := c.Node()
startTime := time.Now()
r, err := node.db.Query(query, args...)
stopTime := time.Now()
c.l.Printf("node: %s; q: %s; t: %s", node.name, query, stopTime.Sub(startTime))
return r, err
}
// QueryContext executes a query that returns rows, typically a SELECT.
// The args are for any placeholder parameters in the query.
// Query uses a free node as the physical db.
func (c *Cluster) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
node := c.Node()
startTime := time.Now()
r, err := node.db.QueryContext(ctx, query, args...)
stopTime := time.Now()
c.l.Printf("node: %s; q: %s; t: %s", node.name, query, stopTime.Sub(startTime))
return r, err
}
// QueryRow executes a query that is expected to return at most one row.
// QueryRow always return a non-nil value.
// Errors are deferred until Row's Scan method is called.
// Query uses a free node as the physical db.
func (c *Cluster) QueryRow(query string, args ...interface{}) *sql.Row {
node := c.Node()
startTime := time.Now()
r := node.db.QueryRow(query, args...)
stopTime := time.Now()
c.l.Printf("node: %s; q: %s; t: %s", node.name, query, stopTime.Sub(startTime))
return r
}
// QueryRowContext executes a query that is expected to return at most one row.
// QueryRowContext always return a non-nil value.
// Errors are deferred until Row's Scan method is called.
// Query uses a free node as the physical db.
func (c *Cluster) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
node := c.Node()
startTime := time.Now()
r := node.db.QueryRowContext(ctx, query, args...)
stopTime := time.Now()
c.l.Printf("node: %s; q: %s; t: %s", node.name, query, stopTime.Sub(startTime))
return r
}