Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
suppermoment committed Apr 2, 2020
0 parents commit ab65a37
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.{vue,js,scss}]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea
*.DS_Store
/vendor
/coverage
sftp-config.json
composer.lock
.subsplit
.php_cs.cache
27 changes: 27 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$header = <<<EOF
This file is part of the tim168/data-dict.
(c) TIM168 <[email protected]>
This source file is subject to the MIT license that is bundled.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
))
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
)
;
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1 align="center"> data-dict </h1>

<p align="center"> A plug-in that generates a data dictionary.</p>


## Installing

```shell
$ composer require tim168/data-dict -vvv
```

## Usage

TODO

## Contributing

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/tim168/data-dict/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/tim168/data-dict/issues).
3. Contribute new features or update the wiki.

_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._

## License

MIT
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "tim168\/data-dict",
"description": "A plug-in that generates a data dictionary",
"license": "MIT",
"authors": [
{
"name": "TIM168",
"email": "[email protected]"
}
],
"require": {
"ext-mysqli": "*",
"phpunit/phpunit": "^8.5",
"mockery/mockery": "^1.3"
},
"autoload": {
"psr-4": {
"Tim168\\DataDict\\": "src"
}
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
Empty file added src/.gitkeep
Empty file.
62 changes: 62 additions & 0 deletions src/Data/Draw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Tim168\DataDict\Data;

class Draw
{
public function dataToHtml($tables)
{
if (!empty($tables)) {
$html = '';
foreach ($tables as $k => $v) {
$html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">';
$html .= '<caption>' . $v['TABLE_NAME'] . ' ' . $v['TABLE_COMMENT'] . '</caption>';
$html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
<th>允许非空</th>
<th>自动递增</th><th>备注</th></tr>';
$html .= '';
foreach ($v['COLUMN'] as $f) {
$html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td>';
$html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
$html .= '<td class="c3"> ' . $f['COLUMN_DEFAULT'] . '</td>';
$html .= '<td class="c4"> ' . $f['IS_NULLABLE'] . '</td>';
$html .= '<td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '' : ' ') . '</td>';
$html .= '<td class="c6"> ' . $f['COLUMN_COMMENT'] . '</td>';
$html .= '</tr>';
}
$html .= '</tbody></table></p>';
}
return $html;
} else {
return false;
}
}

public function ToHtml($title, $html)
{
$content = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>' . $title . '</title>
<style>
body,td,th {font-family:"宋体"; font-size:12px;}
table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;}
table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;}
.c1{ width: 120px;}
.c2{ width: 120px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 270px;}
</style>
</head>
<body>
<h1 style="text-align:center;">' . $title . '</h1>
' . $html . '
</body></html>';

return $content;
}
}
53 changes: 53 additions & 0 deletions src/Data/Fetch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Tim168\DataDict\Data;

use Tim168\DataDict\Exceptions\MysqlErrorException;

class Fetch
{
public function __construct()
{
}

public function conn($dbHost, $dbUserName, $dbPassWord, $dbName, $dbPort)
{
try {
$mysqlConn = mysqli_connect($dbHost, $dbUserName, $dbPassWord, '', $dbPort);
mysqli_select_db($mysqlConn, $dbName);
mysqli_query($mysqlConn, 'set names utf8');
$tableResult = mysqli_query($mysqlConn, 'show tables');
while ($row = mysqli_fetch_array($tableResult)) {
$tables[]['TABLE_NAME'] = $row[0];
}
if (!empty($tables)) {
foreach ($tables as $k => $v) {
$sql = 'SELECT * FROM ';
$sql .= 'INFORMATION_SCHEMA.TABLES ';
$sql .= 'WHERE ';
$sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$dbName}'";
$tableResult = mysqli_query($mysqlConn, $sql);
while ($t = mysqli_fetch_array($tableResult)) {
$tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
}
$sql = 'SELECT * FROM ';
$sql .= 'INFORMATION_SCHEMA.COLUMNS ';
$sql .= 'WHERE ';
$sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$dbName}'";
$fields = array();
$fieldsResult = mysqli_query($mysqlConn, $sql);
while ($t = mysqli_fetch_array($fieldsResult)) {
$fields[] = $t;
}
$tables[$k]['COLUMN'] = $fields;
}
return $tables;
}
return false;
} catch (\Exception $e) {
throw new MysqlErrorException($e->getMessage(), $e->getCode(), $e);
} finally {
if (!empty($mysqlConn)) mysqli_close($mysqlConn);
}
}
}
39 changes: 39 additions & 0 deletions src/DataDict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tim168\DataDict;

use Tim168\DataDict\Data\Draw;
use Tim168\DataDict\Data\Fetch;

class DataDict
{
public $dbHost;
public $dbUserName;
public $dbPassWord;
public $dbName;
public $dbPort;
public $title = '数据字典';

public function __construct($dbHost, $dbUserName, $dbPassWord, $dbName, $dbPort)
{
$this->dbHost = $dbHost;
$this->dbUserName = $dbUserName;
$this->dbPassWord = $dbPassWord;
$this->dbName = $dbName;
$this->dbPort = $dbPort;
}

public function get()
{
$fetch = new Fetch();
$tables = $fetch->conn($this->dbHost, $this->dbUserName, $this->dbPassWord, $this->dbName, $this->dbPort);
var_dump($tables);
exit();
if (!empty($tables)) {
$draw = new Draw();
$html = $draw->dataToHtml($tables);
$content = $draw->ToHtml($this->title, $html);
return $content;
}
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/MysqlErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tim168\DataDict\Exceptions;

class MysqlErrorException extends \Exception
{

}
Empty file added tests/.gitkeep
Empty file.

0 comments on commit ab65a37

Please sign in to comment.