diff --git a/db.go b/db.go index e35228c2a5d4f742bdf3744b158ea8881fc4e9d9..efca4a99dce6c33e4bbdd050e5ded9b384c617f6 100644 --- a/db.go +++ b/db.go @@ -224,6 +224,10 @@ var ( ErrUnsupportedDestination = errors.New(`Unsupported destination type.`) ) +var ( + EnvEnableDebug = `UPPERIO_DB_DEBUG` +) + // Registered wrappers. var wrappers = make(map[string]Database) diff --git a/mongo/collection.go b/mongo/collection.go index 4989457f22494f98e0e2ef5714e732ad762fc2a6..f5acf5699d14a19cedb768879a08c64ab322e051 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -59,6 +59,10 @@ func (self *Collection) Find(terms ...interface{}) db.Result { queryChunks.Conditions = self.compileQuery(terms...) + if debugEnabled() == true { + debugLogQuery(queryChunks) + } + // Actually executing query. result := &Result{ self, diff --git a/mongo/database.go b/mongo/database.go index 7e720e80dae2cdecf29e69d808b9ac78fa95e62c..647edde4575834bb099fde731ef8b50dc53dadf2 100644 --- a/mongo/database.go +++ b/mongo/database.go @@ -26,21 +26,16 @@ package mongo import ( "fmt" "labix.org/v2/mgo" + "log" "net/url" + "os" "strings" "time" "upper.io/db" ) -var Debug = false - const driverName = `mongo` -// Registers this driver. -func init() { - db.Register(driverName, &Source{}) -} - type Source struct { name string config db.Settings @@ -48,6 +43,21 @@ type Source struct { database *mgo.Database } +func debugEnabled() bool { + if os.Getenv(db.EnvEnableDebug) != "" { + return true + } + return false +} + +func init() { + db.Register(driverName, &Source{}) +} + +func debugLogQuery(c *chunks) { + log.Printf("Fields: %v\nLimit: %v\nOffset: %v\nSort: %v\nConditions: %v\n", c.Fields, c.Limit, c.Offset, c.Sort, c.Conditions) +} + // Returns the string name of the database. func (self *Source) Name() string { return self.name diff --git a/mongo/database_test.go b/mongo/database_test.go index 12bc45350f9f420aa0dba5cd93139f660eea45f8..4180bcbffc40d7f236722b628b59db44c24ef0d7 100644 --- a/mongo/database_test.go +++ b/mongo/database_test.go @@ -27,6 +27,7 @@ import ( "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "menteslibres.net/gosexy/to" + "os" "reflect" "strings" "testing" @@ -86,7 +87,7 @@ var testValues = testValuesStruct{ // Enabling outputting some information to stdout, useful for development. func TestEnableDebug(t *testing.T) { - Debug = true + os.Setenv(db.EnvEnableDebug, "TRUE") } // Trying to open an empty datasource, it must succeed (mongo). @@ -671,7 +672,7 @@ func TestDataTypes(t *testing.T) { // We are going to benchmark the engine, so this is no longed needed. func TestDisableDebug(t *testing.T) { - Debug = false + os.Setenv(db.EnvEnableDebug, "") } // Benchmarking raw mgo queries. diff --git a/mysql/database.go b/mysql/database.go index 0ae9125529d7a6fbe86bed4f809574e86387dd68..65a0ada66a0d6e5047382b51b66e1d12c2447902 100644 --- a/mysql/database.go +++ b/mysql/database.go @@ -27,14 +27,14 @@ import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" + "log" + "os" "reflect" "regexp" "strings" "upper.io/db" ) -var Debug = false - // Format for saving dates. const DateFormat = "2006-01-02 15:04:05.000" @@ -45,10 +45,6 @@ var columnPattern = regexp.MustCompile(`^([a-z]+)\(?([0-9,]+)?\)?\s?([a-z]*)?`) const driverName = `mysql` -func init() { - db.Register(driverName, &Source{}) -} - type sqlValues_t []interface{} type Source struct { @@ -62,6 +58,21 @@ type sqlQuery struct { Args []interface{} } +func debugEnabled() bool { + if os.Getenv(db.EnvEnableDebug) != "" { + return true + } + return false +} + +func init() { + db.Register(driverName, &Source{}) +} + +func debugLogQuery(s string, q *sqlQuery) { + log.Printf("SQL: %s\nARGS: %v\n", strings.TrimSpace(s), q.Args) +} + func sqlCompile(terms []interface{}) *sqlQuery { q := &sqlQuery{} @@ -117,9 +128,8 @@ func (self *Source) doExec(terms ...interface{}) (sql.Result, error) { query := strings.Join(chunks.Query, ` `) - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Exec(query, chunks.Args...) @@ -134,9 +144,8 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) { query := strings.Join(chunks.Query, " ") - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Query(query, chunks.Args...) diff --git a/mysql/database_test.go b/mysql/database_test.go index 7362110870e515ca8f42d1137ae376b5ad07edd0..31aa01f85f137788f6420c910a6677a3de119252 100644 --- a/mysql/database_test.go +++ b/mysql/database_test.go @@ -1,5 +1,5 @@ /* - Copyright (c) 2012-2013 JosĂŠ Carlos Nieto, https://menteslibres.net/xiam + Copyright (c) 2012-2014 JosĂŠ Carlos Nieto, https://menteslibres.net/xiam Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -36,6 +36,7 @@ package mysql import ( "database/sql" "menteslibres.net/gosexy/to" + "os" "reflect" "strings" "testing" @@ -100,7 +101,7 @@ var testValues = testValuesStruct{ // Enabling outputting some information to stdout (like the SQL query and its // arguments), useful for development. func TestEnableDebug(t *testing.T) { - Debug = true + os.Setenv(db.EnvEnableDebug, "TRUE") } // Trying to open an empty datasource, it must fail. @@ -640,7 +641,7 @@ func TestDataTypes(t *testing.T) { // We are going to benchmark the engine, so this is no longed needed. func TestDisableDebug(t *testing.T) { - Debug = false + os.Setenv(db.EnvEnableDebug, "") } // Benchmarking raw database/sql. diff --git a/postgresql/database.go b/postgresql/database.go index db80d6f5727ed079559ccf92fc881222d0d83ead..b86a8c4884f7028625791d1d0d084c26a52ca33a 100644 --- a/postgresql/database.go +++ b/postgresql/database.go @@ -27,14 +27,14 @@ import ( "database/sql" "fmt" _ "github.com/xiam/gopostgresql" + "log" + "os" "reflect" "regexp" "strings" "upper.io/db" ) -var Debug = false - // Format for saving dates. var DateFormat = "2006-01-02 15:04:05" @@ -47,10 +47,6 @@ var columnPattern = regexp.MustCompile(`^([a-z]+)\(?([0-9,]+)?\)?\s?([a-z]*)?`) const driverName = `postgresql` -func init() { - db.Register(driverName, &Source{}) -} - type sqlValues_t []interface{} type Source struct { @@ -65,6 +61,21 @@ type sqlQuery struct { Args []interface{} } +func debugEnabled() bool { + if os.Getenv(db.EnvEnableDebug) != "" { + return true + } + return false +} + +func init() { + db.Register(driverName, &Source{}) +} + +func debugLogQuery(s string, q *sqlQuery) { + log.Printf("SQL: %s\nARGS: %v\n", strings.TrimSpace(s), q.Args) +} + func sqlCompile(terms []interface{}) *sqlQuery { q := &sqlQuery{} @@ -124,9 +135,8 @@ func (self *Source) doExec(terms ...interface{}) (sql.Result, error) { query = strings.Replace(query, `?`, fmt.Sprintf(`$%d`, i+1), 1) } - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Exec(query, chunks.Args...) @@ -145,9 +155,8 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) { query = strings.Replace(query, `?`, fmt.Sprintf(`$%d`, i+1), 1) } - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Query(query, chunks.Args...) @@ -166,9 +175,8 @@ func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) { query = strings.Replace(query, `?`, fmt.Sprintf(`$%d`, i+1), 1) } - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.QueryRow(query, chunks.Args...), nil diff --git a/postgresql/database_test.go b/postgresql/database_test.go index 80231380466522759a92f170a819f510205bce83..aa359d1605c7d9d020ede4783fe29b9bbe9ce706 100644 --- a/postgresql/database_test.go +++ b/postgresql/database_test.go @@ -36,6 +36,7 @@ package postgresql import ( "database/sql" "menteslibres.net/gosexy/to" + "os" "reflect" "strings" "testing" @@ -100,7 +101,7 @@ var testValues = testValuesStruct{ // Enabling outputting some information to stdout (like the SQL query and its // arguments), useful for development. func TestEnableDebug(t *testing.T) { - Debug = true + os.Setenv(db.EnvEnableDebug, "TRUE") } // Trying to open an empty datasource, it must fail. @@ -634,7 +635,7 @@ func TestDataTypes(t *testing.T) { // We are going to benchmark the engine, so this is no longed needed. func TestDisableDebug(t *testing.T) { - Debug = false + os.Setenv(db.EnvEnableDebug, "") } // Benchmarking raw database/sql. diff --git a/ql/database.go b/ql/database.go index 3a22d55deb22b9e63cfa69562248d65ef81dfade..9d4db014538bd67c174e4bc21667e5c82aade866 100644 --- a/ql/database.go +++ b/ql/database.go @@ -27,6 +27,8 @@ import ( "database/sql" "fmt" _ "github.com/cznic/ql/driver" + "log" + "os" "reflect" "strings" "time" @@ -45,10 +47,6 @@ var timeType = reflect.TypeOf(time.Time{}).Kind() const driverName = `ql` -func init() { - db.Register(driverName, &Source{}) -} - type sqlValues_t []interface{} type Source struct { @@ -63,6 +61,21 @@ type sqlQuery struct { Args []interface{} } +func debugEnabled() bool { + if os.Getenv(db.EnvEnableDebug) != "" { + return true + } + return false +} + +func init() { + db.Register(driverName, &Source{}) +} + +func debugLogQuery(s string, q *sqlQuery) { + log.Printf("SQL: %s\nARGS: %v\n", strings.TrimSpace(s), q.Args) +} + func sqlCompile(terms []interface{}) *sqlQuery { q := &sqlQuery{} @@ -121,9 +134,8 @@ func (self *Source) doExec(terms ...interface{}) (res sql.Result, err error) { query = strings.Replace(query, `?`, fmt.Sprintf(`$%d`, i+1), 1) } - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } if tx, err = self.session.Begin(); err != nil { @@ -154,9 +166,8 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) { query = strings.Replace(query, `?`, fmt.Sprintf(`$%d`, i+1), 1) } - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Query(query, chunks.Args...) diff --git a/ql/database_test.go b/ql/database_test.go index 844620b1d769f0e3e8ef0567126b8f7bd09f338f..668a059a6a3ec0e7388a79e863f5cb67631b04b8 100644 --- a/ql/database_test.go +++ b/ql/database_test.go @@ -36,7 +36,7 @@ package ql import ( "database/sql" "menteslibres.net/gosexy/to" - //"reflect" + "os" "strings" "testing" "time" @@ -92,7 +92,7 @@ var testValues = testValuesStruct{ // Enabling outputting some information to stdout (like the SQL query and its // arguments), useful for development. func TestEnableDebug(t *testing.T) { - Debug = true + os.Setenv(db.EnvEnableDebug, "TRUE") } // Trying to open an empty datasource, it must fail. @@ -634,7 +634,7 @@ func TestDataTypes(t *testing.T) { // We are going to benchmark the engine, so this is no longed needed. func TestDisableDebug(t *testing.T) { - Debug = false + os.Setenv(db.EnvEnableDebug, "") } // Benchmarking raw database/sql. diff --git a/sqlite/database.go b/sqlite/database.go index ccaf824dd2cb4ec3e814a645115bea636e5541a9..b5026b04871e9ddbc6cd0b91cda7ba135eb1c8c3 100644 --- a/sqlite/database.go +++ b/sqlite/database.go @@ -30,14 +30,14 @@ import ( // See: https://github.com/mattn/go-sqlite3/issues/40 //_ "github.com/xiam/gosqlite3" _ "github.com/mattn/go-sqlite3" + "log" + "os" "reflect" "regexp" "strings" "upper.io/db" ) -var Debug = false - // Format for saving dates. var DateFormat = `2006-01-02 15:04:05` @@ -48,10 +48,6 @@ var columnPattern = regexp.MustCompile(`^([a-zA-Z]+)\(?([0-9,]+)?\)?\s?([a-zA-Z] const driverName = `sqlite` -func init() { - db.Register(driverName, &Source{}) -} - type sqlValues_t []interface{} type Source struct { @@ -66,6 +62,21 @@ type sqlQuery struct { Args []interface{} } +func debugEnabled() bool { + if os.Getenv(db.EnvEnableDebug) != "" { + return true + } + return false +} + +func init() { + db.Register(driverName, &Source{}) +} + +func debugLogQuery(s string, q *sqlQuery) { + log.Printf("SQL: %s\nARGS: %v\n", strings.TrimSpace(s), q.Args) +} + func sqlCompile(terms []interface{}) *sqlQuery { q := &sqlQuery{} @@ -121,9 +132,8 @@ func (self *Source) doExec(terms ...interface{}) (sql.Result, error) { query := strings.Join(chunks.Query, ` `) - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Exec(query, chunks.Args...) @@ -139,9 +149,8 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) { query := strings.Join(chunks.Query, ` `) - if Debug == true { - fmt.Printf("Q: %s\n", query) - fmt.Printf("A: %v\n", chunks.Args) + if debugEnabled() == true { + debugLogQuery(query, chunks) } return self.session.Query(query, chunks.Args...) diff --git a/sqlite/database_test.go b/sqlite/database_test.go index 611a12e0cf484f25ff6c4627b1eb40fd959aea51..6baaaafbcef4a79791e111e3d4131970c5b87388 100644 --- a/sqlite/database_test.go +++ b/sqlite/database_test.go @@ -36,6 +36,7 @@ package sqlite import ( "database/sql" "menteslibres.net/gosexy/to" + "os" "reflect" "strings" "testing" @@ -92,7 +93,7 @@ var testValues = testValuesStruct{ // Enabling outputting some information to stdout (like the SQL query and its // arguments), useful for development. func TestEnableDebug(t *testing.T) { - Debug = true + os.Setenv(db.EnvEnableDebug, "TRUE") } // Trying to open an empty datasource, it must fail. @@ -632,7 +633,7 @@ func TestDataTypes(t *testing.T) { // We are going to benchmark the engine, so this is no longed needed. func TestDisableDebug(t *testing.T) { - Debug = false + os.Setenv(db.EnvEnableDebug, "") } // Benchmarking raw database/sql.