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 ¶
- func DefineTable(name string, row interface{})
- func DeleteRow(execer Execer, row interface{}) (int, error)
- func InsertRow(execer Execer, row interface{}) error
- func SelectRow(queryer Queryer, row interface{}) error
- func UpdateRow(execer Execer, row interface{}) (int, error)
- type Dialect
- type Execer
- type QueryStmt
- type Queryer
- type Schema
- func (s *Schema) DefineTable(name string, row interface{})
- func (s *Schema) DeleteRow(execer Execer, row interface{}) (int, error)
- func (s *Schema) InsertRow(execer Execer, row interface{}) error
- func (s *Schema) MustPrepareQuery(query string) *QueryStmt
- func (s *Schema) PrepareQuery(query string) (*QueryStmt, error)
- func (s *Schema) SelectRow(queryer Queryer, row interface{}) error
- func (s *Schema) UpdateRow(execer Execer, row interface{}) (int, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefineTable ¶
func DefineTable(name string, row interface{})
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 ¶
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 ¶
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 Queryer ¶
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.