summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/database/select.inc44
-rw-r--r--modules/simpletest/tests/database_test.test49
2 files changed, 92 insertions, 1 deletions
diff --git a/includes/database/select.inc b/includes/database/select.inc
index 2be1e08d9..a1947882a 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
@@ -363,6 +364,43 @@ class SelectQuery extends Query implements QueryConditionInterface, QueryAlterab
}
/**
+ * 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.
*
* An expression can be any arbitrary string that is valid SQL. That includes
@@ -640,8 +678,14 @@ class SelectQuery extends Query implements QueryConditionInterface, QueryAlterab
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 adc94a39e..fa17db442 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.
*/