Rewrote to suggestions from @MrPetovan (I tried to maintain the old way of

['scheme' => 'table'] with my version).
This commit is contained in:
Roland Häder 2022-06-21 18:11:59 +02:00
parent 3b8d9a7248
commit 0332a57bfd
No known key found for this signature in database
GPG key ID: C82EDE5DDFA0BA77

View file

@ -526,32 +526,25 @@ class DBA
*/ */
public static function buildTableString(array $tables): string public static function buildTableString(array $tables): string
{ {
$quotedTables = []; // Quote each entry
return implode(',', array_map(['self', 'quoteIdentifier'], $tables));
foreach ($tables as $schema => $table) {
if (is_numeric($schema)) {
$str = '';
foreach (explode('.', $table) as $part) {
$str .= self::quoteIdentifier($part) . '.';
}
$quotedTables[] = rtrim($str, '.');
} else {
$quotedTables[] = self::quoteIdentifier($schema) . '.' . self::quoteIdentifier($table);
}
}
return implode(', ', $quotedTables);
} }
/** /**
* Escape an identifier (table or field name) * Escape an identifier (table or field name) optional with a schema like (schema.)table
* *
* @param $identifier * @param $identifier Table, field name
* @return string * @return string Quotes table or field name
*/ */
public static function quoteIdentifier(string $identifier): string public static function quoteIdentifier(string $identifier): string
{ {
return '`' . str_replace('`', '``', $identifier) . '`'; return implode(
'.',
array_map(
function (string $identifier) { return '`' . str_replace('`', '``', $identifier) . '`'; },
explode('.', $identifier)
)
);
} }
/** /**