From 50c8d98f3a7d595a3564a1caba98a18fcae174d9 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sun, 25 Jul 2021 01:47:02 +0700 Subject: [PATCH] docs: new features --- README.md | 45 ++++++++++++++++++++++++++++++++++++++++----- bob.go | 19 +++++++++++++++---- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b8fb99b..f895e12 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,41 @@ func main() { } ``` +### Drop table + +```go +func main() { + sql, _, err := bob.DropTable("users").ToSql() + if err != nil { + log.Fatal(err) + } +} +``` + +You could also do `bob.DropTableIfExists("users")` to output a `DROP TABLE IF EXISTS "users"` query. + +### Truncate table + +```go +func main() { + sql, _, err := bob.Truncate("users").ToSql() + if err != nil { + log.Fatal(err) + } +} +``` + +### Rename table + +```go +func main() { + sql, _, err := bob.RenameTable("users", "people").ToSql() + if err != nil { + log.Fatal(err) + } +} +``` + ### Placeholder format / Dialect Default placeholder is a question mark (MySQL-like). If you want to change it, simply use something like this: @@ -105,7 +140,7 @@ func main() { Available placeholder formats: * `bob.Question` - `INSERT INTO "users" (name) VALUES (?)` * `bob.Dollar` - `INSERT INTO "users" (name) VALUES ($1)` -* `bob.Colon` - `INSERT INTO "users" (name) VALUES (:1)` +* `bob.Colon` - `INSERT INTO "users" (name) VALUES (:1)` (Yes, I know this is kinda wrong. I'm thinking of removing it.) * `bob.AtP` - `INSERT INTO "users" (name) VALUES (@p1)` ### With pgx (PostgreSQL) @@ -183,15 +218,15 @@ func main() { * `bob.CreateTableIfNotExists(tableName)` - Create table if not exists * `bob.HasTable(tableName)` - Checks if column exists (return error if false, check example above for error handling) * `bob.HasColumn(columnName)` - Check if a column exists on current table +* `bob.DropTable(tableName)` - Drop a table (`drop table "users"`) +* `bob.DropTableIfExists(tableName)` - Drop a table if exists (`drop table if exists "users"`) +* `bob.RenameTable(currentTable, desiredName)` - Rename a table (`rename table "users" to "people"`) +* `bob.Truncate(tableName)` - Truncate a table (`truncate "users"`) ### TODO Meaning these are some ideas for the future development of Bob. -* `bob.DropTable(tableName)` - Drop a table (`drop table "users"`) -* `bob.DropTableIfExists(tableName)` - Drop a table if exists (`drop table if exists "users"`) -* `bob.RenameTable(tableName)` - Rename a table (`rename table "users" to "old_users"`) -* `bob.Truncate(tableName)` - Truncate a table (`truncate "users"`) * `bob.Upsert(tableName)` - UPSERT function (`insert into "users" ("name", "email") values (?, ?) on duplicate key update email = ?`) * `bob.ExecWith()` - Just like Squirrel's [ExecWith](https://pkg.go.dev/github.com/Masterminds/squirrel?utm_source=godoc#ExecWith) * `bob.Count(tableName, columnName)` - Count query (`select count("active") from "users"`) diff --git a/bob.go b/bob.go index 47d30f4..d550e09 100644 --- a/bob.go +++ b/bob.go @@ -6,7 +6,9 @@ import ( "github.com/lann/builder" ) +// ErrEmptyTable is a common database/sql error if a table is empty or no rows is returned by the query. var ErrEmptyTable = errors.New("sql: no rows in result set") +// ErrEmptyTable is a common pgx error if a table is empty or no rows is returned by the query. var ErrEmptyTablePgx = errors.New("no rows in result set") // BobBuilderType is the type for BobBuilder @@ -22,6 +24,7 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder { return CreateBuilder(b).Name(table) } +// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists. func (b BobBuilderType) CreateTableIfNotExists(table string) CreateBuilder { return CreateBuilder(b).Name(table).IfNotExists() } @@ -36,18 +39,22 @@ func (b BobBuilderType) HasColumn(column string) HasBuilder { return HasBuilder(b).HasColumn(column) } +// DropTable drops (delete contents & remove) a table from the database. func (b BobBuilderType) DropTable(table string) DropBuilder { return DropBuilder(b).DropTable(table) } +// DropTable drops (delete contents & remove) a table from the database if the table exists. func (b BobBuilderType) DropTableIfExists(table string) DropBuilder { return DropBuilder(b).DropTable(table).IfExists() } +// RenameTable simply renames an exisisting table. func (b BobBuilderType) RenameTable(from, to string) RenameBuilder { return RenameBuilder(b).From(from).To(to) } +// Truncate performs TRUNCATE function. It deletes all contents from a table but not deleting the table. func (b BobBuilderType) Truncate(table string) TruncateBuilder { return TruncateBuilder(b).Truncate(table) } @@ -55,38 +62,42 @@ func (b BobBuilderType) Truncate(table string) TruncateBuilder { // BobStmtBuilder is the parent builder for BobBuilderType var BobStmtBuilder = BobBuilderType(builder.EmptyBuilder) -// CreateTable creates a table with CreateBuilder interface +// CreateTable creates a table with CreateBuilder interface. func CreateTable(table string) CreateBuilder { return BobStmtBuilder.CreateTable(table) } -// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists +// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists. func CreateTableIfNotExists(table string) CreateBuilder { return BobStmtBuilder.CreateTableIfNotExists(table) } -// HasTable checks if a table exists with HasBuilder interface +// HasTable checks if a table exists with HasBuilder interface. func HasTable(table string) HasBuilder { return BobStmtBuilder.HasTable(table) } -// HasColumn checks if a column exists with HasBuilder interface +// HasColumn checks if a column exists with HasBuilder interface. func HasColumn(col string) HasBuilder { return BobStmtBuilder.HasColumn(col) } +// DropTable drops (delete contents & remove) a table from the database. func DropTable(table string) DropBuilder { return BobStmtBuilder.DropTable(table) } +// DropTable drops (delete contents & remove) a table from the database if the table exists. func DropTableIfExists(table string) DropBuilder { return BobStmtBuilder.DropTableIfExists(table) } +// RenameTable simply renames an exisisting table. func RenameTable(from, to string) RenameBuilder { return BobStmtBuilder.RenameTable(from, to) } +// Truncate performs TRUNCATE function. It deletes all contents from a table but not deleting the table. func Truncate(table string) TruncateBuilder { return BobStmtBuilder.Truncate(table) } \ No newline at end of file