Skip to content

Commit

Permalink
Fixing conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
flavianmissi committed Mar 25, 2012
2 parents 4f6a127 + 2ed6410 commit 8b6bc59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
18 changes: 18 additions & 0 deletions README
@@ -0,0 +1,18 @@
gothic

Experimental database abstraction layer for Go. Works with last weekly (Go 1 RC2).

Collaborate

Make sure you set your GOPATH environment variable [1], then run:

% mkdir -p ${GOPATH}/src/github.com/cobrateam/
% cd ${GOPATH}/src/github.com/cobrateam
% git clone <your-clone-url>

After cloning, you can use the go tool to run tests:

% cd ${GOPATH}/src/github.com/cobrateam/gothic
% go test ./...

[1] http://weekly.golang.org/cmd/go/#GOPATH_environment_variable
16 changes: 8 additions & 8 deletions sqlgen/sqlgen.go
Expand Up @@ -12,15 +12,15 @@ import (

func Select(obj interface{}) string {
t := reflect.TypeOf(obj)
fieldNames := FieldNames(t)
fieldNames := fieldNames(t)

sql := fmt.Sprintf("select %s from %s", strings.Join(fieldNames, ", "), t.Name())
return strings.ToLower(sql)
}

func Insert(obj interface{}) string {
t := reflect.TypeOf(obj)
fieldNames := FieldNames(t)
fieldNames := fieldNames(t)

qm := make([]string, len(fieldNames))
for i := 0; i < len(qm); i++ {
Expand All @@ -39,8 +39,8 @@ func Update(obj interface{}, uFields, fFields []string) string {
t := reflect.TypeOf(obj)
s := reflect.ValueOf(obj)

fieldsAndValues := FieldValues(s, uFields)
filters := FieldValues(s, fFields)
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, ", "))

Expand All @@ -50,7 +50,7 @@ func Update(obj interface{}, uFields, fFields []string) string {
// 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 {
func fieldValues(s reflect.Value, fields []string) []string {
fieldValues := make([]string, len(fields))

for i, v := range fields {
Expand All @@ -68,11 +68,11 @@ func FieldValues(s reflect.Value, fields []string) []string {
return fieldValues
}

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

for i := 0; i < t.NumField(); i++ {
fieldNames = append(fieldNames, t.Field(i).Name)
fieldNames[i] = t.Field(i).Name
}

return fieldNames
Expand Down
12 changes: 10 additions & 2 deletions sqlgen/sqlgen_test.go
Expand Up @@ -2,12 +2,11 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sqlgen_test
package sqlgen

import (
"reflect"
"testing"
. "github.com/cobrateam/gothic/sqlgen"
)

type Person struct {
Expand All @@ -16,6 +15,15 @@ type Person struct {
Age int
}

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

func TestGenerateSelectFromStruct(t *testing.T) {
var p Person
expected := "select id, name, age from person"
Expand Down

0 comments on commit 8b6bc59

Please sign in to comment.