From 8f7868b4a35e69520770b0e30c07a3b16d6c6a40 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <jose.carlos@menteslibres.net>
Date: Sat, 7 Jun 2014 14:28:23 -0500
Subject: [PATCH] Adding test for select star.

---
 util/sqlgen/{fields.go => columns.go} | 13 ++++++++-----
 util/sqlgen/main.go                   | 14 ++++++++------
 util/sqlgen/main_test.go              | 17 +++++++++++++++++
 3 files changed, 33 insertions(+), 11 deletions(-)
 rename util/sqlgen/{fields.go => columns.go} (50%)

diff --git a/util/sqlgen/fields.go b/util/sqlgen/columns.go
similarity index 50%
rename from util/sqlgen/fields.go
rename to util/sqlgen/columns.go
index ab8b063b..90c66717 100644
--- a/util/sqlgen/fields.go
+++ b/util/sqlgen/columns.go
@@ -9,13 +9,16 @@ type Columns struct {
 }
 
 func (self Columns) String() string {
-	out := make([]string, len(self.v))
+	if len(self.v) > 0 {
+		out := make([]string, len(self.v))
 
-	for i := range self.v {
-		out[i] = self.v[i].String()
-	}
+		for i := range self.v {
+			out[i] = self.v[i].String()
+		}
 
-	return strings.Join(out, sqlColumnComma)
+		return strings.Join(out, sqlColumnComma)
+	}
+	return ""
 }
 
 func (self Columns) Len() int {
diff --git a/util/sqlgen/main.go b/util/sqlgen/main.go
index f8ed4017..a67351bc 100644
--- a/util/sqlgen/main.go
+++ b/util/sqlgen/main.go
@@ -18,7 +18,13 @@ const (
 		{{end}}
 	`
 	sqlSelectLayout = `
-		SELECT {{.Columns}}
+		SELECT
+
+			{{if .Columns}}
+				{{.Columns}}
+			{{else}}
+				*
+			{{end}}
 
 			FROM {{.Source}}
 
@@ -80,18 +86,14 @@ const (
 		DROP TABLE {{.Source}}
 	`
 
-	sqlTautology       = `1 = 1`
-	sqlAllFields       = `*`
 	sqlAndKeyword      = `AND`
 	sqlOrKeyword       = `OR`
-	sqlDefaultOperator = `=`
 	sqlDescKeyword     = `DESC`
 	sqlAscKeyword      = `ASC`
+	sqlDefaultOperator = `=`
 	sqlConditionGroup  = `({{.}})`
 
 	sqlColumnValue = `{{.Column}} {{.Operator}} {{.Value}}`
-
-	sqlFunction = `{{.Function}}({{.Value}})`
 )
 
 type Type uint
diff --git a/util/sqlgen/main_test.go b/util/sqlgen/main_test.go
index 43d3ef68..acfc058b 100644
--- a/util/sqlgen/main_test.go
+++ b/util/sqlgen/main_test.go
@@ -103,6 +103,23 @@ func TestSelectCountWhere(t *testing.T) {
 	}
 }
 
+func TestSelectStarFrom(t *testing.T) {
+	var s, e string
+	var stmt Statement
+
+	stmt = Statement{
+		Type:   SqlSelect,
+		Source: Source{"source name"},
+	}
+
+	s = trim(stmt.Compile())
+	e = `SELECT * FROM "source name"`
+
+	if s != e {
+		t.Fatalf("Got: %s, Expecting: %s", s, e)
+	}
+}
+
 func TestSelectFieldsFrom(t *testing.T) {
 	var s, e string
 	var stmt Statement
-- 
GitLab