good morning!!!!

Skip to content
Snippets Groups Projects
Commit ddbf174f authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

PostgreSQL: An error was missing after trying to use tx.Collection() after the...

PostgreSQL: An error was missing after trying to use tx.Collection() after the transaction was completed.
parent b584fa6e
Branches
Tags
No related merge requests found
...@@ -56,7 +56,7 @@ var ( ...@@ -56,7 +56,7 @@ var (
type source struct { type source struct {
config db.Settings config db.Settings
session *sql.DB session *sql.DB
tx *sql.Tx tx *tx
schema *schema.DatabaseSchema schema *schema.DatabaseSchema
} }
...@@ -162,7 +162,7 @@ func (self *source) doExec(stmt sqlgen.Statement, args ...interface{}) (sql.Resu ...@@ -162,7 +162,7 @@ func (self *source) doExec(stmt sqlgen.Statement, args ...interface{}) (sql.Resu
} }
if self.tx != nil { if self.tx != nil {
res, err = self.tx.Exec(query, args...) res, err = self.tx.sqlTx.Exec(query, args...)
} else { } else {
res, err = self.session.Exec(query, args...) res, err = self.session.Exec(query, args...)
} }
...@@ -195,7 +195,7 @@ func (self *source) doQuery(stmt sqlgen.Statement, args ...interface{}) (*sql.Ro ...@@ -195,7 +195,7 @@ func (self *source) doQuery(stmt sqlgen.Statement, args ...interface{}) (*sql.Ro
} }
if self.tx != nil { if self.tx != nil {
rows, err = self.tx.Query(query, args...) rows, err = self.tx.sqlTx.Query(query, args...)
} else { } else {
rows, err = self.session.Query(query, args...) rows, err = self.session.Query(query, args...)
} }
...@@ -228,7 +228,7 @@ func (self *source) doQueryRow(stmt sqlgen.Statement, args ...interface{}) (*sql ...@@ -228,7 +228,7 @@ func (self *source) doQueryRow(stmt sqlgen.Statement, args ...interface{}) (*sql
} }
if self.tx != nil { if self.tx != nil {
row = self.tx.QueryRow(query, args...) row = self.tx.sqlTx.QueryRow(query, args...)
} else { } else {
row = self.session.QueryRow(query, args...) row = self.session.QueryRow(query, args...)
} }
...@@ -275,9 +275,9 @@ func (self *source) Transaction() (db.Tx, error) { ...@@ -275,9 +275,9 @@ func (self *source) Transaction() (db.Tx, error) {
return nil, err return nil, err
} }
tx := &tx{clone} tx := &tx{source: clone, sqlTx: sqlTx}
clone.tx = sqlTx clone.tx = tx
return tx, nil return tx, nil
} }
...@@ -433,6 +433,11 @@ func (self *source) tableExists(names ...string) error { ...@@ -433,6 +433,11 @@ func (self *source) tableExists(names ...string) error {
for i := range names { for i := range names {
if self.schema.HasTable(names[i]) {
// We already know this table exists.
continue
}
stmt = sqlgen.Statement{ stmt = sqlgen.Statement{
Type: sqlgen.SqlSelect, Type: sqlgen.SqlSelect,
Table: sqlgen.Table{`information_schema.tables`}, Table: sqlgen.Table{`information_schema.tables`},
...@@ -521,6 +526,12 @@ func (self *source) Collection(names ...string) (db.Collection, error) { ...@@ -521,6 +526,12 @@ func (self *source) Collection(names ...string) (db.Collection, error) {
return nil, db.ErrMissingCollectionName return nil, db.ErrMissingCollectionName
} }
if self.tx != nil {
if self.tx.done {
return nil, sql.ErrTxDone
}
}
col := &table{ col := &table{
source: self, source: self,
names: names, names: names,
......
...@@ -21,14 +21,24 @@ ...@@ -21,14 +21,24 @@
package postgresql package postgresql
import (
"database/sql"
)
type tx struct { type tx struct {
*source *source
sqlTx *sql.Tx
done bool
} }
func (self *tx) Commit() error { func (self *tx) Commit() (err error) {
return self.source.tx.Commit() err = self.sqlTx.Commit()
if err == nil {
self.done = true
}
return err
} }
func (self *tx) Rollback() error { func (self *tx) Rollback() error {
return self.source.tx.Rollback() return self.sqlTx.Rollback()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment