good morning!!!!

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

Missing db.Sort for PostgreSQL: https://github.com/gosexy/db/issues/3

parent 2c99deab
Branches
Tags
No related merge requests found
......@@ -28,6 +28,7 @@ import (
"fmt"
"github.com/gosexy/db"
"github.com/gosexy/sugar"
"github.com/gosexy/to"
_ "github.com/xiam/gopostgresql"
"reflect"
"regexp"
......@@ -36,6 +37,8 @@ import (
"time"
)
var Debug = false
const dateFormat = "2006-01-02 15:04:05"
const timeFormat = "%d:%02d:%02d"
......@@ -155,10 +158,10 @@ func (pg *PostgresqlDataSource) pgExec(method string, terms ...interface{}) (sql
q := pgCompile(terms)
/*
if Debug {
fmt.Printf("Q: %v\n", q.Query)
fmt.Printf("A: %v\n", q.SqlArgs)
*/
}
qs := strings.Join(q.Query, " ")
......@@ -450,6 +453,7 @@ func (t *PostgresqlTable) FindAll(terms ...interface{}) []db.Item {
conditions := ""
limit := ""
offset := ""
sort := ""
// Analyzing
itop = len(terms)
......@@ -460,6 +464,19 @@ func (t *PostgresqlTable) FindAll(terms ...interface{}) []db.Item {
switch term.(type) {
case db.Limit:
limit = fmt.Sprintf("LIMIT %v", term.(db.Limit))
case db.Sort:
sortBy := []string{}
for k, v := range term.(db.Sort) {
v = strings.ToUpper(to.String(v))
if v == "-1" {
v = "DESC"
}
if v == "1" {
v = "ASC"
}
sortBy = append(sortBy, fmt.Sprintf("%s %s", k, v))
}
sort = fmt.Sprintf("ORDER BY %s", strings.Join(sortBy, ", "))
case db.Offset:
offset = fmt.Sprintf("OFFSET %v", term.(db.Offset))
case db.Fields:
......@@ -481,7 +498,7 @@ func (t *PostgresqlTable) FindAll(terms ...interface{}) []db.Item {
"Query",
fmt.Sprintf("SELECT %s FROM %s", fields, pgTable(t.name)),
fmt.Sprintf("WHERE %s", conditions), args,
limit, offset,
sort, limit, offset,
)
if err != nil {
......
......@@ -15,6 +15,10 @@ const pgDatabase = "gotest"
const pgUser = "gouser"
const pgPassword = "gopass"
func TestEnableDebug(t *testing.T) {
Debug = true
}
func getTestData() db.Item {
_time, _ := time.ParseDuration("17h20m")
......@@ -200,6 +204,7 @@ func TestPgPopulate(t *testing.T) {
people := sess.Collection("people").FindAll(
db.Fields{"id", "name"},
db.Sort{"name": "ASC", "id": -1},
)
for i = 0; i < len(people); i++ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment