good morning!!!!

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

Alternative fetchAll function from MySQL.

parent 5adfedef
No related branches found
No related tags found
No related merge requests found
/* /*
Copyright (c) 2012 José Carlos Nieto, http://xiam.menteslibres.org/ Copyright (c) 2012-2013 José Carlos Nieto, http://xiam.menteslibres.org/
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
......
No preview for this file type
...@@ -29,7 +29,6 @@ import ( ...@@ -29,7 +29,6 @@ import (
"github.com/gosexy/db" "github.com/gosexy/db"
"github.com/gosexy/sugar" "github.com/gosexy/sugar"
"github.com/gosexy/to" "github.com/gosexy/to"
//_ "github.com/xiam/gosqlite3"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
...@@ -41,7 +40,8 @@ func (self *Table) Name() string { ...@@ -41,7 +40,8 @@ func (self *Table) Name() string {
return self.name return self.name
} }
func (t *Table) slFetchAll(rows sql.Rows) []db.Item { // Returns all items from a query.
func (self *Table) slFetchAll(rows sql.Rows) []db.Item {
items := []db.Item{} items := []db.Item{}
...@@ -51,42 +51,46 @@ func (t *Table) slFetchAll(rows sql.Rows) []db.Item { ...@@ -51,42 +51,46 @@ func (t *Table) slFetchAll(rows sql.Rows) []db.Item {
columns[i] = strings.ToLower(columns[i]) columns[i] = strings.ToLower(columns[i])
} }
res := map[string]*sql.RawBytes{} expecting := len(columns)
fargs := []reflect.Value{} values := make([]*sql.RawBytes, expecting)
scanArgs := make([]interface{}, expecting)
for _, name := range columns { for i := range columns {
res[name] = &sql.RawBytes{} scanArgs[i] = &values[i]
fargs = append(fargs, reflect.ValueOf(res[name]))
} }
sn := reflect.ValueOf(&rows)
fn := sn.MethodByName("Scan")
for rows.Next() { for rows.Next() {
item := db.Item{} item := db.Item{}
ret := fn.Call(fargs) err := rows.Scan(scanArgs...)
if ret[0].IsNil() != true { if err != nil {
panic(ret[0].Elem().Interface().(error)) panic(err)
} }
for _, name := range columns { // Pending cleaner reflection magic.
strval := fmt.Sprintf("%s", *res[name]) for i, value := range values {
column := columns[i]
switch t.types[name] { if value == nil {
item[column] = nil
} else {
strval := fmt.Sprintf("%s", *value)
switch self.types[column] {
case reflect.Uint64: case reflect.Uint64:
intval, _ := strconv.Atoi(strval) intval, _ := strconv.Atoi(strval)
item[name] = uint64(intval) item[column] = uint64(intval)
case reflect.Int64: case reflect.Int64:
intval, _ := strconv.Atoi(strval) intval, _ := strconv.Atoi(strval)
item[name] = intval item[column] = intval
case reflect.Float64: case reflect.Float64:
floatval, _ := strconv.ParseFloat(strval, 10) floatval, _ := strconv.ParseFloat(strval, 10)
item[name] = floatval item[column] = floatval
default: default:
item[name] = strval item[column] = strval
}
} }
} }
......
/* /*
Copyright (c) 2012 José Carlos Nieto, http://xiam.menteslibres.org/ Copyright (c) 2012-2013 José Carlos Nieto, http://xiam.menteslibres.org/
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
...@@ -28,6 +28,7 @@ import ( ...@@ -28,6 +28,7 @@ import (
"fmt" "fmt"
"github.com/gosexy/db" "github.com/gosexy/db"
_ "github.com/xiam/gosqlite3" _ "github.com/xiam/gosqlite3"
//_ "github.com/mattn/go-sqlite3"
//_ "bitbucket.org/minux/go.sqlite3" //_ "bitbucket.org/minux/go.sqlite3"
"reflect" "reflect"
"regexp" "regexp"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment