본문 바로가기

개발이야기

[뻘짓금지]저장프로시저 자동 문서화하기

먼저 필요한 건 데이터베이스의 정보입니다. 그 정보를 바로 엑셀에서 읽을 수도 있지만 여기선 그냥 복사하는 형태로 하겠습니다.


다음은 데이터베이스의 정보를 가져오는 쿼리입니다.

SELECT
/*프로시저이름*/ SO.name AS [ObjectName],
/*파라미터이름*/ P.name AS [ParameterName],
/*파라메터타입*/
[ParameterDataType] = CASE TYPE_NAME(P.user_type_id)
WHEN 'varchar' THEN 'varchar('+ CAST(P.max_length  AS VARCHAR(10)) +')'
WHEN 'char' THEN 'char('+ CAST(P.max_length  AS VARCHAR(10)) +')'
ELSE TYPE_NAME(P.user_type_id)
END + '  ' + CASE P.is_output
             WHEN 1 THEN 'OUTPUT'
             ELSE 'IN'
             END
FROM sys.objects AS SO
INNER JOIN sys.parameters AS P
ON SO.OBJECT_ID = P.OBJECT_ID

WHERE SO.OBJECT_ID IN ( SELECT OBJECT_ID
FROM sys.objects
WHERE TYPE IN ('P'))
ORDER BY
SO.name, P.parameter_id

 

이 쿼리를 실행하면 다음과 같이 반환을 하게 됩니다.

  ObjectName ParemeterName ParemeterDataType
1 usp_procedure1 @param1 int IN
2 usp_procedure1 @param2 varchar(50) IN
3 usp_procedure1 @param3 int OUTPUT
4 usp_procedure2 @param1 int
 
이 내용을 복사하여 엑셀에 있는 시트에 붙여 넣으시기 바랍니다.
그럼 다음과 같이 됩니다.
 
 
여기서 A열을 비워 두었는데 다 이유가 있습니다. 그건 각 저장프로시저들의 그룹단위를 표시하기 위해 비워 두었는데 이유는 나중에 데이터를 열마다 읽을 때 어디서부터 어디까지가 하나의 프로시저묶음인지 알기 위해서 입니다. 그럼 A열 2번째 부터 ‘=B2=B1’ 라는 식을 넣고 1번째는 False라는 문자열을 삽입합니다.
 
 
 
이렇게 되었으면 매크로를 통해 하나의 열을 돌아다니며 표를 만들면 끝이 됩니다라는 코드 작성하면 됩니다.
 
늘 말은 짧고 쉽죠~ 코드는 말이 참 많습니다.
 
그럼 말 많은 코드를 공개하겠습니다.
 

