exp

package
v0.0.0-...-e3ce0f0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2016 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Example
package main

import (
	"database/sql"

	"github.com/jjeffery/sqlf/exp"
)

func main() {
	type User struct {
		ID   int64 `db:",pk autoincr"`
		Name string
	}

	schema := &exp.Schema{}
	schema.DefineTable("users", User{})

	var db *sql.DB
	var user User

	if err := schema.InsertRow(db, user); err != nil {
		panic(err)
	}
	if err := schema.SelectRow(db, &user); err != nil {
		panic(err)
	}
	if _, err := schema.UpdateRow(db, user); err != nil {
		panic(err)
	}
	if _, err := schema.DeleteRow(db, user); err != nil {
		panic(err)
	}

	query1 := schema.MustPrepareQuery(`
select distinct {u}
from users u
inner join user_search_terms t
  on t.user_id = u.id
where t.search_term like ?
order by u.family_name, u.given_name, u.email, u.id`)

	var users []User

	if err := query1.Select(db, &users, "lollypop%"); err != nil {
		panic(err)
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefineTable

func DefineTable(name string, row interface{})

func DeleteRow

func DeleteRow(execer Execer, row interface{}) (int, error)

func InsertRow

func InsertRow(execer Execer, row interface{}) error

func SelectRow

func SelectRow(queryer Queryer, row interface{}) error

func UpdateRow

func UpdateRow(execer Execer, row interface{}) (int, error)

Types

type Dialect

type Dialect interface {
	// Name of the dialect.
	Name() string

	// Quote a table name or column name so that it does
	// not clash with any reserved words. The SQL-99 standard
	// specifies double quotes (eg "table_name"), but many
	// dialects, including MySQL use the backtick (eg `table_name`).
	// SQL server uses square brackets (eg [table_name]).
	Quote(name string) string

	// Return the placeholder for binding a variable value.
	// Most SQL dialects support a single question mark (?), but
	// PostgreSQL uses numbered placeholders (eg $1).
	Placeholder(n int) string
}

Dialect is an interface used to handle differences in SQL dialects.

func NewDialect

func NewDialect(driverName string) Dialect

NewDialect returns a dialect for the specified driver. If driverName is blank, then the first driver is chosen from the list of drivers loaded. This works well for the common situation where the calling program has loaded only one database driver -- that database driver will be used to select the dialect.

Supported drivers include:

mysql
postgres
mssql
sqlite3

type Execer

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

Execer implements a single method, Exec, which executes a query without returning any rows. The args are for any parameter placeholders in the query.

This interface is compatible with the standard library package "database/sql". Both *sql.DB and *sql.Tx implement this interface.

type QueryStmt

type QueryStmt struct{}

func MustPrepareQuery

func MustPrepareQuery(query string) *QueryStmt

func PrepareQuery

func PrepareQuery(query string) (*QueryStmt, error)

func (*QueryStmt) Select

func (s *QueryStmt) Select(queryer Queryer, dest interface{}, args ...interface{}) error

type Queryer

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

Queryer implements a single method, Query, which executes a query that returns zero, one or more rows. The args are for any parameter placeholders in the query.

This interface is compatible with the standard library package "database/sql". Both *sql.DB and *sql.Tx implement this interface.

type Schema

type Schema struct {
	// Dialect used for constructing SQL queries.
	Dialect Dialect

	// ColumnNameFor converts a Go struct field name to a column name.
	ColumnNameFor func(fieldName string) string
}

Schema contains information that is common to a database schema. It is used when preparing SQL to run against a particular database.

var DefaultSchema Schema

DefaultSchema provides a default schema instance.

Many programs will not have to create an instance of Schema: if a program uses a single database schema, then the DefaultSchema can be used instead.

func (*Schema) DefineTable

func (s *Schema) DefineTable(name string, row interface{})

func (*Schema) DeleteRow

func (s *Schema) DeleteRow(execer Execer, row interface{}) (int, error)

func (*Schema) InsertRow

func (s *Schema) InsertRow(execer Execer, row interface{}) error

func (*Schema) MustPrepareQuery

func (s *Schema) MustPrepareQuery(query string) *QueryStmt

func (*Schema) PrepareQuery

func (s *Schema) PrepareQuery(query string) (*QueryStmt, error)

func (*Schema) SelectRow

func (s *Schema) SelectRow(queryer Queryer, row interface{}) error

func (*Schema) UpdateRow

func (s *Schema) UpdateRow(execer Execer, row interface{}) (int, error)

Jump to

Keyboard shortcuts

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