2021-06-24 07:43:28 +00:00
|
|
|
package bob
|
|
|
|
|
2021-07-21 08:59:53 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/lann/builder"
|
|
|
|
)
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// ErrEmptyTable is a common database/sql error if a table is empty or no rows is returned by the query.
|
2021-07-21 08:59:53 +00:00
|
|
|
var ErrEmptyTable = errors.New("sql: no rows in result set")
|
2021-07-24 18:47:02 +00:00
|
|
|
// ErrEmptyTable is a common pgx error if a table is empty or no rows is returned by the query.
|
2021-07-21 08:59:53 +00:00
|
|
|
var ErrEmptyTablePgx = errors.New("no rows in result set")
|
2021-07-25 04:41:39 +00:00
|
|
|
// ErrDialectNotSupported tells you whether the dialect is supported or not.
|
|
|
|
var ErrDialectNotSupported = errors.New("provided database dialect is not supported")
|
|
|
|
|
|
|
|
const (
|
|
|
|
Mysql int = iota
|
|
|
|
Postgresql
|
|
|
|
Sqlite
|
|
|
|
MSSql
|
|
|
|
)
|
2021-06-24 07:43:28 +00:00
|
|
|
|
2021-06-26 02:57:08 +00:00
|
|
|
// BobBuilderType is the type for BobBuilder
|
2021-06-24 07:43:28 +00:00
|
|
|
type BobBuilderType builder.Builder
|
|
|
|
|
2021-07-09 06:41:34 +00:00
|
|
|
// BobBuilder interface wraps the ToSql method
|
2021-06-24 07:43:28 +00:00
|
|
|
type BobBuilder interface {
|
2021-07-09 06:41:34 +00:00
|
|
|
ToSql() (string, []interface{}, error)
|
2021-06-24 07:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 02:57:08 +00:00
|
|
|
// CreateTable creates a table with CreateBuilder interface
|
2021-06-24 07:43:28 +00:00
|
|
|
func (b BobBuilderType) CreateTable(table string) CreateBuilder {
|
|
|
|
return CreateBuilder(b).Name(table)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists.
|
2021-07-09 06:39:30 +00:00
|
|
|
func (b BobBuilderType) CreateTableIfNotExists(table string) CreateBuilder {
|
|
|
|
return CreateBuilder(b).Name(table).IfNotExists()
|
|
|
|
}
|
|
|
|
|
2021-06-26 02:57:08 +00:00
|
|
|
// HasTable checks if a table exists with HasBuilder interface
|
2021-06-25 17:04:41 +00:00
|
|
|
func (b BobBuilderType) HasTable(table string) HasBuilder {
|
|
|
|
return HasBuilder(b).HasTable(table)
|
|
|
|
}
|
|
|
|
|
2021-06-26 02:57:08 +00:00
|
|
|
// HasColumn checks if a column exists with HasBuilder interface
|
2021-06-25 17:04:41 +00:00
|
|
|
func (b BobBuilderType) HasColumn(column string) HasBuilder {
|
|
|
|
return HasBuilder(b).HasColumn(column)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// DropTable drops (delete contents & remove) a table from the database.
|
2021-07-24 18:35:12 +00:00
|
|
|
func (b BobBuilderType) DropTable(table string) DropBuilder {
|
|
|
|
return DropBuilder(b).DropTable(table)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// DropTable drops (delete contents & remove) a table from the database if the table exists.
|
2021-07-24 18:35:12 +00:00
|
|
|
func (b BobBuilderType) DropTableIfExists(table string) DropBuilder {
|
|
|
|
return DropBuilder(b).DropTable(table).IfExists()
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// RenameTable simply renames an exisisting table.
|
2021-07-24 18:35:12 +00:00
|
|
|
func (b BobBuilderType) RenameTable(from, to string) RenameBuilder {
|
|
|
|
return RenameBuilder(b).From(from).To(to)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// Truncate performs TRUNCATE function. It deletes all contents from a table but not deleting the table.
|
2021-07-24 18:35:12 +00:00
|
|
|
func (b BobBuilderType) Truncate(table string) TruncateBuilder {
|
|
|
|
return TruncateBuilder(b).Truncate(table)
|
|
|
|
}
|
|
|
|
|
2021-07-25 04:41:39 +00:00
|
|
|
func (b BobBuilderType) Upsert(table string, dialect int) UpsertBuilder {
|
|
|
|
return UpsertBuilder(b).dialect(dialect).Into(table)
|
|
|
|
}
|
|
|
|
|
2021-06-26 02:57:08 +00:00
|
|
|
// BobStmtBuilder is the parent builder for BobBuilderType
|
2021-06-24 07:43:28 +00:00
|
|
|
var BobStmtBuilder = BobBuilderType(builder.EmptyBuilder)
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// CreateTable creates a table with CreateBuilder interface.
|
2021-06-24 07:43:28 +00:00
|
|
|
func CreateTable(table string) CreateBuilder {
|
|
|
|
return BobStmtBuilder.CreateTable(table)
|
|
|
|
}
|
2021-06-25 17:04:41 +00:00
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists.
|
2021-07-09 06:39:30 +00:00
|
|
|
func CreateTableIfNotExists(table string) CreateBuilder {
|
|
|
|
return BobStmtBuilder.CreateTableIfNotExists(table)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// HasTable checks if a table exists with HasBuilder interface.
|
2021-06-25 17:04:41 +00:00
|
|
|
func HasTable(table string) HasBuilder {
|
|
|
|
return BobStmtBuilder.HasTable(table)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// HasColumn checks if a column exists with HasBuilder interface.
|
2021-06-25 17:04:41 +00:00
|
|
|
func HasColumn(col string) HasBuilder {
|
|
|
|
return BobStmtBuilder.HasColumn(col)
|
|
|
|
}
|
2021-07-24 18:35:12 +00:00
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// DropTable drops (delete contents & remove) a table from the database.
|
2021-07-24 18:35:12 +00:00
|
|
|
func DropTable(table string) DropBuilder {
|
|
|
|
return BobStmtBuilder.DropTable(table)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// DropTable drops (delete contents & remove) a table from the database if the table exists.
|
2021-07-24 18:35:12 +00:00
|
|
|
func DropTableIfExists(table string) DropBuilder {
|
|
|
|
return BobStmtBuilder.DropTableIfExists(table)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// RenameTable simply renames an exisisting table.
|
2021-07-24 18:35:12 +00:00
|
|
|
func RenameTable(from, to string) RenameBuilder {
|
|
|
|
return BobStmtBuilder.RenameTable(from, to)
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:47:02 +00:00
|
|
|
// Truncate performs TRUNCATE function. It deletes all contents from a table but not deleting the table.
|
2021-07-24 18:35:12 +00:00
|
|
|
func Truncate(table string) TruncateBuilder {
|
|
|
|
return BobStmtBuilder.Truncate(table)
|
2021-07-25 04:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upsert performs a UPSERT query with specified database dialect.
|
|
|
|
// Supported database includes MySQL, PostgreSQL, SQLite and MSSQL.
|
|
|
|
func Upsert(table string, dialect int) UpsertBuilder {
|
|
|
|
return BobStmtBuilder.Upsert(table, dialect)
|
2021-07-24 18:35:12 +00:00
|
|
|
}
|