dbutil

package module
v0.0.0-...-f6dc8a7 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: MIT Imports: 6 Imported by: 0

README

go-dbutil

Golang utilities for interacting with databases

Install

go get github.com/recursionpharma/go-dbutil

Usage

See also examples_test.go.

Given a DB URL, connect to the DB

This is useful since sql.Open() requires you to pass the driver in addition to the URL, even though the driver is embedded in the URL.

import (
    "os"
    "fmt"
    "github.com/recursionpharma/go-dbutil"
)

func main() {
    db, err := dbutil.Connect("postgres://USER:PASSWORD@HOST:PORT/DBNAME")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(db != nil)
    // Output: true
}
Extract the driver from a DB URL
import (
    "os"
    "fmt"
    "github.com/recursionpharma/go-dbutil"
)

func main() {
    driver, err := GetDriver("postgres://USER:PASSWORD@HOST:PORT/DBNAME")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(driver)
    // Output: postgres
}

Directory Structure

code/go-dbutil/
|-- dbutil.go
|   Code
|-- dbutil_test.go
|   Tests
|-- examples_test.go
|   Examples
|-- .gitignore
|   Files git will ignore
|-- LICENSE
|   MIT License
|-- README.md
|   This file
`-- .travis.yml
    Travis config

The above file tree was generated with tree -a -L 1 --charset ascii.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(dbURL string) (*sql.DB, error)

Connect connects to the database specified by the dbURL. It tests the connection by calling db.Ping().

Example
gp := ghost_postgres.New()
defer gp.Terminate()
if err := gp.Prepare(); err != nil {
	fmt.Println(err)
	return
}
db, err := Connect(gp.URL())
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(db != nil)
Output:

true

func Exists

func Exists(db *sql.DB, q string, args ...interface{}) (bool, error)

Exists wraps a query with a simple exists check,

returning a bool, a la
  SELECT EXISTS (
    -- subquery
  )
returning a bool

example usage:

  if Exists("SELECT * FROM foo WHERE bar = 'baz'") {
		// do something
  }

func GetDriver

func GetDriver(dbURL string) (string, error)

GetDriver extracts the driver from the dbURL; for example, it will return postgres for postgres://USER:PASSWORD@HOST:PORT/DBNAME

Example
driver, err := GetDriver("postgres://")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(driver)
Output:

postgres

Types

type SQLReadWriter

type SQLReadWriter interface {
	SQLReader
	sqlx.Execer
	Preparex(string) (*hnysqlx.Stmt, error)
	NamedExec(string, interface{}) (sql.Result, error)
}

type SQLReader

type SQLReader interface {
	sqlx.Queryer
	QueryRow(string, ...interface{}) *sql.Row
	Select(interface{}, string, ...interface{}) error
	Get(interface{}, string, ...interface{}) error
	Rebind(string) string
}

type WrappedDB

type WrappedDB interface {
	SQLReadWriter
	Beginx() (WrappedTx, error)
	Close() error
	OpenConnections() int
}

func MustConnect

func MustConnect(dbURL string) WrappedDB

type WrappedTx

type WrappedTx interface {
	SQLReadWriter
	Commit() error
	Rollback() error
}

Jump to

Keyboard shortcuts

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