datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
datatable.directory/app/Models/Row.php

149 lines
4.1 KiB

<?php
namespace App\Models;
use App\Tables\Column;
use Illuminate\Database\Eloquent\Builder;
/**
* Row in a data table
*
* @property int $id
* @property object|\RowData $data - JSONB, always containing _id
*/
class Row extends BaseModel
{
protected static $noCasts = false;
protected static $noCastsStack = [];
protected $casts = [
'data' => 'object',
];
protected $guarded = [];
public $timestamps = false;
/**
* Disable casts, pushing the previous value onto a stack
*
* Needed when using the rowsData() revision method that could
* cause a name collision with a database column, confusing Eloquent
* into applying incorrect casts.
*/
public static function disableCasts()
{
array_push(self::$noCastsStack, self::$noCasts);
self::$noCasts = true;
}
/**
* Restore the original value of casts enabled/disabled
*/
public static function enableCasts()
{
self::$noCasts = array_pop(self::$noCastsStack);
}
public function getCasts()
{
// This override is needed to prevent casting 'id' to int when it occurs
// as a field in a result of the rowData query
// (this caused display of zeros in string cols)
return self::$noCasts ? [] : $this->casts;
}
public function getRowIdAttribute() {
return $this->data->_id;
}
public function setRowIdAttribute($value) {
$this->data->_id = $value;
}
public function scopeSortByCol(Builder $query, Column $column, $dir="asc")
{
$q = "data->>'".$column->id."'";
$dir = strtoupper($dir);
switch ($column->type) {
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);
}
/**
* Allocate a single Global Row ID, application-unique.
*
* GRIDs are used to uniquely identify existing or proposed new rows,
* and are preserved after row modifications, to ensure change proposals have
* a clear target.
*
* @return int
*/
public static function allocateRowID()
{
return \DB::selectOne("SELECT nextval('global_row_id_seq') AS rid;")->rid;
}
/**
* Allocate a block of Global Row IDs, application-unique.
*
* @see Row::allocateRowID()
*
* @return int[] first and last
*/
public static function allocateRowIDs($count)
{
if ($count == 0) return null;
$last = \DB::selectOne("SELECT multi_nextval('global_row_id_seq', ?) AS last_id;", [$count])->last_id;
return [$last - $count + 1, $last];
}
/**
* Allocate a single Global Column ID, application-unique.
*
* GCIDs are used to uniquely identify existing or proposed new columns,
* and are preserved after column modifications, to ensure change proposals have
* a clear target. This is the column equivalent of GRID.
*
* @return int
*/
public static function allocateColID()
{
return \DB::selectOne("SELECT nextval('global_column_id_seq') AS cid;")->cid;
}
/**
* Allocate a block of Global Column IDs, application-unique.
*
* @see Row::allocateColID()
*
* @return int[] first and last
*/
public static function allocateColIDs($count)
{
$last = \DB::selectOne("SELECT multi_nextval('global_column_id_seq', ?) AS last_id;", [$count])->last_id;
return [$last - $count + 1, $last];
}
}