add column unique numbering scheme, more efficient selects
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Tables;
|
||||
|
||||
use App\Models\Row;
|
||||
use MightyPork\Utils\Utils;
|
||||
|
||||
/**
|
||||
* Generates a sequence of string codes for internal naming of columns
|
||||
*
|
||||
* Produces database-unique lowercase identifiers for created or proposed columns.
|
||||
* Column identifiers are used in row objects to uniquely identify data even if the
|
||||
* revision header (columns object) is modified, such as reordering, changing name,
|
||||
* adding new column in concurrent proposals, etc.
|
||||
*
|
||||
* Thanks to this uniqueness, it could even be possible to compare or merge forks
|
||||
* of the same table.
|
||||
*/
|
||||
class ColumnNumerator
|
||||
{
|
||||
const ALPHABET = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
||||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
|
||||
|
||||
/** @var int */
|
||||
private $next;
|
||||
/** @var int */
|
||||
private $last;
|
||||
|
||||
/**
|
||||
* Create numerator for the given number of columns
|
||||
*/
|
||||
public function __construct($capacity)
|
||||
{
|
||||
list($this->next, $this->last) = Row::allocateColIDs($capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next column name, incrementing the internal state
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
if ($this->next > $this->last)
|
||||
throw new \OutOfBoundsException("Column numerator has run out of allocated GCID slots");
|
||||
|
||||
$key = Utils::alphabetEncode($this->next, self::ALPHABET);
|
||||
$this->next++;
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user