Skip to content

Commit

Permalink
feat: added has query
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Jun 25, 2021
1 parent 807f5af commit 5c112c7
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 5 deletions.
15 changes: 14 additions & 1 deletion append.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bob

import "io"

func AppendToSql(parts []BobBuilder, w io.Writer, sep string, args []interface{}) ([]interface{}, error) {
func appendToSql(parts []BobBuilder, w io.Writer, sep string, args []interface{}) ([]interface{}, error) {
for i, p := range parts {
partSql, partArgs, err := p.ToSql()
if err != nil {
Expand All @@ -26,3 +26,16 @@ func AppendToSql(parts []BobBuilder, w io.Writer, sep string, args []interface{}
}
return args, nil
}

// createArgs should create an argument []interface{} for SQL query
// I'm using the idiot approach for creating args
func createArgs(keys ...string) []interface{} {
var args []interface{}
for _, v := range keys {
if v == "" {
continue
}
args = append(args, v)
}
return args
}
16 changes: 16 additions & 0 deletions bob.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,24 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder {
return CreateBuilder(b).Name(table)
}

func (b BobBuilderType) HasTable(table string) HasBuilder {
return HasBuilder(b).HasTable(table)
}

func (b BobBuilderType) HasColumn(column string) HasBuilder {
return HasBuilder(b).HasColumn(column)
}

var BobStmtBuilder = BobBuilderType(builder.EmptyBuilder)

func CreateTable(table string) CreateBuilder {
return BobStmtBuilder.CreateTable(table)
}

func HasTable(table string) HasBuilder {
return BobStmtBuilder.HasTable(table)
}

func HasColumn(col string) HasBuilder {
return BobStmtBuilder.HasColumn(col)
}
11 changes: 11 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func TestCreate(t *testing.T) {
}
})

t.Run("should be able to have a schema name", func(t *testing.T) {
sql, _, err := bob.CreateTable("users").WithSchema("private").Columns("name", "password", "date").Types("varchar(255)", "text", "date").ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "CREATE TABLE `private`.`users` (`name` varchar(255), `password` text, `date` date);"
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
})

t.Run("should emit error on unmatched column and types length", func(t *testing.T) {
_, _, err := bob.CreateTable("users").
Columns("id", "name", "email", "password", "date").
Expand Down
51 changes: 48 additions & 3 deletions has.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package bob

import "github.com/lann/builder"
import (
"bytes"
"errors"

"github.com/lann/builder"
)

// TODO - The whole file is a todo
// Meant to find two things: HasTable and HasColumn(s)
Expand All @@ -9,7 +14,9 @@ type HasBuilder builder.Builder

type hasData struct {
Name string
Placeholder PlaceholderFormat
Column string
Schema string
Placeholder string
}

func init() {
Expand All @@ -20,6 +27,44 @@ func (h HasBuilder) HasTable(table string) HasBuilder {
return builder.Set(h, "Name", table).(HasBuilder)
}

func (h HasBuilder) PlaceholderFormat(f PlaceholderFormat) HasBuilder {
func (h HasBuilder) HasColumn(column string) HasBuilder {
return builder.Set(h, "Column", column).(HasBuilder)
}

func (h HasBuilder) WithSchema(schema string) HasBuilder {
return builder.Set(h, "Schema", schema).(HasBuilder)
}

func (h HasBuilder) PlaceholderFormat(f string) HasBuilder {
return builder.Set(h, "Placeholder", f).(HasBuilder)
}

func (h HasBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(h).(hasData)
return data.ToSql()
}

func (d *hasData) ToSql() (sqlStr string, args []interface{}, err error) {
sql := &bytes.Buffer{}
if d.Name == "" {
err = errors.New("has statement should have a table name")
return
}

if d.Column != "" && d.Name != "" {
// search for column
sql.WriteString("SELECT * FROM information_schema.columns WHERE table_name = ? AND column_name = ?")
} else if d.Name != "" && d.Column == "" {
sql.WriteString("SELECT * FROM information_schema.tables WHERE table_name = ?")
}

if d.Schema != "" {
sql.WriteString(" AND table_schema = ?;")
} else {
sql.WriteString(" AND table_schema = current_schema();")
}

sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
args = createArgs(d.Name, d.Column, d.Schema)
return
}
43 changes: 43 additions & 0 deletions has_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bob_test

import (
"testing"

"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()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}

if len(args) != 1 {
t.Fatal("args is not equal with argsResult:", args)
}
})

t.Run("should be able to create a hasColumn query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.columns WHERE table_name = ? AND column_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}

if len(args) != 2 {
t.Fatal("args is not equal with argsResult:", args)
}
})
}
17 changes: 16 additions & 1 deletion placeholder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package bob

// TODO
import "strings"

const (
Question = "?"
Dollar = "$"
Colon = ":"
AtP = "@p"
)

type PlaceholderFormat interface {
ReplacePlaceholders(sql string) (string, error)
}

// TODO - test this one
func ReplacePlaceholder(sql string, format string) string {
if format == "" {
format = Question
}
return strings.ReplaceAll(sql, "?", format)
}

0 comments on commit 5c112c7

Please sign in to comment.