MySQL schema 可以藉助預存程序匯出為 GitHub markdown 格式。下面是一個使用兩個預存程序的範例,它們可以連結在一起以獲得所需的輸出。
儲存過程鏈
第一個預存程序準備用於報告的資料並分配一個會話號碼以保持輸出隔離。
CREATE PROCEDURE `Reporting101a`.`describeTables_v2a`( IN dbName varchar(100), -- the dbname to report table structures OUT theSession int, -- OUT parameter for session# assigned IN deleteSessionRows BOOL, -- true for delete rows when done from main reporting table for this session# IN callTheSecondStoredProc BOOL -- TRUE = output is from Pretty output in Second Stored Proc. FALSE= not so pretty output ) BEGIN DECLARE thisTable CHAR(100); DROP TEMPORARY TABLE IF EXISTS Reporting101a.tOutput; CREATE TEMPORARY TABLE Reporting101a.tOutput ( id int auto_increment primary key, tblName varchar(100) not null, ordVal int not null, cField varchar(100) not null, cType varchar(100) not null, cNull varchar(100) not null, cKey varchar(100) not null, cDefault varchar(100) null, cExtra varchar(100) null ); INSERT Reporting101a.tOutput(tblName,ordVal,cField,cType,cNull,cKey,cDefault,cExtra) SELECT TABLE_NAME,ORDINAL_POSITION,COLUMN_NAME AS Field, COLUMN_TYPE AS TYPE, RPAD(IS_NULLABLE,4,' ') AS 'Null', RPAD(COLUMN_KEY,3,' ') AS 'Key',RPAD(COLUMN_DEFAULT,7,' ') AS 'DEFAULT',EXTRA AS Extra FROM information_schema.columns WHERE table_schema = dbName ORDER BY table_name,ordinal_position; -- select * from information_schema.columns WHERE table_schema = '57security' order by table_name,ordinal_position; ... END$$
第二個預存程序使用遊標和特殊表產生漂亮的輸出,類似 MySQL 的 DESCRIBE 指令。
CREATE PROCEDURE `Reporting101a`.`Print_Tables_Like_Describe`( pSessionId INT ) BEGIN DECLARE done INT DEFAULT FALSE; ... CREATE TABLE IF NOT EXISTS Reporting101a.reportOutput ( lineNum INT AUTO_INCREMENT PRIMARY KEY, sessionId INT NOT NULL, lineOut varchar(100) NOT NULL ); ... END$$
用法
使用所需參數呼叫第一個預存程序。
SET @theOutVar =-1; -- A variable used as the OUT variable below -- See **Note3** -- Note: with `TRUE` as the 4th parameter, this is a one call deal. Meaning, you are done. call Reporting101a.describeTables_v2a('stackoverflow',@theOutVar,false,true); -- See **Note4** -- Primarily used if the 4th parameter above is false call Reporting101a.Print_Tables_Like_Describe(@theOutVar); -- loads data for prettier results in chunk format.
輸出
輸出將在reportOutput表中生成,並可依下列方式擷取:
select lineOut as '' from Reporting101a.reportOutput where sessionId=pSessionId order by lineNum;
以上是如何使用預存程序將 MySQL 架構匯出到 GitHub Markdown?的詳細內容。更多資訊請關注PHP中文網其他相關文章!