|
|
|
<?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 $casts = [
|
|
|
|
'data' => 'object',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $guarded = [];
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
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."'";
|
|
|
|
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') {
|
|
|
|
$q .= ' ASC';
|
|
|
|
} else {
|
|
|
|
$q .= ' DESC';
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$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];
|
|
|
|
}
|
|
|
|
}
|