Convert some SQL to HEREDOCS

This commit is contained in:
Benoît Burnichon
2016-03-29 15:28:42 +02:00
parent e10e83dd36
commit 11abedd0ed
2 changed files with 14 additions and 10 deletions

View File

@@ -131,28 +131,30 @@ class BuildSubdefs extends Command
*/ */
protected function generateSQL(array $subdefNames, array $recordTypes, $min, $max, $withSubstitution, $substitutionOnly) protected function generateSQL(array $subdefNames, array $recordTypes, $min, $max, $withSubstitution, $substitutionOnly)
{ {
$sql = "SELECT DISTINCT(r.record_id) AS record_id" $sql = <<<'SQL'
. " FROM record r LEFT JOIN subdef s ON (r.record_id = s.record_id AND s.name IN (?))" SELECT DISTINCT(r.record_id) AS record_id
. " WHERE r.type IN (?)"; FROM record r LEFT JOIN subdef s ON (r.record_id = s.record_id AND s.name IN (?))
WHERE r.type IN (?)
SQL;
$types = array(Connection::PARAM_STR_ARRAY, Connection::PARAM_STR_ARRAY); $types = array(Connection::PARAM_STR_ARRAY, Connection::PARAM_STR_ARRAY);
$params = array($subdefNames, $recordTypes); $params = array($subdefNames, $recordTypes);
if (null !== $min) { if (null !== $min) {
$sql .= " AND (r.record_id >= ?)"; $sql .= ' AND (r.record_id >= ?)';
$params[] = (int)$min; $params[] = (int)$min;
$types[] = \PDO::PARAM_INT; $types[] = \PDO::PARAM_INT;
} }
if (null !== $max) { if (null !== $max) {
$sql .= " AND (r.record_id <= ?)"; $sql .= ' AND (r.record_id <= ?)';
$params[] = (int)$max; $params[] = (int)$max;
$types[] = \PDO::PARAM_INT; $types[] = \PDO::PARAM_INT;
} }
if (false === $withSubstitution) { if (false === $withSubstitution) {
$sql .= " AND (ISNULL(s.substit) OR s.substit = ?)"; $sql .= ' AND (ISNULL(s.substit) OR s.substit = ?)';
$params[] = $substitutionOnly ? 1 : 0; $params[] = $substitutionOnly ? 1 : 0;
$types[] = \PDO::PARAM_INT; $types[] = \PDO::PARAM_INT;
} }

View File

@@ -75,10 +75,12 @@ class caption_record implements caption_interface, cache_cacheableInterface
try { try {
$fields = $this->get_data_from_cache(); $fields = $this->get_data_from_cache();
} catch (\Exception $e) { } catch (\Exception $e) {
$sql = "SELECT m.id AS meta_id, s.id AS structure_id, value, VocabularyType, VocabularyId" $sql = <<<'SQL'
. " FROM metadatas m, metadatas_structure s" SELECT m.id AS meta_id, s.id AS structure_id, value, VocabularyType, VocabularyId
. " WHERE m.record_id = :record_id AND s.id = m.meta_struct_id" FROM metadatas m INNER JOIN metadatas_structure s ON s.id = m.meta_struct_id
. " ORDER BY s.sorter ASC"; WHERE m.record_id = :record_id
ORDER BY s.sorter ASC
SQL;
$stmt = $this->databox->get_connection()->prepare($sql); $stmt = $this->databox->get_connection()->prepare($sql);
$stmt->execute([':record_id' => $this->record->getRecordId()]); $stmt->execute([':record_id' => $this->record->getRecordId()]);
$fields = $stmt->fetchAll(PDO::FETCH_ASSOC); $fields = $stmt->fetchAll(PDO::FETCH_ASSOC);