good morning!!!!

Skip to content
Snippets Groups Projects
Commit f69511d9 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

QL: Upgrading QL to meet latest sqlutil fixes.

parent 55b2343c
Branches
Tags
No related merge requests found
...@@ -35,6 +35,7 @@ const defaultOperator = `==` ...@@ -35,6 +35,7 @@ const defaultOperator = `==`
type Table struct { type Table struct {
sqlutil.T sqlutil.T
columnTypes map[string]reflect.Kind
source *Source source *Source
names []string names []string
} }
...@@ -180,7 +181,6 @@ func (self *Table) Find(terms ...interface{}) db.Result { ...@@ -180,7 +181,6 @@ func (self *Table) Find(terms ...interface{}) db.Result {
table: self, table: self,
where: where, where: where,
arguments: arguments, arguments: arguments,
t: &t{&self.T},
} }
return result return result
......
...@@ -35,6 +35,7 @@ import ( ...@@ -35,6 +35,7 @@ import (
"upper.io/db/util/sqlutil" "upper.io/db/util/sqlutil"
) )
// Public adapters name under which this adapter registers itself.
const Adapter = `ql` const Adapter = `ql`
var ( var (
...@@ -407,8 +408,6 @@ func (self *Source) Collection(names ...string) (db.Collection, error) { ...@@ -407,8 +408,6 @@ func (self *Source) Collection(names ...string) (db.Collection, error) {
names: names, names: names,
} }
col.PrimaryKey = `id`
columns_t := []columnSchema_t{} columns_t := []columnSchema_t{}
for _, name := range names { for _, name := range names {
...@@ -438,17 +437,20 @@ func (self *Source) Collection(names ...string) (db.Collection, error) { ...@@ -438,17 +437,20 @@ func (self *Source) Collection(names ...string) (db.Collection, error) {
return nil, err return nil, err
} }
if err = col.FetchRows(&columns_t, rows); err != nil { if err = sqlutil.FetchRows(rows, &columns_t); err != nil {
return nil, err return nil, err
} }
col.ColumnTypes = make(map[string]reflect.Kind, len(columns_t)) col.Columns = make([]string, len(columns_t))
col.columnTypes = make(map[string]reflect.Kind)
for _, column := range columns_t { for i, column := range columns_t {
column.ColumnName = strings.ToLower(column.ColumnName) column.ColumnName = strings.ToLower(column.ColumnName)
column.DataType = strings.ToLower(column.DataType) column.DataType = strings.ToLower(column.DataType)
col.Columns[i] = column.ColumnName
// Default properties. // Default properties.
dtype := column.DataType dtype := column.DataType
ctype := reflect.String ctype := reflect.String
...@@ -485,7 +487,7 @@ func (self *Source) Collection(names ...string) (db.Collection, error) { ...@@ -485,7 +487,7 @@ func (self *Source) Collection(names ...string) (db.Collection, error) {
ctype = reflect.String ctype = reflect.String
} }
col.ColumnTypes[column.ColumnName] = ctype col.columnTypes[column.ColumnName] = ctype
} }
} }
......
...@@ -28,6 +28,7 @@ import ( ...@@ -28,6 +28,7 @@ import (
"upper.io/db" "upper.io/db"
"upper.io/db/util/sqlgen" "upper.io/db/util/sqlgen"
"upper.io/db/util/sqlutil"
) )
type counter_t struct { type counter_t struct {
...@@ -43,7 +44,6 @@ type Result struct { ...@@ -43,7 +44,6 @@ type Result struct {
where sqlgen.Where where sqlgen.Where
orderBy sqlgen.OrderBy orderBy sqlgen.OrderBy
arguments []interface{} arguments []interface{}
t *t
} }
// Executes a SELECT statement that can feed Next(), All() or One(). // Executes a SELECT statement that can feed Next(), All() or One().
...@@ -149,7 +149,7 @@ func (self *Result) All(dst interface{}) error { ...@@ -149,7 +149,7 @@ func (self *Result) All(dst interface{}) error {
defer self.Close() defer self.Close()
// Fetching all results within the cursor. // Fetching all results within the cursor.
err = self.t.qlFetchRows(dst, self.cursor) err = self.table.fetchRows(self.cursor, dst)
return err return err
} }
...@@ -182,7 +182,7 @@ func (self *Result) Next(dst interface{}) error { ...@@ -182,7 +182,7 @@ func (self *Result) Next(dst interface{}) error {
} }
// Fetching the next result from the cursor. // Fetching the next result from the cursor.
err = self.t.qlFetchRow(dst, self.cursor) err = self.table.fetchRow(self.cursor, dst)
if err != nil { if err != nil {
self.Close() self.Close()
...@@ -257,7 +257,10 @@ func (self *Result) Count() (uint64, error) { ...@@ -257,7 +257,10 @@ func (self *Result) Count() (uint64, error) {
defer rows.Close() defer rows.Close()
dst := counter_t{} dst := counter_t{}
self.t.FetchRow(&dst, rows)
if err = sqlutil.FetchRow(rows, &dst); err != nil {
return 0, err
}
return dst.Total, nil return dst.Total, nil
} }
/* // Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam
Copyright (c) 2014 José Carlos Nieto, https://menteslibres.net/xiam //
// Permission is hereby granted, free of charge, to any person obtaining
Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the
a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including
"Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish,
without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to
distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to
permit persons to whom the Software is furnished to do so, subject to // the following conditions:
the following conditions: //
// The above copyright notice and this permission notice shall be
The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software.
included in all copies or substantial portions of the Software. //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package ql package ql
import ( import (
"database/sql" "database/sql"
"reflect" "reflect"
"strings"
"menteslibres.net/gosexy/to" "menteslibres.net/gosexy/to"
"upper.io/db" "upper.io/db"
"upper.io/db/util" "upper.io/db/util"
"upper.io/db/util/sqlutil"
) )
// Wrapper for sqlutil.T // Returns (lowercased) columns names.
type t struct { func getRowColumns(rows *sql.Rows) ([]string, error) {
*sqlutil.T // Column names.
columns, err := rows.Columns()
if err != nil {
return nil, err
}
// Column names to lower case.
for i, _ := range columns {
columns[i] = strings.ToLower(columns[i])
}
return columns, nil
} }
func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error { func (self *Table) fetchRow(rows *sql.Rows, dst interface{}) error {
var err error
dstv := reflect.ValueOf(dst) dstv := reflect.ValueOf(dst)
...@@ -48,12 +59,6 @@ func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error { ...@@ -48,12 +59,6 @@ func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error {
item_v := dstv.Elem() item_v := dstv.Elem()
columns, err := sqlutil.GetRowColumns(rows)
if err != nil {
return err
}
next := rows.Next() next := rows.Next()
if next == false { if next == false {
...@@ -63,7 +68,13 @@ func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error { ...@@ -63,7 +68,13 @@ func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error {
return db.ErrNoMoreRows return db.ErrNoMoreRows
} }
item, err := self.qlFetchResult(item_v.Type(), rows, columns) var columns []string
if columns, err = getRowColumns(rows); err != nil {
return err
}
item, err := self.fetchResult(item_v.Type(), rows, columns)
if err != nil { if err != nil {
return err return err
...@@ -74,7 +85,7 @@ func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error { ...@@ -74,7 +85,7 @@ func (self *t) qlFetchRow(dst interface{}, rows *sql.Rows) error {
return nil return nil
} }
func (self *t) qlFetchResult(item_t reflect.Type, rows *sql.Rows, columns []string) (item reflect.Value, err error) { func (self *Table) fetchResult(item_t reflect.Type, rows *sql.Rows, columns []string) (item reflect.Value, err error) {
expecting := len(columns) expecting := len(columns)
scanArgs := make([]interface{}, expecting) scanArgs := make([]interface{}, expecting)
...@@ -114,8 +125,8 @@ func (self *t) qlFetchResult(item_t reflect.Type, rows *sql.Rows, columns []stri ...@@ -114,8 +125,8 @@ func (self *t) qlFetchResult(item_t reflect.Type, rows *sql.Rows, columns []stri
var val_v reflect.Value var val_v reflect.Value
if _, ok := self.ColumnTypes[columnName]; ok == true { if _, ok := self.columnTypes[columnName]; ok == true {
v, _ := to.Convert(val_s, self.ColumnTypes[columnName]) v, _ := to.Convert(val_s, self.columnTypes[columnName])
val_v = reflect.ValueOf(v) val_v = reflect.ValueOf(v)
} else { } else {
v, _ := to.Convert(val_s, reflect.String) v, _ := to.Convert(val_s, reflect.String)
...@@ -135,7 +146,8 @@ func (self *t) qlFetchResult(item_t reflect.Type, rows *sql.Rows, columns []stri ...@@ -135,7 +146,8 @@ func (self *t) qlFetchResult(item_t reflect.Type, rows *sql.Rows, columns []stri
return item, nil return item, nil
} }
func (self *t) qlFetchRows(dst interface{}, rows *sql.Rows) error { func (self *Table) fetchRows(rows *sql.Rows, dst interface{}) error {
var err error
// Destination. // Destination.
dstv := reflect.ValueOf(dst) dstv := reflect.ValueOf(dst)
...@@ -152,18 +164,18 @@ func (self *t) qlFetchRows(dst interface{}, rows *sql.Rows) error { ...@@ -152,18 +164,18 @@ func (self *t) qlFetchRows(dst interface{}, rows *sql.Rows) error {
return db.ErrExpectingSliceMapStruct return db.ErrExpectingSliceMapStruct
} }
columns, err := sqlutil.GetRowColumns(rows) slicev := dstv.Elem()
item_t := slicev.Type().Elem()
if err != nil { var columns []string
if columns, err = getRowColumns(rows); err != nil {
return err return err
} }
slicev := dstv.Elem()
item_t := slicev.Type().Elem()
for rows.Next() { for rows.Next() {
item, err := self.qlFetchResult(item_t, rows, columns) item, err := self.fetchResult(item_t, rows, columns)
if err != nil { if err != nil {
return err return err
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment