diff --git a/sqlite/database.go b/sqlite/database.go index 008f1df69c45a126f5ad9376c00f0a7a32c058ee..d05bcb0da194c17ce4e48fd9f9303cefc507a016 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 8c7feba1ccb7ac034376d4d668ea48cae8810037..f314942c90929d06e8f35094c1808f21af3cbc95 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