From 7d8e68fb4279a2cd77ebb0832652330ff130e307 Mon Sep 17 00:00:00 2001 From: "alejandro.labad" Date: Fri, 21 Aug 2020 10:55:31 +0200 Subject: [PATCH 1/3] Propose solution for parametrized queries --- conn.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/conn.go b/conn.go index 972858d..107745d 100644 --- a/conn.go +++ b/conn.go @@ -46,8 +46,24 @@ func (c *Conn) Close() error { return releaseHandle(h) } -//Query method executes the stament with out prepare +//Query method executes the statement directly if no params present, or as prepared statement func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) { + if len(args) == 0 { + // Going to original implementation + return c.noParamsQuery(query) + } else { + // This part is implemented as the original Query method did not use the provided args + stmt, err := c.Prepare(query) + if err != nil { + return nil, err + } + defer stmt.Close() + return stmt.Query(args) + } +} + +//noParamsQuery method executes the statement without prepare +func (c *Conn) noParamsQuery(query string) (driver.Rows, error) { var out api.SQLHANDLE var os *ODBCStmt ret := api.SQLAllocHandle(api.SQL_HANDLE_STMT, api.SQLHANDLE(c.h), &out) @@ -77,4 +93,4 @@ func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) { return nil, err } return &Rows{os: os}, nil -} \ No newline at end of file +} From f6c6a03cb4082481b6b2d6b775edd49ec6fdce77 Mon Sep 17 00:00:00 2001 From: "alejandro.labad" Date: Thu, 27 Aug 2020 12:40:55 +0200 Subject: [PATCH 2/3] Improved solution as it is done by sql.go --- conn.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/conn.go b/conn.go index 107745d..4d25cb4 100644 --- a/conn.go +++ b/conn.go @@ -48,22 +48,10 @@ func (c *Conn) Close() error { //Query method executes the statement directly if no params present, or as prepared statement func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) { - if len(args) == 0 { - // Going to original implementation - return c.noParamsQuery(query) - } else { - // This part is implemented as the original Query method did not use the provided args - stmt, err := c.Prepare(query) - if err != nil { - return nil, err - } - defer stmt.Close() - return stmt.Query(args) + if len(args) > 0 { + // Not implemented for queries with parameters + return nil, driver.ErrSkip } -} - -//noParamsQuery method executes the statement without prepare -func (c *Conn) noParamsQuery(query string) (driver.Rows, error) { var out api.SQLHANDLE var os *ODBCStmt ret := api.SQLAllocHandle(api.SQL_HANDLE_STMT, api.SQLHANDLE(c.h), &out) From b4eacd8e3e35e1ce8ec7e22e734ab8e451d56c86 Mon Sep 17 00:00:00 2001 From: "alejandro.labad" Date: Thu, 27 Aug 2020 12:44:55 +0200 Subject: [PATCH 3/3] Improve comment --- conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn.go b/conn.go index 4d25cb4..658411d 100644 --- a/conn.go +++ b/conn.go @@ -46,7 +46,7 @@ func (c *Conn) Close() error { return releaseHandle(h) } -//Query method executes the statement directly if no params present, or as prepared statement +//Query method executes the statement with out prepare if no args provided, and a driver.ErrSkip otherwise (handled by sql.go to execute usual preparedStmt) func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) { if len(args) > 0 { // Not implemented for queries with parameters