Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
224
Merge Requests
224
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
a4065554
Commit
a4065554
authored
Oct 25, 2008
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#318440
by Crell: Make it easier to add multiple fields in a dynamic SELECT statement.
parent
a7481a7c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
1 deletion
+92
-1
includes/database/select.inc
includes/database/select.inc
+44
-0
modules/simpletest/tests/database_test.test
modules/simpletest/tests/database_test.test
+48
-1
No files found.
includes/database/select.inc
View file @
a4065554
...
...
@@ -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
.
=
"
\n
FROM "
;
foreach
(
$this
->
tables
as
$alias
=>
$table
)
{
...
...
modules/simpletest/tests/database_test.test
View file @
a4065554
...
...
@@ -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.
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment