0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBAでPL/SQLのPackageを解析する【作成中】

Posted at

自分用メモです。
現状、コメントを取り除いた文字列をデバッグ出力するだけのサンプルになっています。
加筆していきます。

PK_ANALYZE.vb
Option Explicit

Enum LineStatus
    OutOfPackage
    InOfPackage
    InOfPF
    InOfPFBegin
    InOfBlockComment
End Enum

Enum PackageType
    Package
    PackageBody
End Enum


Sub PK_ANALYZE()
    Dim FSO As New FileSystemObject
    Dim TSRead As TextStream
    Dim TSWrite As TextStream
    Dim SqlNameRead As String
    Dim SqlNameWrite As String
    Dim TargetLine As String
    Dim TempString As String
    Dim LStatus As LineStatus
    Dim TStatus As LineStatus ' ブロックコメント直前の状態を保存
    
    SqlNameRead = "C:\Sample\PKG_TESTB.sql"
    SqlNameWrite = "C:\Sample\PKG_TESTB_.sql"
    
    Set TSRead = FSO.OpenTextFile(SqlNameRead, ForReading)
    Set TSWrite = FSO.OpenTextFile(SqlNameWrite, ForWriting, True)
    
    Debug.Print "----------"
    
    Do While Not TSRead.AtEndOfStream
        TargetLine = TSRead.ReadLine
        TempString = TargetLine
        
        
        ' ブロックコメント外の場合、判定から行コメントを取り除く
        ' 下記パターンは考慮しないものとする
        ' /*
        '  *  BLOCK COMMENT
        '  */ STATEMENT -- COMMENT
        If (LStatus <> InOfBlockComment) And (InStr(TempString, "--") > 0) Then
            TempString = Left(TempString, InStr(TempString, "--") - 1)
        End If
        
        
        ' ブロックコメント内でブロックコメント末尾を発見した場合はそこまでをブロックコメントとする
        If LStatus = InOfBlockComment Then
            If InStr(TempString, "*/") > 0 Then
                TempString = Right(TempString, Len(TempString) - InStr(TempString, "*/") - 1)
                LStatus = TStatus
            Else
                TempString = ""
            End If
        End If
        
        Do While (LStatus <> InOfBlockComment) And (InStr(TempString, "/*") > 0)
            If InStr(InStr(TempString, "/*"), TempString, "*/") > 0 Then
                ' ブロックコメント外で途中にブロックコメントがある場合は取り除く
                TempString = Left(TempString, InStr(TempString, "/*") - 1) & Right(TempString, Len(TempString) - InStr(InStr(TempString, "/*"), TempString, "*/") - 1)
            Else
                ' ブロックコメント開始だけがあった場合は、そこまでを判定対象とする
                TempString = Left(TempString, InStr(TempString, "/*") - 1)
                TStatus = LStatus
                LStatus = InOfBlockComment
                Exit Do
            End If
        Loop
        
        Debug.Print TempString
        
        
        Select Case LStatus
        ' パッケージ外
        Case LineStatus.OutOfPackage

        Case LineStatus.InOfPackage
        
        Case LineStatus.InOfPF
        
        Case LineStatus.InOfPFBegin
        
        Case LineStatus.InOfBlockComment
        
        End Select
        TSWrite.WriteLine TargetLine
    Loop
    
    TSRead.Close
    TSWrite.Close
    
    Set TSRead = Nothing
    Set TSWrite = Nothing
End Sub
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?