Documentation ¶
Overview ¶
Package sqlbuilder is a Standard Query Language builder for golang supporting Sqlite3, MySQL and Postgres
See https://github.com/go-corelibs/go-sqlbuilder for more information
Example ¶
db, err := sql.Open("sqlite3", ":memory:") if err != nil { fmt.Println(err.Error()) return } // Set dialect first // dialects are in github.com/go-corelibs/go-sqlbuilder/dialects SetDialect(TestingDialect{}) // Define a table tbl_person := NewTable( "PERSON", &TableOption{}, IntColumn("id", &ColumnOption{ PrimaryKey: true, }), StringColumn("name", &ColumnOption{ Unique: true, Size: 255, Default: "no_name", }), DateColumn("birth", nil), ) // Create Table query, args, err := CreateTable(tbl_person).ToSql() if err != nil { fmt.Println(err.Error()) return } _, err = db.Exec(query, args...) if err != nil { fmt.Println(err.Error()) return } // Insert data // (Table).C function returns a column object. query, args, err = Insert(tbl_person). Set(tbl_person.C("name"), "Kurisu Makise"). Set(tbl_person.C("birth"), time.Date(1992, time.July, 25, 0, 0, 0, 0, time.UTC)). ToSql() _, err = db.Exec(query, args...) if err != nil { fmt.Println(err.Error()) return } // Query var birth time.Time query, args, err = Select(tbl_person).Columns( tbl_person.C("birth"), ).Where( tbl_person.C("name").Eq("Kurisu Makise"), ).ToSql() err = db.QueryRow(query, args...).Scan(&birth) if err != nil { fmt.Println(err.Error()) return } fmt.Printf("Kurisu's birthday is %s,%d %d", birth.Month().String(), birth.Day(), birth.Year())
Output: Kurisu's birthday is July,25 1992
Index ¶
- func GetColumnError(c Column) (err error)
- func IsColumnError(c Column) bool
- func SameColumn(a, b Column) (same bool)
- func SetDialect(opt Dialect)
- type AlterTableBuilder
- type Buildable
- type Column
- type ColumnConfig
- func AnyColumn(name string, opt *ColumnOption) ColumnConfig
- func BoolColumn(name string, opt *ColumnOption) ColumnConfig
- func BytesColumn(name string, opt *ColumnOption) ColumnConfig
- func DateColumn(name string, opt *ColumnOption) ColumnConfig
- func FloatColumn(name string, opt *ColumnOption) ColumnConfig
- func IntColumn(name string, opt *ColumnOption) ColumnConfig
- func StringColumn(name string, opt *ColumnOption) ColumnConfig
- func TimeColumn(name string, opt *ColumnOption) ColumnConfig
- type ColumnList
- type ColumnOption
- type ColumnType
- type Condition
- type CreateIndexBuilder
- type CreateTableBuilder
- type DeleteBuilder
- type Dialect
- type DropTableBuilder
- type InsertBuilder
- type SelectBuilder
- type SqlFunc
- type Statement
- type Table
- type TableOption
- type TestingDialect
- func (td TestingDialect) BindVar(i int) string
- func (td TestingDialect) ColumnOptionToString(co *ColumnOption) (string, error)
- func (td TestingDialect) ColumnTypeToString(cc ColumnConfig) (string, error)
- func (td TestingDialect) Name() string
- func (td TestingDialect) QuerySuffix() string
- func (td TestingDialect) QuoteField(field interface{}) string
- func (td TestingDialect) TableOptionToString(to *TableOption) (string, error)
- type UpdateBuilder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetColumnError ¶ added in v1.1.0
func IsColumnError ¶ added in v1.1.0
func SameColumn ¶ added in v1.1.0
func SetDialect ¶
func SetDialect(opt Dialect)
SetDialect sets dialect for SQL server. Must set dialect at first.
Types ¶
type AlterTableBuilder ¶
type AlterTableBuilder interface { RenameTo(name string) AlterTableBuilder AddColumn(col ColumnConfig) AlterTableBuilder AddColumnAfter(col ColumnConfig, after Column) AlterTableBuilder AddColumnFirst(col ColumnConfig) AlterTableBuilder DropColumn(col Column) AlterTableBuilder ChangeColumn(old_column Column, new_column ColumnConfig) AlterTableBuilder ChangeColumnAfter(old_column Column, new_column ColumnConfig, after Column) AlterTableBuilder ChangeColumnFirst(old_column Column, new_column ColumnConfig) AlterTableBuilder ToSql() (query string, args []interface{}, err error) ApplyToTable() error // contains filtered or unexported methods }
AlterTableBuilder is the Buildable interface wrapping of AlterTable
func AlterTable ¶
func AlterTable(tbl Table) AlterTableBuilder
type Buildable ¶
type Buildable interface { // Dialect returns the Dialect associated with this Buildable instance Dialect() Dialect // NewTable wraps the NewTable package-level function and for all named // tables, stores the Table reference within the Buildable instance for // later retrieval with the T method NewTable(name string, option *TableOption, column_configs ...ColumnConfig) (t Table) // AlterTable starts a new ALTER TABLE statement builder AlterTable(tbl Table) AlterTableBuilder // CreateTable starts a new CREATE TABLE statement builder CreateTable(tbl Table) CreateTableBuilder // CreateIndex starts a new ADD INDEX statement builder CreateIndex(tbl Table) CreateIndexBuilder // Delete starts a new DELETE statement builder Delete(from Table) DeleteBuilder // Insert starts a new INSERT statement builder Insert(into Table) InsertBuilder // Select starts a new SELECT statement builder Select(from Table) SelectBuilder }
Buildable is an interface for performing the package-level functions which all require a package-level Dialect to be configured using a call to SetDialect. Buildable allows for using one or more independent Dialects at the same time within the same codebase
Buildable is concurrency-safe
func NewBuildable ¶
NewBuildable constructs a new Buildable instance with the given Dialect
type Column ¶
type Column interface { // As creates Column alias. As(alias string) Column // Eq creates Condition for "column==right". Type for right is column's one or other Column. Eq(right interface{}) Condition // NotEq creates Condition for "column<>right". Type for right is column's one or other Column. NotEq(right interface{}) Condition // Gt creates Condition for "column>right". Type for right is column's one or other Column. Gt(right interface{}) Condition // GtEq creates Condition for "column>=right". Type for right is column's one or other Column. GtEq(right interface{}) Condition // Lt creates Condition for "column<right". Type for right is column's one or other Column. Lt(right interface{}) Condition // LtEq creates Condition for "column<=right". Type for right is column's one or other Column. LtEq(right interface{}) Condition // Like creates Condition for "column LIKE right". Type for right is column's one or other Column. Like(right string) Condition // NotLike creates Condition for "column NOT LIKE right". Type for right is column's one or other Column. NotLike(right string) Condition // Between creates Condition for "column BETWEEN lower AND higher". Type for lower/higher is int or time.Time. Between(lower, higher interface{}) Condition // In creates Condition for "column IN (values[0], values[1] ...)". Type for values is column's one or other Column. In(values ...interface{}) Condition // NotIn creates Condition for "column NOT IN (values[0], values[1] ...)". Type for values is column's one or other Column. NotIn(values ...interface{}) Condition // contains filtered or unexported methods }
Column represents a table column.
type ColumnConfig ¶
type ColumnConfig interface { Name() string Type() ColumnType Option() *ColumnOption Describe() (output string) // contains filtered or unexported methods }
ColumnConfig represents a config for table's column. This has a name, data type and some options.
func AnyColumn ¶
func AnyColumn(name string, opt *ColumnOption) ColumnConfig
AnyColumn creates config for any types.
func BoolColumn ¶
func BoolColumn(name string, opt *ColumnOption) ColumnConfig
BoolColumn creates config for BOOLEAN type column.
func BytesColumn ¶
func BytesColumn(name string, opt *ColumnOption) ColumnConfig
BytesColumn creates config for BLOB type column.
func DateColumn ¶
func DateColumn(name string, opt *ColumnOption) ColumnConfig
DateColumn creates config for DATETIME type column.
func FloatColumn ¶
func FloatColumn(name string, opt *ColumnOption) ColumnConfig
FloatColumn creates config for REAL or FLOAT type column.
func IntColumn ¶
func IntColumn(name string, opt *ColumnOption) ColumnConfig
IntColumn creates config for INTEGER type column.
func StringColumn ¶
func StringColumn(name string, opt *ColumnOption) ColumnConfig
StringColumn creates config for TEXT or VARCHAR type column.
func TimeColumn ¶ added in v1.1.0
func TimeColumn(name string, opt *ColumnOption) ColumnConfig
TimeColumn creates config for DATETIME type column.
type ColumnList ¶
type ColumnList []Column
ColumnList represents list of Column.
func (ColumnList) Describe ¶ added in v1.1.0
func (c ColumnList) Describe() (output string)
type ColumnOption ¶
type ColumnOption struct { PrimaryKey bool NotNull bool Unique bool AutoIncrement bool Size int SqlType string Default interface{} }
ColumnOption represents option for a column. ex: primary key.
func (ColumnOption) Describe ¶ added in v1.1.0
func (c ColumnOption) Describe() (output string)
type ColumnType ¶
type ColumnType int
ColumnType reprecents a type of column. Dialects handle this for know column options.
const ( ColumnTypeAny ColumnType = iota ColumnTypeInt ColumnTypeString ColumnTypeDate ColumnTypeFloat ColumnTypeBool ColumnTypeBytes )
func (ColumnType) CapableTypes ¶
func (t ColumnType) CapableTypes() []reflect.Type
func (ColumnType) String ¶
func (t ColumnType) String() string
type Condition ¶
type Condition interface { Describe() (output string) // contains filtered or unexported methods }
Condition represents a condition for WHERE clause and other.
type CreateIndexBuilder ¶
type CreateIndexBuilder interface { IfNotExists() CreateIndexBuilder Columns(columns ...Column) CreateIndexBuilder Name(name string) CreateIndexBuilder ToSql() (query string, args []interface{}, err error) // contains filtered or unexported methods }
CreateIndexBuilder is the Buildable interface wrapping of CreateTable
func CreateIndex ¶
func CreateIndex(tbl Table) CreateIndexBuilder
CreateIndex returns new "CREATE INDEX" statement. The table is Table object to create index.
type CreateTableBuilder ¶
type CreateTableBuilder interface { IfNotExists() CreateTableBuilder ToSql() (query string, args []interface{}, err error) // contains filtered or unexported methods }
CreateTableBuilder is the Buildable interface wrapping of CreateIndex
func CreateTable ¶
func CreateTable(tbl Table) CreateTableBuilder
CreateTable returns new "CREATE TABLE" statement. The table is Table object to create.
type DeleteBuilder ¶
type DeleteBuilder interface { Where(cond Condition) DeleteBuilder ToSql() (query string, args []interface{}, err error) // contains filtered or unexported methods }
DeleteBuilder is the Buildable interface wrapping of Delete
func Delete ¶
func Delete(from Table) DeleteBuilder
Delete returns new DELETE statement. The table is Table object to delete from.
type Dialect ¶
type Dialect interface { Name() string QuerySuffix() string BindVar(i int) string QuoteField(field interface{}) string ColumnTypeToString(ColumnConfig) (string, error) ColumnOptionToString(*ColumnOption) (string, error) TableOptionToString(*TableOption) (string, error) }
Dialect encapsulates behaviors that differ across SQL database.
type DropTableBuilder ¶ added in v1.1.0
type DropTableBuilder interface { ToSql() (query string, args []interface{}, err error) // contains filtered or unexported methods }
DropTableBuilder is the Buildable interface wrapping of DeleteTable
func DropTable ¶
func DropTable(tbl Table) DropTableBuilder
DropTable returns new "DROP TABLE" statement. The table is Table object to drop.
type InsertBuilder ¶
type InsertBuilder interface { Columns(columns ...Column) InsertBuilder Values(values ...interface{}) InsertBuilder Set(column Column, value interface{}) InsertBuilder ToSql() (query string, args []interface{}, err error) // contains filtered or unexported methods }
InsertBuilder is the Buildable interface wrapping of Insert
func Insert ¶
func Insert(into Table) InsertBuilder
Insert returns new INSERT statement. The table is Table object for into.
type SelectBuilder ¶
type SelectBuilder interface { // Distinct adds a DISTINCT clause to the Columns selection Distinct() SelectBuilder // Columns specifies one or more Columns to return Columns(columns ...Column) SelectBuilder Where(cond Condition) SelectBuilder Having(cond Condition) SelectBuilder GroupBy(columns ...Column) SelectBuilder OrderBy(desc bool, columns ...Column) SelectBuilder Limit(limit int) SelectBuilder Offset(offset int) SelectBuilder ToSql() (query string, args []interface{}, err error) ToSubquery(alias string) Table // Describe returns a description of the configured Table instance, useful // when unit testing and needing to confirm that the correct values were // given during Table construction Describe() (output string) // contains filtered or unexported methods }
SelectBuilder is the Buildable interface wrapping of Select
func Select ¶
func Select(from Table) SelectBuilder
Select returns new SELECT statement with from as FROM clause.
type SqlFunc ¶
type SqlFunc interface { Column // contains filtered or unexported methods }
SqlFunc represents an SQL function (ex:count(*)) which can be use in the same way as a Column
type Table ¶
type Table interface { // C returns table's column by the name. C(name string) Column // Name returns table' name. // returns empty if it is joined table or subquery. Name() string // Option returns table's option(table constraint). // returns nil if it is joined table or subquery. Option() *TableOption // Columns returns all columns. Columns() []Column // InnerJoin returns a joined table use with "INNER JOIN" clause. // The joined table can be handled in same way as single table. InnerJoin(Table, Condition) Table // LeftOuterJoin returns a joined table use with "LEFT OUTER JOIN" clause. // The joined table can be handled in same way as single table. LeftOuterJoin(Table, Condition) Table // RightOuterJoin returns a joined table use with "RIGHT OUTER JOIN" clause. // The joined table can be handled in same way as single table. RightOuterJoin(Table, Condition) Table // FullOuterJoin returns a joined table use with "FULL OUTER JOIN" clause. // The joined table can be handled in same way as single table. FullOuterJoin(Table, Condition) Table // Describe returns a string representation of the complete structure Describe() (output string) // contains filtered or unexported methods }
Table represents a table.
func NewTable ¶
func NewTable(name string, option *TableOption, column_configs ...ColumnConfig) Table
NewTable returns a new table named by the name. Specify table columns by the column_config. Panic if column is empty.
type TableOption ¶
type TableOption struct {
Unique [][]string
}
TableOption represents constraint of a table.
func (TableOption) Describe ¶ added in v1.1.0
func (t TableOption) Describe() (output string)
Describe returns a string representation of the TableOption
type TestingDialect ¶ added in v1.1.0
type TestingDialect struct{}
func (TestingDialect) BindVar ¶ added in v1.1.0
func (td TestingDialect) BindVar(i int) string
func (TestingDialect) ColumnOptionToString ¶ added in v1.1.0
func (td TestingDialect) ColumnOptionToString(co *ColumnOption) (string, error)
func (TestingDialect) ColumnTypeToString ¶ added in v1.1.0
func (td TestingDialect) ColumnTypeToString(cc ColumnConfig) (string, error)
func (TestingDialect) Name ¶ added in v1.1.0
func (td TestingDialect) Name() string
func (TestingDialect) QuerySuffix ¶ added in v1.1.0
func (td TestingDialect) QuerySuffix() string
func (TestingDialect) QuoteField ¶ added in v1.1.0
func (td TestingDialect) QuoteField(field interface{}) string
func (TestingDialect) TableOptionToString ¶ added in v1.1.0
func (td TestingDialect) TableOptionToString(to *TableOption) (string, error)
type UpdateBuilder ¶ added in v1.1.0
type UpdateBuilder interface { Set(col Column, val interface{}) UpdateBuilder Where(cond Condition) UpdateBuilder Limit(limit int) UpdateBuilder Offset(offset int) UpdateBuilder OrderBy(desc bool, columns ...Column) UpdateBuilder ToSql() (query string, args []interface{}, err error) // contains filtered or unexported methods }
UpdateBuilder is the Buildable interface wrapping of Update
func Update ¶
func Update(tbl Table) UpdateBuilder
Update returns new UPDATE statement. The table is Table object to update.
Source Files ¶
- alter-table-add-column.go
- alter-table-change-column.go
- alter-table.go
- column-alias.go
- column-as-alias.go
- column-config.go
- column-error.go
- column-impl-config.go
- column-impl.go
- column-list.go
- column-option.go
- column-type.go
- column.go
- condition-and-or.go
- condition-between.go
- condition-binary-op.go
- condition-in.go
- condition.go
- create-index-column-list.go
- create-index.go
- create-table-column-list.go
- create-table.go
- delete.go
- dialect-testing.go
- dialect.go
- drop.go
- errors.go
- insert.go
- literal.go
- select-column-list.go
- select-order-by.go
- select-subquery.go
- select.go
- serializable.go
- sqlbuildable.go
- sqlbuilder.go
- sqlfunc-column-list.go
- sqlfunc.go
- table-join-type.go
- table-join.go
- table-option.go
- table.go
- update-value.go
- update.go