From c74ad21857c897f105740e94103036779a6c05f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <jose.carlos@menteslibres.net> Date: Tue, 17 Jun 2014 16:43:11 -0500 Subject: [PATCH] Adding support for debugLog(). --- sqlite/database.go | 83 +++++++++++++++++++++++++++++++--------------- sqlite/result.go | 10 ++++-- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/sqlite/database.go b/sqlite/database.go index 008f1df6..d05bcb0d 100644 --- a/sqlite/database.go +++ b/sqlite/database.go @@ -70,6 +70,13 @@ func debugEnabled() bool { return false } +func debugLog(query string, args []interface{}, err error) { + if debugEnabled() == true { + d := sqlutil.Debug{query, args, err} + d.Print() + } +} + func init() { template = &sqlgen.Template{ @@ -106,74 +113,96 @@ func init() { } func (self *Source) doExec(stmt sqlgen.Statement, args ...interface{}) (sql.Result, error) { + var query string + var res sql.Result + var err error + + defer func() { + debugLog(query, args, err) + }() if self.session == nil { return nil, db.ErrNotConnected } - query := stmt.Compile(template) - - if debugEnabled() == true { - sqlutil.DebugQuery(query, args) - } + query = stmt.Compile(template) if self.tx != nil { - return self.tx.Exec(query, args...) + res, err = self.tx.Exec(query, args...) + } else { + res, err = self.session.Exec(query, args...) } - return self.session.Exec(query, args...) + return res, err } func (self *Source) doQuery(stmt sqlgen.Statement, args ...interface{}) (*sql.Rows, error) { + var rows *sql.Rows + var query string + var err error + + defer func() { + debugLog(query, args, err) + }() + if self.session == nil { return nil, db.ErrNotConnected } - query := stmt.Compile(template) - - if debugEnabled() == true { - sqlutil.DebugQuery(query, args) - } + query = stmt.Compile(template) if self.tx != nil { - return self.tx.Query(query, args...) + rows, err = self.tx.Query(query, args...) + } else { + rows, err = self.session.Query(query, args...) } - return self.session.Query(query, args...) + return rows, err } func (self *Source) doQueryRow(stmt sqlgen.Statement, args ...interface{}) (*sql.Row, error) { + var query string + var row *sql.Row + var err error + + defer func() { + debugLog(query, args, err) + }() + if self.session == nil { return nil, db.ErrNotConnected } - query := stmt.Compile(template) - - if debugEnabled() == true { - sqlutil.DebugQuery(query, args) - } + query = stmt.Compile(template) if self.tx != nil { - return self.tx.QueryRow(query, args...), nil + row = self.tx.QueryRow(query, args...) + } else { + row = self.session.QueryRow(query, args...) } - return self.session.QueryRow(query, args...), nil + return row, err } func (self *Source) doRawQuery(query string, args ...interface{}) (*sql.Rows, error) { + var rows *sql.Rows + var err error + + defer func() { + debugLog(query, args, err) + }() + if self.session == nil { return nil, db.ErrNotConnected } - if debugEnabled() == true { - sqlutil.DebugQuery(query, args) - } - if self.tx != nil { - return self.tx.Query(query, args...) + rows, err = self.tx.Query(query, args...) + } else { + rows, err = self.session.Query(query, args...) } - return self.session.Query(query, args...) + return rows, err } // Returns the string name of the database. diff --git a/sqlite/result.go b/sqlite/result.go index 8c7feba1..f314942c 100644 --- a/sqlite/result.go +++ b/sqlite/result.go @@ -23,6 +23,7 @@ package sqlite import ( "database/sql" + "fmt" "strings" "upper.io/db" "upper.io/db/util/sqlgen" @@ -112,12 +113,17 @@ func (self *Result) Sort(fields ...string) db.Result { } // Retrieves only the given fields. -func (self *Result) Select(fields ...string) db.Result { +func (self *Result) Select(fields ...interface{}) db.Result { self.columns = make(sqlgen.Columns, 0, len(fields)) l := len(fields) for i := 0; i < l; i++ { - self.columns = append(self.columns, sqlgen.Column{fields[i]}) + switch value := fields[i].(type) { + case db.Raw: + self.columns = append(self.columns, sqlgen.Column{sqlgen.Raw{fmt.Sprintf(`%v`, value.Value)}}) + default: + self.columns = append(self.columns, sqlgen.Column{value}) + } } return self -- GitLab