|
|
@ -31,17 +31,30 @@ class Row extends BaseModel |
|
|
|
public function scopeSortByCol(Builder $query, Column $column, $dir="asc") |
|
|
|
public function scopeSortByCol(Builder $query, Column $column, $dir="asc") |
|
|
|
{ |
|
|
|
{ |
|
|
|
$q = "data->>'".$column->id."'"; |
|
|
|
$q = "data->>'".$column->id."'"; |
|
|
|
if ($column->type == 'int' || $column->type == 'float') { |
|
|
|
|
|
|
|
$q = "CAST($q AS INTEGER)"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if ($column->type == 'float') { |
|
|
|
|
|
|
|
$q = "CAST($q AS FLOAT)"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($dir == 'asc') { |
|
|
|
$dir = strtoupper($dir); |
|
|
|
$q .= ' ASC'; |
|
|
|
|
|
|
|
} else { |
|
|
|
switch ($column->type) { |
|
|
|
$q .= ' DESC'; |
|
|
|
case 'int': |
|
|
|
|
|
|
|
$q = "CAST($q AS INTEGER) $dir"; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'float': |
|
|
|
|
|
|
|
$q = "CAST($q AS FLOAT) $dir"; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'string': |
|
|
|
|
|
|
|
// Without a size restriction and a cast, this query becomes VERY slow |
|
|
|
|
|
|
|
$q = "CAST($q AS CHAR(25)) $dir, data->>'_id' $dir"; |
|
|
|
|
|
|
|
// added _id as a fallback to ensure the order is the same between revisions |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'bool': |
|
|
|
|
|
|
|
$q = "CAST($q AS boolean) $dir"; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new \LogicException("Missing SQL cast for column type $column->type, performance will suffer."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $query->orderByRaw($q); |
|
|
|
return $query->orderByRaw($q); |
|
|
|