Skip to content

Commit

Permalink
Refine some dialect-specific SQL string quotations
Browse files Browse the repository at this point in the history
Backticks only apply to MySQL, while square brackets are used with MSSQL and Access. joyfullservice#442
  • Loading branch information
joyfullservice committed Oct 2, 2023
1 parent 62fde81 commit 3273089
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions Version Control.accda.src/modules/clsSqlFormatter.cls
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,9 @@ End Function
' Procedure : GetQuotedString
' Author : Adam Waller
' Date : 8/12/2023
' Purpose : Return a quoted string (Applies four possible rules)
' Purpose : Return a quoted string (dialect-specific)
' : https://stackoverflow.com/q/10573922/4121863
' : https://stackoverflow.com/q/9719869/4121863
'---------------------------------------------------------------------------------------
'
Private Function GetQuotedString(Optional lngStartOffset As Long = 0) As String
Expand All @@ -733,26 +735,28 @@ Private Function GetQuotedString(Optional lngStartOffset As Long = 0) As String
' Build out RegEx expression
.Add "^("

' (1) backtick quoted string using `` to escape
.Add "((`[^`]*($|`))+)|"

' (2) square bracket quoted string (SQL Server) using ]] to escape
.Add "((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|"

' Accomodate dialect-specific variants
Select Case m_intDialect

Case esdMySQL
' (3) double quoted string using "" or \" to escape
' Backtick quoted string using `` to escape
.Add "((`[^`]*($|`))+)|"

' Double quoted string using "" or \" to escape
.Add "((""[^""\\\\]*(?:\\\\.[^""\\\\]*)*(""|$))+)|"

' (4) single quoted string using '' or \' to escape
.Add "((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+)" ' sx',
' Single quoted string using '' or \' to escape
.Add "((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+)"

Case Else
' (3) double quoted string using "" to escape
' Square bracket quoted string (SQL Server) using ]] to escape
.Add "((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|"

' Double quoted string using "" to escape
.Add "((""[^""]*(""|$))+)|"

' (4) single quoted string using '' to escape
.Add "((\'[^\']*(\'|$))+)" ' sx',
' Single quoted string using '' to escape
.Add "((\'[^\']*(\'|$))+)"

End Select
.Add ")"
Expand Down

0 comments on commit 3273089

Please sign in to comment.