-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
28 lines (22 loc) · 804 Bytes
/
query.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
package dbal
import (
"database/sql"
"github.com/magicalbanana/dbal/sqltmpl"
)
// Query uses the given sqlFile and params to execute sql.Query. Before it
// gets executed the sqlFile is first retrieved from the virtual file system
// that was supplied and then passed to the sqltmpl to be parsed so that the
// named parameters are then replaced with positional parameters that are
// dependent on the driver that was used when initializing the DAL.
func (d *dbal) Query(sqlStmnt string, params QueryParams) (*sql.Rows, error) {
if params != nil {
tmpl := sqltmpl.NewParser(sqlStmnt)
tmpl.SetValuesFromMap(params)
stmt, err := d.db.Prepare(tmpl.GetParsedQuery())
if err != nil {
return nil, err
}
return stmt.Query(tmpl.GetParsedParameters()...)
}
return d.db.Query(sqlStmnt)
}