Skip to content

Commit d22606b

Browse files
committed
Add SqlModes class with SQL modes constants
Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent 9cf3396 commit d22606b

File tree

5 files changed

+305
-98
lines changed

5 files changed

+305
-98
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Add missing return types annotations
66
* Improve the WITH statements parser (#363)
7+
* Add SqlModes class with SQL modes constants
78

89
## [5.5.0] - 2021-12-08
910

psalm-baseline.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.18.0.0">
2+
<files psalm-version="4.18.1@dda05fa913f4dc6eb3386f2f7ce5a45d37a71bcb">
33
<file src="src/Component.php">
44
<MixedReturnStatement occurrences="1">
55
<code>static::build($this)</code>
@@ -589,7 +589,7 @@
589589
<code>static::$MODE</code>
590590
</MixedAssignment>
591591
<MixedOperand occurrences="1">
592-
<code>constant('static::SQL_MODE_' . $m)</code>
592+
<code>constant(SqlModes::class . '::' . $m)</code>
593593
</MixedOperand>
594594
</file>
595595
<file src="src/Contexts/ContextMariaDb100000.php">

src/Context.php

Lines changed: 78 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -143,123 +143,103 @@ abstract class Context
143143
];
144144

145145
/**
146-
* The mode of the MySQL server that will be used in lexing, parsing and
147-
* building the statements.
146+
* The mode of the MySQL server that will be used in lexing, parsing and building the statements.
147+
*
148+
* @internal use the {@see Context::getMode()} method instead.
148149
*
149150
* @var int
150151
*/
151-
public static $MODE = 0;
152-
153-
/*
154-
* Server SQL Modes
155-
* https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html
156-
*/
152+
public static $MODE = SqlModes::NONE;
157153

158-
// Compatibility mode for Microsoft's SQL server.
159-
// This is the equivalent of ANSI_QUOTES.
160-
public const SQL_MODE_COMPAT_MYSQL = 2;
154+
/** @deprecated Use {@see SqlModes::COMPAT_MYSQL} instead. */
155+
public const SQL_MODE_COMPAT_MYSQL = SqlModes::COMPAT_MYSQL;
161156

162-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates
163-
public const SQL_MODE_ALLOW_INVALID_DATES = 1;
157+
/** @deprecated Use {@see SqlModes::ALLOW_INVALID_DATES} instead. */
158+
public const SQL_MODE_ALLOW_INVALID_DATES = SqlModes::ALLOW_INVALID_DATES;
164159

165-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ansi_quotes
166-
public const SQL_MODE_ANSI_QUOTES = 2;
160+
/** @deprecated Use {@see SqlModes::ANSI_QUOTES} instead. */
161+
public const SQL_MODE_ANSI_QUOTES = SqlModes::ANSI_QUOTES;
167162

168-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_error_for_division_by_zero
169-
public const SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO = 4;
163+
/** @deprecated Use {@see SqlModes::ERROR_FOR_DIVISION_BY_ZERO} instead. */
164+
public const SQL_MODE_ERROR_FOR_DIVISION_BY_ZERO = SqlModes::ERROR_FOR_DIVISION_BY_ZERO;
170165

171-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_high_not_precedence
172-
public const SQL_MODE_HIGH_NOT_PRECEDENCE = 8;
166+
/** @deprecated Use {@see SqlModes::HIGH_NOT_PRECEDENCE} instead. */
167+
public const SQL_MODE_HIGH_NOT_PRECEDENCE = SqlModes::HIGH_NOT_PRECEDENCE;
173168

174-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ignore_space
175-
public const SQL_MODE_IGNORE_SPACE = 16;
169+
/** @deprecated Use {@see SqlModes::IGNORE_SPACE} instead. */
170+
public const SQL_MODE_IGNORE_SPACE = SqlModes::IGNORE_SPACE;
176171

177-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_create_user
178-
public const SQL_MODE_NO_AUTO_CREATE_USER = 32;
172+
/** @deprecated Use {@see SqlModes::NO_AUTO_CREATE_USER} instead. */
173+
public const SQL_MODE_NO_AUTO_CREATE_USER = SqlModes::NO_AUTO_CREATE_USER;
179174

180-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_auto_value_on_zero
181-
public const SQL_MODE_NO_AUTO_VALUE_ON_ZERO = 64;
175+
/** @deprecated Use {@see SqlModes::NO_AUTO_VALUE_ON_ZERO} instead. */
176+
public const SQL_MODE_NO_AUTO_VALUE_ON_ZERO = SqlModes::NO_AUTO_VALUE_ON_ZERO;
182177

183-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_backslash_escapes
184-
public const SQL_MODE_NO_BACKSLASH_ESCAPES = 128;
178+
/** @deprecated Use {@see SqlModes::NO_BACKSLASH_ESCAPES} instead. */
179+
public const SQL_MODE_NO_BACKSLASH_ESCAPES = SqlModes::NO_BACKSLASH_ESCAPES;
185180

