~abyxcos/sqlh

7cfcd260e96282d716a1a089e645184c95a9ca46 — abyxcos 2 years ago 09e5df2 master
Add SELECT query support.
2 files changed, 26 insertions(+), 1 deletions(-)

M sqlh.go
M sqlh_test.go
M sqlh.go => sqlh.go +12 -1
@@ 37,6 37,17 @@ var SkipFields = "ID"



// BuildSelect is a helper function that returns all the variables needed for a SELECT query in one call. BuildSelect returns the column names. BuildSelect automatically passes SkipFields to the Columns() call.
//
//		columns := sqlh.BuildSelect(person)
//		query := fmt.Sprintf("SELECT (%s) FROM people", columns)
//		row, err := db.Query(query)
//
//		`SELECT (first_name, last_name, eMail) FROM people`
func BuildSelect(i interface{}) string {
	return Columns(i)
}

// BuildInsert is a helper function that returns all the variables needed for an INSERT query in one call. BuildInsert returns the column names, the parameter placeholder string, and the values. BuildInsert automatically passes SkipFields to the Columns() call.
//
// 	columns, params, values := sqlh.BuildInsert(person)


@@ 168,7 179,7 @@ ParseLoop:
	return paramCount
}

// Values walks through a struct's fields and returns their values in a format suitable for passing to database/sqlwhich does not believe structs exist. The values []interface{} returned by Values is suitable for passing directly in to database/sql's args ...interface{} parameter.
// Values walks through a struct's fields and returns their values in a format suitable for passing to database/sql, which does not believe structs exist. The values []interface{} returned by Values is suitable for passing directly in to database/sql's args ...interface{} parameter.
func Values(i interface{}) []interface{} {
	return getValues(i)
}

M sqlh_test.go => sqlh_test.go +14 -0
@@ 37,6 37,20 @@ func makeTestVar() Test {
	}
}

func TestBuildSelect(t *testing.T) {
	FormatChar = "$"
	FormatCounter = true
	SkipFields = "ID, C,Name"

	t.Log("Testing Select")
	columns := BuildSelect(makeTestVar())

	if columns != "count, a, b, TEST3, db, api_test_func" {
		t.Log("Columns are wrong")
		t.Fail()
	}
}

func TestBuildInsert(t *testing.T) {
	FormatChar = "$"
	FormatCounter = true