Skip to content

Commit

Permalink
Cleanup SQL queries
Browse files Browse the repository at this point in the history
Collapsed whitespace for easier reading in logs.
  • Loading branch information
mcaskill committed Oct 31, 2019
1 parent 99a4d23 commit 4992196
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
31 changes: 10 additions & 21 deletions src/Charcoal/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,30 +301,19 @@ public function saveProperties(array $properties = null)
public function loadFromL10n($key, $value, array $langs)
{
$switch = [];
$where = [];
$where = [];
foreach ($langs as $lang) {
$switch[] = 'when `'.$key.'_'.$lang.'` = :ident then \''.$lang.'\'';
$where[] = '`'.$key.'_'.$lang.'` = :ident';
$switch[] = 'WHEN `'.$key.'_'.$lang.'` = :ident THEN \''.$lang.'\'';
$where[] = '`'.$key.'_'.$lang.'` = :ident';
}

$q = '
SELECT
*,
(case
'.implode("\n", $switch).'
end) as _lang
FROM
`'.$this->source()->table().'`
WHERE
('.implode(' OR ', $where).')
LIMIT
1';

$binds = [
'ident' => $value
];

$sth = $this->source()->dbQuery($q, $binds);
$source = $this->source();

$sql = 'SELECT *, (CASE '.implode("\n", $switch).' END) AS _lang ';
$sql .= 'FROM `'.$source->table().'` ';
$sql .= 'WHERE ('.implode(' OR ', $where).') LIMIT 1';

$sth = $source->dbQuery($sql, $binds);
if ($sth === false) {
throw new PDOException('Could not load item.');
}
Expand Down
14 changes: 5 additions & 9 deletions src/Charcoal/Source/DatabaseSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,11 @@ public function loadItemFromKey($key, $ident, StorableInterface $item = null)
}

$table = $this->table();
$query = sprintf('
SELECT
*
FROM
`%s`
WHERE
`%s` = :ident
LIMIT
1', $table, $key);
$query = sprintf(
'SELECT * FROM `%s` WHERE `%s` = :ident LIMIT 1',
$table,
$key
);

$binds = [
'ident' => $ident
Expand Down

0 comments on commit 4992196

Please sign in to comment.