Commit c19740f8 authored by alexpott's avatar alexpott

Issue #2658438 by kristiaanvandeneynde, Torenware, dawehner, alexpott: Array...

Issue #2658438 by kristiaanvandeneynde, Torenware, dawehner, alexpott: Array valued 'extra' values generate invalid SQL in Views joins
parent 262ff525
...@@ -303,24 +303,18 @@ public function buildJoin($select_query, $table, $view_query) { ...@@ -303,24 +303,18 @@ public function buildJoin($select_query, $table, $view_query) {
if (is_array($info['value']) && count($info['value']) == 1) { if (is_array($info['value']) && count($info['value']) == 1) {
$info['value'] = array_shift($info['value']); $info['value'] = array_shift($info['value']);
} }
if (is_array($info['value'])) { if (is_array($info['value'])) {
// With an array of values, we need multiple placeholders and the // We use an SA-CORE-2014-005 conformant placeholder for our array
// 'IN' operator is implicit. // of values. Also, note that the 'IN' operator is implicit.
$local_arguments = array(); // @see https://www.drupal.org/node/2401615.
foreach ($info['value'] as $value) {
$placeholder_i = ':views_join_condition_' . $select_query->nextPlaceholder();
$local_arguments[$placeholder_i] = $value;
}
$operator = !empty($info['operator']) ? $info['operator'] : 'IN'; $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
$placeholder = '( ' . implode(', ', array_keys($local_arguments)) . ' )'; $placeholder = ':views_join_condition_' . $select_query->nextPlaceholder() . '[]';
$arguments += $local_arguments; $placeholder_sql = "( $placeholder )";
} }
else { else {
// With a single value, the '=' operator is implicit. // With a single value, the '=' operator is implicit.
$operator = !empty($info['operator']) ? $info['operator'] : '='; $operator = !empty($info['operator']) ? $info['operator'] : '=';
$placeholder = ':views_join_condition_' . $select_query->nextPlaceholder(); $placeholder = $placeholder_sql = ':views_join_condition_' . $select_query->nextPlaceholder();
} }
// Set 'field' as join table field if available or set 'left field' as // Set 'field' as join table field if available or set 'left field' as
// join table field is not set. // join table field is not set.
...@@ -329,7 +323,7 @@ public function buildJoin($select_query, $table, $view_query) { ...@@ -329,7 +323,7 @@ public function buildJoin($select_query, $table, $view_query) {
// Allow the value to be set either with the 'value' element or // Allow the value to be set either with the 'value' element or
// with 'left_field'. // with 'left_field'.
if (isset($info['left_field'])) { if (isset($info['left_field'])) {
$placeholder = "$left[alias].$info[left_field]"; $placeholder_sql = "$left[alias].$info[left_field]";
} }
else { else {
$arguments[$placeholder] = $info['value']; $arguments[$placeholder] = $info['value'];
...@@ -340,7 +334,8 @@ public function buildJoin($select_query, $table, $view_query) { ...@@ -340,7 +334,8 @@ public function buildJoin($select_query, $table, $view_query) {
$join_table_field = "$left[alias].$info[left_field]"; $join_table_field = "$left[alias].$info[left_field]";
$arguments[$placeholder] = $info['value']; $arguments[$placeholder] = $info['value'];
} }
$extras[] = "$join_table_field $operator $placeholder"; // Render out the SQL fragment with parameters.
$extras[] = "$join_table_field $operator $placeholder_sql";
} }
if ($extras) { if ($extras) {
......
...@@ -172,7 +172,8 @@ public function testBasePlugin() { ...@@ -172,7 +172,8 @@ public function testBasePlugin() {
$join_info = $tables['users4']; $join_info = $tables['users4'];
$this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users4.uid") !== FALSE, 'Make sure the join condition appears in the query.'); $this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users4.uid") !== FALSE, 'Make sure the join condition appears in the query.');
$this->assertTrue(strpos($join_info['condition'], "users4.name = :views_join_condition_2") !== FALSE, 'Make sure the first extra join condition appears in the query.'); $this->assertTrue(strpos($join_info['condition'], "users4.name = :views_join_condition_2") !== FALSE, 'Make sure the first extra join condition appears in the query.');
$this->assertTrue(strpos($join_info['condition'], "users4.name IN ( :views_join_condition_3, :views_join_condition_4, :views_join_condition_5 )") !== FALSE, 'The IN condition for the join is properly formed.'); $this->assertTrue(strpos($join_info['condition'], "users4.name IN ( :views_join_condition_3[] )") !== FALSE, 'The IN condition for the join is properly formed.');
$this->assertEqual($join_info['arguments'][':views_join_condition_3[]'], array($random_name_2, $random_name_3, $random_name_4), 'Make sure the IN arguments are still part of an array.');
// Test that all the conditions are properly built. // Test that all the conditions are properly built.
$configuration['extra'] = array( $configuration['extra'] = array(
...@@ -197,8 +198,8 @@ public function testBasePlugin() { ...@@ -197,8 +198,8 @@ public function testBasePlugin() {
$tables = $query->getTables(); $tables = $query->getTables();
$join_info = $tables['users5']; $join_info = $tables['users5'];
$this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users5.uid") !== FALSE, 'Make sure the join condition appears in the query.'); $this->assertTrue(strpos($join_info['condition'], "views_test_data.uid = users5.uid") !== FALSE, 'Make sure the join condition appears in the query.');
$this->assertTrue(strpos($join_info['condition'], "users5.langcode = :views_join_condition_6") !== FALSE, 'Make sure the first extra join condition appears in the query.'); $this->assertTrue(strpos($join_info['condition'], "users5.langcode = :views_join_condition_4") !== FALSE, 'Make sure the first extra join condition appears in the query.');
$this->assertTrue(strpos($join_info['condition'], "views_test_data.status = :views_join_condition_7") !== FALSE, 'Make sure the second extra join condition appears in the query.'); $this->assertTrue(strpos($join_info['condition'], "views_test_data.status = :views_join_condition_5") !== FALSE, 'Make sure the second extra join condition appears in the query.');
$this->assertTrue(strpos($join_info['condition'], "users5.name = views_test_data.name") !== FALSE, 'Make sure the third extra join condition appears in the query.'); $this->assertTrue(strpos($join_info['condition'], "users5.name = views_test_data.name") !== FALSE, 'Make sure the third extra join condition appears in the query.');
$this->assertEqual(array_values($join_info['arguments']), array('en', 0), 'Make sure the arguments are in the right order'); $this->assertEqual(array_values($join_info['arguments']), array('en', 0), 'Make sure the arguments are in the right order');
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment