Skip to content

Commit

Permalink
Refactor Python3 SqlModes out.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Dec 3, 2024
1 parent 3df956d commit 50d967d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
21 changes: 2 additions & 19 deletions sql/mysql/Oracle/Python3/MySQLLexerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
else:
from typing.io import TextIO
from SqlMode import SqlMode
from SqlModes import SqlModes
from MySQLParser import MySQLParser

class MySQLLexerBase(Lexer):
Expand All @@ -23,35 +24,17 @@ class MySQLLexerBase(Lexer):
def __init__(self, input=None, output:TextIO = sys.stdout):
super().__init__(input, output)
self.serverVersion = 0
self.sqlModes = set()
self.sqlModes = SqlModes.sqlModeFromString("ANSI_QUOTES")
self.supportMle = True
self.charSets = set()
self.inVersionComment = False
self.pendingTokens = deque()
self.justEmitedDot = False
self.serverVersion = 80200;
self.sqlModeFromString("ANSI_QUOTES");

def isSqlModeActive(self, mode):
return mode in self.sqlModes

def sqlModeFromString(self, modes):
self.sqlModes.clear()
parts = modes.upper().split(",")
for mode in parts:
if mode in {"ANSI", "DB2", "MAXDB", "MSSQL", "ORACLE", "POSTGRESQL"}:
self.sqlModes.update({SqlMode.AnsiQuotes, SqlMode.PipesAsConcat, SqlMode.IgnoreSpace})
elif mode == "ANSI_QUOTES":
self.sqlModes.add(SqlMode.AnsiQuotes)
elif mode == "PIPES_AS_CONCAT":
self.sqlModes.add(SqlMode.PipesAsConcat)
elif mode == "NO_BACKSLASH_ESCAPES":
self.sqlModes.add(SqlMode.NoBackslashEscapes)
elif mode == "IGNORE_SPACE":
self.sqlModes.add(SqlMode.IgnoreSpace)
elif mode in {"HIGH_NOT_PRECEDENCE", "MYSQL323", "MYSQL40"}:
self.sqlModes.add(SqlMode.HighNotPrecedence)

def reset(self):
self.inVersionComment = False
super().reset()
Expand Down
21 changes: 2 additions & 19 deletions sql/mysql/Oracle/Python3/MySQLParserBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
else:
from typing.io import TextIO
from SqlMode import SqlMode
from SqlModes import SqlModes

class MySQLParserBase(Parser):
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
super().__init__(input, output)
self.serverVersion = 0
self.sqlModes = set()
self.sqlModes = SqlModes.sqlModeFromString("ANSI_QUOTES")
self.supportMle = True
self.serverVersion = 80200;
self.sqlModeFromString("ANSI_QUOTES");

def isSqlModeActive(self, mode):
"""
Expand Down Expand Up @@ -45,23 +45,6 @@ def isStoredRoutineBody(self):
def isSelectStatementWithInto(self):
return self.serverVersion >= 80024 and self.serverVersion < 80031;

def sqlModeFromString(self, modes):
self.sqlModes.clear()
parts = modes.upper().split(",")
for mode in parts:
if mode in {"ANSI", "DB2", "MAXDB", "MSSQL", "ORACLE", "POSTGRESQL"}:
self.sqlModes.update({SqlMode.AnsiQuotes, SqlMode.PipesAsConcat, SqlMode.IgnoreSpace})
elif mode == "ANSI_QUOTES":
self.sqlModes.add(SqlMode.AnsiQuotes)
elif mode == "PIPES_AS_CONCAT":
self.sqlModes.add(SqlMode.PipesAsConcat)
elif mode == "NO_BACKSLASH_ESCAPES":
self.sqlModes.add(SqlMode.NoBackslashEscapes)
elif mode == "IGNORE_SPACE":
self.sqlModes.add(SqlMode.IgnoreSpace)
elif mode in {"HIGH_NOT_PRECEDENCE", "MYSQL323", "MYSQL40"}:
self.sqlModes.add(SqlMode.HighNotPrecedence)

def isServerVersionGe80004(self):
return self.serverVersion >= 80004

Expand Down
24 changes: 24 additions & 0 deletions sql/mysql/Oracle/Python3/SqlModes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
from readchar import readchar
from SqlMode import SqlMode

class SqlModes:

@staticmethod
def sqlModeFromString(modes):
sqlModes = set()
parts = modes.upper().split(",")
for mode in parts:
if mode in {"ANSI", "DB2", "MAXDB", "MSSQL", "ORACLE", "POSTGRESQL"}:
sqlModes.update({SqlMode.AnsiQuotes, SqlMode.PipesAsConcat, SqlMode.IgnoreSpace})
elif mode == "ANSI_QUOTES":
sqlModes.add(SqlMode.AnsiQuotes)
elif mode == "PIPES_AS_CONCAT":
sqlModes.add(SqlMode.PipesAsConcat)
elif mode == "NO_BACKSLASH_ESCAPES":
sqlModes.add(SqlMode.NoBackslashEscapes)
elif mode == "IGNORE_SPACE":
sqlModes.add(SqlMode.IgnoreSpace)
elif mode in {"HIGH_NOT_PRECEDENCE", "MYSQL323", "MYSQL40"}:
sqlModes.add(SqlMode.HighNotPrecedence)
return sqlModes

0 comments on commit 50d967d

Please sign in to comment.