Skip to content

Commit

Permalink
remove unnecessary files
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Apr 9, 2022
1 parent d73d1e5 commit ce5057c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 108 deletions.
14 changes: 0 additions & 14 deletions helper_test.go

This file was deleted.

22 changes: 0 additions & 22 deletions helpers.go

This file was deleted.

41 changes: 0 additions & 41 deletions insert.go

This file was deleted.

31 changes: 0 additions & 31 deletions insert_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,56 @@ func Raw(sql string, args ...interface{}) *raw {
func NewQueryBuilder[OUTPUT any](s *schema) *QueryBuilder[OUTPUT] {
return &QueryBuilder[OUTPUT]{schema: s}
}

type insertStmt struct {
PlaceHolderGenerator func(n int) []string
Table string
Columns []string
Values [][]interface{}
}

func (i insertStmt) flatValues() []interface{} {
var values []interface{}
for _, row := range i.Values {
values = append(values, row...)
}
return values
}

func (i insertStmt) getValuesStr() string {
phs := i.PlaceHolderGenerator(len(i.Values) * len(i.Values[0]))

var output []string
for _, valueRow := range i.Values {
output = append(output, fmt.Sprintf("(%s)", strings.Join(phs[:len(valueRow)], ",")))
phs = phs[len(valueRow):]
}
return strings.Join(output, ",")
}

func (i insertStmt) ToSql() (string, []interface{}) {
base := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s",
i.Table,
strings.Join(i.Columns, ","),
i.getValuesStr(),
)
return base, i.flatValues()
}


func postgresPlaceholder(n int) []string {
output := []string{}
for i := 1; i < n+1; i++ {
output = append(output, fmt.Sprintf("$%d", i))
}
return output
}

func questionMarks(n int) []string {
output := []string{}
for i := 0; i < n; i++ {
output = append(output, "?")
}

return output
}
31 changes: 31 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,34 @@ func TestDelete(t *testing.T) {
assert.EqualValues(t, []interface{}{"2012-01-10"}, args)
})
}

func TestInsert(t *testing.T) {
t.Run("insert into multiple rows", func(t *testing.T) {
i := insertStmt{}
i.Table = "users"
i.PlaceHolderGenerator = Dialects.MySQL.PlaceHolderGenerator
i.Columns = []string{"name", "age"}
i.Values = append(i.Values, []interface{}{"amirreza", 11}, []interface{}{"parsa", 10})
s, args := i.ToSql()
assert.Equal(t, `INSERT INTO users (name,age) VALUES (?,?),(?,?)`, s)
assert.EqualValues(t, []interface{}{"amirreza", 11, "parsa", 10}, args)
})

t.Run("insert into single row", func(t *testing.T) {
i := insertStmt{}
i.Table = "users"
i.PlaceHolderGenerator = Dialects.MySQL.PlaceHolderGenerator
i.Columns = []string{"name", "age"}
i.Values = append(i.Values, []interface{}{"amirreza", 11})
s, args := i.ToSql()
assert.Equal(t, `INSERT INTO users (name,age) VALUES (?,?)`, s)
assert.Equal(t, []interface{}{"amirreza", 11}, args)
})
}

func TestPostgresPlaceholder(t *testing.T) {
t.Run("for 5 it should have 5", func(t *testing.T) {
phs := postgresPlaceholder(5)
assert.EqualValues(t, []string{"$1", "$2", "$3", "$4", "$5"}, phs)
})
}

0 comments on commit ce5057c

Please sign in to comment.