Skip to content

Commit 2f91c10

Browse files
committed
[update] setting up the database type for different pattern use cases
Also fixed #1058
1 parent 62187ff commit 2f91c10

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

src/Medoo.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ class Medoo
182182
*/
183183
public $errorInfo = null;
184184

185+
/**
186+
* The keyword to connect the table alias.
187+
*
188+
* @var string
189+
*/
190+
protected $tableAliasConnector = ' AS ';
191+
192+
/**
193+
* The quote pattern string.
194+
*
195+
* @var string
196+
*/
197+
protected $quotePattern = '"$1"';
198+
185199
/**
186200
* Regular expression pattern for valid table names.
187201
*
@@ -254,11 +268,7 @@ public function __construct(array $options)
254268
}
255269
}
256270

257-
$this->type = strtolower($options['type']);
258-
259-
if ($this->type === 'mariadb') {
260-
$this->type = 'mysql';
261-
}
271+
$this->setupType($options['type']);
262272

263273
if (isset($options['logging']) && is_bool($options['logging'])) {
264274
$this->logging = $options['logging'];
@@ -506,6 +516,30 @@ public function __construct(array $options)
506516
}
507517
}
508518

519+
/**
520+
* Setup the database type.
521+
*
522+
* @return void
523+
*/
524+
public function setupType(string $type)
525+
{
526+
$databaseType = strtolower($type);
527+
528+
if ($databaseType === 'mariadb') {
529+
$databaseType = 'mysql';
530+
}
531+
532+
if ($databaseType === 'oracle') {
533+
$this->tableAliasConnector = ' ';
534+
} elseif ($databaseType === 'mysql') {
535+
$this->quotePattern = '`$1`';
536+
} elseif ($databaseType === 'mssql') {
537+
$this->quotePattern = '[$1]';
538+
}
539+
540+
$this->type = $databaseType;
541+
}
542+
509543
/**
510544
* Generate a new map key for the placeholder.
511545
*
@@ -618,14 +652,9 @@ public function exec(string $statement, array $map = [], ?callable $callback = n
618652
*/
619653
protected function generate(string $statement, array $map): string
620654
{
621-
$identifier = [
622-
'mysql' => '`$1`',
623-
'mssql' => '[$1]'
624-
];
625-
626655
$statement = preg_replace(
627-
'/(?!\'[^\s]+\s?)"([\p{L}_][\p{L}\p{N}@$#\-_]*)"(?!\s?[^\s]+\')/u',
628-
$identifier[$this->type] ?? '"$1"',
656+
'/(?!\'[^\s]+\s?)"(' . $this::COLUMN_PATTERN . ')"(?!\s?[^\s]+\')/u',
657+
$this->quotePattern,
629658
$statement
630659
);
631660

@@ -1214,7 +1243,7 @@ protected function selectContext(
12141243
if (isset($tableMatch['table'], $tableMatch['alias'])) {
12151244
$table = $this->tableQuote($tableMatch['table']);
12161245
$tableAlias = $this->tableQuote($tableMatch['alias']);
1217-
$tableQuery = "{$table} AS {$tableAlias}";
1246+
$tableQuery = "{$table}{$this->tableAliasConnector}{$tableAlias}";
12181247
} else {
12191248
$table = $this->tableQuote($table);
12201249
$tableQuery = $table;
@@ -1352,7 +1381,7 @@ protected function buildJoin(string $table, array $join, array &$map): string
13521381
$tableName = $this->tableQuote($match['table']);
13531382

13541383
if (isset($match['alias'])) {
1355-
$tableName .= ' AS ' . $this->tableQuote($match['alias']);
1384+
$tableName .= $this->tableAliasConnector . $this->tableQuote($match['alias']);
13561385
}
13571386

13581387
$tableJoin[] = $type[$match['join']] . " JOIN {$tableName} {$relation}";

tests/MedooTestCase.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class MedooTestCase extends TestCase
99
{
1010
protected $database;
1111

12+
public $tableAliasConnector = ' AS ';
13+
public $quotePattern = '"$1"';
14+
1215
public function setUp(): void
1316
{
1417
$this->database = new Medoo([
@@ -29,21 +32,30 @@ public function typesProvider(): array
2932

3033
public function setType($type): void
3134
{
32-
$this->database->type = $type;
35+
$this->database->setupType($type);
36+
37+
if ($type === 'oracle') {
38+
$this->tableAliasConnector = ' ';
39+
} elseif ($type === 'mysql') {
40+
$this->quotePattern = '`$1`';
41+
} elseif ($type === 'mssql') {
42+
$this->quotePattern = '[$1]';
43+
}
3344
}
3445

3546
public function expectedQuery($expected): string
3647
{
37-
$identifier = [
38-
'mysql' => '`$1`',
39-
'mssql' => '[$1]'
40-
];
41-
42-
return preg_replace(
48+
$result = preg_replace(
4349
'/(?!\'[^\s]+\s?)"([\p{L}_][\p{L}\p{N}@$#\-_]*)"(?!\s?[^\s]+\')/u',
44-
$identifier[$this->database->type] ?? '"$1"',
50+
$this->quotePattern,
4551
str_replace("\n", " ", $expected)
4652
);
53+
54+
return str_replace(
55+
' @AS ',
56+
$this->tableAliasConnector,
57+
$result
58+
);
4759
}
4860

4961
public function assertQuery($expected, $query): void

tests/SelectTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testSelectTableWithAlias($type)
4646
$this->assertQuery(
4747
<<<EOD
4848
SELECT "name"
49-
FROM "account" AS "user"
49+
FROM "account" @AS "user"
5050
EOD,
5151
$this->database->queryString
5252
);
@@ -385,7 +385,7 @@ public function testSelectWithAliasJoin($type)
385385
<<<EOD
386386
SELECT "account"."name","main_post"."title"
387387
FROM "account"
388-
LEFT JOIN "post" AS "main_post"
388+
LEFT JOIN "post" @AS "main_post"
389389
ON "account"."user_id" = "main_post"."author_id"
390390
EOD,
391391
$this->database->queryString

0 commit comments

Comments
 (0)