186-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create
187-
public const SQL_MODE_NO_DIR_IN_CREATE = 256;
181+
/** @deprecated Use {@see SqlModes::NO_DIR_IN_CREATE} instead. */
182+
public const SQL_MODE_NO_DIR_IN_CREATE = SqlModes::NO_DIR_IN_CREATE;
188183

189-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_dir_in_create
190-
public const SQL_MODE_NO_ENGINE_SUBSTITUTION = 512;
184+
/** @deprecated Use {@see SqlModes::NO_ENGINE_SUBSTITUTION} instead. */
185+
public const SQL_MODE_NO_ENGINE_SUBSTITUTION = SqlModes::NO_ENGINE_SUBSTITUTION;
191186

192-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_field_options
193-
public const SQL_MODE_NO_FIELD_OPTIONS = 1024;
187+
/** @deprecated Use {@see SqlModes::NO_FIELD_OPTIONS} instead. */
188+
public const SQL_MODE_NO_FIELD_OPTIONS = SqlModes::NO_FIELD_OPTIONS;
194189

195-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_key_options
196-
public const SQL_MODE_NO_KEY_OPTIONS = 2048;
190+
/** @deprecated Use {@see SqlModes::NO_KEY_OPTIONS} instead. */
191+
public const SQL_MODE_NO_KEY_OPTIONS = SqlModes::NO_KEY_OPTIONS;
197192

198-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_table_options
199-
public const SQL_MODE_NO_TABLE_OPTIONS = 4096;
193+
/** @deprecated Use {@see SqlModes::NO_TABLE_OPTIONS} instead. */
194+
public const SQL_MODE_NO_TABLE_OPTIONS = SqlModes::NO_TABLE_OPTIONS;
200195

201-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_unsigned_subtraction
202-
public const SQL_MODE_NO_UNSIGNED_SUBTRACTION = 8192;
196+
/** @deprecated Use {@see SqlModes::NO_UNSIGNED_SUBTRACTION} instead. */
197+
public const SQL_MODE_NO_UNSIGNED_SUBTRACTION = SqlModes::NO_UNSIGNED_SUBTRACTION;
203198

204-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_date
205-
public const SQL_MODE_NO_ZERO_DATE = 16384;
199+
/** @deprecated Use {@see SqlModes::NO_ZERO_DATE} instead. */
200+
public const SQL_MODE_NO_ZERO_DATE = SqlModes::NO_ZERO_DATE;
206201

207-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_no_zero_in_date
208-
public const SQL_MODE_NO_ZERO_IN_DATE = 32768;
202+
/** @deprecated Use {@see SqlModes::NO_ZERO_IN_DATE} instead. */
203+
public const SQL_MODE_NO_ZERO_IN_DATE = SqlModes::NO_ZERO_IN_DATE;
209204

210-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_only_full_group_by
211-
public const SQL_MODE_ONLY_FULL_GROUP_BY = 65536;
205+
/** @deprecated Use {@see SqlModes::ONLY_FULL_GROUP_BY} instead. */
206+
public const SQL_MODE_ONLY_FULL_GROUP_BY = SqlModes::ONLY_FULL_GROUP_BY;
212207

213-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_pipes_as_concat
214-
public const SQL_MODE_PIPES_AS_CONCAT = 131072;
208+
/** @deprecated Use {@see SqlModes::PIPES_AS_CONCAT} instead. */
209+
public const SQL_MODE_PIPES_AS_CONCAT = SqlModes::PIPES_AS_CONCAT;
215210

216-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_real_as_float
217-
public const SQL_MODE_REAL_AS_FLOAT = 262144;
211+
/** @deprecated Use {@see SqlModes::REAL_AS_FLOAT} instead. */
212+
public const SQL_MODE_REAL_AS_FLOAT = SqlModes::REAL_AS_FLOAT;
218213

219-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_all_tables
220-
public const SQL_MODE_STRICT_ALL_TABLES = 524288;
214+
/** @deprecated Use {@see SqlModes::STRICT_ALL_TABLES} instead. */
215+
public const SQL_MODE_STRICT_ALL_TABLES = SqlModes::STRICT_ALL_TABLES;
221216

222-
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_strict_trans_tables
223-
public const SQL_MODE_STRICT_TRANS_TABLES = 1048576;
217+
/** @deprecated Use {@see SqlModes::STRICT_TRANS_TABLES} instead. */
218+
public const SQL_MODE_STRICT_TRANS_TABLES = SqlModes::STRICT_TRANS_TABLES;
224219

