Skip to content

Commit

Permalink
test: refactor & running fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Jul 31, 2021
1 parent 4a02222 commit b5df4ae
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 143 deletions.
8 changes: 5 additions & 3 deletions bob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

// 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")

// ErrDialectNotSupported tells you whether the dialect is supported or not.
var ErrDialectNotSupported = errors.New("provided database dialect is not supported")

Expand Down Expand Up @@ -147,7 +149,7 @@ func Truncate(table string) TruncateBuilder {
// Replace("age", 25).
// PlaceholderFormat(bob.Question).
// ToSql()
//
//
// // Another example for PostgreSQL:
// sql, args, err = bob.
// Upsert("users", bob.PostgreSQL).
Expand All @@ -157,7 +159,7 @@ func Truncate(table string) TruncateBuilder {
// Replace("age", 40).
// PlaceholderFormat(bob.Dollar).
// ToSql()
//
//
// // One more time, for MSSQL / SQL Server:
// sql, args, err = bob.
// Upsert("users", bob.MSSQL).
Expand All @@ -169,4 +171,4 @@ func Truncate(table string) TruncateBuilder {
// ToSql()
func Upsert(table string, dialect int) UpsertBuilder {
return BobStmtBuilder.Upsert(table, dialect)
}
}
8 changes: 4 additions & 4 deletions drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type DropBuilder builder.Builder

type dropData struct {
TableName string
IfExists bool
IfExists bool
}

func init() {
Expand Down Expand Up @@ -45,9 +45,9 @@ func (d *dropData) ToSql() (sqlStr string, args []interface{}, err error) {
if d.IfExists {
sql.WriteString("IF EXISTS ")
}
sql.WriteString("\""+d.TableName+"\";")

sql.WriteString("\"" + d.TableName + "\";")

sqlStr = sql.String()
return
}
}
4 changes: 2 additions & 2 deletions drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestDrop(t *testing.T) {
t.Run("should be able to create regular drop query", func (t *testing.T) {
t.Run("should be able to create regular drop query", func(t *testing.T) {
sql, _, err := bob.DropTable("users").ToSql()
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -37,4 +37,4 @@ func TestDrop(t *testing.T) {
t.Error(err)
}
})
}
}
2 changes: 0 additions & 2 deletions has_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/aldy505/bob"
)

// TODO - do more test

func TestHas(t *testing.T) {
t.Run("should be able to create a hasTable query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").ToSql()
Expand Down
6 changes: 3 additions & 3 deletions rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type RenameBuilder builder.Builder

type renameData struct {
From string
To string
To string
}

func init() {
Expand Down Expand Up @@ -38,6 +38,6 @@ func (d *renameData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.From) == 0 || d.From == "" || len(d.To) == 0 || d.To == "" {
err = errors.New("rename statement must specify a table")
}
sqlStr = "RENAME TABLE \""+d.From+"\" TO \""+d.To+"\";"
sqlStr = "RENAME TABLE \"" + d.From + "\" TO \"" + d.To + "\";"
return
}
}
4 changes: 2 additions & 2 deletions rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestRename(t *testing.T) {
t.Run("should be able to create rename query", func (t *testing.T) {
t.Run("should be able to create rename query", func(t *testing.T) {
sql, _, err := bob.RenameTable("users", "teachers").ToSql()
if err != nil {
t.Error(err)
Expand All @@ -25,4 +25,4 @@ func TestRename(t *testing.T) {
t.Error(err)
}
})
}
}
4 changes: 2 additions & 2 deletions truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ func (d *truncateData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("truncate statement must specify a table")
}
sqlStr = "TRUNCATE \""+d.TableName+"\";"
sqlStr = "TRUNCATE \"" + d.TableName + "\";"
return
}
}
4 changes: 2 additions & 2 deletions truncate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestTruncate(t *testing.T) {
t.Run("should be able to create truncate query", func (t *testing.T) {
t.Run("should be able to create truncate query", func(t *testing.T) {
sql, _, err := bob.Truncate("users").ToSql()
if err != nil {
t.Error(err)
Expand All @@ -25,4 +25,4 @@ func TestTruncate(t *testing.T) {
t.Error(err)
}
})
}
}
54 changes: 27 additions & 27 deletions upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
type UpsertBuilder builder.Builder

type upsertData struct {
Dialect int
Into string
Columns []string
Values [][]interface{}
Key []interface{}
Replace [][]interface{}
Dialect int
Into string
Columns []string
Values [][]interface{}
Key []interface{}
Replace [][]interface{}
Placeholder string
}

Expand Down Expand Up @@ -60,7 +60,7 @@ func (u UpsertBuilder) Key(key ...interface{}) UpsertBuilder {
}

// Replace sets the column and value respectively for the data to be changed on
// a specific row.
// a specific row.
func (u UpsertBuilder) Replace(column interface{}, value interface{}) UpsertBuilder {
return builder.Append(u, "Replace", []interface{}{column, value}).(UpsertBuilder)
}
Expand Down Expand Up @@ -105,13 +105,13 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
err = errors.New("unique key and value must be provided for MS SQL")
return
}
sql.WriteString("IF NOT EXISTS (SELECT * FROM \""+d.Into+"\" WHERE \""+d.Key[0].(string)+"\" = ?) ")

sql.WriteString("IF NOT EXISTS (SELECT * FROM \"" + d.Into + "\" WHERE \"" + d.Key[0].(string) + "\" = ?) ")
args = append(args, d.Key[1])
}

sql.WriteString("INSERT INTO ")
sql.WriteString("\""+d.Into+"\"")
sql.WriteString("\"" + d.Into + "\"")
sql.WriteString(" ")

var columns []string
Expand All @@ -134,7 +134,7 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
}
values = append(values, "("+strings.Join(tempValues, ", ")+")")
}

