diff --git a/mysql/database.go b/mysql/database.go index 1f60c97386c030b48a6780b21f6b5d0ba48cef85..1b1830f260754e0fbdf4fa9f967da6f4316430f0 100644 --- a/mysql/database.go +++ b/mysql/database.go @@ -25,7 +25,6 @@ import ( "database/sql" "fmt" "os" - "reflect" "regexp" "strings" @@ -59,8 +58,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 { @@ -381,8 +379,6 @@ func (self *Source) Collection(names ...string) (db.Collection, error) { names: names, } - col.PrimaryKey = `id` - columns_t := []columnSchema_t{} for _, name := range names { @@ -413,46 +409,16 @@ 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 `tinyint`, `smallint`, `mediumint`, `int`, `bigint`: - if dextra == `unsigned` { - ctype = reflect.Uint64 - } else { - ctype = reflect.Int64 - } - case `decimal`, `float`, `double`: - ctype = reflect.Float64 - } - - col.ColumnTypes[column.ColumnName] = ctype + column.Name = strings.ToLower(column.Name) + col.Columns = append(col.Columns, column.Name) } - } } diff --git a/mysql/result.go b/mysql/result.go index 3ebb2a6369d983c8ea702f0669152730092fcf51..9fe53ad858d928f2dcb008495e601992a42a2a43 100644 --- a/mysql/result.go +++ b/mysql/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 } @@ -181,7 +182,7 @@ func (self *Result) Next(dst interface{}) error { } // Fetching the next result from the cursor. - err = self.table.T.FetchRow(dst, self.cursor) + err = sqlutil.FetchRow(self.cursor, dst) if err != nil { self.Close() @@ -256,7 +257,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 }