Skip to content

Commit

Permalink
manual merge
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascamata committed Mar 25, 2012
2 parents c5cc0a3 + a1cc0d6 commit e0eb8f8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
39 changes: 38 additions & 1 deletion sqlgen/sqlgen.go
Expand Up @@ -22,7 +22,7 @@ func Insert(obj interface{}) string {
t := reflect.TypeOf(obj)
fieldNames := fieldNames(t)

qm := make([]string, len(fieldNames)) // supply the question marks for the sql stmt
qm := make([]string, len(fieldNames))
for i := 0; i < len(qm); i++ {
qm[i] = "?"
}
Expand All @@ -41,6 +41,43 @@ func Delete(obj interface{}, filters []string) string {
return sql
}

// obj is the struct to be updated in the database
// uFields are the fields that are gonna be update
// fFields are the fields that are gonna be used as filter
// to the where clause
func Update(obj interface{}, uFields, fFields []string) string {
t := reflect.TypeOf(obj)
s := reflect.ValueOf(obj)

fieldsAndValues := fieldValues(s, uFields)
filters := fieldValues(s, fFields)

sql := fmt.Sprintf("update %s %s where %s", strings.ToLower(t.Name()), strings.Join(fieldsAndValues, ", "), strings.Join(filters, ", "))

return sql
}

// Receives a reflect.Value and the fields you want form the struct
// returns the respective values from the fields passed in the form of
// field=value, if value is a string, add " around it
func fieldValues(s reflect.Value, fields []string) []string {
fieldValues := make([]string, len(fields))

for i, v := range fields {
f := s.FieldByName(strings.Title(v))

var stmt string
if f.Type().Kind() == reflect.String {
stmt = fmt.Sprintf(`%s="%v"`, strings.ToLower(v), f.Interface())
} else {
stmt = fmt.Sprintf("%s=%v", strings.ToLower(v), f.Interface())
}
fieldValues[i] = stmt
}

return fieldValues
}

func fieldNames(t reflect.Type) []string {
fieldNames := make([]string, t.NumField())

Expand Down
29 changes: 25 additions & 4 deletions sqlgen/sqlgen_test.go
Expand Up @@ -10,22 +10,33 @@ import (
)

type Person struct {
Id int
Name string
Age int
}

func TestFieldNames(t *testing.T) {
var p Person
expected := []string{"Name", "Age"}
expected := []string{"Id", "Name", "Age"}
got := fieldNames(reflect.TypeOf(p))
if !reflect.DeepEqual(expected, got) {
t.Errorf("Expected %q. Got %q.", expected, got)
}
}

func TestFieldValues(t *testing.T) {
p := Person{Id: 1, Name: "Umi", Age: 6}
expected := []string{"id=1", `name="Umi"`, "age=6"}
got := fieldValues(reflect.ValueOf(p), []string{"id", "name", "age"})

if !reflect.DeepEqual(expected, got) {
t.Errorf("Expected %q. Got %q.", expected, got)
}
}

func TestGenerateSelectFromStruct(t *testing.T) {
var p Person
expected := "select name, age from person"
expected := "select id, name, age from person"
got := Select(p)
if expected != got {
t.Errorf(`SELECT generation for %q. Was expecting "%s", got %s.`, reflect.TypeOf(p), expected, got)
Expand All @@ -34,18 +45,28 @@ func TestGenerateSelectFromStruct(t *testing.T) {

func TestGenerateInsertFromStruct(t *testing.T) {
var p Person
expected := "insert into person (name, age) values (?, ?)"
expected := "insert into person (id, name, age) values (?, ?, ?)"
got := Insert(p)
if expected != got {
t.Errorf(`INSERT generation for %q. Was expecting "%s", got %s.`, reflect.TypeOf(p), expected, got)
}
}

func TestSimpleDeleteFromStruct(t *testing.T) {
p := Person{"Chuck", 32}
p := Person{1, "Chuck", 32}
expected := "delete from person where name = Chuck"
got := Delete(p, []string{"Name"})
if expected != got {
t.Errorf(`DELETE generation for %q. Was expecting "%s", got %s.`, reflect.TypeOf(p), expected, got)
}
}

func TestGenerateUpdateFromStruct(t *testing.T) {
p := Person{Id: 1, Name: "Umi", Age: 6}
expected := `update person name="Umi", age=6 where id=1`
got := Update(p, []string{"name", "age"}, []string{"id"})

if expected != got {
t.Errorf(`UPDATE generation for %q. Was expecting "%s", got %s.`, reflect.TypeOf(p), expected, got)
}
}

0 comments on commit e0eb8f8

Please sign in to comment.