dotsql: github.com/gchaincl/dotsql Index | Examples | Files

package dotsql

import "github.com/gchaincl/dotsql"

Package dotsql provides a way to separate your code from SQL queries.

It is not an ORM, it is not a query builder. Dotsql is a library that helps you keep sql files in one place and use it with ease.

For more usage examples see https://github.com/gchaincl/dotsql

Index

Examples

Package Files

dotsql.go scanner.go

type DotSql Uses

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

DotSql represents a dotSQL queries holder.

func Load Uses

func Load(r io.Reader) (*DotSql, error)

Load imports sql queries from any io.Reader.

func LoadFromFile Uses

func LoadFromFile(sqlFile string) (*DotSql, error)

LoadFromFile imports SQL queries from the file.

Code:

db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
    panic(err)
}

dot, err := dotsql.LoadFromFile("queries.sql")
if err != nil {
    panic(err)
}

/* queries.sql looks like:
-- name: create-users-table
CREATE TABLE users (
	id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
	name VARCHAR(255),
	email VARCHAR(255)
);

-- name: create-user
INSERT INTO users (name, email) VALUES(?, ?)

-- name: find-users-by-email
SELECT id,name,email FROM users WHERE email = ?

-- name: find-one-user-by-email
SELECT id,name,email FROM users WHERE email = ? LIMIT 1

--name: drop-users-table
DROP TABLE users
*/

// Exec
if _, err := dot.Exec(db, "create-users-table"); err != nil {
    panic(err)
}

if _, err := dot.Exec(db, "create-user", "user@example.com"); err != nil {
    panic(err)
}

// Query
rows, err := dot.Query(db, "find-user-by-email", "user@example.com")
if err != nil {
    panic(err)
}
defer rows.Close()

for rows.Next() {
    var id int
    var name, email string

    err = rows.Scan(&id, &name, &email)

    if err != nil {
        panic(err)
    }

    log.Println(email)
}

// QueryRow
row, err := dot.QueryRow(db, "find-one-user-by-email", "user@example.com")
if err != nil {
    panic(err)
}

var id int
var name, email string
row.Scan(&id, &name, &email)

log.Println(email)

func LoadFromString Uses

func LoadFromString(sql string) (*DotSql, error)

LoadFromString imports SQL queries from the string.

func Merge Uses

func Merge(dots ...*DotSql) *DotSql

Merge takes one or more *DotSql and merge its queries It's in-order, so the last source will override queries with the same name in the previous arguments if any.

func (DotSql) Exec Uses

func (d DotSql) Exec(db Execer, name string, args ...interface{}) (sql.Result, error)

Exec is a wrapper for database/sql's Exec(), using dotsql named query.

func (DotSql) ExecContext Uses

func (d DotSql) ExecContext(ctx context.Context, db ExecerContext, name string, args ...interface{}) (sql.Result, error)

ExecContext is a wrapper for database/sql's ExecContext(), using dotsql named query.

func (DotSql) Prepare Uses

func (d DotSql) Prepare(db Preparer, name string) (*sql.Stmt, error)

Prepare is a wrapper for database/sql's Prepare(), using dotsql named query.

func (DotSql) PrepareContext Uses

func (d DotSql) PrepareContext(ctx context.Context, db PreparerContext, name string) (*sql.Stmt, error)

PrepareContext is a wrapper for database/sql's PrepareContext(), using dotsql named query.

func (DotSql) Query Uses

func (d DotSql) Query(db Queryer, name string, args ...interface{}) (*sql.Rows, error)

Query is a wrapper for database/sql's Query(), using dotsql named query.

func (DotSql) QueryContext Uses

func (d DotSql) QueryContext(ctx context.Context, db QueryerContext, name string, args ...interface{}) (*sql.Rows, error)

QueryContext is a wrapper for database/sql's QueryContext(), using dotsql named query.

func (DotSql) QueryMap Uses

func (d DotSql) QueryMap() map[string]string

QueryMap returns a map[string]string of loaded queries

func (DotSql) QueryRow Uses

func (d DotSql) QueryRow(db QueryRower, name string, args ...interface{}) (*sql.Row, error)

QueryRow is a wrapper for database/sql's QueryRow(), using dotsql named query.

func (DotSql) QueryRowContext Uses

func (d DotSql) QueryRowContext(ctx context.Context, db QueryRowerContext, name string, args ...interface{}) (*sql.Row, error)

QueryRowContext is a wrapper for database/sql's QueryRowContext(), using dotsql named query.

func (DotSql) Raw Uses

func (d DotSql) Raw(name string) (string, error)

Raw returns the query, everything after the --name tag

type Execer Uses

type Execer interface {
    Exec(query string, args ...interface{}) (sql.Result, error)
}

Execer is an interface used by Exec.

type ExecerContext Uses

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

ExecerContext is an interface used by ExecContext.

type Preparer Uses

type Preparer interface {
    Prepare(query string) (*sql.Stmt, error)
}

Preparer is an interface used by Prepare.

type PreparerContext Uses

type PreparerContext interface {
    PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

PreparerContext is an interface used by PrepareContext.

type QueryRower Uses

type QueryRower interface {
    QueryRow(query string, args ...interface{}) *sql.Row
}

QueryRower is an interface used by QueryRow.

type QueryRowerContext Uses

type QueryRowerContext interface {
    QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

QueryRowerContext is an interface used by QueryRowContext.

type Queryer Uses

type Queryer interface {
    Query(query string, args ...interface{}) (*sql.Rows, error)
}

Queryer is an interface used by Query.

type QueryerContext Uses

type QueryerContext interface {
    QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

QueryerContext is an interface used by QueryContext.

type Scanner Uses

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

func (*Scanner) Run Uses

func (s *Scanner) Run(io *bufio.Scanner) map[string]string

Package dotsql imports 9 packages (graph) and is imported by 14 packages. Updated 2019-08-20. Refresh now. Tools for package owners.