2020-04-28 16:34:17 +08:00
|
|
|
<?php
|
2020-04-28 22:02:03 +08:00
|
|
|
namespace catcher\generate\factory;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
|
|
|
2020-04-28 22:02:03 +08:00
|
|
|
use catcher\exceptions\FailedException;
|
|
|
|
use think\facade\Db;
|
|
|
|
|
2020-04-30 18:15:48 +08:00
|
|
|
class SQL extends Factory
|
2020-04-28 16:34:17 +08:00
|
|
|
{
|
|
|
|
protected $index = '';
|
|
|
|
|
|
|
|
|
|
|
|
public function done($params)
|
|
|
|
{
|
2020-04-28 22:02:03 +08:00
|
|
|
Db::execute($this->createSQL($params));
|
|
|
|
|
|
|
|
// 判断表是否创建成功
|
|
|
|
if (!$this->hasTableExists($params['table'])) {
|
2020-05-20 11:03:21 +08:00
|
|
|
throw new FailedException(sprintf('create table [%s] failed', $params['table']));
|
2020-04-28 22:02:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create table sql
|
|
|
|
*
|
|
|
|
* @time 2020年04月28日
|
|
|
|
* @param $params
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function createSQL($params)
|
|
|
|
{
|
|
|
|
if (!$params['table'] ?? false) {
|
|
|
|
throw new FailedException('table name has lost~');
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:03:21 +08:00
|
|
|
if ($this->hasTableExists($params['table'])) {
|
2020-04-28 22:02:03 +08:00
|
|
|
throw new FailedException(sprintf('table [%s] has existed', $params['table']));
|
|
|
|
}
|
2020-04-28 16:34:17 +08:00
|
|
|
|
2020-04-28 22:02:03 +08:00
|
|
|
$extra = $params['extra'];
|
2020-04-28 16:34:17 +08:00
|
|
|
// 主键
|
|
|
|
$createSql = $this->primaryKey($extra['primary_key']);
|
|
|
|
// 字段
|
|
|
|
$ifHaveNotFields = true;
|
2020-04-28 22:02:03 +08:00
|
|
|
foreach ($params['sql'] as $sql) {
|
2020-04-28 16:34:17 +08:00
|
|
|
if (!$sql['field'] || !$sql['type']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$ifHaveNotFields = false;
|
|
|
|
$createSql .= $this->parseSQL($sql);
|
|
|
|
}
|
|
|
|
// 如果没有设置数据库字段
|
|
|
|
if ($ifHaveNotFields) {
|
2020-04-28 22:02:03 +08:00
|
|
|
throw new FailedException('Do you have set mysql fields?');
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
// 创建时间
|
|
|
|
if ($extra['created_at'] ?? false) {
|
|
|
|
$createSql .= $this->parseCreatedAt();
|
|
|
|
}
|
|
|
|
// 软删除
|
|
|
|
if ($extra['soft_delete'] ?? false) {
|
|
|
|
$createSql .= $this->parseDeletedAt();
|
|
|
|
}
|
|
|
|
// 索引
|
|
|
|
if ($this->index) {
|
|
|
|
$createSql .= $this->index;
|
|
|
|
}
|
2020-04-28 22:02:03 +08:00
|
|
|
$createSql = rtrim($createSql, ',' . PHP_EOL);
|
2020-04-28 16:34:17 +08:00
|
|
|
// 创建表 SQL
|
2020-05-20 11:03:21 +08:00
|
|
|
return $this->createTable($params['table'], $createSql, $extra['engine'], 'utf8mb4', $extra['comment']);
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse sql
|
|
|
|
*
|
|
|
|
* @time 2020年04月27日
|
|
|
|
* @param $sql
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function parseSQL($sql)
|
|
|
|
{
|
|
|
|
|
|
|
|
// 解析索引
|
|
|
|
if ($sql['index']) {
|
|
|
|
$this->parseIndex($sql['index'], $sql['field']);
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:52:44 +08:00
|
|
|
// 字段
|
|
|
|
$_sql[] = sprintf('`%s`', $sql['field']);
|
|
|
|
// 类型
|
|
|
|
$_sql[] = $sql['type'] . ($sql['length'] ? sprintf('(%s)', $sql['length']) : '');
|
|
|
|
|
|
|
|
if ($sql['unsigned']) {
|
|
|
|
$_sql[] = 'unsigned';
|
|
|
|
}
|
|
|
|
// 默认值
|
|
|
|
if (!$sql['nullable']) {
|
|
|
|
$_sql[] = 'not null';
|
|
|
|
if (!$sql['default']) {
|
|
|
|
$_sql[] = ' default \'\'';
|
|
|
|
} else {
|
2020-05-20 11:03:21 +08:00
|
|
|
if (strpos('int', $sql['type']) === false) {
|
|
|
|
$_sql[] = ' default "' . $sql['default'] . '"';
|
|
|
|
} else {
|
|
|
|
$_sql[] = ' default ' . $sql['default'];
|
|
|
|
}
|
2020-05-05 21:52:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 11:03:21 +08:00
|
|
|
|
2020-05-05 21:52:44 +08:00
|
|
|
// 字段注释
|
|
|
|
$_sql[] = $sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : '';
|
|
|
|
|
|
|
|
|
|
|
|
return implode(' ', $_sql) . ','. PHP_EOL;
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
protected function parseIndex($index, $field)
|
|
|
|
{
|
|
|
|
if ($index == 'unique') {
|
2020-04-28 22:02:03 +08:00
|
|
|
$this->index .= "unique index unique_$field($field)," . PHP_EOL;
|
2020-04-28 16:34:17 +08:00
|
|
|
} elseif ($index == 'index') {
|
2020-04-28 22:02:03 +08:00
|
|
|
$this->index .= "index($field),". PHP_EOL;
|
2020-04-28 16:34:17 +08:00
|
|
|
} elseif ($index == 'fulltext') {
|
2020-04-28 22:02:03 +08:00
|
|
|
$this->index .= "fulltext key fulltext_$field($field)," . PHP_EOL;
|
2020-04-28 16:34:17 +08:00
|
|
|
} elseif ($index == 'spatial') {
|
2020-04-28 22:02:03 +08:00
|
|
|
$this->index .= "spatial index spatial_$field($field),". PHP_EOL;
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|