Sub GeneratorParamDoc()
    Dim tableGr As Range
    Dim writeCur As Range
    Dim firstCur As Range
    Dim rowCnt As Integer
    Set writeCur = ActiveCell
    Set firstCur = writeCur.Offset(0, -1)
    rowCnt = 0
     For Each tableGr In Range("A1:A10000")
        If tableGr = False Or tableGr = "" Then
            If rowCnt <> 0 Then
                Range(firstCur.Offset(3, 0), firstCur.Offset(3, 0).Offset(rowCnt, 0)).Merge
                '매개변수
                With Range(firstCur.Offset(4, 1), firstCur.Offset(4, 1).Offset(rowCnt, 3)).Borders(xlInsideHorizontal)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlThin
                End With
                '매개변수 중앙정렬
                With Range(firstCur.Offset(3, 1), firstCur.Offset(3, 1).Offset(rowCnt, 3))
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                End With
                '큰 틀 그리기
                With Range(firstCur, firstCur.Offset(rowCnt + 3, 4)).Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlMedium
                End With
                With Range(firstCur, firstCur.Offset(rowCnt + 3, 4)).Borders(xlEdgeRight)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlMedium
                End With
                With Range(firstCur, firstCur.Offset(rowCnt + 3, 4)).Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlMedium
                End With
                With Range(firstCur, firstCur.Offset(rowCnt + 3, 4)).Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlMedium
                End With
                With Range(firstCur, firstCur.Offset(rowCnt + 3, 4)).Borders(xlInsideVertical)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlThin
                End With
                If tableGr = "" Then
                    Exit For
                End If
            End If
            rowCnt = 1
            Set writeCur = writeCur.Offset(4, 0)
            Set firstCur = writeCur.Offset(0, -1)
            writeCur.Offset(0, -1).Value = "프로시저명"
            writeCur.Offset(3, 0).Value = "이름"
            writeCur.Offset(3, 1).Value = "형태"
            writeCur.Offset(3, 2).Value = "설명"
            writeCur.Offset(3, 3).Value = "비고"
            writeCur.Offset(1, -1).Value = "프로시저 설명"
            writeCur.Offset(2, -1).Value = "반환형태"
            writeCur.Offset(3, -1).Value = "파라매터"
            '!

            '셀합치기
            Range(writeCur, writeCur.Offset(0, 3)).Merge
            Range(writeCur.Offset(1, 0), writeCur.Offset(1, 0).Offset(0, 3)).Merge
            Range(writeCur.Offset(2, 0), writeCur.Offset(2, 0).Offset(0, 3)).Merge
            '셀 합치기 끝
            With Range(writeCur.Offset(0, -1), writeCur.Offset(0, -1).Offset(0, 4)).Borders(xlEdgeBottom)
                .LineStyle = xlDouble
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Weight = xlThick
            End With

            With Range(writeCur.Offset(1, -1), writeCur.Offset(1, -1).Offset(0, 4)).Borders(xlEdgeBottom)
                .LineStyle = xlDouble
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Weight = xlThick
            End With

            With Range(writeCur.Offset(2, -1), writeCur.Offset(2, -1).Offset(0, 4)).Borders(xlEdgeBottom)
                .LineStyle = xlDouble
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Weight = xlThick
            End With
            With Range(writeCur.Offset(3, -1), writeCur.Offset(3, -1).Offset(0, 4)).Borders(xlEdgeBottom)
                .LineStyle = xlDouble
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Weight = xlThick
            End With
            writeCur.Value = tableGr.Offset(0, 1)
            Set writeCur = writeCur.Offset(4, 0)
            writeCur.Value = tableGr.Offset(0, 2)
            writeCur.Offset(0, 1).Value = tableGr.Offset(0, 3)
            writeCur.Offset(0, 2).Value = tableGr.Offset(0, 4)
        Else
            rowCnt = rowCnt + 1
            Set writeCur = writeCur.Offset(1, 0)
            writeCur.Value = tableGr.Offset(0, 2)
            writeCur.Offset(0, 1).Value = tableGr.Offset(0, 3)
            writeCur.Offset(0, 2).Value = tableGr.Offset(0, 4)
        End If
     Next tableGr
End Sub


 

이 코드는 표를 그리고 정렬하고 데이터 넣고 하는 VBA코드입니다. 이렇게 만들어지기까지 수많은 시행착오가 있었…(모르게 눈물이)

아무튼 멀찌감치 커서를 놓고 위의 매크로를 실행합니다.

실행 결과는 다음과 같습니다.

 

표들이 보이시죠?

그것을 복사해서 문서화하면 되는 것 입니다.

여기서 안타까운건 설명컬럼입니다. 설명컬럼은 일일이 그 컬럼에 가서 써야 하지요. 성격상 또 반복 작업스러운건 질색입니다. 그래서 MSSQL에 있는 설명글을 가져와서 붙이고 싶었지만 MSSQL내부의 프로시저에 설명을 작성하지 않아서 일일이 또 작업을 해야만 했습니다.
만약 설명을 틈틈히 프로시저 만들때 하였다면 이 설명까지 가져와 깔끔한 문서를 생성했겠지요.

MSSQL뿐만 아니라 다른 DBMS의 정보를 맨 처음 쿼리 같이 리스트를 뽑는다면 쉽게 문서화 할 수 있겠죠?

프로시저뿐만아니라 테이블 등등…
더이상 문서를 만들진 맙시다. 개발자는 코드로 시크하게 답변해주자고요~

아무튼 뻘짓금지는 계속됩니다.