mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-07 18:42:49 +08:00
修复seeder错误;删除第三方的think迁移工具,在extend中重新实现;
This commit is contained in:
801
extend/phinx/Db/Table/Column.php
Normal file
801
extend/phinx/Db/Table/Column.php
Normal file
@@ -0,0 +1,801 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use Phinx\Config\FeatureFlags;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Adapter\PostgresAdapter;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* This object is based loosely on: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
|
||||
*/
|
||||
class Column
|
||||
{
|
||||
public const BIGINTEGER = AdapterInterface::PHINX_TYPE_BIG_INTEGER;
|
||||
public const SMALLINTEGER = AdapterInterface::PHINX_TYPE_SMALL_INTEGER;
|
||||
public const TINYINTEGER = AdapterInterface::PHINX_TYPE_TINY_INTEGER;
|
||||
public const BINARY = AdapterInterface::PHINX_TYPE_BINARY;
|
||||
public const BOOLEAN = AdapterInterface::PHINX_TYPE_BOOLEAN;
|
||||
public const CHAR = AdapterInterface::PHINX_TYPE_CHAR;
|
||||
public const DATE = AdapterInterface::PHINX_TYPE_DATE;
|
||||
public const DATETIME = AdapterInterface::PHINX_TYPE_DATETIME;
|
||||
public const DECIMAL = AdapterInterface::PHINX_TYPE_DECIMAL;
|
||||
public const FLOAT = AdapterInterface::PHINX_TYPE_FLOAT;
|
||||
public const INTEGER = AdapterInterface::PHINX_TYPE_INTEGER;
|
||||
public const STRING = AdapterInterface::PHINX_TYPE_STRING;
|
||||
public const TEXT = AdapterInterface::PHINX_TYPE_TEXT;
|
||||
public const TIME = AdapterInterface::PHINX_TYPE_TIME;
|
||||
public const TIMESTAMP = AdapterInterface::PHINX_TYPE_TIMESTAMP;
|
||||
public const UUID = AdapterInterface::PHINX_TYPE_UUID;
|
||||
public const BINARYUUID = AdapterInterface::PHINX_TYPE_BINARYUUID;
|
||||
/** MySQL-only column type */
|
||||
public const MEDIUMINTEGER = AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER;
|
||||
/** MySQL-only column type */
|
||||
public const ENUM = AdapterInterface::PHINX_TYPE_ENUM;
|
||||
/** MySQL-only column type */
|
||||
public const SET = AdapterInterface::PHINX_TYPE_STRING;
|
||||
/** MySQL-only column type */
|
||||
public const BLOB = AdapterInterface::PHINX_TYPE_BLOB;
|
||||
/** MySQL-only column type */
|
||||
public const YEAR = AdapterInterface::PHINX_TYPE_YEAR;
|
||||
/** MySQL/Postgres-only column type */
|
||||
public const JSON = AdapterInterface::PHINX_TYPE_JSON;
|
||||
/** Postgres-only column type */
|
||||
public const JSONB = AdapterInterface::PHINX_TYPE_JSONB;
|
||||
/** Postgres-only column type */
|
||||
public const CIDR = AdapterInterface::PHINX_TYPE_CIDR;
|
||||
/** Postgres-only column type */
|
||||
public const INET = AdapterInterface::PHINX_TYPE_INET;
|
||||
/** Postgres-only column type */
|
||||
public const MACADDR = AdapterInterface::PHINX_TYPE_MACADDR;
|
||||
/** Postgres-only column type */
|
||||
public const INTERVAL = AdapterInterface::PHINX_TYPE_INTERVAL;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var string|\Phinx\Util\Literal
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected $limit;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $null = true;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $default;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $identity = false;
|
||||
|
||||
/**
|
||||
* Postgres-only column option for identity (always|default)
|
||||
*
|
||||
* @var ?string
|
||||
*/
|
||||
protected $generated = PostgresAdapter::GENERATED_ALWAYS;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected $seed;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected $increment;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected $scale;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $after;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $update;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $comment;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $signed = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $timezone = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $properties = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $collation;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $encoding;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected $srid;
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
protected $values;
|
||||
|
||||
/**
|
||||
* Column constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->null = FeatureFlags::$columnNullDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column name.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column type.
|
||||
*
|
||||
* @param string|\Phinx\Util\Literal $type Column type
|
||||
* @return $this
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column type.
|
||||
*
|
||||
* @return string|\Phinx\Util\Literal
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column limit.
|
||||
*
|
||||
* @param int|null $limit Limit
|
||||
* @return $this
|
||||
*/
|
||||
public function setLimit(?int $limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column limit.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getLimit(): ?int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the column allows nulls.
|
||||
*
|
||||
* @param bool $null Null
|
||||
* @return $this
|
||||
*/
|
||||
public function setNull(bool $null)
|
||||
{
|
||||
$this->null = (bool)$null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether the column allows nulls.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getNull(): bool
|
||||
{
|
||||
return $this->null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the column allow nulls?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isNull(): bool
|
||||
{
|
||||
return $this->getNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default column value.
|
||||
*
|
||||
* @param mixed $default Default
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefault($default)
|
||||
{
|
||||
$this->default = $default;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default column value.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefault()
|
||||
{
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets generated option for identity columns. Ignored otherwise.
|
||||
*
|
||||
* @param string|null $generated Generated option
|
||||
* @return $this
|
||||
*/
|
||||
public function setGenerated(?string $generated)
|
||||
{
|
||||
$this->generated = $generated;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets generated option for identity columns. Null otherwise
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getGenerated(): ?string
|
||||
{
|
||||
return $this->generated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether or not the column is an identity column.
|
||||
*
|
||||
* @param bool $identity Identity
|
||||
* @return $this
|
||||
*/
|
||||
public function setIdentity(bool $identity)
|
||||
{
|
||||
$this->identity = $identity;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether or not the column is an identity column.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getIdentity(): bool
|
||||
{
|
||||
return $this->identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the column an identity column?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isIdentity(): bool
|
||||
{
|
||||
return $this->getIdentity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the column to add this column after.
|
||||
*
|
||||
* @param string $after After
|
||||
* @return $this
|
||||
*/
|
||||
public function setAfter(string $after)
|
||||
{
|
||||
$this->after = $after;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the column to add this column after.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAfter(): ?string
|
||||
{
|
||||
return $this->after;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'ON UPDATE' mysql column function.
|
||||
*
|
||||
* @param string $update On Update function
|
||||
* @return $this
|
||||
*/
|
||||
public function setUpdate(string $update)
|
||||
{
|
||||
$this->update = $update;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the ON UPDATE column function.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUpdate(): ?string
|
||||
{
|
||||
return $this->update;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number precision for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @param int|null $precision Number precision
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrecision(?int $precision)
|
||||
{
|
||||
$this->setLimit($precision);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number precision for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getPrecision(): ?int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column identity increment.
|
||||
*
|
||||
* @param int $increment Number increment
|
||||
* @return $this
|
||||
*/
|
||||
public function setIncrement(int $increment)
|
||||
{
|
||||
$this->increment = $increment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column identity increment.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getIncrement(): ?int
|
||||
{
|
||||
return $this->increment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column identity seed.
|
||||
*
|
||||
* @param int $seed Number seed
|
||||
* @return $this
|
||||
*/
|
||||
public function setSeed(int $seed)
|
||||
{
|
||||
$this->seed = $seed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column identity seed.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSeed(): ?int
|
||||
{
|
||||
return $this->seed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number scale for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @param int|null $scale Number scale
|
||||
* @return $this
|
||||
*/
|
||||
public function setScale(?int $scale)
|
||||
{
|
||||
$this->scale = $scale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number scale for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getScale(): ?int
|
||||
{
|
||||
return $this->scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number precision and scale for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @param int $precision Number precision
|
||||
* @param int $scale Number scale
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrecisionAndScale(int $precision, int $scale)
|
||||
{
|
||||
$this->setLimit($precision);
|
||||
$this->scale = $scale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column comment.
|
||||
*
|
||||
* @param string|null $comment Comment
|
||||
* @return $this
|
||||
*/
|
||||
public function setComment(?string $comment)
|
||||
{
|
||||
$this->comment = $comment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column comment.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getComment(): ?string
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether field should be signed.
|
||||
*
|
||||
* @param bool $signed Signed
|
||||
* @return $this
|
||||
*/
|
||||
public function setSigned(bool $signed)
|
||||
{
|
||||
$this->signed = (bool)$signed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether field should be signed.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSigned(): bool
|
||||
{
|
||||
return $this->signed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the column be signed?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSigned(): bool
|
||||
{
|
||||
return $this->getSigned();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the field should have a timezone identifier.
|
||||
* Used for date/time columns only!
|
||||
*
|
||||
* @param bool $timezone Timezone
|
||||
* @return $this
|
||||
*/
|
||||
public function setTimezone(bool $timezone)
|
||||
{
|
||||
$this->timezone = (bool)$timezone;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether field has a timezone identifier.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getTimezone(): bool
|
||||
{
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the column have a timezone?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isTimezone(): bool
|
||||
{
|
||||
return $this->getTimezone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets field properties.
|
||||
*
|
||||
* @param array $properties Properties
|
||||
* @return $this
|
||||
*/
|
||||
public function setProperties(array $properties)
|
||||
{
|
||||
$this->properties = $properties;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets field properties
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties(): array
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets field values.
|
||||
*
|
||||
* @param string[]|string $values Value(s)
|
||||
* @return $this
|
||||
*/
|
||||
public function setValues($values)
|
||||
{
|
||||
if (!is_array($values)) {
|
||||
$values = preg_split('/,\s*/', $values) ?: [];
|
||||
}
|
||||
$this->values = $values;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets field values
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function getValues(): ?array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column collation.
|
||||
*
|
||||
* @param string $collation Collation
|
||||
* @return $this
|
||||
*/
|
||||
public function setCollation(string $collation)
|
||||
{
|
||||
$this->collation = $collation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column collation.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getCollation(): ?string
|
||||
{
|
||||
return $this->collation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column character set.
|
||||
*
|
||||
* @param string $encoding Encoding
|
||||
* @return $this
|
||||
*/
|
||||
public function setEncoding(string $encoding)
|
||||
{
|
||||
$this->encoding = $encoding;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column character set.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getEncoding(): ?string
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column SRID.
|
||||
*
|
||||
* @param int $srid SRID
|
||||
* @return $this
|
||||
*/
|
||||
public function setSrid(int $srid)
|
||||
{
|
||||
$this->srid = $srid;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column SRID.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getSrid(): ?int
|
||||
{
|
||||
return $this->srid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all allowed options. Each option must have a corresponding `setFoo` method.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getValidOptions(): array
|
||||
{
|
||||
return [
|
||||
'limit',
|
||||
'default',
|
||||
'null',
|
||||
'identity',
|
||||
'scale',
|
||||
'after',
|
||||
'update',
|
||||
'comment',
|
||||
'signed',
|
||||
'timezone',
|
||||
'properties',
|
||||
'values',
|
||||
'collation',
|
||||
'encoding',
|
||||
'srid',
|
||||
'seed',
|
||||
'increment',
|
||||
'generated',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all aliased options. Each alias must reference a valid option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAliasedOptions(): array
|
||||
{
|
||||
return [
|
||||
'length' => 'limit',
|
||||
'precision' => 'limit',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that maps an array of column options to this objects methods.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$validOptions = $this->getValidOptions();
|
||||
$aliasOptions = $this->getAliasedOptions();
|
||||
|
||||
if (isset($options['identity']) && $options['identity'] && !isset($options['null'])) {
|
||||
$options['null'] = false;
|
||||
}
|
||||
|
||||
foreach ($options as $option => $value) {
|
||||
if (isset($aliasOptions[$option])) {
|
||||
// proxy alias -> option
|
||||
$option = $aliasOptions[$option];
|
||||
}
|
||||
|
||||
if (!in_array($option, $validOptions, true)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a valid column option.', $option));
|
||||
}
|
||||
|
||||
$method = 'set' . ucfirst($option);
|
||||
$this->$method($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
237
extend/phinx/Db/Table/ForeignKey.php
Normal file
237
extend/phinx/Db/Table/ForeignKey.php
Normal file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
class ForeignKey
|
||||
{
|
||||
public const CASCADE = 'CASCADE';
|
||||
public const RESTRICT = 'RESTRICT';
|
||||
public const SET_NULL = 'SET NULL';
|
||||
public const NO_ACTION = 'NO ACTION';
|
||||
|
||||
/**
|
||||
* @var array<string>
|
||||
*/
|
||||
protected static $validOptions = ['delete', 'update', 'constraint'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $columns = [];
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Table\Table
|
||||
*/
|
||||
protected $referencedTable;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $referencedColumns = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $onDelete;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $onUpdate;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $constraint;
|
||||
|
||||
/**
|
||||
* Sets the foreign key columns.
|
||||
*
|
||||
* @param string[]|string $columns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setColumns($columns)
|
||||
{
|
||||
$this->columns = is_string($columns) ? [$columns] : $columns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the foreign key columns.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the foreign key referenced table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table this KEY is pointing to
|
||||
* @return $this
|
||||
*/
|
||||
public function setReferencedTable(Table $table)
|
||||
{
|
||||
$this->referencedTable = $table;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the foreign key referenced table.
|
||||
*
|
||||
* @return \Phinx\Db\Table\Table
|
||||
*/
|
||||
public function getReferencedTable(): Table
|
||||
{
|
||||
return $this->referencedTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the foreign key referenced columns.
|
||||
*
|
||||
* @param string[] $referencedColumns Referenced columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setReferencedColumns(array $referencedColumns)
|
||||
{
|
||||
$this->referencedColumns = $referencedColumns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the foreign key referenced columns.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getReferencedColumns(): array
|
||||
{
|
||||
return $this->referencedColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ON DELETE action for the foreign key.
|
||||
*
|
||||
* @param string $onDelete On Delete
|
||||
* @return $this
|
||||
*/
|
||||
public function setOnDelete(string $onDelete)
|
||||
{
|
||||
$this->onDelete = $this->normalizeAction($onDelete);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets ON DELETE action for the foreign key.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOnDelete(): ?string
|
||||
{
|
||||
return $this->onDelete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets ON UPDATE action for the foreign key.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOnUpdate(): ?string
|
||||
{
|
||||
return $this->onUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ON UPDATE action for the foreign key.
|
||||
*
|
||||
* @param string $onUpdate On Update
|
||||
* @return $this
|
||||
*/
|
||||
public function setOnUpdate(string $onUpdate)
|
||||
{
|
||||
$this->onUpdate = $this->normalizeAction($onUpdate);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets constraint for the foreign key.
|
||||
*
|
||||
* @param string $constraint Constraint
|
||||
* @return $this
|
||||
*/
|
||||
public function setConstraint(string $constraint)
|
||||
{
|
||||
$this->constraint = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets constraint name for the foreign key.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getConstraint(): ?string
|
||||
{
|
||||
return $this->constraint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that maps an array of index options to this objects methods.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $option => $value) {
|
||||
if (!in_array($option, static::$validOptions, true)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a valid foreign key option.', $option));
|
||||
}
|
||||
|
||||
// handle $options['delete'] as $options['update']
|
||||
if ($option === 'delete') {
|
||||
$this->setOnDelete($value);
|
||||
} elseif ($option === 'update') {
|
||||
$this->setOnUpdate($value);
|
||||
} else {
|
||||
$method = 'set' . ucfirst($option);
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* From passed value checks if it's correct and fixes if needed
|
||||
*
|
||||
* @param string $action Action
|
||||
* @throws \InvalidArgumentException
|
||||
* @return string
|
||||
*/
|
||||
protected function normalizeAction(string $action): string
|
||||
{
|
||||
$constantName = 'static::' . str_replace(' ', '_', strtoupper(trim($action)));
|
||||
if (!defined($constantName)) {
|
||||
throw new InvalidArgumentException('Unknown action passed: ' . $action);
|
||||
}
|
||||
|
||||
return constant($constantName);
|
||||
}
|
||||
}
|
||||
227
extend/phinx/Db/Table/Index.php
Normal file
227
extend/phinx/Db/Table/Index.php
Normal file
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class Index
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const UNIQUE = 'unique';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const INDEX = 'index';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FULLTEXT = 'fulltext';
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected $columns;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type = self::INDEX;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var int|array|null
|
||||
*/
|
||||
protected $limit;
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected $order;
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected $includedColumns;
|
||||
|
||||
/**
|
||||
* Sets the index columns.
|
||||
*
|
||||
* @param string|string[] $columns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setColumns($columns)
|
||||
{
|
||||
$this->columns = is_string($columns) ? [$columns] : $columns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index columns.
|
||||
*
|
||||
* @return string[]|null
|
||||
*/
|
||||
public function getColumns(): ?array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index type.
|
||||
*
|
||||
* @param string $type Type
|
||||
* @return $this
|
||||
*/
|
||||
public function setType(string $type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index name.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index limit.
|
||||
*
|
||||
* @param int|array $limit limit value or array of limit value
|
||||
* @return $this
|
||||
*/
|
||||
public function setLimit($limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index limit.
|
||||
*
|
||||
* @return int|array|null
|
||||
*/
|
||||
public function getLimit()
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index columns sort order.
|
||||
*
|
||||
* @param string[] $order column name sort order key value pair
|
||||
* @return $this
|
||||
*/
|
||||
public function setOrder(array $order)
|
||||
{
|
||||
$this->order = $order;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index columns sort order.
|
||||
*
|
||||
* @return string[]|null
|
||||
*/
|
||||
public function getOrder(): ?array
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index included columns.
|
||||
*
|
||||
* @param string[] $includedColumns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setInclude(array $includedColumns)
|
||||
{
|
||||
$this->includedColumns = $includedColumns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index included columns.
|
||||
*
|
||||
* @return string[]|null
|
||||
*/
|
||||
public function getInclude(): ?array
|
||||
{
|
||||
return $this->includedColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that maps an array of index options to this objects methods.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
// Valid Options
|
||||
$validOptions = ['type', 'unique', 'name', 'limit', 'order', 'include'];
|
||||
foreach ($options as $option => $value) {
|
||||
if (!in_array($option, $validOptions, true)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a valid index option.', $option));
|
||||
}
|
||||
|
||||
// handle $options['unique']
|
||||
if (strcasecmp($option, self::UNIQUE) === 0) {
|
||||
if ((bool)$value) {
|
||||
$this->setType(self::UNIQUE);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$method = 'set' . ucfirst($option);
|
||||
$this->$method($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
84
extend/phinx/Db/Table/Table.php
Normal file
84
extend/phinx/Db/Table/Table.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Table
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @param string $name The table name
|
||||
* @param array<string, mixed> $options The creation options for this table
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($name, array $options = [])
|
||||
{
|
||||
if (empty($name)) {
|
||||
throw new InvalidArgumentException('Cannot use an empty table name');
|
||||
}
|
||||
|
||||
$this->name = $name;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the table name.
|
||||
*
|
||||
* @param string $name The name of the table
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table options
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the table options
|
||||
*
|
||||
* @param array<string, mixed> $options The options for the table creation
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user