225-
// Custom modes.
226-
227-
// The table and column names and any other field that must be escaped will
228-
// not be.
229-
// Reserved keywords are being escaped regardless this mode is used or not.
230-
public const SQL_MODE_NO_ENCLOSING_QUOTES = 1073741824;
231-
232-
/*
233-
* Combination SQL Modes
234-
* https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sql-mode-combo
235-
*/
220+
/** @deprecated Use {@see SqlModes::NO_ENCLOSING_QUOTES} instead. */
221+
public const SQL_MODE_NO_ENCLOSING_QUOTES = SqlModes::NO_ENCLOSING_QUOTES;
236222

237-
// REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE
238-
public const SQL_MODE_ANSI = 393234;
223+
/** @deprecated Use {@see SqlModes::ANSI} instead. */
224+
public const SQL_MODE_ANSI = SqlModes::ANSI;
239225

240-
// PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS,
241-
// NO_TABLE_OPTIONS, NO_FIELD_OPTIONS,
242-
public const SQL_MODE_DB2 = 138258;
226+
/** @deprecated Use {@see SqlModes::DB2} instead. */
227+
public const SQL_MODE_DB2 = SqlModes::DB2;
243228

244-
// PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS,
245-
// NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER
246-
public const SQL_MODE_MAXDB = 138290;
229+
/** @deprecated Use {@see SqlModes::MAXDB} instead. */
230+
public const SQL_MODE_MAXDB = SqlModes::MAXDB;
247231

248-
// PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS,
249-
// NO_TABLE_OPTIONS, NO_FIELD_OPTIONS
250-
public const SQL_MODE_MSSQL = 138258;
232+
/** @deprecated Use {@see SqlModes::MSSQL} instead. */
233+
public const SQL_MODE_MSSQL = SqlModes::MSSQL;
251234

252-
// PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS,
253-
// NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER
254-
public const SQL_MODE_ORACLE = 138290;
235+
/** @deprecated Use {@see SqlModes::ORACLE} instead. */
236+
public const SQL_MODE_ORACLE = SqlModes::ORACLE;
255237

256-
// PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS,
257-
// NO_TABLE_OPTIONS, NO_FIELD_OPTIONS
258-
public const SQL_MODE_POSTGRESQL = 138258;
238+
/** @deprecated Use {@see SqlModes::POSTGRESQL} instead. */
239+
public const SQL_MODE_POSTGRESQL = SqlModes::POSTGRESQL;
259240

260-
// STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE,
261-
// ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER
262-
public const SQL_MODE_TRADITIONAL = 1622052;
241+
/** @deprecated Use {@see SqlModes::TRADITIONAL} instead. */
242+
public const SQL_MODE_TRADITIONAL = SqlModes::TRADITIONAL;
263243

264244
// -------------------------------------------------------------------------
265245
// Keyword.
@@ -572,17 +552,25 @@ public static function loadClosest($context = '')
572552
*/
573553
public static function setMode($mode = '')
574554
{
575-
static::$MODE = 0;
555+
static::$MODE = SqlModes::NONE;
576556
if (empty($mode)) {
577557
return;
578558
}
579559

580560
$mode = explode(',', $mode);
581561
foreach ($mode as $m) {
582-
static::$MODE |= constant('static::SQL_MODE_' . $m);
562+
static::$MODE |= constant(SqlModes::class . '::' . $m);
583563
}
584564
}
585565

566+
/**
567+
* Gets the SQL mode.
568+
*/
569+
public static function getMode(): int
570+
{
571+
return static::$MODE;
572+
}
573+
586574
/**
587575
* Escapes the symbol by adding surrounding backticks.
588576
*
@@ -601,11 +589,11 @@ public static function escape($str, $quote = '`')
601589
return $str;
602590
}
603591

604-
if ((static::$MODE & self::SQL_MODE_NO_ENCLOSING_QUOTES) && (! static::isKeyword($str, true))) {
592+
if ((static::$MODE & SqlModes::NO_ENCLOSING_QUOTES) && (! static::isKeyword($str, true))) {
605593
return $str;
606594
}
607595

608-
if (static::$MODE & self::SQL_MODE_ANSI_QUOTES) {
596+
if (static::$MODE & SqlModes::ANSI_QUOTES) {
609597
$quote = '"';
610598
}
611599

@@ -619,13 +607,13 @@ public static function escape($str, $quote = '`')
619607
*/
620608
public static function getIdentifierQuote()
621609
{
622-
return self::hasMode(self::SQL_MODE_ANSI_QUOTES) ? '"' : '`';
610+
return self::hasMode(SqlModes::ANSI_QUOTES) ? '"' : '`';
623611
}
624612

625613
/**
626614
* Function verifies that given SQL Mode constant is currently set
627615
*
628-
* @param int $flag for example Context::SQL_MODE_ANSI_QUOTES
616+
* @param int $flag for example {@see SqlModes::ANSI_QUOTES}
629617
*
630618
* @return bool false on empty param, true/false on given constant/int value
631619
*/

0 commit comments

Comments
 (0)