Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Jul 8, 2024
1 parent 0fb6202 commit f6dd8ab
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 143 deletions.
148 changes: 148 additions & 0 deletions database/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package database

import (
"context"
"database/sql/driver"
"sync"

"github.com/mattn/go-sqlite3"
)

type Driver struct {
mutex sync.Mutex
wrapped driver.Driver
dsn string
}

func (d *Driver) Open(dsn string) (conn driver.Conn, err error) {
d.wrapped = &sqlite3.SQLiteDriver{}
conn, err = d.wrapped.Open(dsn)
if err != nil {
return
}
conn = &Conn{
mutex: &d.mutex,
wrapped: conn,
}
return
}

func (d *Driver) OpenConnector(dsn string) (dc driver.Connector, err error) {
d.dsn = dsn
dc = d
return
}

func (d *Driver) Connect(context.Context) (conn driver.Conn, err error) {
conn, err = d.Open(d.dsn)
return
}

func (d *Driver) Driver() driver.Driver {
return d
}

type Conn struct {
mutex *sync.Mutex
wrapped driver.Conn
}

func (c *Conn) Ping(ctx context.Context) (err error) {
if p, cast := c.wrapped.(driver.Pinger); cast {
err = p.Ping(ctx)
}
return
}

func (c *Conn) ResetSession(ctx context.Context) (err error) {
if p, cast := c.wrapped.(driver.SessionResetter); cast {
err = p.ResetSession(ctx)
}
return
}
func (c *Conn) IsValid() (b bool) {
if p, cast := c.wrapped.(driver.Validator); cast {
b = p.IsValid()
}
return
}

func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Rows, err error) {
if p, cast := c.wrapped.(driver.QueryerContext); cast {
r, err = p.QueryContext(ctx, query, args)
}
return
}

func (c *Conn) PrepareContext(ctx context.Context, query string) (s driver.Stmt, err error) {
if p, cast := c.wrapped.(driver.ConnPrepareContext); cast {
s, err = p.PrepareContext(ctx, query)
}
return
}

func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
if p, cast := c.wrapped.(driver.ExecerContext); cast {
r, err = p.ExecContext(ctx, query, args)
}
return
}

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
if p, cast := c.wrapped.(driver.ConnBeginTx); cast {
tx, err = p.BeginTx(ctx, opts)
} else {
tx, err = c.wrapped.Begin()
}
if err != nil {
return
}
tx = &Tx{
mutex: c.mutex,
wrapped: tx,
}
c.mutex.Lock()
return
}

func (c *Conn) Prepare(query string) (s driver.Stmt, err error) {
s, err = c.wrapped.Prepare(query)
return
}

func (c *Conn) Close() (err error) {
err = c.wrapped.Close()
return
}

func (c *Conn) Begin() (tx driver.Tx, err error) {
tx, err = c.wrapped.Begin()
if err != nil {
return
}
tx = &Tx{
mutex: c.mutex,
wrapped: tx,
}
return
}

type Tx struct {
mutex *sync.Mutex
wrapped driver.Tx
}

func (tx *Tx) Commit() (err error) {
defer func() {
tx.mutex.Unlock()
}()
err = tx.wrapped.Commit()
return
}
func (tx *Tx) Rollback() (err error) {
defer func() {
tx.mutex.Unlock()
}()
err = tx.wrapped.Rollback()
return
}
143 changes: 0 additions & 143 deletions database/pkg.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package database

import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"sync"

liberr "github.com/jortel/go-utils/error"
"github.com/jortel/go-utils/logr"
"github.com/konveyor/tackle2-hub/model"
"github.com/konveyor/tackle2-hub/settings"
"github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
Expand Down Expand Up @@ -78,142 +74,3 @@ func Close(db *gorm.DB) (err error) {
}
return
}

type Driver struct {
mutex sync.Mutex
wrapped driver.Driver
dsn string
}

func (d *Driver) Open(dsn string) (conn driver.Conn, err error) {
d.wrapped = &sqlite3.SQLiteDriver{}
conn, err = d.wrapped.Open(dsn)
if err != nil {
return
}
conn = &Conn{
mutex: &d.mutex,
wrapped: conn,
}
return
}

func (d *Driver) OpenConnector(dsn string) (dc driver.Connector, err error) {
d.dsn = dsn
dc = d
return
}

func (d *Driver) Connect(context.Context) (conn driver.Conn, err error) {
conn, err = d.Open(d.dsn)
return
}

func (d *Driver) Driver() driver.Driver {
return d
}

type Conn struct {
mutex *sync.Mutex
wrapped driver.Conn
}

func (c *Conn) Ping(ctx context.Context) (err error) {
if p, cast := c.wrapped.(driver.Pinger); cast {
err = p.Ping(ctx)
}
return
}

func (c *Conn) ResetSession(ctx context.Context) (err error) {
if p, cast := c.wrapped.(driver.SessionResetter); cast {
err = p.ResetSession(ctx)
}
return
}
func (c *Conn) IsValid() (b bool) {
if p, cast := c.wrapped.(driver.Validator); cast {
b = p.IsValid()
}
return
}

func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Rows, err error) {
if p, cast := c.wrapped.(driver.QueryerContext); cast {
r, err = p.QueryContext(ctx, query, args)
}
return
}

func (c *Conn) PrepareContext(ctx context.Context, query string) (s driver.Stmt, err error) {
if p, cast := c.wrapped.(driver.ConnPrepareContext); cast {
s, err = p.PrepareContext(ctx, query)
}
return
}

func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
if p, cast := c.wrapped.(driver.ExecerContext); cast {
r, err = p.ExecContext(ctx, query, args)
}
return
}

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
if p, cast := c.wrapped.(driver.ConnBeginTx); cast {
tx, err = p.BeginTx(ctx, opts)
} else {
tx, err = c.wrapped.Begin()
}
if err != nil {
return
}
tx = &Tx{
mutex: c.mutex,
wrapped: tx,
}
c.mutex.Lock()
return
}

func (c *Conn) Prepare(query string) (s driver.Stmt, err error) {
s, err = c.wrapped.Prepare(query)
return
}

func (c *Conn) Close() (err error) {
err = c.wrapped.Close()
return
}

func (c *Conn) Begin() (tx driver.Tx, err error) {
tx, err = c.wrapped.Begin()
if err != nil {
return
}
tx = &Tx{
mutex: c.mutex,
wrapped: tx,
}
return
}

type Tx struct {
mutex *sync.Mutex
wrapped driver.Tx
}

func (tx *Tx) Commit() (err error) {
defer func() {
tx.mutex.Unlock()
}()
err = tx.wrapped.Commit()
return
}
func (tx *Tx) Rollback() (err error) {
defer func() {
tx.mutex.Unlock()
}()
err = tx.wrapped.Rollback()
return
}

0 comments on commit f6dd8ab

Please sign in to comment.