Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mysql/oracle] Add remaining ports to support all targets. #4345

Merged
merged 29 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
00a96c9
Update existing ports of Oracle MySQL to set defaults in constructors…
kaby76 Nov 29, 2024
fce1f94
For Python3 port, move custom initialization to constructor in base c…
kaby76 Nov 29, 2024
5ba847f
Moved SqlMode to separate file for Antlr4ng. Fixed constructor for le…
kaby76 Nov 29, 2024
73924b6
Clean up and make ts ports work. Apparently you cannot nest an enum i…
kaby76 Nov 29, 2024
4c3d911
Clean up the Java port.
kaby76 Nov 29, 2024
8bf6c26
Add in Cpp port (incomplete).
kaby76 Nov 29, 2024
334e994
Clean up, add Cpp target.
kaby76 Nov 30, 2024
10bc14d
Forgot to add this new file.
kaby76 Nov 30, 2024
bf5c91f
Add JavaScript port (generated from TypeScript port).
kaby76 Nov 30, 2024
32f4c28
Adding Go port (incomplete).
kaby76 Dec 1, 2024
102dd65
Clean up.
kaby76 Dec 1, 2024
2cfb87a
Clean up Dart port.
kaby76 Dec 1, 2024
e9d22d2
Split actions to different lines so transformGrammar.py doesn't need …
kaby76 Dec 2, 2024
7250871
Updates for Go port (incomplete still).
kaby76 Dec 2, 2024
e988c93
Fix properties of tokens with doEmit() and following token.
kaby76 Dec 2, 2024
7ff2a3a
Fix Cpp token BS, now identical to Antlr4ng and correct.
kaby76 Dec 2, 2024
5dcbf13
Fix explicit expressions in parser grammar, make target agnostic.
kaby76 Dec 2, 2024
2f8c5af
Fix CSharp target for target agnostic parser.
kaby76 Dec 2, 2024
94ea90e
Update Cpp port with target agnostic methods.
kaby76 Dec 2, 2024
4baed19
Clean up CSharp port code.
kaby76 Dec 2, 2024
e8ab64e
CSharp token props now perfect.
kaby76 Dec 2, 2024
49b9709
Fix formatting.
kaby76 Dec 3, 2024
12b8236
Fix Dart port.
kaby76 Dec 3, 2024
7856947
Fixed Go port, now working.
kaby76 Dec 3, 2024
6872b82
Fixed Java port.
kaby76 Dec 3, 2024
af5d69b
Fixed TypeScript and JavaScript ports.
kaby76 Dec 3, 2024
3df956d
Fixed Python3 port.
kaby76 Dec 3, 2024
50d967d
Refactor Python3 SqlModes out.
kaby76 Dec 3, 2024
2f05925
Update readme.
kaby76 Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 68 additions & 89 deletions sql/mysql/Oracle/Antlr4ng/MySQLLexerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
/* eslint-disable no-underscore-dangle */
/* cspell: ignore antlr, longlong, ULONGLONG, MAXDB */

import { Lexer, Token } from "antlr4ng";
import { CharStream, Lexer, Token } from "antlr4ng";
import { MySQLLexer } from "./MySQLLexer.js";

/** SQL modes that control parsing behavior. */
export enum SqlMode {
NoMode,
AnsiQuotes,
HighNotPrecedence,
PipesAsConcat,
IgnoreSpace,
NoBackslashEscapes,
}
import SqlMode from "./SqlMode.js";
import SqlModes from "./SqlModes.js";

