mirror of https://github.com/aldy505/bob.git
feat: added has query
This commit is contained in:
parent
807f5af0ba
commit
5c112c73f6
15
append.go
15
append.go
|
@ -2,7 +2,7 @@ package bob
|
||||||
|
|
||||||
import "io"
|
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 {
|
for i, p := range parts {
|
||||||
partSql, partArgs, err := p.ToSql()
|
partSql, partArgs, err := p.ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -26,3 +26,16 @@ func AppendToSql(parts []BobBuilder, w io.Writer, sep string, args []interface{}
|
||||||
}
|
}
|
||||||
return args, nil
|
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
bob.go
16
bob.go
|
@ -12,8 +12,24 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder {
|
||||||
return CreateBuilder(b).Name(table)
|
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)
|
var BobStmtBuilder = BobBuilderType(builder.EmptyBuilder)
|
||||||
|
|
||||||
func CreateTable(table string) CreateBuilder {
|
func CreateTable(table string) CreateBuilder {
|
||||||
return BobStmtBuilder.CreateTable(table)
|
return BobStmtBuilder.CreateTable(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasTable(table string) HasBuilder {
|
||||||
|
return BobStmtBuilder.HasTable(table)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasColumn(col string) HasBuilder {
|
||||||
|
return BobStmtBuilder.HasColumn(col)
|
||||||
|
}
|
||||||
|
|
|
@ -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) {
|
t.Run("should emit error on unmatched column and types length", func(t *testing.T) {
|
||||||
_, _, err := bob.CreateTable("users").
|
_, _, err := bob.CreateTable("users").
|
||||||
Columns("id", "name", "email", "password", "date").
|
Columns("id", "name", "email", "password", "date").
|
||||||
|
|
51
has.go
51
has.go
|
@ -1,6 +1,11 @@
|
||||||
package bob
|
package bob
|
||||||
|
|
||||||
import "github.com/lann/builder"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/lann/builder"
|
||||||
|
)
|
||||||
|
|
||||||
// TODO - The whole file is a todo
|
// TODO - The whole file is a todo
|
||||||
// Meant to find two things: HasTable and HasColumn(s)
|
// Meant to find two things: HasTable and HasColumn(s)
|
||||||
|
@ -9,7 +14,9 @@ type HasBuilder builder.Builder
|
||||||
|
|
||||||
type hasData struct {
|
type hasData struct {
|
||||||
Name string
|
Name string
|
||||||
Placeholder PlaceholderFormat
|
Column string
|
||||||
|
Schema string
|
||||||
|
Placeholder string
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -20,6 +27,44 @@ func (h HasBuilder) HasTable(table string) HasBuilder {
|
||||||
return builder.Set(h, "Name", table).(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)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,7 +1,22 @@
|
||||||
package bob
|
package bob
|
||||||
|
|
||||||
// TODO
|
import "strings"
|
||||||
|
|
||||||
|
const (
|
||||||
|
Question = "?"
|
||||||
|
Dollar = "$"
|
||||||
|
Colon = ":"
|
||||||
|
AtP = "@p"
|
||||||
|
)
|
||||||
|
|
||||||
type PlaceholderFormat interface {
|
type PlaceholderFormat interface {
|
||||||
ReplacePlaceholders(sql string) (string, error)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue