I'm trying to figure out what the pattern is for using named parameters in go's built-in database/sql package. I looked at the oracle driver, but it seems like just a wrapper for the C library. Have people solved this in an elegant way? So far I've just worked around the problem by putting {0}
, {1}
as the parameters in the unit tests, but it sure would be nice to be able to use them normally as a map[string]interface{}
or something. Does anyone have an idea or an implementation that seems idiomatic?
For reference, here is a test:
db := testConn()
stmt, err := db.Prepare("return {0} as int1, {1} as int2")
if err != nil {
t.Fatal(err)
}
rows, err := stmt.Query(123, 456)
if err != nil {
t.Fatal(err)
}
rows.Next()
var test int
var test2 int
err = rows.Scan(&test, &test2)
if err != nil {
t.Fatal(err)
}
if test != 123 {
t.Fatal("test != 123;", test)
}
if test2 != 456 {
t.Fatal("test2 != 456;", test2)
}
And what I'm doing in Query
is:
func (stmt *cypherStmt) Query(args []driver.Value) (driver.Rows, error) {
cyphReq := cypherRequest{
Query: stmt.query,
}
if len(args) > 0 {
cyphReq.Params = make(map[string]interface{})
}
for idx, e := range args {
cyphReq.Params[strconv.Itoa(idx)] = e
}
...
db.Prepare
method, but as far as I undestand, you are trying to create an SQL query object and add paramenters in OO manner, so you can pass values to be used inWHERE
clause (as an example). There is a stored procedure in MS SQL calledsp_executesql
that can take SQL statement with parameters' values marked by placeholders, and then populate defined parameters' values from variables. Oracle should have the same thing. If this is at all close to what your are looking for, I can have a look further into this. – Stoleg Jan 8 at 12:38