Skip to content

Commit 50c8d98

Browse files
author
Reinaldy Rafli
committed
docs: new features
1 parent c9b01ee commit 50c8d98

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

README.md

+40-5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@ func main() {
8181
}
8282
```
8383

84+
### Drop table
85+
86+
```go
87+
func main() {
88+
sql, _, err := bob.DropTable("users").ToSql()
89+
if err != nil {
90+
log.Fatal(err)
91+
}
92+
}
93+
```
94+
95+
You could also do `bob.DropTableIfExists("users")` to output a `DROP TABLE IF EXISTS "users"` query.
96+
97+
### Truncate table
98+
99+
```go
100+
func main() {
101+
sql, _, err := bob.Truncate("users").ToSql()
102+
if err != nil {
103+
log.Fatal(err)
104+
}
105+
}
106+
```
107+
108+
### Rename table
109+
110+
```go
111+
func main() {
112+
sql, _, err := bob.RenameTable("users", "people").ToSql()
113+
if err != nil {
114+
log.Fatal(err)
115+
}
116+
}
117+
```
118+
84119
### Placeholder format / Dialect
85120

86121
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() {
105140
Available placeholder formats:
106141
* `bob.Question` - `INSERT INTO "users" (name) VALUES (?)`
107142
* `bob.Dollar` - `INSERT INTO "users" (name) VALUES ($1)`
108-
* `bob.Colon` - `INSERT INTO "users" (name) VALUES (:1)`
143+
* `bob.Colon` - `INSERT INTO "users" (name) VALUES (:1)` (Yes, I know this is kinda wrong. I'm thinking of removing it.)
109144
* `bob.AtP` - `INSERT INTO "users" (name) VALUES (@p1)`
110145

111146
### With pgx (PostgreSQL)
@@ -183,15 +218,15 @@ func main() {
183218
* `bob.CreateTableIfNotExists(tableName)` - Create table if not exists
184219
* `bob.HasTable(tableName)` - Checks if column exists (return error if false, check example above for error handling)
185220
* `bob.HasColumn(columnName)` - Check if a column exists on current table
221+
* `bob.DropTable(tableName)` - Drop a table (`drop table "users"`)
222+
* `bob.DropTableIfExists(tableName)` - Drop a table if exists (`drop table if exists "users"`)
223+
* `bob.RenameTable(currentTable, desiredName)` - Rename a table (`rename table "users" to "people"`)
224+
* `bob.Truncate(tableName)` - Truncate a table (`truncate "users"`)
186225

187226
### TODO
188227

189228
Meaning these are some ideas for the future development of Bob.
190229

191-
* `bob.DropTable(tableName)` - Drop a table (`drop table "users"`)
192-
* `bob.DropTableIfExists(tableName)` - Drop a table if exists (`drop table if exists "users"`)
193-
* `bob.RenameTable(tableName)` - Rename a table (`rename table "users" to "old_users"`)
194-
* `bob.Truncate(tableName)` - Truncate a table (`truncate "users"`)
195230
* `bob.Upsert(tableName)` - UPSERT function (`insert into "users" ("name", "email") values (?, ?) on duplicate key update email = ?`)
196231
* `bob.ExecWith()` - Just like Squirrel's [ExecWith](https://pkg.go.dev/github.com/Masterminds/squirrel?utm_source=godoc#ExecWith)
197232
* `bob.Count(tableName, columnName)` - Count query (`select count("active") from "users"`)

bob.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"github.com/lann/builder"
77
)
88

9+
// ErrEmptyTable is a common database/sql error if a table is empty or no rows is returned by the query.
910
var ErrEmptyTable = errors.New("sql: no rows in result set")
11+
// ErrEmptyTable is a common pgx error if a table is empty or no rows is returned by the query.
1012
var ErrEmptyTablePgx = errors.New("no rows in result set")
1113

1214
// BobBuilderType is the type for BobBuilder
@@ -22,6 +24,7 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder {
2224
return CreateBuilder(b).Name(table)
2325
}
2426

27+
// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists.
2528
func (b BobBuilderType) CreateTableIfNotExists(table string) CreateBuilder {
2629
return CreateBuilder(b).Name(table).IfNotExists()
2730
}
@@ -36,57 +39,65 @@ func (b BobBuilderType) HasColumn(column string) HasBuilder {
3639
return HasBuilder(b).HasColumn(column)
3740
}
3841

42+
// DropTable drops (delete contents & remove) a table from the database.
3943
func (b BobBuilderType) DropTable(table string) DropBuilder {
4044
return DropBuilder(b).DropTable(table)
4145
}
4246

47+
// DropTable drops (delete contents & remove) a table from the database if the table exists.
4348
func (b BobBuilderType) DropTableIfExists(table string) DropBuilder {
4449
return DropBuilder(b).DropTable(table).IfExists()
4550
}
4651

52+
// RenameTable simply renames an exisisting table.
4753
func (b BobBuilderType) RenameTable(from, to string) RenameBuilder {
4854
return RenameBuilder(b).From(from).To(to)
4955
}
5056

57+
// Truncate performs TRUNCATE function. It deletes all contents from a table but not deleting the table.
5158
func (b BobBuilderType) Truncate(table string) TruncateBuilder {
5259
return TruncateBuilder(b).Truncate(table)
5360
}
5461

5562
// BobStmtBuilder is the parent builder for BobBuilderType
5663
var BobStmtBuilder = BobBuilderType(builder.EmptyBuilder)
5764

58-
// CreateTable creates a table with CreateBuilder interface
65+
// CreateTable creates a table with CreateBuilder interface.
5966
func CreateTable(table string) CreateBuilder {
6067
return BobStmtBuilder.CreateTable(table)
6168
}
6269

63-
// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists
70+
// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists.
6471
func CreateTableIfNotExists(table string) CreateBuilder {
6572
return BobStmtBuilder.CreateTableIfNotExists(table)
6673
}
6774

68-
// HasTable checks if a table exists with HasBuilder interface
75+
// HasTable checks if a table exists with HasBuilder interface.
6976
func HasTable(table string) HasBuilder {
7077
return BobStmtBuilder.HasTable(table)
7178
}
7279

73-
// HasColumn checks if a column exists with HasBuilder interface
80+
// HasColumn checks if a column exists with HasBuilder interface.
7481
func HasColumn(col string) HasBuilder {
7582
return BobStmtBuilder.HasColumn(col)
7683
}
7784

85+
// DropTable drops (delete contents & remove) a table from the database.
7886
func DropTable(table string) DropBuilder {
7987
return BobStmtBuilder.DropTable(table)
8088
}
8189

90+
// DropTable drops (delete contents & remove) a table from the database if the table exists.
8291
func DropTableIfExists(table string) DropBuilder {
8392
return BobStmtBuilder.DropTableIfExists(table)
8493
}
8594

95+
// RenameTable simply renames an exisisting table.
8696
func RenameTable(from, to string) RenameBuilder {
8797
return BobStmtBuilder.RenameTable(from, to)
8898
}
8999

100+
// Truncate performs TRUNCATE function. It deletes all contents from a table but not deleting the table.
90101
func Truncate(table string) TruncateBuilder {
91102
return BobStmtBuilder.Truncate(table)
92103
}

0 commit comments

Comments
 (0)