add column unique numbering scheme, more efficient selects

This commit is contained in:
2018-08-04 19:57:59 +02:00
parent 69bda25bbb
commit fabc3ad24e
23 changed files with 452 additions and 173 deletions
+2 -2
View File
@@ -10,7 +10,7 @@ class BaseModel extends Model
{
public function getAttribute($key)
{
if (! $key) {
if ($this->exists && ! $key) {
throw new \LogicException("No attribute ".var_export($key, true));
}
@@ -25,7 +25,7 @@ class BaseModel extends Model
*/
public function getRelationValue($key)
{
if (!method_exists($this, $key)) {
if ($this->exists && !method_exists($this, $key)) {
throw new \LogicException("No attribute or relation ".var_export($key, true));
}
+17 -1
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Tables\Column;
use Illuminate\Support\Collection;
use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
@@ -32,7 +33,22 @@ class Revision extends BaseModel
/** Included rows */
public function rows()
{
return $this->belongsToMany(Row::class, 'revision_row_pivot');
return $this->belongsToMany(Row::class, 'revision_row_pivot')->as('_row_pivot');
}
/** Included rows
* @param Column[] $columns
* @return \Illuminate\Database\Query\Builder|static
*/
public function rowsData($columns, $withId=true)
{
$selects = $withId ? ["data->>'_id' as _id"] : [];
foreach ($columns as $col) {
$selects[] = "data->>'$col->id' as $col->name";
}
return $this->rows()->select([])->selectRaw(implode(', ', $selects));
}
/** Proposal that lead to this revision */
+37 -10
View File
@@ -6,7 +6,7 @@ namespace App\Models;
* Row in a data table
*
* @property int $id
* @property string $data - JSONB, always containing _grid
* @property string $data - JSONB, always containing _id
*/
class Row extends BaseModel
{
@@ -17,12 +17,12 @@ class Row extends BaseModel
protected $guarded = [];
public $timestamps = false;
public function getGridAttribute() {
return $this->data->_grid;
public function getRowIdAttribute() {
return $this->data->_id;
}
public function setGridAttribute($value) {
$this->data->_grid = $value;
public function setRowIdAttribute($value) {
$this->data->_id = $value;
}
/**
@@ -34,21 +34,48 @@ class Row extends BaseModel
*
* @return int
*/
public static function allocateGRID()
public static function allocateRowID()
{
return \DB::selectOne("SELECT nextval('global_row_id_seq') AS grid;")->grid;
return \DB::selectOne("SELECT nextval('global_row_id_seq') AS rid;")->rid;
}
/**
* Allocate a block of Global Row IDs, application-unique.
*
* @see Row::allocateGRID()
* @see Row::allocateRowID()
*
* @return int[] first and last
*/
public static function allocateGRIDs($count)
public static function allocateRowIDs($count)
{
$last = \DB::selectOne("SELECT multi_nextval('global_row_id_seq', ?) AS last_grid;", [$count])->last_grid;
$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];
}
}
+10
View File
@@ -226,4 +226,14 @@ class User extends BaseModel implements
$this->notify(new ConfirmEmail($newEmail, $confirmation->token));
}
/**
* Check if this user has a table with the givenname
*
* @param string $name
*/
public function hasTable(string $name)
{
$this->tables()->where('name', $name)->exists();
}
}