sqlproxy

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 6 Imported by: 1

README

go-sqldb

Go Reference license Build Status Code Coverage Scrutinizer Code Quality

go-sqlDB it's a muti DB Engine wrapper for the GO database/sql package. It provides a set of standard error codes, providing abstraction from the implemented DB engine and allowing changing it (almost) just modifying configuration, without the need to modify source code. Besides that, provides a very limited cross-engine sql translator.

General

The sqlClient contract resides on the go-commons repository: github.com/cdleo/go-commons/sqlcommons/sqlClient.go:

type SQLClient interface {
	Open() error
	Close()
	IsOpen() error

	Begin() (SQLTx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (SQLTx, error)

	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	Prepare(query string) (SQLStmt, error)
	PrepareContext(ctx context.Context, query string) (SQLStmt, error)

	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

Supported Engines Currently, the next set of engines are supported:

Usage This example program shows the initialization and the use at basic level:

package sqldb_test

import (
	"fmt"
	"os"
	"strconv"

	"github.com/cdleo/go-sqldb"
	"github.com/cdleo/go-sqldb/engines"
	"github.com/cdleo/go-sqldb/translators"
)

type People struct {
	Id       int    `db:"id"`
	Nombre   string `db:"firstname"`
	Apellido string `db:"lastname"`
}

func Example_sqlConn() {

	adapter := engines.NewSqlite3Adapter(":memory:")
	translator := translators.NewNoopTranslator()
	sqlConn := sqldb.NewSQLDB(adapter, translator)

	if err := sqlConn.Open(); err != nil {
		fmt.Println("Unable to connect to DB")
		os.Exit(1)
	}
	defer sqlConn.Close()

	statement, err := sqlConn.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)")
	if err != nil {
		fmt.Printf("Unable to prepare statement %v\n", err)
		os.Exit(1)
	}
	_, err = statement.Exec()
	if err != nil {
		fmt.Printf("Unable to exec statement %v\n", err)
		os.Exit(1)
	}

	statement, err = sqlConn.Prepare("INSERT INTO people (firstname, lastname) VALUES (?, ?)")
	if err != nil {
		fmt.Printf("Unable to prepare statement %v\n", err)
		os.Exit(1)
	}
	_, err = statement.Exec("Gene", "Kranz")
	if err != nil {
		fmt.Printf("Unable to exec statement %v\n", err)
		os.Exit(1)
	}

	rows, err := sqlConn.Query("SELECT id, firstname, lastname FROM people")
	if err != nil {
		fmt.Printf("Unable to query data %v\n", err)
		os.Exit(1)
	}

	var p People
	for rows.Next() {
		_ = rows.Scan(&p.Id, &p.Nombre, &p.Apellido)
		fmt.Println(strconv.Itoa(p.Id) + ": " + p.Nombre + " " + p.Apellido)
	}

	// Output:
	// 1: Gene Kranz
}

Sample

You can find a sample of the use of go-sqldb project HERE

Contributing

Comments, suggestions and/or recommendations are always welcomed. Please check the Contributing Guide to learn how to get started contributing.

Documentation

Overview

Example (SqlDBBuilder)
package main

import (
	"database/sql"
	"fmt"
	"os"
	"strconv"

	"github.com/cdleo/go-commons/logger"
	"github.com/cdleo/go-sqldb/adapter"
	"github.com/cdleo/go-sqldb/connector"
)

type People struct {
	Id       int    `db:"id"`
	Nombre   string `db:"firstname"`
	Apellido string `db:"lastname"`
}

func main() {

	sqlProxy := NewSQLProxyBuilder(connector.NewSqlite3Connector(":memory:")).
		WithAdapter(adapter.NewSQLite3Adapter()).
		WithLogger(logger.NewNoLogLogger()).
		Build()

	var sqlDB *sql.DB
	var err error

	if sqlDB, err = sqlProxy.Open(); err != nil {
		fmt.Println("Unable to connect to DB")
		os.Exit(1)
	}
	defer sqlProxy.Close()

	statement, err := sqlDB.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)")
	if err != nil {
		fmt.Printf("Unable to prepare statement %v\n", err)
		os.Exit(1)
	}
	_, err = statement.Exec()
	if err != nil {
		fmt.Printf("Unable to exec statement %v\n", err)
		os.Exit(1)
	}

	statement, err = sqlDB.Prepare("INSERT INTO people (firstname, lastname) VALUES (?, ?)")
	if err != nil {
		fmt.Printf("Unable to prepare statement %v\n", err)
		os.Exit(1)
	}
	_, err = statement.Exec("Gene", "Kranz")
	if err != nil {
		fmt.Printf("Unable to exec statement %v\n", err)
		os.Exit(1)
	}

	rows, err := sqlDB.Query("SELECT id, firstname, lastname FROM people")
	if err != nil {
		fmt.Printf("Unable to query data %v\n", err)
		os.Exit(1)
	}

	var p People
	for rows.Next() {
		_ = rows.Scan(&p.Id, &p.Nombre, &p.Apellido)
		fmt.Println(strconv.Itoa(p.Id) + ": " + p.Nombre + " " + p.Apellido)
	}

}
Output:

1: Gene Kranz

Index

Examples

Constants

This section is empty.

Variables

View Source
var DBEngines = []DBEngine{
	Oracle,
	PostgreSQL,
	SQLite3,
	MockDB,
}
View Source
var SQLSintaxTranslators = []SQLSintaxTranslator{
	None,
	ToOracle,
	ToPostgreSQL,
	ToSQLite3,
}

Functions

func NewSQLProxyBuilder

func NewSQLProxyBuilder(connector sqlcommons.SQLConnector) *sqlProxyBuilder

Types

type DBEngine

type DBEngine string
const (
	Oracle     DBEngine = "Oracle"
	PostgreSQL DBEngine = "PostgreSQL"
	SQLite3    DBEngine = "SQLite3"
	MockDB     DBEngine = "MockDB"
)

type SQLSintaxTranslator

type SQLSintaxTranslator string
const (
	None         SQLSintaxTranslator = "None"
	ToOracle     SQLSintaxTranslator = "ToOracle"
	ToPostgreSQL SQLSintaxTranslator = "ToPostgreSQL"
	ToSQLite3    SQLSintaxTranslator = "ToSQLite3"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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