Skip to content

Commit

Permalink
Add method for re-using connection (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
galeone authored Jun 25, 2023
1 parent 97d95e9 commit 11a7a65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/galeone/fitbit v1.0.3
github.com/galeone/igor v1.0.8
github.com/galeone/igor v1.0.9
github.com/joho/godotenv v1.5.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/galeone/fitbit v1.0.3 h1:iYC3japYbCsWc6SyMz3JXTwKnCnG3y8oLgZ9uy4yQKM=
github.com/galeone/fitbit v1.0.3/go.mod h1:osb+HFcVMMkAP/NH2+HVB2UAYP7A1kOpKYmkfbtL2fY=
github.com/galeone/igor v1.0.8 h1:necystLPnQ1slRNfsp5n9Z1dtgee5PJCndDHem5CNpU=
github.com/galeone/igor v1.0.8/go.mod h1:YhORY+h+kG1n6RyuvoEh82h8Dkc/KXHZzaAkVPWtcSc=
github.com/galeone/igor v1.0.9 h1:nGS0WhMkzlwC0hHOUiRNJEFCHOInm+fTSiopy5FzyKc=
github.com/galeone/igor v1.0.9/go.mod h1:YhORY+h+kG1n6RyuvoEh82h8Dkc/KXHZzaAkVPWtcSc=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
20 changes: 19 additions & 1 deletion pgdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package fitbit_pgdb
// [2]: https://github.com/galeone/fitbit

import (
"database/sql"

"github.com/galeone/fitbit/types"
"github.com/galeone/igor"

Expand All @@ -26,7 +28,7 @@ type PGDB struct {
// You must provide the connection string to use for connecting to a
// running PostgreSQL instance.
//
// It implements the `fitbit.Storage` interface.
// *PGDB implements the `fitbit.Storage` interface.
func NewPGDB(connectionString string) *PGDB {
var err error
var db *igor.Database
Expand All @@ -39,6 +41,22 @@ func NewPGDB(connectionString string) *PGDB {
}
}

// NewPGDBFromConnection creates a new instance of PGDB without creating
// a new connection. It re-uses the database connection provided as input.
//
// *PGDB implements the `fitbit.Storage` interface.
func NewPGDBFromConnection(connection *sql.DB) *PGDB {
var err error
var db *igor.Database
if db, err = igor.Wrap(connection); err != nil {
panic(err.Error())
}

return &PGDB{
db,
}
}

// InsertAuhorizingUser executes a CREATE query on the PostgreSQL database
// inserting the authorizing user in the associated table.
func (s *PGDB) InsertAuhorizingUser(authorizing *types.AuthorizingUser) error {
Expand Down
16 changes: 16 additions & 0 deletions pgdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package fitbit_pgdb_test

import (
"database/sql"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -67,6 +68,21 @@ func init() {

var db fitbit.Storage

func TestPGDBNew(t *testing.T) {
// Test connection re-use
if connection, err := sql.Open("postgres", _connectionString); err != nil {
if db = pgdb.NewPGDBFromConnection(connection); db == nil {
t.Errorf("Unable to re-use db connection when creating PGDB object")
}
}

// Test connection creation
if db = pgdb.NewPGDB(_connectionString); db == nil {
t.Errorf("Unable to create a PGDB object creating a connection")
}

}

func TestAuthorizingUser(t *testing.T) {
db = pgdb.NewPGDB(_connectionString)
var err error
Expand Down

0 comments on commit 11a7a65

Please sign in to comment.