sqlconn

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 7 Imported by: 0

README

go-sqlconn

A simple SQL data adapter

Query Sample

    type User struct {
        ID sql.NullString `db:"id"`
        Name sql.NullString `db:"name"`
    }

    dbs := new(sqlconn.Databases)

    db1 := dbs.NewInstance("DB1")
    rows, err := db1.Query(`SELECT id, name FROM User WHERE id = ?`, "ID1")
    defer rows.Close()

    if err != nil {
        return nil, err
    }

    var got []User
    for rows.Next() {
        var u User
        err = rows.Scan(&u.ID, &u.Name)
        if err != nil {
            panic(err)
        }
        got = append(got, u)
    }

    return got, nil

Switch between prod and dev environment

    CONFIG=configs/dbs.dev.yml

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBConfig

type DBConfig struct {
	Databases map[string]Database `yaml:"databases"`
}

func UnmarshalConfig

func UnmarshalConfig(filename string) (*DBConfig, error)

type DBConn

type DBConn struct {
	Driver     string
	DataSource string
}

func (*DBConn) Exec

func (d *DBConn) Exec(query string, args ...interface{}) (sql.Result, error)

func (*DBConn) Query

func (d *DBConn) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*DBConn) QueryRow

func (d *DBConn) QueryRow(query string, args ...interface{}) *sql.Row

type Database

type Database struct {
	Driver   string
	Host     string
	Database string
	User     string
	Password string
}

type Databases

type Databases struct{}

func (*Databases) NewInstance

func (d *Databases) NewInstance(db string) *DBConn

Jump to

Keyboard shortcuts

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