sql.WriteString(strings.Join(values, ", "))
sql.WriteString(" ")

Expand All @@ -147,47 +147,47 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {

if d.Dialect == MySQL {
// INSERT INTO table (col) VALUES (values) ON DUPLICATE KEY UPDATE col = value

sql.WriteString("ON DUPLICATE KEY UPDATE ")
sql.WriteString(strings.Join(replaces, ", "))
} else if d.Dialect == PostgreSQL || d.Dialect == SQLite {
// INSERT INTO players (user_name, age) VALUES('steven', 32) ON CONFLICT(user_name) DO UPDATE SET age=excluded.age;

if len(d.Key) == 0 {
err = errors.New("unique key must be provided for PostgreSQL and SQLite")
return
}

sql.WriteString("ON CONFLICT ")
sql.WriteString("(\""+d.Key[0].(string)+"\") ")
sql.WriteString("(\"" + d.Key[0].(string) + "\") ")
sql.WriteString("DO UPDATE SET ")
sql.WriteString(strings.Join(replaces, ", "))

} else if d.Dialect == MSSQL {
// IF NOT EXISTS (SELECT * FROM dbo.Table1 WHERE ID = @ID)
// INSERT INTO dbo.Table1(ID, Name, ItemName, ItemCatName, ItemQty)
// VALUES(@ID, @Name, @ItemName, @ItemCatName, @ItemQty)
// ELSE
// UPDATE dbo.Table1
// SET Name = @Name,
// ItemName = @ItemName,
// ItemCatName = @ItemCatName,
// ItemQty = @ItemQty
// WHERE ID = @ID
// INSERT INTO dbo.Table1(ID, Name, ItemName, ItemCatName, ItemQty)
// VALUES(@ID, @Name, @ItemName, @ItemCatName, @ItemQty)
// ELSE
// UPDATE dbo.Table1
// SET Name = @Name,
// ItemName = @ItemName,
// ItemCatName = @ItemCatName,
// ItemQty = @ItemQty
// WHERE ID = @ID

sql.WriteString("ELSE ")
sql.WriteString("UPDATE \""+d.Into+"\" SET ")
sql.WriteString("UPDATE \"" + d.Into + "\" SET ")
sql.WriteString(strings.Join(replaces, ", "))
sql.WriteString(" WHERE \""+d.Key[0].(string)+"\" = ?")
sql.WriteString(" WHERE \"" + d.Key[0].(string) + "\" = ?")
args = append(args, d.Key[1])

} else {
err = ErrDialectNotSupported
return
}

sql.WriteString(";")

sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
return
}
}

0 comments on commit b5df4ae

Please sign in to comment.