Changed to public SqlCommon interface

This commit is contained in:
Alvaro Viebrantz 2016-01-17 16:09:41 -03:00
parent cd40e372d3
commit 6248976861
3 changed files with 13 additions and 11 deletions

View File

@ -4,7 +4,7 @@ import (
"database/sql"
)
type sqlCommon interface {
type SqlCommon interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
@ -25,9 +25,11 @@ type Database interface {
DB() *sql.DB
New() Database
NewScope(value interface{}) *Scope
CommonDB() sqlCommon
Callback() *callback
SetLogger(l logger)
CommonDB() SqlCommon
//Callback() *callback
//SetLogger(l logger)
LogMode(enable bool) Database
SingularTable(enable bool)

10
main.go
View File

@ -24,7 +24,7 @@ type DB struct {
Error error
RowsAffected int64
callback *callback
db sqlCommon
db SqlCommon
parent *DB
search *search
logMode int
@ -44,7 +44,7 @@ func Open(dialect string, args ...interface{}) (DB, error) {
err = errors.New("invalid database source")
} else {
var source string
var dbSql sqlCommon
var dbSql SqlCommon
switch value := args[0].(type) {
case string:
@ -59,7 +59,7 @@ func Open(dialect string, args ...interface{}) (DB, error) {
driver = "postgres" // FoundationDB speaks a postgres-compatible protocol.
}
dbSql, err = sql.Open(driver, source)
case sqlCommon:
case SqlCommon:
source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String()
dbSql = value
}
@ -107,7 +107,7 @@ func (db *DB) NewScope(value interface{}) *Scope {
// CommonDB Return the underlying sql.DB or sql.Tx instance.
// Use of this method is discouraged. It's mainly intended to allow
// coexistence with legacy non-GORM code.
func (s *DB) CommonDB() sqlCommon {
func (s *DB) CommonDB() SqlCommon {
return s.db
}
@ -338,7 +338,7 @@ func (s *DB) Begin() Database {
c := s.clone()
if db, ok := c.db.(sqlDb); ok {
tx, err := db.Begin()
c.db = interface{}(tx).(sqlCommon)
c.db = interface{}(tx).(SqlCommon)
c.AddError(err)
} else {
c.AddError(CantStartTransaction)

View File

@ -66,7 +66,7 @@ func (scope *Scope) DB() *DB {
}
// SqlDB return *sql.DB
func (scope *Scope) SqlDB() sqlCommon {
func (scope *Scope) SqlDB() SqlCommon {
return scope.db.db
}
@ -366,7 +366,7 @@ func (scope *Scope) Trace(t time.Time) {
func (scope *Scope) Begin() *Scope {
if db, ok := scope.SqlDB().(sqlDb); ok {
if tx, err := db.Begin(); err == nil {
scope.db.db = interface{}(tx).(sqlCommon)
scope.db.db = interface{}(tx).(SqlCommon)
scope.InstanceSet("gorm:started_transaction", true)
}
}