gosqldb

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2023 License: MIT Imports: 5 Imported by: 0

README

Go SQL Database Module

This Go lang module simplifies running queries to SQL-based databases. It provides an interface and implementations for managing database connections and executing queries.

Installation

To install this module using go get, you can run the following command:

go get -u github.com/hk1997/gosqldb

Or, you can import it in your Go project:

import (
    "github.com/hk1997/gosqldb"
)
Interface

The module provides the following interface:

import "database/sql"

type SqlDatabase interface {
    // DbInit performs initialization operation for the database.
    DbInit(driver, connectorString string, maxRetries int)

    // DbClose performs cleanup operation for the database.
    DbClose()

    // RunQuery runs the query on the database.
    RunQuery(query string, args ...interface{}) (*sql.Rows, error)
}

Usage

The module includes two implementations of the SqlDatabase interface: PostgresImpl and MockSqlDatabase.

PostgresSQL Database

To initialize a PostgreSQL database, you can use the PostgresImpl implementation:

Create a global database accessor to be used across your module. This will help to share the same database object across multiple packages.

package appModule

import (
	db "github.com/hk1997/gosqldb"
)

var AppDb db.SqlDatabase

func InitDatabase(database db.SqlDatabase) {
	AppDb = database
}

func InitPostgresDatabase(driver, connectorString string, maxRetries int) {
	var boozingoDatabase db.SqlDatabase
	AppDb = &db.PostgresImpl{}
	AppDb.DbInit(driver, connectorString, maxRetries)
	InitDatabase(AppDb)
}

In your main.go, initialize the postgres database:

package main

import (
	"api/appModule"
	"api/server"
	"github.com/gorilla/mux"
	"log"
	"net/http"
)

func main() {
	r := mux.NewRouter()

	appModule.InitPostgresDatabase(appModule.DB_DRIVER, appModule.DB_CONNECTOR_STRING, appModule.DB_RETRIES)

	server.RegisterRoutes(r)

	srv := &http.Server{
		Addr:    ":8080",
		Handler: r,
	}

	log.Println("Server started on port 8080")
	log.Fatal(srv.ListenAndServe())
}
Running queries

To run the query simply import the appModule and call database accessor:

func SendOtp(extension, mobile string) (bool, error) {
	otp := generateOTP()
	retries := 3

	rows, err := appModule.AppDb.RunQuery(INSERT_OTP_QUERY, extension, mobile, otp, retries)
	if err != nil {
		fmt.Println("Error running query", err)
		return false, err
	}
	defer rows.Close()

	if rows.Next() {
		var success bool
		err := rows.Scan(&success)
		if err != nil {
			return false, err
		}
		return success, nil
	}

	return false, nil
}
Testing
  • Initialize mock database using sql mocking library before each test
import (
	"api/appModule"
	"database/sql"
	"github.com/DATA-DOG/go-sqlmock"
	database "github.com/hk1997/gosqldb"
	"os"
	"testing"
)

var db *sql.DB
var mock sqlmock.Sqlmock

func TestMain(m *testing.M) {
	db, mock, _ = sqlmock.New()
	defer db.Close()
	var mockDb database.SqlDatabase = &database.MockSqlDatabase{Db: db}
	appModule.InitDatabase(mockDb)

	exitCode := m.Run()

	os.Exit(exitCode)
}
  • Write the test to mock the result of the query
func TestSendOtp(t *testing.T) {

	// Set up the expected query and result for SendOtp
	mockRows := sqlmock.NewRows([]string{"retriesZero"}).AddRow(true)
	mock.ExpectQuery(regexp.QuoteMeta(INSERT_OTP_QUERY)).WithArgs("test_extension", "test_mobile", sqlmock.AnyArg(), 3).
		WillReturnRows(mockRows)

	// Call the SendOtp function with test data
	retriesZero, err := SendOtp("test_extension", "test_mobile")
	if err != nil {
		t.Errorf("Expected no error, got: %v", err)
	}
	if !retriesZero {
		t.Error("Expected retriesZero to be true")
	}

	// Check that all expectations were met
	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("Unfulfilled expectations: %v", err)
	}
}
License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockSqlDatabase

type MockSqlDatabase struct {
	Db *sql.DB
}

func (*MockSqlDatabase) BeginTransaction added in v1.0.3

func (mdb *MockSqlDatabase) BeginTransaction() (*sql.Tx, error)

func (*MockSqlDatabase) DbClose

func (mdb *MockSqlDatabase) DbClose()

func (*MockSqlDatabase) DbInit

func (mdb *MockSqlDatabase) DbInit(_, _ string, _ int)

func (*MockSqlDatabase) RunQuery

func (mdb *MockSqlDatabase) RunQuery(query string, args ...interface{}) (*sql.Rows, error)

func (*MockSqlDatabase) RunTransaction added in v1.0.2

func (mdb *MockSqlDatabase) RunTransaction(queries []Query) error

type PostgresImpl

type PostgresImpl struct {
	// contains filtered or unexported fields
}

func (*PostgresImpl) BeginTransaction added in v1.0.3

func (postgres *PostgresImpl) BeginTransaction() (*sql.Tx, error)

func (*PostgresImpl) DbClose

func (postgres *PostgresImpl) DbClose()

func (*PostgresImpl) DbInit

func (postgres *PostgresImpl) DbInit(driver string, connectorString string, maxRetries int)

func (*PostgresImpl) RunQuery

func (postgres *PostgresImpl) RunQuery(query string, args ...interface{}) (*sql.Rows, error)

func (*PostgresImpl) RunTransaction added in v1.0.2

func (postgres *PostgresImpl) RunTransaction(queries []Query) error

type Query added in v1.0.2

type Query struct {
	QueryString string
	Args        []interface{}
}

type SqlDatabase

type SqlDatabase interface {
	// DbInit Performs initialization operation for the database.
	DbInit(driver, connectorString string, maxRetries int)

	// DbClose Performs cleanup operation for the database.
	DbClose()

	// RunQuery Runs thr QueryString to the database
	RunQuery(query string, args ...interface{}) (*sql.Rows, error)

	// RunTransaction Runs all the queries as a transaction
	RunTransaction(queries []Query) error

	BeginTransaction() (*sql.Tx, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL