update:优化代码生成
This commit is contained in:
parent
7b7f13536f
commit
822ff3874a
@ -1 +0,0 @@
|
|||||||
<?php
|
|
@ -1,161 +0,0 @@
|
|||||||
<?php
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Catch-CMS Design On 2020
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | CatchAdmin [Just Like ~ ]
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
|
|
||||||
namespace catchAdmin\cms\form;
|
|
||||||
|
|
||||||
use catcher\exceptions\FailedException;
|
|
||||||
use FormBuilder\Exception\FormBuilderException;
|
|
||||||
use FormBuilder\Factory\Elm;
|
|
||||||
use FormBuilder\UI\Elm\Components\Upload;
|
|
||||||
use FormBuilder\UI\Elm\Traits\CascaderFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\CheckBoxFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\ColorPickerFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\DatePickerFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\FormStyleFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\FrameFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\GroupFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\HiddenFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\InputFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\InputNumberFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\RadioFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\RateFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\SelectFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\SliderFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\SwitchesFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\TimePickerFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\TreeFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\UploadFactoryTrait;
|
|
||||||
use FormBuilder\UI\Elm\Traits\ValidateFactoryTrait;
|
|
||||||
|
|
||||||
abstract class Form
|
|
||||||
{
|
|
||||||
use CascaderFactoryTrait;
|
|
||||||
use CheckBoxFactoryTrait;
|
|
||||||
use ColorPickerFactoryTrait;
|
|
||||||
use DatePickerFactoryTrait;
|
|
||||||
use FrameFactoryTrait;
|
|
||||||
use HiddenFactoryTrait;
|
|
||||||
use InputNumberFactoryTrait;
|
|
||||||
use InputFactoryTrait;
|
|
||||||
use RadioFactoryTrait;
|
|
||||||
use RateFactoryTrait;
|
|
||||||
use SliderFactoryTrait;
|
|
||||||
use SelectFactoryTrait;
|
|
||||||
use FormStyleFactoryTrait;
|
|
||||||
use SwitchesFactoryTrait;
|
|
||||||
use TimePickerFactoryTrait;
|
|
||||||
use TreeFactoryTrait;
|
|
||||||
use UploadFactoryTrait;
|
|
||||||
use ValidateFactoryTrait;
|
|
||||||
use GroupFactoryTrait;
|
|
||||||
use FormOptions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 必须实现的
|
|
||||||
*
|
|
||||||
* @time 2021年03月06日
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
abstract public function fields(): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建 Form
|
|
||||||
*
|
|
||||||
* @time 2021年03月06日
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function create(): array
|
|
||||||
{
|
|
||||||
return $this->rule($this->fields());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* form rule
|
|
||||||
*
|
|
||||||
* @time 2021年03月06日
|
|
||||||
* @param array $rules
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function rule(array $rules): array
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return Elm::createForm('', $rules)->formRule();
|
|
||||||
} catch (FormBuilderException $e) {
|
|
||||||
throw new FailedException('Form Created Failed: ' .$e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传图片地址
|
|
||||||
*
|
|
||||||
* @time 2021年03月03日
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function uploadImageUrl(): string
|
|
||||||
{
|
|
||||||
return env('app.domain') . '/cms/upload/image';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传图片地址
|
|
||||||
*
|
|
||||||
* @time 2021年03月03日
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function uploadFileUrl(): string
|
|
||||||
{
|
|
||||||
return env('app.domain') . '/cms/upload/file';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected function authorization(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'authorization' => 'Bearer ' . request()->user()->remember_token,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传图片
|
|
||||||
*
|
|
||||||
* @time 2021年03月03日
|
|
||||||
* @param $title
|
|
||||||
* @param string $value
|
|
||||||
* @return \FormBuilder\UI\Elm\Components\Upload
|
|
||||||
*/
|
|
||||||
public function image(string $title, string $value = ''): Upload
|
|
||||||
{
|
|
||||||
return self::uploadImage('image', $title, $this->uploadImageUrl(), $value)
|
|
||||||
->uploadName('image')
|
|
||||||
->data(['none' => ''])
|
|
||||||
->headers($this->authorization());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多图
|
|
||||||
*
|
|
||||||
* @time 2021年03月03日
|
|
||||||
* @param $title
|
|
||||||
* @param array $value
|
|
||||||
* @return \FormBuilder\UI\Elm\Components\Upload
|
|
||||||
*/
|
|
||||||
public function images(string $title, array $value = []): Upload
|
|
||||||
{
|
|
||||||
return self::uploadImages('image', $title, $this->uploadImageUrl(), $value)
|
|
||||||
->uploadName('image')
|
|
||||||
->data(['none' => ''])
|
|
||||||
->headers($this->authorization());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace catchAdmin\cms\form;
|
|
||||||
|
|
||||||
|
|
||||||
class FormFactory
|
|
||||||
{
|
|
||||||
public static function create($name)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,7 +8,9 @@ use catcher\generate\factory\Migration;
|
|||||||
use catcher\generate\factory\Model;
|
use catcher\generate\factory\Model;
|
||||||
use catcher\generate\factory\Route;
|
use catcher\generate\factory\Route;
|
||||||
use catcher\generate\factory\SQL;
|
use catcher\generate\factory\SQL;
|
||||||
|
use catcher\generate\support\Table;
|
||||||
use catcher\library\Composer;
|
use catcher\library\Composer;
|
||||||
|
use catcher\Utils;
|
||||||
use think\facade\Db;
|
use think\facade\Db;
|
||||||
|
|
||||||
class Generator
|
class Generator
|
||||||
@ -22,8 +24,11 @@ class Generator
|
|||||||
* @time 2020年04月29日
|
* @time 2020年04月29日
|
||||||
* @param $params
|
* @param $params
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws \think\db\exception\DbException
|
||||||
|
* @throws \think\db\exception\ModelNotFoundException
|
||||||
|
* @throws \think\db\exception\DataNotFoundException
|
||||||
*/
|
*/
|
||||||
public function done($params)
|
public function done($params): array
|
||||||
{
|
{
|
||||||
// 判断是否安装了扩展包
|
// 判断是否安装了扩展包
|
||||||
if (!(new Composer)->hasPackage(self::NEED_PACKAGE)) {
|
if (!(new Composer)->hasPackage(self::NEED_PACKAGE)) {
|
||||||
@ -49,7 +54,7 @@ class Generator
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($params['create_table']) {
|
if ($params['create_table']) {
|
||||||
$table = (new SQL)->done($model);
|
(new SQL)->done($model);
|
||||||
array_push($message, 'table created successfully');
|
array_push($message, 'table created successfully');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,18 +70,18 @@ class Generator
|
|||||||
|
|
||||||
// 只有创建了 Controller 最后成功才写入 route
|
// 只有创建了 Controller 最后成功才写入 route
|
||||||
if ($params['create_controller']) {
|
if ($params['create_controller']) {
|
||||||
(new Route())->controller($controller['controller'])
|
(new Route)->controller($controller['controller'])
|
||||||
->restful($controller['restful'])
|
->restful($controller['restful'])
|
||||||
// ->methods((new Controller())->parseOtherMethods($controller['other_function']))
|
|
||||||
->done();
|
->done();
|
||||||
}
|
}
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
if (!$exception instanceof TableExistException) {
|
||||||
|
$this->rollback($files, $migration);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (\Exception $exception) {
|
throw new FailedException($exception->getMessage());
|
||||||
$this->rollback($files, $migration, $table);
|
|
||||||
throw new FailedException($exception->getFile() . $exception->getLine() . $exception->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,9 +102,9 @@ class Generator
|
|||||||
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 'controller':
|
case 'controller':
|
||||||
return (new Controller())->getContent($controller);
|
return (new Controller)->getContent($controller);
|
||||||
case 'model':
|
case 'model':
|
||||||
return (new Model())->getContent($model);
|
return (new Model)->getContent($model);
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -113,7 +118,7 @@ class Generator
|
|||||||
* @param $params
|
* @param $params
|
||||||
* @return array[]
|
* @return array[]
|
||||||
*/
|
*/
|
||||||
protected function parseParams($params)
|
protected function parseParams($params): array
|
||||||
{
|
{
|
||||||
$module = $params['controller']['module'] ?? false;
|
$module = $params['controller']['module'] ?? false;
|
||||||
|
|
||||||
@ -126,14 +131,14 @@ class Generator
|
|||||||
'model' => $params['controller']['model'] ?? '',
|
'model' => $params['controller']['model'] ?? '',
|
||||||
'controller' => $params['controller']['controller'] ?? '',
|
'controller' => $params['controller']['controller'] ?? '',
|
||||||
'restful' => $params['controller']['restful'],
|
'restful' => $params['controller']['restful'],
|
||||||
// 'other_function' => $params['controller']['other_function'],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$table = $params['controller']['table'] ?? '';
|
$table = $params['controller']['table'] ?? '';
|
||||||
if ($table) {
|
|
||||||
$table = \config('database.connections.mysql.prefix') . $table;
|
|
||||||
|
|
||||||
|
if ($table) {
|
||||||
|
$table = Utils::tableWithPrefix($table);
|
||||||
}
|
}
|
||||||
|
|
||||||
$model = [
|
$model = [
|
||||||
'table' => $table,
|
'table' => $table,
|
||||||
'model' => $params['controller']['model'] ?? '',
|
'model' => $params['controller']['model'] ?? '',
|
||||||
@ -151,17 +156,14 @@ class Generator
|
|||||||
*
|
*
|
||||||
* @param $files
|
* @param $files
|
||||||
* @param $migration
|
* @param $migration
|
||||||
* @param $table
|
|
||||||
* @throws \think\db\exception\DataNotFoundException
|
* @throws \think\db\exception\DataNotFoundException
|
||||||
* @throws \think\db\exception\DbException
|
* @throws \think\db\exception\DbException
|
||||||
* @throws \think\db\exception\ModelNotFoundException
|
* @throws \think\db\exception\ModelNotFoundException
|
||||||
* @author JaguarJack <njphper@gmail.com>
|
|
||||||
* @date 2020/7/11
|
|
||||||
*/
|
*/
|
||||||
protected function rollback($files, $migration, $table)
|
protected function rollback($files, $migration)
|
||||||
{
|
{
|
||||||
if ((new SQL())->hasTableExists($table)) {
|
if (Table::exist()) {
|
||||||
Db::query(sprintf('drop table %s', $table));
|
Table::drop();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
|
9
extend/catcher/generate/TableExistException.php
Normal file
9
extend/catcher/generate/TableExistException.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
namespace catcher\generate;
|
||||||
|
|
||||||
|
use catcher\exceptions\CatchException;
|
||||||
|
|
||||||
|
class TableExistException extends CatchException
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -36,7 +36,7 @@ class Controller extends Factory
|
|||||||
* @param $params
|
* @param $params
|
||||||
* @return bool|string|string[]
|
* @return bool|string|string[]
|
||||||
*/
|
*/
|
||||||
public function done($params)
|
public function done(array $params)
|
||||||
{
|
{
|
||||||
// 写入成功之后
|
// 写入成功之后
|
||||||
$controllerPath = $this->getGeneratePath($params['controller']);
|
$controllerPath = $this->getGeneratePath($params['controller']);
|
||||||
|
@ -6,7 +6,7 @@ use think\facade\Db;
|
|||||||
|
|
||||||
abstract class Factory
|
abstract class Factory
|
||||||
{
|
{
|
||||||
abstract public function done($param);
|
abstract public function done(array $params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parse psr4 path
|
* parse psr4 path
|
||||||
@ -28,7 +28,7 @@ abstract class Factory
|
|||||||
* @param $filePath
|
* @param $filePath
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getGeneratePath($filePath)
|
protected function getGeneratePath($filePath): string
|
||||||
{
|
{
|
||||||
$path = explode('\\', $filePath);
|
$path = explode('\\', $filePath);
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ abstract class Factory
|
|||||||
* @param $filePath
|
* @param $filePath
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getModulePath($filePath)
|
public function getModulePath($filePath): string
|
||||||
{
|
{
|
||||||
$path = explode('\\', $filePath);
|
$path = explode('\\', $filePath);
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ abstract class Factory
|
|||||||
* @param $filename
|
* @param $filename
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function parseFilename($filename)
|
public function parseFilename($filename): array
|
||||||
{
|
{
|
||||||
$namespace = explode('\\', $filename);
|
$namespace = explode('\\', $filename);
|
||||||
|
|
||||||
@ -89,7 +89,7 @@ abstract class Factory
|
|||||||
* @param $table
|
* @param $table
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasTableExists($table)
|
public function hasTableExists($table): bool
|
||||||
{
|
{
|
||||||
$tables = Db::connect()->getTables();
|
$tables = Db::connect()->getTables();
|
||||||
|
|
||||||
|
@ -11,7 +11,15 @@ use think\helper\Str;
|
|||||||
|
|
||||||
class Migration extends Factory
|
class Migration extends Factory
|
||||||
{
|
{
|
||||||
public function done($params)
|
/**
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param array $params
|
||||||
|
* @throws \Doctrine\DBAL\DBALException
|
||||||
|
* @throws \JaguarJack\MigrateGenerator\Exceptions\EmptyInDatabaseException
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function done(array $params): string
|
||||||
{
|
{
|
||||||
[$module, $tableName] = $params;
|
[$module, $tableName] = $params;
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ class Model extends Factory
|
|||||||
* @param $params
|
* @param $params
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function done($params)
|
public function done(array $params): string
|
||||||
{
|
{
|
||||||
$content = $this->getContent($params);
|
$content = $this->getContent($params);
|
||||||
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
<?php
|
|
@ -16,7 +16,7 @@ class Route extends Factory
|
|||||||
|
|
||||||
protected $methods = [];
|
protected $methods = [];
|
||||||
|
|
||||||
public function done($params = [])
|
public function done(array $params = [])
|
||||||
{
|
{
|
||||||
$route = [];
|
$route = [];
|
||||||
|
|
||||||
|
@ -1,213 +1,156 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace catcher\generate\factory;
|
namespace catcher\generate\factory;
|
||||||
|
|
||||||
|
|
||||||
use catcher\exceptions\FailedException;
|
use catcher\exceptions\FailedException;
|
||||||
use think\facade\Db;
|
use catcher\generate\support\Table;
|
||||||
|
use catcher\generate\support\TableColumn;
|
||||||
|
use catcher\generate\TableExistException;
|
||||||
|
use Phinx\Db\Adapter\AdapterInterface;
|
||||||
|
|
||||||
class SQL extends Factory
|
class SQL extends Factory
|
||||||
{
|
{
|
||||||
protected $index = '';
|
public function done(array $params)
|
||||||
|
|
||||||
|
|
||||||
public function done($params)
|
|
||||||
{
|
|
||||||
Db::execute($this->createSQL($params));
|
|
||||||
|
|
||||||
// 判断表是否创建成功
|
|
||||||
if (!$this->hasTableExists($params['table'])) {
|
|
||||||
throw new FailedException(sprintf('create table [%s] failed', $params['table']));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $params['table'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* create table sql
|
|
||||||
*
|
|
||||||
* @time 2020年04月28日
|
|
||||||
* @param $params
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function createSQL($params)
|
|
||||||
{
|
{
|
||||||
if (!$params['table'] ?? false) {
|
if (!$params['table'] ?? false) {
|
||||||
throw new FailedException('table name has lost~');
|
throw new FailedException('table name has lost~');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->hasTableExists($params['table'])) {
|
$this->createTable($params);
|
||||||
throw new FailedException(sprintf('table [%s] has existed', $params['table']));
|
|
||||||
}
|
|
||||||
|
|
||||||
$extra = $params['extra'];
|
$this->createTableColumns($params['sql'], $params['extra']);
|
||||||
// 主键
|
|
||||||
$createSql = $this->primaryKey($extra['primary_key']);
|
|
||||||
// 字段
|
|
||||||
$ifHaveNotFields = true;
|
|
||||||
foreach ($params['sql'] as $sql) {
|
|
||||||
if (!$sql['field'] || !$sql['type']) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$ifHaveNotFields = false;
|
|
||||||
$createSql .= $this->parseSQL($sql);
|
|
||||||
}
|
|
||||||
// 如果没有设置数据库字段
|
|
||||||
if ($ifHaveNotFields) {
|
|
||||||
throw new FailedException('Do you have set mysql fields?');
|
|
||||||
}
|
|
||||||
// 创建人
|
|
||||||
if ($extra['creator_id'] ?? false) {
|
|
||||||
$createSql .= $this->parseCreatorId();
|
|
||||||
}
|
|
||||||
// 创建时间
|
|
||||||
if ($extra['created_at'] ?? false) {
|
|
||||||
$createSql .= $this->parseCreatedAt();
|
|
||||||
}
|
|
||||||
// 软删除
|
|
||||||
if ($extra['soft_delete'] ?? false) {
|
|
||||||
$createSql .= $this->parseDeletedAt();
|
|
||||||
}
|
|
||||||
// 索引
|
|
||||||
if ($this->index) {
|
|
||||||
$createSql .= $this->index;
|
|
||||||
}
|
|
||||||
$createSql = rtrim($createSql, ',' . PHP_EOL);
|
|
||||||
|
|
||||||
// 创建表 SQL
|
$this->createTableIndex($this->getIndexColumns($params['sql']));
|
||||||
return $this->createTable($params['table'], $createSql, $extra['engine'], 'utf8mb4', $extra['comment']);
|
|
||||||
|
return $params['table'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parse sql
|
* 创建表
|
||||||
*
|
*
|
||||||
* @time 2020年04月27日
|
* @time 2021年03月13日
|
||||||
* @param $sql
|
* @param array $params
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function parseSQL($sql)
|
|
||||||
{
|
|
||||||
|
|
||||||
// 解析索引
|
|
||||||
if ($sql['index']) {
|
|
||||||
$this->parseIndex($sql['index'], $sql['field']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 字段
|
|
||||||
$_sql[] = sprintf('`%s`', $sql['field']);
|
|
||||||
// 类型
|
|
||||||
$_sql[] = $sql['type'] . ($sql['length'] ? sprintf('(%s)', $sql['length']) : '');
|
|
||||||
|
|
||||||
if ($sql['unsigned']) {
|
|
||||||
$_sql[] = 'unsigned';
|
|
||||||
}
|
|
||||||
// 默认值
|
|
||||||
$default = trim(trim($sql['default'], '\''));
|
|
||||||
if (!$sql['nullable']) {
|
|
||||||
$_sql[] = 'not null';
|
|
||||||
if ($default == '' || $default === '') {
|
|
||||||
if (!$this->doNotNeedDefaultValueType($sql['type'])) {
|
|
||||||
$_sql[] = ' default \'\'';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (strpos('int', $sql['type']) === false) {
|
|
||||||
$_sql[] = ' default ' . (int)$default ;
|
|
||||||
} else {
|
|
||||||
$_sql[] = ' default ' . $default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 字段注释
|
|
||||||
$_sql[] = $sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : '';
|
|
||||||
|
|
||||||
return implode(' ', $_sql) . ','. PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parse primary key
|
|
||||||
*
|
|
||||||
* @time 2020年04月27日
|
|
||||||
* @param $id
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function primaryKey($id)
|
|
||||||
{
|
|
||||||
return sprintf('`%s`', $id) . ' int unsigned not null auto_increment primary key,'. PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parse created_at & updated_at
|
|
||||||
*
|
|
||||||
* @time 2020年04月27日
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function parseCreatedAt()
|
|
||||||
{
|
|
||||||
return sprintf('`created_at` int unsigned not null default 0 comment \'%s\',', '创建时间') . PHP_EOL .
|
|
||||||
sprintf('`updated_at` int unsigned not null default 0 comment \'%s\',', '更新时间') . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parse deleted_at
|
|
||||||
*
|
|
||||||
* @time 2020年04月27日
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function parseDeletedAt()
|
|
||||||
{
|
|
||||||
return sprintf('`deleted_at` int unsigned not null default 0 comment \'%s\',', '软删除') . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parse creator id
|
|
||||||
*
|
|
||||||
* @time 2020年07月01日
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function parseCreatorId()
|
|
||||||
{
|
|
||||||
return sprintf('`creator_id` int unsigned not null default 0 comment \'%s\',', '创建人ID') . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* created table
|
|
||||||
*
|
|
||||||
* @time 2020年04月27日
|
|
||||||
* @param $table
|
|
||||||
* @param $sql
|
|
||||||
* @param string $engine
|
|
||||||
* @param string $charset
|
|
||||||
* @param string $comment
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function createTable($table, $sql, $engine='InnoDB', $charset = 'utf8mb4', $comment = '')
|
|
||||||
{
|
|
||||||
return sprintf('create table `%s`(' . PHP_EOL.
|
|
||||||
'%s)'.PHP_EOL .
|
|
||||||
'engine=%s default charset=%s comment=\'%s\'', $table, $sql, $engine, $charset, $comment);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parse index
|
|
||||||
*
|
|
||||||
* @time 2020年04月27日
|
|
||||||
* @param $index
|
|
||||||
* @param $field
|
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function parseIndex($index, $field)
|
protected function createTable(array $params)
|
||||||
{
|
{
|
||||||
if ($index == 'unique') {
|
$table = new Table($params['table']);
|
||||||
$this->index .= "unique index unique_$field($field)," . PHP_EOL;
|
|
||||||
} elseif ($index == 'index') {
|
if ($table::exist()) {
|
||||||
$this->index .= "index($field),". PHP_EOL;
|
throw new TableExistException(sprintf('Table [%s] has been existed', $params['table']));
|
||||||
} elseif ($index == 'fulltext') {
|
}
|
||||||
$this->index .= "fulltext key fulltext_$field($field)," . PHP_EOL;
|
|
||||||
} elseif ($index == 'spatial') {
|
if(!$table::create(
|
||||||
$this->index .= "spatial index spatial_$field($field),". PHP_EOL;
|
$params['extra']['primary_key'],
|
||||||
|
$params['extra']['engine'],
|
||||||
|
$params['extra']['comment']
|
||||||
|
)) {
|
||||||
|
throw new FailedException(sprintf('created table [%s] failed', $params['table']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 columns
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param $columns
|
||||||
|
* @param $extra
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createTableColumns($columns, $extra)
|
||||||
|
{
|
||||||
|
$tableColumns = [];
|
||||||
|
|
||||||
|
foreach ($columns as $column) {
|
||||||
|
if ($column['type'] === AdapterInterface::PHINX_TYPE_DECIMAL) {
|
||||||
|
$tableColumn = (new TableColumn)->{$column['type']}($column['field']);
|
||||||
|
} else if ($column['type'] === AdapterInterface::PHINX_TYPE_ENUM || $column['type'] === AdapterInterface::PHINX_TYPE_SET) {
|
||||||
|
$tableColumn = (new TableColumn)->{$column['type']}($column['field'], $column['default']);
|
||||||
|
}else {
|
||||||
|
$tableColumn = (new TableColumn)->{$column['type']}($column['field'], $column['length'] ?? 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($column['nullable']) {
|
||||||
|
$tableColumn->setNullable();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($column['unsigned']) {
|
||||||
|
$tableColumn->setUnsigned();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ($column['comment']) {
|
||||||
|
$tableColumn->setComment($column['comment']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->doNotNeedDefaultValueType($column['type'])) {
|
||||||
|
$tableColumn->setDefault($column['default']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tableColumns[] = $tableColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ($extra['created_at']) {
|
||||||
|
$tableColumns[] = $this->createCreateAtColumn();
|
||||||
|
$tableColumns[] = $this->createUpdateAtColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($extra['soft_delete']) {
|
||||||
|
$tableColumns[] = $this->createDeleteAtColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($extra['creator_id']) {
|
||||||
|
$tableColumns[] = $this->createCreatorIdColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($tableColumns as $column) {
|
||||||
|
if (!Table::addColumn($column)) {
|
||||||
|
throw new FailedException('创建失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 index
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param $indexes
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createTableIndex($indexes)
|
||||||
|
{
|
||||||
|
$method = [
|
||||||
|
'index' => 'addIndex',
|
||||||
|
'unique' => 'addUniqueIndex',
|
||||||
|
'fulltext' => 'addFulltextIndex',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($indexes as $type => $index) {
|
||||||
|
foreach ($index as $i) {
|
||||||
|
Table::{$method[$type]}($i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取有索引的 column
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param $columns
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getIndexColumns($columns): array
|
||||||
|
{
|
||||||
|
$index = [];
|
||||||
|
|
||||||
|
foreach ($columns as $column) {
|
||||||
|
if ($column['index']) {
|
||||||
|
$index[$column['index']][] = $column['field'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $index;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 不需要默认值
|
* 不需要默认值
|
||||||
@ -216,12 +159,64 @@ class SQL extends Factory
|
|||||||
* @time 2020年10月23日
|
* @time 2020年10月23日
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function doNotNeedDefaultValueType(string $type)
|
protected function doNotNeedDefaultValueType(string $type): bool
|
||||||
{
|
{
|
||||||
return in_array($type, [
|
return in_array($type, [
|
||||||
'blob', 'text', 'geometry', 'json',
|
'blob', 'text', 'geometry', 'json',
|
||||||
'tinytext', 'mediumtext', 'longtext',
|
'tinytext', 'mediumtext', 'longtext',
|
||||||
'tinyblob', 'mediumblob', 'longblob'
|
'tinyblob', 'mediumblob', 'longblob', 'enum', 'set',
|
||||||
|
'date', 'datetime', 'time', 'timestamp', 'year'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @return \think\migration\db\Column
|
||||||
|
*/
|
||||||
|
protected function createCreateAtColumn(): \think\migration\db\Column
|
||||||
|
{
|
||||||
|
return (new TableColumn)->int('created_at', 10)
|
||||||
|
->setUnsigned()
|
||||||
|
->setDefault(0)
|
||||||
|
->setComment('创建时间');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新时间
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @return \think\migration\db\Column
|
||||||
|
*/
|
||||||
|
protected function createUpdateAtColumn(): \think\migration\db\Column
|
||||||
|
{
|
||||||
|
return (new TableColumn)->int('updated_at', 10)
|
||||||
|
->setUnsigned()->setDefault(0)->setComment('更新时间');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 软删除
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @return \think\migration\db\Column
|
||||||
|
*/
|
||||||
|
protected function createDeleteAtColumn(): \think\migration\db\Column
|
||||||
|
{
|
||||||
|
return (new TableColumn)->int('deleted_at', 10)
|
||||||
|
->setUnsigned()->setDefault(0)->setComment('软删除字段');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @return \think\migration\db\Column
|
||||||
|
*/
|
||||||
|
protected function createCreatorIdColumn(): \think\migration\db\Column
|
||||||
|
{
|
||||||
|
return (new TableColumn)->int('creator_id', 10)
|
||||||
|
->setUnsigned()->setDefault(0)->setComment('创建人ID');
|
||||||
|
}
|
||||||
}
|
}
|
272
extend/catcher/generate/support/Table.php
Normal file
272
extend/catcher/generate/support/Table.php
Normal file
@ -0,0 +1,272 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | CatchAdmin [Just Like ~ ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
namespace catcher\generate\support;
|
||||||
|
|
||||||
|
use catcher\exceptions\FailedException;
|
||||||
|
use catcher\Utils;
|
||||||
|
use Phinx\Db\Adapter\AdapterFactory;
|
||||||
|
use think\facade\Db;
|
||||||
|
use think\migration\db\Column;
|
||||||
|
|
||||||
|
class Table
|
||||||
|
{
|
||||||
|
protected static $adapter = null;
|
||||||
|
|
||||||
|
protected static $table = null;
|
||||||
|
|
||||||
|
protected static $tableName = null;
|
||||||
|
|
||||||
|
public function __construct(string $tableName)
|
||||||
|
{
|
||||||
|
self::$tableName = $tableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 table 对象
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @return \think\migration\db\Table
|
||||||
|
*/
|
||||||
|
protected static function getTable(): \think\migration\db\Table
|
||||||
|
{
|
||||||
|
if (self::$table) {
|
||||||
|
return self::$table;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new \think\migration\db\Table(Utils::tableWithPrefix(self::$tableName)))->setAdapter(self::getAdapter());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create table
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @param string $primaryKey
|
||||||
|
* @param string $engine
|
||||||
|
* @param string $comment
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function create(string $primaryKey, string $engine, string $comment): bool
|
||||||
|
{
|
||||||
|
self::getTable()
|
||||||
|
->setId($primaryKey)
|
||||||
|
->setPrimaryKey($primaryKey)
|
||||||
|
->setEngine($engine)
|
||||||
|
->setComment($comment)
|
||||||
|
->setCollation('utf8mb4_general_ci')
|
||||||
|
->create();
|
||||||
|
|
||||||
|
return self::exist();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表是否存在
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function exist(): bool
|
||||||
|
{
|
||||||
|
return self::getTable()->exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除表
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function drop(): bool
|
||||||
|
{
|
||||||
|
if (!self::exist()) {
|
||||||
|
throw new FailedException(sprintf('table [%s] not exist, drop failed', self::$tableName));
|
||||||
|
}
|
||||||
|
|
||||||
|
self::getTable()->drop();
|
||||||
|
|
||||||
|
return self::exist();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增 column
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @param mixed $column
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function addColumn($column): bool
|
||||||
|
{
|
||||||
|
if ($column instanceof \Closure) {
|
||||||
|
$column = $column();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$column instanceof Column) {
|
||||||
|
throw new FailedException('Column Must Be "think\migration\db\Column');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增字段
|
||||||
|
self::getTable()
|
||||||
|
->addColumn($column)
|
||||||
|
->update();
|
||||||
|
|
||||||
|
return self::hasColumn($column->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否存在 column
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $column
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function hasColumn(string $column): bool
|
||||||
|
{
|
||||||
|
return self::getTable()->hasColumn($column);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取表结构信息
|
||||||
|
*
|
||||||
|
* @time 2021年03月05日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function columns(): array
|
||||||
|
{
|
||||||
|
return array_values(Db::getFields(Utils::tableWithPrefix(self::$tableName)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除 column
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @param string $column
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function dropColumn(string $column): bool
|
||||||
|
{
|
||||||
|
self::getTable()->removeColumn($column)->update();
|
||||||
|
|
||||||
|
if (self::getTable()->hasColumn($column)) {
|
||||||
|
throw new FailedException('remove column ['.$column.'] failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 唯一索引
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string| array $columns
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function addUniqueIndex($columns)
|
||||||
|
{
|
||||||
|
self::getTable()->addIndex($columns, [
|
||||||
|
'unique' => true, 'name' => self::$tableName . '_' . (is_string($columns) ? $columns : implode('_', $columns))
|
||||||
|
])->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加普通索引
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string| array $columns
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function addIndex($columns)
|
||||||
|
{
|
||||||
|
self::getTable()->addIndex($columns, [
|
||||||
|
'name' => self::$tableName . '_' . (is_string($columns) ? $columns : implode('_', $columns))
|
||||||
|
])->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加全文索引
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string| array $columns
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function addFulltextIndex($columns)
|
||||||
|
{
|
||||||
|
self::getTable()->addIndex($columns, [
|
||||||
|
'type' => 'fulltext',
|
||||||
|
'name' => self::$tableName . '_' . (is_string($columns) ? $columns : implode('_', $columns))
|
||||||
|
])->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取适配器
|
||||||
|
*
|
||||||
|
* @time 2021年03月04日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function getAdapter()
|
||||||
|
{
|
||||||
|
if (self::$adapter) {
|
||||||
|
return self::$adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = self::getDbConfig();
|
||||||
|
|
||||||
|
$adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
|
||||||
|
|
||||||
|
if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
|
||||||
|
$adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$adapter = $adapter;
|
||||||
|
|
||||||
|
return $adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据库配置
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected static function getDbConfig(): array
|
||||||
|
{
|
||||||
|
$default = app()->config->get('database.default');
|
||||||
|
|
||||||
|
$config = app()->config->get("database.connections.{$default}");
|
||||||
|
|
||||||
|
if (0 == $config['deploy']) {
|
||||||
|
$dbConfig = [
|
||||||
|
'adapter' => $config['type'],
|
||||||
|
'host' => $config['hostname'],
|
||||||
|
'name' => $config['database'],
|
||||||
|
'user' => $config['username'],
|
||||||
|
'pass' => $config['password'],
|
||||||
|
'port' => $config['hostport'],
|
||||||
|
'charset' => $config['charset'],
|
||||||
|
'table_prefix' => $config['prefix'],
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
$dbConfig = [
|
||||||
|
'adapter' => explode(',', $config['type'])[0],
|
||||||
|
'host' => explode(',', $config['hostname'])[0],
|
||||||
|
'name' => explode(',', $config['database'])[0],
|
||||||
|
'user' => explode(',', $config['username'])[0],
|
||||||
|
'pass' => explode(',', $config['password'])[0],
|
||||||
|
'port' => explode(',', $config['hostport'])[0],
|
||||||
|
'charset' => explode(',', $config['charset'])[0],
|
||||||
|
'table_prefix' => explode(',', $config['prefix'])[0],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$table = app()->config->get('database.migration_table', 'migrations');
|
||||||
|
|
||||||
|
$dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table;
|
||||||
|
|
||||||
|
return $dbConfig;
|
||||||
|
}
|
||||||
|
}
|
376
extend/catcher/generate/support/TableColumn.php
Normal file
376
extend/catcher/generate/support/TableColumn.php
Normal file
@ -0,0 +1,376 @@
|
|||||||
|
<?php
|
||||||
|
namespace catcher\generate\support;
|
||||||
|
|
||||||
|
use catcher\Utils;
|
||||||
|
use Phinx\Db\Adapter\AdapterInterface;
|
||||||
|
use Phinx\Db\Adapter\MysqlAdapter;
|
||||||
|
use think\migration\db\Column;
|
||||||
|
|
||||||
|
class TableColumn
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* tinyint
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function tinyint(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::tinyInteger($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* boolean
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function boolean(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::boolean($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* smallint
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function smallint(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::smallInteger($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* int
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function int(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::integer($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mediumint
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function mediumint(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::mediumInteger($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bigint
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function bigint(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::bigInteger($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 浮点数
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function float(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::float($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 浮点数
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $precision
|
||||||
|
* @param int $scale
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function decimal(string $name, $precision = 8, $scale = 2): Column
|
||||||
|
{
|
||||||
|
return Column::decimal($name, $precision, $scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* string 类型
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function varchar(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::string($name, $length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* char
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function char(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::char($name, $length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 普通文本
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function text(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::text($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小文本
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function tinytext(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_TEXT, ['length' => MysqlAdapter::TEXT_TINY]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中长文本
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function mediumtext(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::mediumText($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超大文本
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function longtext(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::longText($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* binary
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function binary(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::binary($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* varbinary
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function varbinary(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_VARBINARY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tinyblob
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function tinyblob(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_TINY]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* blob
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function blob(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_REGULAR]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mediumblob
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function mediumblob(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_MEDIUM]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* longblob
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function longblob(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_LONG]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间类型
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function date(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::date($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期时间
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function datetime(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::dateTime($name)->setOptions(['default' => 'CURRENT_TIMESTAMP']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实践格式
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function time(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::time($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间戳
|
||||||
|
*
|
||||||
|
* @time 2021年03月08日
|
||||||
|
* @param string $name
|
||||||
|
* @param int $length
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function timestamp(string $name, int $length): Column
|
||||||
|
{
|
||||||
|
return Column::timestamp($name)->setOptions(['default' => 'CURRENT_TIMESTAMP']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enum 类型
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param $name
|
||||||
|
* @param $values
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function enum(string $name, $values): Column
|
||||||
|
{
|
||||||
|
return Column::enum($name, is_string($values) ? Utils::stringToArrayBy($values) : $values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set 类型
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @param $values
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function set(string $name, $values): Column
|
||||||
|
{
|
||||||
|
$values = is_string($values) ? Utils::stringToArrayBy($values) : $values;
|
||||||
|
|
||||||
|
return Column::make($name, AdapterInterface::PHINX_TYPE_SET, compact('values'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json 穿
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function json(string $name): Column
|
||||||
|
{
|
||||||
|
return Column::json($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* uuid
|
||||||
|
*
|
||||||
|
* @time 2021年03月13日
|
||||||
|
* @param string $name
|
||||||
|
* @return Column
|
||||||
|
*/
|
||||||
|
public function uuid(string $name): Column
|
||||||
|
{
|
||||||
|
return Column::uuid($name);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user