Order by column simplified.
This commit is contained in:
parent
ba3cd37734
commit
8049b2ec01
4 changed files with 167 additions and 75 deletions
|
|
@ -4,7 +4,6 @@ package sqlbuilder
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/dropbox/godropbox/database/sqltypes"
|
||||
"regexp"
|
||||
|
||||
"github.com/dropbox/godropbox/errors"
|
||||
|
|
@ -34,6 +33,9 @@ type Column interface {
|
|||
|
||||
Lte(rhs Expression) BoolExpression
|
||||
LteLiteral(rhs interface{}) BoolExpression
|
||||
|
||||
Asc() OrderByClause
|
||||
Desc() OrderByClause
|
||||
}
|
||||
|
||||
type NullableColumn bool
|
||||
|
|
@ -93,7 +95,7 @@ func (c *baseColumn) SerializeSqlForColumnList(out *bytes.Buffer) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *baseColumn) SerializeSql(out *bytes.Buffer) error {
|
||||
func (c baseColumn) SerializeSql(out *bytes.Buffer) error {
|
||||
if c.table != "" {
|
||||
_, _ = out.WriteString(c.table)
|
||||
_, _ = out.WriteString(".")
|
||||
|
|
@ -123,6 +125,14 @@ func (c *baseColumn) LteLiteral(literal interface{}) BoolExpression {
|
|||
return Lte(c, Literal(literal))
|
||||
}
|
||||
|
||||
func (c *baseColumn) Asc() OrderByClause {
|
||||
return Asc(c)
|
||||
}
|
||||
|
||||
func (c *baseColumn) Desc() OrderByClause {
|
||||
return Desc(c)
|
||||
}
|
||||
|
||||
type bytesColumn struct {
|
||||
baseColumn
|
||||
isExpression
|
||||
|
|
@ -295,66 +305,75 @@ func validIdentifierName(name string) bool {
|
|||
return validIdentifierRegexp.MatchString(name)
|
||||
}
|
||||
|
||||
// Pseudo Column type returned by table.C(name)
|
||||
type deferredLookupColumn struct {
|
||||
isProjection
|
||||
isExpression
|
||||
table *Table
|
||||
colName string
|
||||
|
||||
cachedColumn NonAliasColumn
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) Name() string {
|
||||
return c.colName
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) SerializeSqlForColumnList(
|
||||
out *bytes.Buffer) error {
|
||||
|
||||
return c.SerializeSql(out)
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) SerializeSql(out *bytes.Buffer) error {
|
||||
if c.cachedColumn != nil {
|
||||
return c.cachedColumn.SerializeSql(out)
|
||||
}
|
||||
|
||||
col, err := c.table.getColumn(c.colName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.cachedColumn = col
|
||||
return col.SerializeSql(out)
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) setTableName(table string) error {
|
||||
return errors.Newf(
|
||||
"Lookup column '%s' should never have setTableName called on it",
|
||||
c.colName)
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) Eq(rhs Expression) BoolExpression {
|
||||
lit, ok := rhs.(*literalExpression)
|
||||
if ok && sqltypes.Value(lit.value).IsNull() {
|
||||
return newBoolExpression(c, rhs, []byte(" IS "))
|
||||
}
|
||||
return newBoolExpression(c, rhs, []byte(" = "))
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) Gte(rhs Expression) BoolExpression {
|
||||
return Gte(c, rhs)
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) GteLiteral(rhs interface{}) BoolExpression {
|
||||
return Gte(c, Literal(rhs))
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) Lte(rhs Expression) BoolExpression {
|
||||
return Lte(c, rhs)
|
||||
}
|
||||
|
||||
func (c *deferredLookupColumn) LteLiteral(literal interface{}) BoolExpression {
|
||||
return Lte(c, Literal(literal))
|
||||
}
|
||||
//
|
||||
//// Pseudo Column type returned by table.C(name)
|
||||
//type deferredLookupColumn struct {
|
||||
// isProjection
|
||||
// isExpression
|
||||
// table *Table
|
||||
// colName string
|
||||
//
|
||||
// cachedColumn NonAliasColumn
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) Name() string {
|
||||
// return c.colName
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) SerializeSqlForColumnList(
|
||||
// out *bytes.Buffer) error {
|
||||
//
|
||||
// return c.SerializeSql(out)
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) SerializeSql(out *bytes.Buffer) error {
|
||||
// if c.cachedColumn != nil {
|
||||
// return c.cachedColumn.SerializeSql(out)
|
||||
// }
|
||||
//
|
||||
// col, err := c.table.getColumn(c.colName)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// c.cachedColumn = col
|
||||
// return col.SerializeSql(out)
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) setTableName(table string) error {
|
||||
// return errors.Newf(
|
||||
// "Lookup column '%s' should never have setTableName called on it",
|
||||
// c.colName)
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) Eq(rhs Expression) BoolExpression {
|
||||
// lit, ok := rhs.(*literalExpression)
|
||||
// if ok && sqltypes.Value(lit.value).IsNull() {
|
||||
// return newBoolExpression(c, rhs, []byte(" IS "))
|
||||
// }
|
||||
// return newBoolExpression(c, rhs, []byte(" = "))
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) Gte(rhs Expression) BoolExpression {
|
||||
// return Gte(c, rhs)
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) GteLiteral(rhs interface{}) BoolExpression {
|
||||
// return Gte(c, Literal(rhs))
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) Lte(rhs Expression) BoolExpression {
|
||||
// return Lte(c, rhs)
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) LteLiteral(literal interface{}) BoolExpression {
|
||||
// return Lte(c, Literal(literal))
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) Asc() OrderByClause {
|
||||
// return sqlbuilder.Asc(c)
|
||||
//}
|
||||
//
|
||||
//func (c *deferredLookupColumn) Desc() OrderByClause {
|
||||
// return sqlbuilder.Desc(c)
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/serenize/snaker"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -36,6 +37,7 @@ func Execute(db *sql.DB, query string, destinationPtr interface{}) error {
|
|||
rowData := createScanValue(columnTypes)
|
||||
|
||||
scanContext := &scanContext{
|
||||
|
||||
columnNames: columnNames,
|
||||
uniqueObjectsMap: make(map[string]interface{}),
|
||||
}
|
||||
|
|
@ -47,6 +49,8 @@ func Execute(db *sql.DB, query string, destinationPtr interface{}) error {
|
|||
return err
|
||||
}
|
||||
|
||||
scanContext.rowNum++
|
||||
|
||||
columnProcessed := make([]bool, len(columnTypes))
|
||||
|
||||
if destinationType.Elem().Kind() == reflect.Slice {
|
||||
|
|
@ -70,6 +74,7 @@ func Execute(db *sql.DB, query string, destinationPtr interface{}) error {
|
|||
}
|
||||
|
||||
type scanContext struct {
|
||||
rowNum int
|
||||
columnNames []string
|
||||
uniqueObjectsMap map[string]interface{}
|
||||
}
|
||||
|
|
@ -107,13 +112,13 @@ func getGroupKey(scanContext *scanContext, row []interface{}, structType reflect
|
|||
columnName := snaker.CamelToSnake(structName) + "." + snaker.CamelToSnake(fieldName)
|
||||
|
||||
//fmt.Println(fieldName)
|
||||
rowIndex := getIndex(scanContext.columnNames, columnName)
|
||||
index := getIndex(scanContext.columnNames, columnName)
|
||||
|
||||
if rowIndex < 0 {
|
||||
if index < 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
rowValue := reflect.ValueOf(row[rowIndex])
|
||||
rowValue := reflect.ValueOf(row[index])
|
||||
|
||||
groupKey = groupKey + reflectValueToString(rowValue)
|
||||
} else if !isDbBaseType(fieldType.Type) {
|
||||
|
|
@ -161,7 +166,13 @@ func mapRowToSlice(scanContext *scanContext, groupKey string, columnProcessed []
|
|||
|
||||
structType := getSliceStructType(destinationPtr)
|
||||
|
||||
groupKey = groupKey + ":" + getGroupKey(scanContext, row, structType)
|
||||
structGroupKey := getGroupKey(scanContext, row, structType)
|
||||
|
||||
if structGroupKey == "" {
|
||||
structGroupKey = strconv.Itoa(scanContext.rowNum)
|
||||
}
|
||||
|
||||
groupKey = groupKey + ":" + structGroupKey
|
||||
|
||||
objPtr, ok := scanContext.uniqueObjectsMap[groupKey]
|
||||
|
||||
|
|
|
|||
|
|
@ -97,12 +97,12 @@ func (t *Table) getColumn(name string) (NonAliasColumn, error) {
|
|||
|
||||
// Returns a pseudo column representation of the column name. Error checking
|
||||
// is deferred to SerializeSql.
|
||||
func (t *Table) C(name string) NonAliasColumn {
|
||||
return &deferredLookupColumn{
|
||||
table: t,
|
||||
colName: name,
|
||||
}
|
||||
}
|
||||
//func (t *Table) C(name string) NonAliasColumn {
|
||||
// return &deferredLookupColumn{
|
||||
// table: t,
|
||||
// colName: name,
|
||||
// }
|
||||
//}
|
||||
|
||||
// Returns all columns for a table as a slice of projections
|
||||
func (t *Table) Projections() []Projection {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue