diff --git a/includes/database/select.inc b/includes/database/select.inc index 2be1e08d956cb2d949c204cb74db4c700779ee55..a1947882ae6cb389b724a3a8851cfff63b8ae475 100644 --- a/includes/database/select.inc +++ b/includes/database/select.inc @@ -37,6 +37,7 @@ class SelectQuery extends Query implements QueryConditionInterface, QueryAlterab * 'alias' => $alias_of_the_table, * 'condition' => $condition_clause_on_which_to_join, * 'arguments' => $array_of_arguments_for_placeholders_in_the condition. + * 'all_fields' => TRUE to SELECT $alias.*, FALSE or NULL otherwise. * ) * * @var array @@ -362,6 +363,43 @@ public function addField($table_alias, $field, $alias = NULL) { return $alias; } + /** + * Add multiple fields from the same table to be SELECTed. + * + * This method does not return the aliases set for the passed fields. In the + * majority of cases that is not a problem, as the alias will be the field + * name. However, if you do need to know the alias you can call getFields() + * and examine the result to determine what alias was created. Alternatively, + * simply use addField() for the few fields you care about and this method for + * the rest. + * + * @param $table_alias + * The name of the table from which the field comes, as an alias. Generally + * you will want to use the return value of join() here to ensure that it is + * valid. + * @param $fields + * An indexed array of fields present in the specified table that should be + * included in this query. If not specified, $table_alias.* will be generated + * without any aliases. + * @return + * The called object. + */ + public function fields($table_alias, Array $fields = array()) { + + if ($fields) { + foreach ($fields as $field) { + // We don't care what alias was assigned. + $this->addField($table_alias, $field); + } + } + else { + // We want all fields from this table. + $this->tables[$table_alias]['all_fields'] = TRUE; + } + + return $this; + } + /** * Adds an expression to the list of "fields" to be SELECTed. * @@ -640,8 +678,14 @@ public function __toString() { foreach ($this->expressions as $alias => $expression) { $fields[] = $expression['expression'] . ' AS ' . $expression['alias']; } + foreach ($this->tables as $alias => $table) { + if (!empty($table['all_fields'])) { + $fields[] = $alias . '.*'; + } + } $query .= implode(', ', $fields); + // FROM - We presume all queries have a FROM, as any query that doesn't won't need the query builder anyway. $query .= "\nFROM "; foreach ($this->tables as $alias => $table) { diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index adc94a39ec244558db33bfbc9d06bde88757e840..fa17db44257c080fadfd1825843044f3b9e04f5b 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1042,6 +1042,54 @@ class DatabaseSelectTestCase extends DatabaseTestCase { $this->assertEqual($record->$name_field, 'George', t('Fetched name is correct.')); $this->assertEqual($record->$age_field, 27*2, t('Fetched age expression is correct.')); } + + /** + * Test adding multiple fields to a select statement at the same time. + */ + function testSimpleSelectMultipleFields() { + + $record = db_select('test') + ->fields('test', array('id', 'name', 'age', 'job')) + ->condition('age', 27) + ->execute()->fetchObject(); + + // Check that all fields we asked for are present. + $this->assertNotNull($record->id, t('ID field is present.')); + $this->assertNotNull($record->name, t('Name field is present.')); + $this->assertNotNull($record->age, t('Age field is present.')); + $this->assertNotNull($record->job, t('Job field is present.')); + + // Ensure that we got the right record. + // Check that all fields we asked for are present. + $this->assertEqual($record->id, 2, t('ID field has the correct value.')); + $this->assertEqual($record->name, 'George', t('Name field has the correct value.')); + $this->assertEqual($record->age, 27, t('Age field has the correct value.')); + $this->assertEqual($record->job, 'Singer', t('Job field has the correct value.')); + } + + /** + * Test adding all fields from a given table to a select statement. + */ + function testSimpleSelectAllFields() { + + $record = db_select('test') + ->fields('test') + ->condition('age', 27) + ->execute()->fetchObject(); + + // Check that all fields we asked for are present. + $this->assertNotNull($record->id, t('ID field is present.')); + $this->assertNotNull($record->name, t('Name field is present.')); + $this->assertNotNull($record->age, t('Age field is present.')); + $this->assertNotNull($record->job, t('Job field is present.')); + + // Ensure that we got the right record. + // Check that all fields we asked for are present. + $this->assertEqual($record->id, 2, t('ID field has the correct value.')); + $this->assertEqual($record->name, 'George', t('Name field has the correct value.')); + $this->assertEqual($record->age, 27, t('Age field has the correct value.')); + $this->assertEqual($record->job, 'Singer', t('Job field has the correct value.')); + } } /** @@ -1119,7 +1167,6 @@ class DatabaseSelectOrderedTestCase extends DatabaseTestCase { } } - /** * Test order by descending. */