From a265e79f3ca2cea9f2c84a8e6dd5eae6d6be451f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <jose.carlos@menteslibres.net>
Date: Sun, 22 Jun 2014 18:23:51 -0500
Subject: [PATCH] MySQL: Upgrading MySQL to meet latest sqlutil fixes.

---
 postgresql/collection.go |  6 +++---
 postgresql/database.go   | 42 ++++------------------------------------
 postgresql/result.go     |  9 ++++++---
 3 files changed, 13 insertions(+), 44 deletions(-)

diff --git a/postgresql/collection.go b/postgresql/collection.go
index 04beea90..892b69d0 100644
--- a/postgresql/collection.go
+++ b/postgresql/collection.go
@@ -236,9 +236,9 @@ func (self *Table) Append(item interface{}) (interface{}, error) {
 
 	var extra string
 
-	if _, ok := self.ColumnTypes[self.PrimaryKey]; ok == true {
-		extra = fmt.Sprintf(`RETURNING %s`, self.PrimaryKey)
-	}
+	//if _, ok := self.ColumnTypes[self.PrimaryKey]; ok == true {
+	//	extra = fmt.Sprintf(`RETURNING %s`, self.PrimaryKey)
+	//}
 
 	row, err := self.source.doQueryRow(sqlgen.Statement{
 		Type:    sqlgen.SqlInsert,
diff --git a/postgresql/database.go b/postgresql/database.go
index 25672ed8..a57fd2fb 100644
--- a/postgresql/database.go
+++ b/postgresql/database.go
@@ -25,7 +25,6 @@ import (
 	"database/sql"
 	"fmt"
 	"os"
-	"reflect"
 	"regexp"
 	"strings"
 
@@ -60,8 +59,7 @@ type Source struct {
 }
 
 type columnSchema_t struct {
-	ColumnName string `db:"column_name"`
-	DataType   string `db:"data_type"`
+	Name string `db:"column_name"`
 }
 
 func debugEnabled() bool {
@@ -398,8 +396,6 @@ func (self *Source) Collection(names ...string) (db.Collection, error) {
 		names:  names,
 	}
 
-	col.PrimaryKey = `id`
-
 	columns_t := []columnSchema_t{}
 
 	for _, name := range names {
@@ -430,44 +426,14 @@ func (self *Source) Collection(names ...string) (db.Collection, error) {
 				return nil, err
 			}
 
-			if err = col.FetchRows(&columns_t, rows); err != nil {
+			if err = sqlutil.FetchRows(rows, &columns_t); err != nil {
 				return nil, err
 			}
 
-			col.ColumnTypes = make(map[string]reflect.Kind, len(columns_t))
+			col.Columns = make([]string, 0, len(columns_t))
 
 			for _, column := range columns_t {
-
-				column.ColumnName = strings.ToLower(column.ColumnName)
-				column.DataType = strings.ToLower(column.DataType)
-
-				results := columnPattern.FindStringSubmatch(column.DataType)
-
-				// Default properties.
-				dextra := ""
-				dtype := `varchar`
-
-				dtype = results[1]
-
-				if len(results) > 3 {
-					dextra = results[3]
-				}
-
-				ctype := reflect.String
-
-				// Guessing datatypes.
-				switch dtype {
-				case `smallint`, `integer`, `bigint`, `serial`, `bigserial`:
-					if dextra == `unsigned` {
-						ctype = reflect.Uint64
-					} else {
-						ctype = reflect.Int64
-					}
-				case `real`, `double`:
-					ctype = reflect.Float64
-				}
-
-				col.ColumnTypes[column.ColumnName] = ctype
+				col.Columns = append(col.Columns, strings.ToLower(column.Name))
 			}
 
 		}
diff --git a/postgresql/result.go b/postgresql/result.go
index ea8cb0d9..5976113b 100644
--- a/postgresql/result.go
+++ b/postgresql/result.go
@@ -28,6 +28,7 @@ import (
 
 	"upper.io/db"
 	"upper.io/db/util/sqlgen"
+	"upper.io/db/util/sqlutil"
 )
 
 type counter_t struct {
@@ -148,7 +149,7 @@ func (self *Result) All(dst interface{}) error {
 	defer self.Close()
 
 	// Fetching all results within the cursor.
-	err = self.table.T.FetchRows(dst, self.cursor)
+	err = sqlutil.FetchRows(self.cursor, dst)
 
 	return err
 }
@@ -176,7 +177,7 @@ func (self *Result) Next(dst interface{}) error {
 		return err
 	}
 
-	err = self.table.T.FetchRow(dst, self.cursor)
+	err = sqlutil.FetchRow(self.cursor, dst)
 	if err != nil {
 		self.Close()
 		return err
@@ -251,7 +252,9 @@ func (self *Result) Count() (uint64, error) {
 	defer rows.Close()
 
 	dst := counter_t{}
-	self.table.T.FetchRow(&dst, rows)
+	if err = sqlutil.FetchRow(rows, &dst); err != nil {
+		return 0, err
+	}
 
 	return dst.Total, nil
 }
-- 
GitLab