@@ -91,6 +91,7 @@ public function updateDbItem(Request $request, Response $response): Response
9191 $ apiToken = $ this ->getParam ($ request , 'api_token ' , '' );
9292 $ tableInfo = $ this ->getParam ($ request , 'table_info ' , '' );
9393 $ tableDetail = $ this ->getParam ($ request , 'table_detail ' , '' );
94+ $ tableIndex = $ this ->getParam ($ request , 'table_index ' , '' );
9495 $ sNumber = $ this ->getParam ($ request , 's_number ' , 99 );
9596 $ catName = $ this ->getParam ($ request , 'cat_name ' , '' );
9697
@@ -110,7 +111,8 @@ public function updateDbItem(Request $request, Response $response): Response
110111 $ catName = str_replace (PHP_EOL , '' , $ catName );
111112 $ tableInfo = str_replace ("_this_and_change_ " , "& " , $ tableInfo );
112113 $ tableDetail = str_replace ("_this_and_change_ " , "& " , $ tableDetail );
113- $ tables = $ this ->analyzeDbStructureToArray ($ tableInfo , $ tableDetail );
114+ $ tableIndex = str_replace ("_this_and_change_ " , "& " , $ tableIndex );
115+ $ tables = $ this ->analyzeDbStructureToArray ($ tableInfo , $ tableDetail , $ tableIndex );
114116
115117 $ result = false ;
116118 if (!empty ($ tables )) {
@@ -136,7 +138,7 @@ public function updateDbItem(Request $request, Response $response): Response
136138 /**
137139 * 解析数据库结构为数组
138140 */
139- private function analyzeDbStructureToArray (string $ tableInfo , string $ tableDetail ): array
141+ private function analyzeDbStructureToArray (string $ tableInfo , string $ tableDetail, string $ tableIndex = '' ): array
140142 {
141143 $ tables = [];
142144
@@ -186,18 +188,81 @@ private function analyzeDbStructureToArray(string $tableInfo, string $tableDetai
186188 }
187189 }
188190
191+ // 解析 table_index
192+ if (!empty ($ tableIndex )) {
193+ $ array = explode ("\n" , $ tableIndex );
194+ if (!empty ($ array )) {
195+ foreach ($ array as $ key => $ value ) {
196+ if ($ key == 0 ) {
197+ continue ; // 跳过表头
198+ }
199+ $ array2 = explode ("\t" , $ value );
200+ if (empty ($ array2 [0 ]) || empty ($ array2 [1 ])) {
201+ continue ;
202+ }
203+ $ tableName = str_replace (PHP_EOL , '' , $ array2 [0 ]);
204+ $ indexName = str_replace (PHP_EOL , '' , $ array2 [1 ]);
205+ $ columnName = str_replace (PHP_EOL , '' , $ array2 [2 ] ?? '' );
206+ $ seqInIndex = (int )($ array2 [3 ] ?? 0 );
207+ $ nonUnique = (int )($ array2 [4 ] ?? 1 );
208+ $ indexType = str_replace (PHP_EOL , '' , $ array2 [5 ] ?? '' );
209+ $ indexComment = str_replace (PHP_EOL , '' , $ array2 [6 ] ?? '' );
210+
211+ if (!isset ($ tables [$ tableName ])) {
212+ $ tables [$ tableName ] = [
213+ 'table_name ' => $ tableName ,
214+ 'table_comment ' => '' ,
215+ ];
216+ }
217+ if (!isset ($ tables [$ tableName ]['indexes ' ])) {
218+ $ tables [$ tableName ]['indexes ' ] = [];
219+ }
220+ if (!isset ($ tables [$ tableName ]['indexes ' ][$ indexName ])) {
221+ $ tables [$ tableName ]['indexes ' ][$ indexName ] = [
222+ 'index_name ' => $ indexName ,
223+ 'non_unique ' => $ nonUnique ,
224+ 'index_type ' => $ indexType ,
225+ 'index_comment ' => $ indexComment ,
226+ 'columns ' => [],
227+ ];
228+ }
229+ // 按序号添加列
230+ $ tables [$ tableName ]['indexes ' ][$ indexName ]['columns ' ][$ seqInIndex ] = $ columnName ;
231+ }
232+ }
233+ }
234+
189235 // 生成 markdown 内容
190236 if (!empty ($ tables )) {
191237 foreach ($ tables as $ key => $ value ) {
192238 $ markdown = '' ;
193239 $ markdown .= "- {$ value ['table_comment ' ]} \n \n" ;
240+
241+ // 字段表格
242+ $ markdown .= "## 字段说明 \n \n" ;
194243 $ markdown .= "|字段|类型|允许空|默认|注释| \n " ;
195244 $ markdown .= "|:---- |:------- |:--- |----|------ | \n " ;
196245 if (!empty ($ value ['columns ' ])) {
197246 foreach ($ value ['columns ' ] as $ value2 ) {
198247 $ markdown .= "| {$ value2 ['column_name ' ]} | {$ value2 ['column_type ' ]} | {$ value2 ['is_nullable ' ]} | {$ value2 ['default ' ]} | {$ value2 ['column_comment ' ]} | \n " ;
199248 }
200249 }
250+
251+ // 索引表格
252+ if (!empty ($ value ['indexes ' ])) {
253+ $ markdown .= " \n \n## 索引说明 \n \n" ;
254+ $ markdown .= "|索引名|类型|唯一|字段|注释| \n " ;
255+ $ markdown .= "|:---- |:------- |:--- |----|------ | \n " ;
256+ foreach ($ value ['indexes ' ] as $ index ) {
257+ // 按序号排序列
258+ ksort ($ index ['columns ' ]);
259+ $ columnsStr = implode (', ' , $ index ['columns ' ]);
260+ $ uniqueStr = $ index ['non_unique ' ] == 0 ? '是 ' : '否 ' ;
261+ $ indexComment = !empty ($ index ['index_comment ' ]) ? $ index ['index_comment ' ] : '无 ' ;
262+ $ markdown .= "| {$ index ['index_name ' ]} | {$ index ['index_type ' ]} | {$ uniqueStr } | {$ columnsStr } | {$ indexComment } | \n " ;
263+ }
264+ }
265+
201266 $ tables [$ key ]['markdown ' ] = $ markdown ;
202267 }
203268 }
0 commit comments