码迷,mamicode.com
首页 > 数据库 > 详细

使用shell脚本生成数据库markdown文档

时间:2018-09-15 18:44:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:The   sch   com   实践   xxx   form   orm   art   mysql   

学习shell脚本编程的一次实践,通过shell脚本生成数据库的markdown文档,代码如下:

 HOST=xxxxxx
 PORT=xxxx
 USER="xxxxx"
 PASSWORD="xxxxxx"
 DATABASE_PREFIX="xxxxxx"
  
 QUERY_DATABASE="select distinct TABLE_SCHEMA from information_schema.TABLES where TABLE_TYPE='BASE TABLE'"
 databaseList=$(mysql -h ${HOST} -u ${USER} -p${PASSWORD} -e "${QUERY_DATABASE}")

 for database in ${databaseList}
 do
     if [[ ${database} == *${DATABASE_PREFIX}* ]]
     then
         echo "Start ${database}"
         echo "### ${database}" >> test.md
         queryTable="select  distinct TABLE_NAME from information_schema.COLUMNS where TABLE_SCHEMA='${database}'"
         tableList=$(mysql -h ${HOST} -u ${USER} -p${PASSWORD} -N -e "${queryTable}")
         for table in ${tableList}
         do
                
             if [ $(grep -c "#### ${table%_*}_0" test.md) -ne '0' ] 
             then
                 continue
             fi
             

             echo "Start print table:${table}"
             echo "#### ${table}" >> test.md
             echo "名称 | 类型 | 是否可为空 | Key | 说明" >> test.md
             echo "--|--|--|--|--" >> test.md
             queryCloumns="select COLUMN_NAME, replace(COLUMN_TYPE,' ',''),IS_NULLABLE,CONCAT(COLUMN_KEY,'+++++++++'),CONCAT(replace(COLUMN_COMMENT,' ',''),'+++++++++') from information_schema.COLUMNS where TABLE_NAME='${table}' and TABLE_SCHEMA='${database}' order by ORDINAL_POSITION"

             columnList=$(mysql -h ${HOST} -u ${USER} -p${PASSWORD} -N -e "${queryCloumns}")
             count=0
             row=""
              for column in ${columnList}
              do
                  row="${row} ${column} | "

                  let count++
                  if [[ ${count} == 5 ]]
                  then
                      echo ${row} >> test.md
                      count=0
                      row=""
                  fi
              done
          done
 
      fi
  done

使用shell脚本生成数据库markdown文档

标签:The   sch   com   实践   xxx   form   orm   art   mysql   

原文地址:https://www.cnblogs.com/vitasyuan/p/9650643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!