Skip to content

Commit

Permalink
Add a tool to convert SQL into table / 增加转换sql为表格的工具
Browse files Browse the repository at this point in the history
  • Loading branch information
star7th committed Mar 21, 2022
1 parent bd792a8 commit 37651c2
Show file tree
Hide file tree
Showing 676 changed files with 35,168 additions and 18 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"jasig/phpcas": "^1.3",
"gregwar/captcha": "1.*",
"async-aws/s3": "^1.10",
"psr/container": "1.1.1"
"psr/container": "1.1.1",
"greenlion/php-sql-parser": "^4.5"
},
"config": {
"vendor-dir": "./server/vendor"
}
}
}
70 changes: 68 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions server/Application/Api/Controller/PageController.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Api\Controller;
use Think\Controller;
use Api\Helper\Convert;

class PageController extends BaseController {

//页面详情
Expand Down Expand Up @@ -405,4 +407,13 @@ public function setLock(){

}

// 转换 SQL 为 Markdown 表格
public function sqlToMarkdownTable(){
$sql = I("sql");
$object = new Convert();
$res = $object->convertSqlToMarkdownTable($sql);
$this->sendResult(array("markdown"=>$res));

}

}
123 changes: 123 additions & 0 deletions server/Application/Api/Helper/Convert.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/*
存放一些转换的逻辑代码,比如从xx格式转成markdown格式
*/

namespace Api\Helper;
use PHPSQLParser\PHPSQLParser;


