From bd5bf8bcdb0f1880162f0d1570095c433f6f2f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <jose.carlos@menteslibres.net> Date: Wed, 7 Oct 2015 09:41:50 -0500 Subject: [PATCH] Removing custom tx (duplicated functionality). --- internal/sqladapter/database.go | 5 ++- internal/sqlutil/tx/tx.go | 6 ++++ postgresql/database.go | 3 +- postgresql/tx.go | 58 --------------------------------- 4 files changed, 12 insertions(+), 60 deletions(-) delete mode 100644 postgresql/tx.go diff --git a/internal/sqladapter/database.go b/internal/sqladapter/database.go index 830c5a75..4a9ec70d 100644 --- a/internal/sqladapter/database.go +++ b/internal/sqladapter/database.go @@ -34,9 +34,9 @@ type Database interface { type BaseDatabase struct { partial PartialDatabase sess *sqlx.DB + tx *sqltx.Tx connURL db.ConnectionURL - tx *sqltx.Tx schema *schema.DatabaseSchema cachedStatements *cache.Cache collections map[string]db.Collection @@ -250,6 +250,9 @@ func (d *BaseDatabase) Builder() db.QueryBuilder { // Driver returns the underlying *sqlx.DB instance. func (d *BaseDatabase) Driver() interface{} { + if d.tx != nil { + return d.tx.Tx + } return d.sess } diff --git a/internal/sqlutil/tx/tx.go b/internal/sqlutil/tx/tx.go index 18ebbf3c..ac45498a 100644 --- a/internal/sqlutil/tx/tx.go +++ b/internal/sqlutil/tx/tx.go @@ -23,8 +23,14 @@ package sqltx import ( "github.com/jmoiron/sqlx" + "upper.io/db" ) +type Database struct { + db.Database + *Tx +} + type Tx struct { *sqlx.Tx done bool diff --git a/postgresql/database.go b/postgresql/database.go index 6c112a03..2a195d70 100644 --- a/postgresql/database.go +++ b/postgresql/database.go @@ -30,6 +30,7 @@ import ( "upper.io/builder/sqlgen" "upper.io/db" "upper.io/db/internal/sqladapter" + "upper.io/db/internal/sqlutil/tx" ) type database struct { @@ -174,7 +175,7 @@ func (d *database) Transaction() (db.Tx, error) { clone.BindTx(sqlTx) - return &tx{Tx: clone.Tx(), database: clone}, nil + return &sqltx.Database{Database: clone, Tx: clone.Tx()}, nil } // PopulateSchema looks up for the table info in the database and populates its diff --git a/postgresql/tx.go b/postgresql/tx.go deleted file mode 100644 index 5a91a828..00000000 --- a/postgresql/tx.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -package postgresql - -import ( - "upper.io/db" - "upper.io/db/internal/sqlutil/tx" -) - -type tx struct { - *sqltx.Tx - *database -} - -var _ = db.Tx(&tx{}) - -// Driver returns the current transaction session. -func (t *tx) Driver() interface{} { - if t != nil && t.Tx != nil { - return t.Tx.Tx - } - return nil -} - -// Commit commits the current transaction. -func (t *tx) Commit() error { - if err := t.Tx.Commit(); err != nil { - return err - } - return nil -} - -// Rollback discards the current transaction. -func (t *tx) Rollback() error { - if err := t.Tx.Rollback(); err != nil { - return err - } - return nil -} -- GitLab