From 4328713f091915ef9834063eb9b934b7eceba967 Mon Sep 17 00:00:00 2001 From: joyfullservice Date: Wed, 15 Nov 2023 15:08:47 -0600 Subject: [PATCH] Implement dialect in SQL formatting This was previously only partially implemented. #457 --- .../modules/clsDbQuery.cls | 2 +- .../modules/clsSqlFormatter.cls | 26 +++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Version Control.accda.src/modules/clsDbQuery.cls b/Version Control.accda.src/modules/clsDbQuery.cls index cd7d08df..728716cf 100644 --- a/Version Control.accda.src/modules/clsDbQuery.cls +++ b/Version Control.accda.src/modules/clsDbQuery.cls @@ -69,7 +69,7 @@ Private Sub IDbComponent_Export(Optional strAlternatePath As String) Case Else With New clsSqlFormatter Perf.OperationStart "Format SQL" - WriteFile .FormatSQL(strSql), strFile + WriteFile .FormatSQL(strSql, esdAccess), strFile Perf.OperationEnd End With End Select diff --git a/Version Control.accda.src/modules/clsSqlFormatter.cls b/Version Control.accda.src/modules/clsSqlFormatter.cls index ea683dba..6ee0f6d7 100644 --- a/Version Control.accda.src/modules/clsSqlFormatter.cls +++ b/Version Control.accda.src/modules/clsSqlFormatter.cls @@ -115,11 +115,8 @@ Public Function FormatSQL(Optional strSql As String, Optional intDialect As eSql Perf.CategoryStart "Format SQL" Perf.OperationStart "Formating" - ' Set SQL dialect - m_intDialect = intDialect - ' Tokenize the string, if provided - If strSql <> vbNullString Then Tokenize strSql + If strSql <> vbNullString Then Tokenize strSql, intDialect ' Set up collection to hold types of indents Set colIndents = New Collection @@ -437,7 +434,7 @@ End Function ' : Each token is an array with type(0) and value(1). '--------------------------------------------------------------------------------------- ' -Private Sub Tokenize(strSql As String) +Private Sub Tokenize(strSql As String, intDialect As eSqlDialect) Const cstrBreakAfter As String = "LIMIT" & ";;;;" @@ -451,6 +448,9 @@ Private Sub Tokenize(strSql As String) m_varWordCache(1) = Empty m_varWordCache(2) = Empty + ' Set SQL dialect + m_intDialect = intDialect + Perf.CategoryStart "Tokenize SQL" ' Loop through SQL, converting string into tokens @@ -1297,16 +1297,13 @@ Public Sub SelfTest() Dim strActual As String - ' Test performance - TestPerformance - ' Test GetNextWords - Tokenize " LEFT " & vbTab & vbCrLf & " JOIN test on 1=2" + Tokenize " LEFT " & vbTab & vbCrLf & " JOIN test on 1=2", esdAccess Debug.Assert GetNextWords(2) = "LEFT JOIN" Debug.Assert GetNextWords(1) = "LEFT" ' Test simple query with a few features - Tokenize "SELECT 5 AS `TEST`" + Tokenize "SELECT 5 AS `TEST`", esdMySQL ' Verify tokens Debug.Assert m_colTokens.Count = 7 @@ -1331,7 +1328,7 @@ Public Sub SelfTest() ' Test Access date literal with MySQL inline comment - Tokenize "SELECT (#1/1/2000#) AS SampleDate # MySQL inline ## comment" + Tokenize "SELECT (#1/1/2000#) AS SampleDate # MySQL inline ## comment", esdAccess ' Verify tokens Debug.Assert m_colTokens.Count = 11 @@ -1362,7 +1359,7 @@ Public Sub SelfTest() ' Example query from https://github.com/doctrine/sql-formatter Tokenize "SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1`" & _ " WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 >= NOW()) )" & _ - " GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10" + " GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10", esdMySQL ' Verify tokens Debug.Assert m_colTokens.Count = 66 @@ -1522,7 +1519,7 @@ Private Function BuildTestFromTokens() Dim intLine As Integer Debug.Print vbCrLf & vbCrLf & " ' Verify tokens" - Debug.Print vbCrLf & " Debug.Assert m_colTokens.Count = " & m_colTokens.Count + Debug.Print " Debug.Assert m_colTokens.Count = " & m_colTokens.Count ' Loop through tokens For intToken = 1 To m_colTokens.Count @@ -1550,6 +1547,7 @@ Private Function BuildTestFromTokens() Debug.Print " strActual = .GetStr" Debug.Print " End With" Debug.Print " Debug.Assert (strActual = FormatSQL)" + Debug.Print " If (strActual <> FormatSQL) Then Diff.Strings strActual, FormatSQL" End Function @@ -1661,7 +1659,7 @@ Private Function TestPerformance() For lngCnt = 1 To lngMax Tokenize "SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1`" & _ " WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 >= NOW()) )" & _ - " GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10" + " GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10", esdMySQL Next lngCnt ' Test performance of formatting SQL