class Convert {


/**
* 转换 SQL 为 Markdown 表格
*/
public function convertSqlToMarkdownTable($sql)
{

$sql_array = $this->convertSqlToArray($sql);

$headers = [
['字段', '类型', '允许空', '默认', '说明'],
['---', '---', '---', '---', '---',],
];
$markdowns = $sql_array['fields'] ;
array_unshift($markdowns, ...$headers);

$html = "\n- {$sql_array['table']} {$sql_array['comment']}\n\n" ;
foreach ($markdowns as $line) {
$html .= '| ' . implode(' | ', $line) . ' | ' . "\n";
}

return $html."\n";

}

// 把sql转换成解析数组
public function convertSqlToArray($sql){
$return = array(
'table'=>'',// 表名
'comment'=>'', // 注释
'fields'=>array()
);

try {
$parser = new PHPSQLParser();
$parsed = $parser->parse($sql);

if (!isset($parsed['CREATE'])) {
return null;
}

// var_dump($parsed);exit();

if ($parsed['CREATE']['expr_type'] === 'table') {
$fields = $parsed['TABLE']['create-def']['sub_tree'];
$tableName = $parsed['TABLE']['base_expr']; // 表名

foreach ($fields as $field) {
if ($field['sub_tree'][0]['expr_type'] == 'constraint') {
continue;
}

// 如果当前行不是列定义,则没有 sub_tree,比如 PRIMARY KEY(id)
if (!isset($field['sub_tree'][1]['sub_tree'])) {
continue;
}

$type = $length = '';
foreach ($field['sub_tree'][1]['sub_tree'] as $item) {
if ($item['expr_type'] == 'data-type') {
$type = $item['base_expr'] ?? '';
$length = $item['length'] ?? '';
}
}

$name = $field['sub_tree'][0]['base_expr'];
$comment = trim($field['sub_tree'][1]['comment'] ?? '', "'");
$nullable = $field['sub_tree'][1]['nullable'] ?? false;
$default = $field['sub_tree'][1]['default'] ?? '';

$type = empty($length) ? $type : "{$type} ($length)";
$markdowns[] = [trim($name, '`'), $type, $nullable ? 'Y' : 'N',$default , $comment];
$return['fields'][] = array(
'name'=>trim($name, '`') ,
'type'=>$type ,
'nullable'=>$nullable ? '' : '' ,
'default'=>trim($default, "'") ,
'comment'=>$comment?$comment:'-' ,
);

}

$tableComment = '';
$options = $parsed['TABLE']['options'] ?? [];
if (!$options || empty($options)) {
$options = [];
}

foreach ($options as $option) {
$type = strtoupper($option['sub_tree'][0]['base_expr'] ?? '');
if ($type === 'COMMENT') {
// var_dump($option['sub_tree']);exit();
$tableComment = trim($option['sub_tree'][2]['base_expr'] ?? '', "'");
break;
}
}
$return['table'] =trim($tableName, '`') ;// 表名
$return['comment'] = $tableComment ;// 表注释

}
} catch (Exception $ex) {
return "{$ex->getMessage()} @{$ex->getFile()}:{$ex->getLine()}";
}



return $return ;


}

}
17 changes: 13 additions & 4 deletions server/vendor/composer/InstalledVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class InstalledVersions
private static $installed = array (
'root' =>
array (
'pretty_version' => 'v2.10.1',
'version' => '2.10.1.0',
'pretty_version' => 'v2.10.3',
'version' => '2.10.3.0',
'aliases' =>
array (
),
Expand All @@ -53,6 +53,15 @@ class InstalledVersions
),
'reference' => 'cddc990e50f6db96bab5d1e9e2f814c3d3a9d00b',
),
'greenlion/php-sql-parser' =>
array (
'pretty_version' => 'v4.5.0',
'version' => '4.5.0.0',
'aliases' =>
array (
),
'reference' => 'a5d5c292d97271c95140192e6f0e962916e39b50',
),
'gregwar/captcha' =>
array (
'pretty_version' => 'v1.1.9',
Expand Down Expand Up @@ -218,8 +227,8 @@ class InstalledVersions
),
'showdoc/showdoc' =>
array (
'pretty_version' => 'v2.10.1',
'version' => '2.10.1.0',
'pretty_version' => 'v2.10.3',
'version' => '2.10.3.0',
'aliases' =>
array (
),
Expand Down
2 changes: 1 addition & 1 deletion server/vendor/composer/autoload_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
'6e3fae29631ef280660b3cdad06f25a8' => $vendorDir . '/symfony/deprecation-contracts/function.php',
'7b11c4dc42b3b3023073cb14e519683c' => $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php',
'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php',
'25072dd6e2470089de65ae7bf11d3109' => $vendorDir . '/symfony/polyfill-php72/bootstrap.php',
'e69f7f6ee287b969198c3c9d6777bd38' => $vendorDir . '/symfony/polyfill-intl-normalizer/bootstrap.php',
'25072dd6e2470089de65ae7bf11d3109' => $vendorDir . '/symfony/polyfill-php72/bootstrap.php',
'f598d06aa772fa33d905e87be6398fb1' => $vendorDir . '/symfony/polyfill-intl-idn/bootstrap.php',
'37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php',
'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir . '/symfony/polyfill-php80/bootstrap.php',
Expand Down
1 change: 1 addition & 0 deletions server/vendor/composer/autoload_namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
$baseDir = dirname(dirname($vendorDir));

return array(
'PHPSQLParser\\' => array($vendorDir . '/greenlion/php-sql-parser/src'),
);
13 changes: 12 additions & 1 deletion server/vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ComposerStaticInit434a4eb2fb23c7f559a3771baabf0fbb
'6e3fae29631ef280660b3cdad06f25a8' => __DIR__ . '/..' . '/symfony/deprecation-contracts/function.php',
'7b11c4dc42b3b3023073cb14e519683c' => __DIR__ . '/..' . '/ralouphie/getallheaders/src/getallheaders.php',
'a0edc8309cc5e1d60e3047b5df6b7052' => __DIR__ . '/..' . '/guzzlehttp/psr7/src/functions_include.php',
'25072dd6e2470089de65ae7bf11d3109' => __DIR__ . '/..' . '/symfony/polyfill-php72/bootstrap.php',
'e69f7f6ee287b969198c3c9d6777bd38' => __DIR__ . '/..' . '/symfony/polyfill-intl-normalizer/bootstrap.php',
'25072dd6e2470089de65ae7bf11d3109' => __DIR__ . '/..' . '/symfony/polyfill-php72/bootstrap.php',
'f598d06aa772fa33d905e87be6398fb1' => __DIR__ . '/..' . '/symfony/polyfill-intl-idn/bootstrap.php',
'37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php',
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
Expand Down Expand Up @@ -159,6 +159,16 @@ class ComposerStaticInit434a4eb2fb23c7f559a3771baabf0fbb
),
);

public static $prefixesPsr0 = array (
'P' =>
array (
'PHPSQLParser\\' =>
array (
0 => __DIR__ . '/..' . '/greenlion/php-sql-parser/src',
),
),
);

public static $classMap = array (
'Attribute' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
'CAS_AuthenticationException' => __DIR__ . '/..' . '/jasig/phpcas/source/CAS/AuthenticationException.php',
Expand Down Expand Up @@ -222,6 +232,7 @@ public static function getInitializer(ClassLoader $loader)
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit434a4eb2fb23c7f559a3771baabf0fbb::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit434a4eb2fb23c7f559a3771baabf0fbb::$prefixDirsPsr4;
$loader->prefixesPsr0 = ComposerStaticInit434a4eb2fb23c7f559a3771baabf0fbb::$prefixesPsr0;
$loader->classMap = ComposerStaticInit434a4eb2fb23c7f559a3771baabf0fbb::$classMap;

}, null, ClassLoader::class);
Expand Down
Loading

0 comments on commit 37651c2

Please sign in to comment.