/** The base lexer class provides a number of functions needed in actions in the lexer (grammar). */
export abstract class MySQLLexerBase extends Lexer {
Expand All @@ -30,6 +22,7 @@ export abstract class MySQLLexerBase extends Lexer {
protected inVersionComment = false;

private pendingTokens: Token[] = [];
private justEmittedDot: boolean;

static #longString = "2147483647";
static #longLength = 10;
Expand All @@ -41,6 +34,12 @@ export abstract class MySQLLexerBase extends Lexer {
static #unsignedLongLongString = "18446744073709551615";
static #unsignedLongLongLength = 20;

constructor(input: CharStream) {
super(input);
this.serverVersion = 80200;
this.sqlModes = SqlModes.sqlModeFromString("ANSI_QUOTES");
}

/**
* Determines if the given SQL mode is currently active in the lexer.
*
Expand All @@ -52,33 +51,6 @@ export abstract class MySQLLexerBase extends Lexer {
return this.sqlModes.has(mode);
}

/**
* Converts a mode string into individual mode flags.
*
* @param modes The input string to parse.
*/
public sqlModeFromString(modes: string): void {
this.sqlModes = new Set<SqlMode>();

const parts = modes.toUpperCase().split(",");
parts.forEach((mode: string) => {
if (mode === "ANSI" || mode === "DB2" || mode === "MAXDB" || mode === "MSSQL" || mode === "ORACLE" ||
mode === "POSTGRESQL") {
this.sqlModes.add(SqlMode.AnsiQuotes).add(SqlMode.PipesAsConcat).add(SqlMode.IgnoreSpace);
} else if (mode === "ANSI_QUOTES") {
this.sqlModes.add(SqlMode.AnsiQuotes);
} else if (mode === "PIPES_AS_CONCAT") {
this.sqlModes.add(SqlMode.PipesAsConcat);
} else if (mode === "NO_BACKSLASH_ESCAPES") {
this.sqlModes.add(SqlMode.NoBackslashEscapes);
} else if (mode === "IGNORE_SPACE") {
this.sqlModes.add(SqlMode.IgnoreSpace);
} else if (mode === "HIGH_NOT_PRECEDENCE" || mode === "MYSQL323" || mode === "MYSQL40") {
this.sqlModes.add(SqlMode.HighNotPrecedence);
}
});
}

/**
* Resets the lexer by setting initial values to transient member, resetting the input stream position etc.
*/
Expand Down Expand Up @@ -106,7 +78,6 @@ export abstract class MySQLLexerBase extends Lexer {
pending = this.pendingTokens.shift();
if (pending) {
this.pendingTokens.push(next);

return pending;
}

Expand All @@ -128,7 +99,6 @@ export abstract class MySQLLexerBase extends Lexer {
const version = parseInt(text.substring(3), 10);
if (version <= this.serverVersion) {
this.inVersionComment = true;

return true;
}

Expand Down Expand Up @@ -258,12 +228,21 @@ export abstract class MySQLLexerBase extends Lexer {
*/
protected emitDot(): void {
this.pendingTokens.push(this.tokenFactory.create([this, this.inputStream], MySQLLexer.DOT_SYMBOL,
this.text, this.channel, this.tokenStartCharIndex, this.tokenStartCharIndex, this.line,
this.column,
".", this.channel, this.tokenStartCharIndex, this.tokenStartCharIndex, this.line,
this.column - this.text.length,
));

++this.column;
++this.tokenStartCharIndex;
this.justEmittedDot = true;
}

public override emit(): Token
{
let t = super.emit();
if (this.justEmittedDot) {
t.column = t.column + 1;
this.justEmittedDot = false;
}
return t;
}

public isServerVersionLt80024(): boolean
Expand Down Expand Up @@ -313,226 +292,226 @@ export abstract class MySQLLexerBase extends Lexer {

public doLogicalOr(): void
{
this.type = this.isSqlModeActive(SqlMode.PipesAsConcat) ? MySQLLexer.CONCAT_PIPES_SYMBOL : MySQLLexer.LOGICAL_OR_OPERATOR;
this.type = this.isSqlModeActive(SqlMode.PipesAsConcat) ? MySQLLexer.CONCAT_PIPES_SYMBOL : MySQLLexer.LOGICAL_OR_OPERATOR;
}

public doIntNumber(): void
{
this.type = this.determineNumericType(this.text);
this.type = this.determineNumericType(this.text);
}

public doAdddate(): void
{
this.type = this.determineFunction(MySQLLexer.ADDDATE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.ADDDATE_SYMBOL);
}

public doBitAnd(): void
{
this.type = this.determineFunction(MySQLLexer.BIT_AND_SYMBOL);
this.type = this.determineFunction(MySQLLexer.BIT_AND_SYMBOL);
}

public doBitOr(): void
{
this.type = this.determineFunction(MySQLLexer.BIT_OR_SYMBOL);
this.type = this.determineFunction(MySQLLexer.BIT_OR_SYMBOL);
}

public doBitXor(): void
{
this.type = this.determineFunction(MySQLLexer.BIT_XOR_SYMBOL);
this.type = this.determineFunction(MySQLLexer.BIT_XOR_SYMBOL);
}

public doCast(): void
{
this.type = this.determineFunction(MySQLLexer.CAST_SYMBOL);
this.type = this.determineFunction(MySQLLexer.CAST_SYMBOL);
}

public doCount(): void
{
this.type = this.determineFunction(MySQLLexer.COUNT_SYMBOL);
this.type = this.determineFunction(MySQLLexer.COUNT_SYMBOL);
}

public doCurdate(): void
{
this.type = this.determineFunction(MySQLLexer.CURDATE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.CURDATE_SYMBOL);
}

public doCurrentDate(): void
{
this.type = this.determineFunction(MySQLLexer.CURDATE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.CURDATE_SYMBOL);
}

public doCurrentTime(): void
{
this.type = this.determineFunction(MySQLLexer.CURTIME_SYMBOL);
this.type = this.determineFunction(MySQLLexer.CURTIME_SYMBOL);
}

public doCurtime(): void
{
this.type = this.determineFunction(MySQLLexer.CURTIME_SYMBOL);
this.type = this.determineFunction(MySQLLexer.CURTIME_SYMBOL);
}

public doDateAdd(): void
{
this.type = this.determineFunction(MySQLLexer.DATE_ADD_SYMBOL);
this.type = this.determineFunction(MySQLLexer.DATE_ADD_SYMBOL);
}

public doDateSub(): void
{
this.type = this.determineFunction(MySQLLexer.DATE_SUB_SYMBOL);
this.type = this.determineFunction(MySQLLexer.DATE_SUB_SYMBOL);
}

public doExtract(): void
{
this.type = this.determineFunction(MySQLLexer.EXTRACT_SYMBOL);
this.type = this.determineFunction(MySQLLexer.EXTRACT_SYMBOL);
}

public doGroupConcat(): void
{
this.type = this.determineFunction(MySQLLexer.GROUP_CONCAT_SYMBOL);
this.type = this.determineFunction(MySQLLexer.GROUP_CONCAT_SYMBOL);
}

public doMax(): void
{
this.type = this.determineFunction(MySQLLexer.MAX_SYMBOL);
this.type = this.determineFunction(MySQLLexer.MAX_SYMBOL);
}

public doMid(): void
{
this.type = this.determineFunction(MySQLLexer.SUBSTRING_SYMBOL);
this.type = this.determineFunction(MySQLLexer.SUBSTRING_SYMBOL);
}

public doMin(): void
{
this.type = this.determineFunction(MySQLLexer.MIN_SYMBOL);
this.type = this.determineFunction(MySQLLexer.MIN_SYMBOL);
}

public doNot(): void
{
this.type = this.isSqlModeActive(SqlMode.HighNotPrecedence) ? MySQLLexer.NOT2_SYMBOL: MySQLLexer.NOT_SYMBOL;
this.type = this.isSqlModeActive(SqlMode.HighNotPrecedence) ? MySQLLexer.NOT2_SYMBOL: MySQLLexer.NOT_SYMBOL;
}

public doNow(): void
{
this.type = this.determineFunction(MySQLLexer.NOW_SYMBOL);
this.type = this.determineFunction(MySQLLexer.NOW_SYMBOL);
}

public doPosition(): void
{
this.type = this.determineFunction(MySQLLexer.POSITION_SYMBOL);
this.type = this.determineFunction(MySQLLexer.POSITION_SYMBOL);
}

public doSessionUser(): void
{
this.type = this.determineFunction(MySQLLexer.USER_SYMBOL);
this.type = this.determineFunction(MySQLLexer.USER_SYMBOL);
}

public doStddevSamp(): void
{
this.type = this.determineFunction(MySQLLexer.STDDEV_SAMP_SYMBOL);
this.type = this.determineFunction(MySQLLexer.STDDEV_SAMP_SYMBOL);
}

public doStddev(): void
{
this.type = this.determineFunction(MySQLLexer.STD_SYMBOL);
this.type = this.determineFunction(MySQLLexer.STD_SYMBOL);
}

public doStddevPop(): void
{
this.type = this.determineFunction(MySQLLexer.STD_SYMBOL);
this.type = this.determineFunction(MySQLLexer.STD_SYMBOL);
}

public doStd(): void
{
this.type = this.determineFunction(MySQLLexer.STD_SYMBOL);
this.type = this.determineFunction(MySQLLexer.STD_SYMBOL);
}

public doSubdate(): void
{
this.type = this.determineFunction(MySQLLexer.SUBDATE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.SUBDATE_SYMBOL);
}

public doSubstr(): void
{
this.type = this.determineFunction(MySQLLexer.SUBSTRING_SYMBOL);
this.type = this.determineFunction(MySQLLexer.SUBSTRING_SYMBOL);
}

public doSubstring(): void
{
this.type = this.determineFunction(MySQLLexer.SUBSTRING_SYMBOL);
this.type = this.determineFunction(MySQLLexer.SUBSTRING_SYMBOL);
}

public doSum(): void
{
this.type = this.determineFunction(MySQLLexer.SUM_SYMBOL);
this.type = this.determineFunction(MySQLLexer.SUM_SYMBOL);
}

public doSysdate(): void
{
this.type = this.determineFunction(MySQLLexer.SYSDATE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.SYSDATE_SYMBOL);
}

public doSystemUser(): void
{
this.type = this.determineFunction(MySQLLexer.USER_SYMBOL);
this.type = this.determineFunction(MySQLLexer.USER_SYMBOL);
}

public doTrim(): void
{
this.type = this.determineFunction(MySQLLexer.TRIM_SYMBOL);
this.type = this.determineFunction(MySQLLexer.TRIM_SYMBOL);
}

public doVariance(): void
{
this.type = this.determineFunction(MySQLLexer.VARIANCE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.VARIANCE_SYMBOL);
}

public doVarPop(): void
{
this.type = this.determineFunction(MySQLLexer.VARIANCE_SYMBOL);
this.type = this.determineFunction(MySQLLexer.VARIANCE_SYMBOL);
}

public doVarSamp(): void
{
this.type = this.determineFunction(MySQLLexer.VAR_SAMP_SYMBOL);
this.type = this.determineFunction(MySQLLexer.VAR_SAMP_SYMBOL);
}

public doUnderscoreCharset(): void
{
this.type = this.checkCharset(this.text);
this.type = this.checkCharset(this.text);
}

public isVersionComment(): boolean
{
return this.checkMySQLVersion(this.text);
return this.checkMySQLVersion(this.text);
}

public isBackTickQuotedId(): boolean
{
return !this.isSqlModeActive(SqlMode.NoBackslashEscapes);
return !this.isSqlModeActive(SqlMode.NoBackslashEscapes);
}

public isDoubleQuotedText(): boolean
{
return !this.isSqlModeActive(SqlMode.NoBackslashEscapes);
return !this.isSqlModeActive(SqlMode.NoBackslashEscapes);
}

public isSingleQuotedText(): boolean
{
return !this.isSqlModeActive(SqlMode.NoBackslashEscapes);
return !this.isSqlModeActive(SqlMode.NoBackslashEscapes);
}

public startInVersionComment(): void
{
this.inVersionComment = true;
this.inVersionComment = true;
}

public endInVersionComment(): void
{
this.inVersionComment = false;
this.inVersionComment = false;
}

public isInVersionComment(): boolean
{
return this.inVersionComment;
return this.inVersionComment;
}
}
Loading
Loading