-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdq.go
166 lines (130 loc) · 5.01 KB
/
dq.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
157
158
159
160
161
162
163
164
165
166
package dynaQ
import (
"context"
"database/sql"
"github.com/syke99/dynaQ/pkg/resources/timeFmt"
"github.com/syke99/dynaQ/internal"
"github.com/syke99/dynaQ/pkg/resources/models"
conServ "github.com/syke99/dynaQ/pkg/services/conn"
dbServ "github.com/syke99/dynaQ/pkg/services/db"
)
// DynaQ is the base dynamic querier
type DynaQ struct {
db *sql.DB
conn *sql.Conn
connAutoClose bool
timeFormat string
dbService dbServ.DataBase
conService conServ.Connection
}
// DynaQOptions are options for configuring a dynamic querier
type DynaQOptions func(*DynaQ)
// WithTimeFormat allows for setting the time format to check for in database values to something other than the Default Time Format "2006-01-02 15:04"
func WithTimeFormat(timeFormat string) DynaQOptions {
return func(dq *DynaQ) {
dq.timeFormat = timeFormat
}
}
// WithConnectionAutoClose allows for setting the automatic closing of any and all connections on this dynamic queriers after a query is executed,
// if desired. By default, DynaQ does not automatically close individual database connections
func WithConnectionAutoClose() DynaQOptions {
return func(dq *DynaQ) {
dq.connAutoClose = true
}
}
// NewDynaQ returns a new dynamic querier with the provided configurations. The user must pass in a created database connection, but can be optionally configured
// with different options using the provided pre-defined DynaQOptions
func NewDynaQ(db *sql.DB, opts ...DynaQOptions) DynaQ {
dbService := dbServ.NewDbService()
dummyConn, _ := db.Conn(context.Background())
dq := &DynaQ{
db: db,
conn: dummyConn,
connAutoClose: false,
timeFormat: timeFmt.DefaultTimeFormat,
dbService: dbService,
conService: conServ.Connection{},
}
for _, opt := range opts {
opt(dq)
}
return *dq
}
// NewDqConn allows your dynamic querier to make dynamic queries on a specific database connection
func (dq DynaQ) NewDqConn(con *sql.Conn) DynaQ {
conService := conServ.NewConnectionService()
dq.conn = con
dq.conService = conService
return dq
}
// DatabaseQuery takes the query the user wishes to execute, along with any arguments required as arguments and executes the query against the database instance. It then returns a
// models.ResultRows holding all the rows of the result set returned by the query executed
func (dq DynaQ) DatabaseQuery(query string, args ...interface{}) (models.ResultRows, error) {
var dud []models.Row
rows := models.ResultRows{
CurrentRow: 1,
Results: dud,
}
queryArgs := internal.QueryArgs{Args: args}
r, err := dq.dbService.Query(dq.db, query, dq.timeFormat, queryArgs)
if err != nil {
return rows, err
}
rows.Results = r
return rows, nil
}
// DatabaseQueryContext takes the specific context, query the user wishes to execute, and any arguments required as arguments and executes the query against a database instance. It then returns a
// models.ResultRows holding all the rows of the result set returned by the query executed
func (dq DynaQ) DatabaseQueryContext(ctx context.Context, query string, args ...interface{}) (models.ResultRows, error) {
var dud []models.Row
rows := models.ResultRows{
CurrentRow: 1,
Results: dud,
}
queryArgs := internal.QueryArgs{Args: args}
r, err := dq.dbService.QueryWithContext(dq.db, ctx, query, dq.timeFormat, queryArgs)
if err != nil {
return rows, err
}
rows.Results = r
return rows, nil
}
// ConnectionQuery query the user wishes to execute and any arguments required as arguments and executes the query against the specific database connection.
// It then returns a models.ResultRows holding all the rows of the result set returned by the query executed. ConnectionQuery uses context.Background() by default.
// To use a specific context, use DynaQ.ConnectionQueryContext(ctx context.Context, query string, args ...interface{})
func (dq DynaQ) ConnectionQuery(query string, args ...interface{}) (models.ResultRows, error) {
if dq.connAutoClose {
defer dq.conn.Close()
}
var dud []models.Row
rows := models.ResultRows{
CurrentRow: 1,
Results: dud,
}
queryArgs := internal.QueryArgs{Args: args}
r, err := dq.conService.QueryWithDefaultContext(dq.conn, query, dq.timeFormat, queryArgs)
if err != nil {
return rows, err
}
rows.Results = r
return rows, nil
}
// ConnectionQueryContext takes the specific context, query the user wishes to execute, and any arguments required as arguments and executes the query against the specific database connection.
// It then returns a models.ResultRows holding all the rows of the result set returned by the query executed
func (dq DynaQ) ConnectionQueryContext(ctx context.Context, query string, args ...interface{}) (models.ResultRows, error) {
if dq.connAutoClose {
defer dq.conn.Close()
}
var dud []models.Row
rows := models.ResultRows{
CurrentRow: 1,
Results: dud,
}
queryArgs := internal.QueryArgs{Args: args}
r, err := dq.conService.QueryWithContext(dq.conn, ctx, query, dq.timeFormat, queryArgs)
if err != nil {
return rows, err
}
rows.Results = r
return rows, nil
}