fixed:SQL解析错误

This commit is contained in:
JaguarJack 2020-10-23 15:45:47 +08:00
parent e0b2aafd2c
commit 3d58942844

View File

@ -72,6 +72,7 @@ class SQL extends Factory
$createSql .= $this->index; $createSql .= $this->index;
} }
$createSql = rtrim($createSql, ',' . PHP_EOL); $createSql = rtrim($createSql, ',' . PHP_EOL);
// 创建表 SQL // 创建表 SQL
return $this->createTable($params['table'], $createSql, $extra['engine'], 'utf8mb4', $extra['comment']); return $this->createTable($params['table'], $createSql, $extra['engine'], 'utf8mb4', $extra['comment']);
} }
@ -104,7 +105,9 @@ class SQL extends Factory
if (!$sql['nullable']) { if (!$sql['nullable']) {
$_sql[] = 'not null'; $_sql[] = 'not null';
if ($default == '' || $default === '') { if ($default == '' || $default === '') {
$_sql[] = ' default \'\''; if (!$this->doNotNeedDefaultValueType($sql['type'])) {
$_sql[] = ' default \'\'';
}
} else { } else {
if (strpos('int', $sql['type']) === false) { if (strpos('int', $sql['type']) === false) {
$_sql[] = ' default ' . (int)$default ; $_sql[] = ' default ' . (int)$default ;
@ -117,7 +120,6 @@ class SQL extends Factory
// 字段注释 // 字段注释
$_sql[] = $sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : ''; $_sql[] = $sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : '';
return implode(' ', $_sql) . ','. PHP_EOL; return implode(' ', $_sql) . ','. PHP_EOL;
} }
@ -142,7 +144,7 @@ class SQL extends Factory
protected function parseCreatedAt() protected function parseCreatedAt()
{ {
return sprintf('`created_at` int unsigned not null default 0 comment \'%s\',', '创建时间') . PHP_EOL . 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; sprintf('`updated_at` int unsigned not null default 0 comment \'%s\',', '更新时间') . PHP_EOL;
} }
/** /**
@ -181,8 +183,8 @@ class SQL extends Factory
protected function createTable($table, $sql, $engine='InnoDB', $charset = 'utf8mb4', $comment = '') protected function createTable($table, $sql, $engine='InnoDB', $charset = 'utf8mb4', $comment = '')
{ {
return sprintf('create table `%s`(' . PHP_EOL. return sprintf('create table `%s`(' . PHP_EOL.
'%s)'.PHP_EOL . '%s)'.PHP_EOL .
'engine=%s default charset=%s comment=\'%s\'', $table, $sql, $engine, $charset, $comment); 'engine=%s default charset=%s comment=\'%s\'', $table, $sql, $engine, $charset, $comment);
} }
/** /**
@ -205,4 +207,21 @@ class SQL extends Factory
$this->index .= "spatial index spatial_$field($field),". PHP_EOL; $this->index .= "spatial index spatial_$field($field),". PHP_EOL;
} }
} }
/**
* 不需要默认值
*
* @param string $type
* @time 2020年10月23日
* @return bool
*/
protected function doNotNeedDefaultValueType(string $type)
{
return in_array($type, [
'blob', 'text', 'geometry', 'json',
'tinytext', 'mediumtext', 'longtext',
'tinyblob', 'mediumblob', 'longblob'
]);
}
} }