1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* Implements hook_query_alter().
|
5 |
|
|
*/
|
6 |
|
|
function database_test_query_alter(QueryAlterableInterface $query) {
|
7 |
|
|
|
8 |
|
|
if ($query->hasTag('database_test_alter_add_range')) {
|
9 |
|
|
$query->range(0, 2);
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
if ($query->hasTag('database_test_alter_add_join')) {
|
13 |
|
|
$people_alias = $query->join('test', 'people', "test_task.pid = %alias.id");
|
14 |
|
|
$name_field = $query->addField($people_alias, 'name', 'name');
|
15 |
|
|
$query->condition($people_alias . '.id', 2);
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
if ($query->hasTag('database_test_alter_change_conditional')) {
|
19 |
|
|
$conditions =& $query->conditions();
|
20 |
|
|
$conditions[0]['value'] = 2;
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
if ($query->hasTag('database_test_alter_change_fields')) {
|
24 |
|
|
$fields =& $query->getFields();
|
25 |
|
|
unset($fields['age']);
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
if ($query->hasTag('database_test_alter_change_expressions')) {
|
29 |
|
|
$expressions =& $query->getExpressions();
|
30 |
|
|
$expressions['double_age']['expression'] = 'age*3';
|
31 |
|
|
}
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
/**
|
36 |
|
|
* Implements hook_query_TAG_alter().
|
37 |
|
|
*
|
38 |
|
|
* Called by DatabaseTestCase::testAlterRemoveRange.
|
39 |
|
|
*/
|
40 |
|
|
function database_test_query_database_test_alter_remove_range_alter(QueryAlterableInterface $query) {
|
41 |
|
|
$query->range();
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Implements hook_menu().
|
46 |
|
|
*/
|
47 |
|
|
function database_test_menu() {
|
48 |
|
|
$items['database_test/db_query_temporary'] = array(
|
49 |
|
|
'access callback' => TRUE,
|
50 |
|
|
'page callback' => 'database_test_db_query_temporary',
|
51 |
|
|
);
|
52 |
|
|
$items['database_test/pager_query_even'] = array(
|
53 |
|
|
'access callback' => TRUE,
|
54 |
|
|
'page callback' => 'database_test_even_pager_query',
|
55 |
|
|
);
|
56 |
|
|
$items['database_test/pager_query_odd'] = array(
|
57 |
|
|
'access callback' => TRUE,
|
58 |
|
|
'page callback' => 'database_test_odd_pager_query',
|
59 |
|
|
);
|
60 |
|
|
$items['database_test/tablesort'] = array(
|
61 |
|
|
'access callback' => TRUE,
|
62 |
|
|
'page callback' => 'database_test_tablesort',
|
63 |
|
|
);
|
64 |
|
|
$items['database_test/tablesort_first'] = array(
|
65 |
|
|
'access callback' => TRUE,
|
66 |
|
|
'page callback' => 'database_test_tablesort_first',
|
67 |
|
|
);
|
68 |
|
|
$items['database_test/tablesort_default_sort'] = array(
|
69 |
|
|
'access callback' => TRUE,
|
70 |
|
|
'page callback' => 'drupal_get_form',
|
71 |
|
|
'page arguments' => array('database_test_theme_tablesort'),
|
72 |
|
|
);
|
73 |
|
|
return $items;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
/**
|
77 |
|
|
* Run a db_query_temporary and output the table name and its number of rows.
|
78 |
|
|
*
|
79 |
|
|
* We need to test that the table created is temporary, so we run it here, in a
|
80 |
|
|
* separate menu callback request; After this request is done, the temporary
|
81 |
|
|
* table should automatically dropped.
|
82 |
|
|
*/
|
83 |
|
|
function database_test_db_query_temporary() {
|
84 |
|
|
$table_name = db_query_temporary('SELECT status FROM {system}', array());
|
85 |
|
|
drupal_json_output(array(
|
86 |
|
|
'table_name' => $table_name,
|
87 |
|
|
'row_count' => db_select($table_name)->countQuery()->execute()->fetchField(),
|
88 |
|
|
));
|
89 |
|
|
exit;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
/**
|
93 |
|
|
* Run a pager query and return the results.
|
94 |
|
|
*
|
95 |
|
|
* This function does care about the page GET parameter, as set by the
|
96 |
|
|
* simpletest HTTP call.
|
97 |
|
|
*/
|
98 |
|
|
function database_test_even_pager_query($limit) {
|
99 |
|
|
|
100 |
|
|
$query = db_select('test', 't');
|
101 |
|
|
$query
|
102 |
|
|
->fields('t', array('name'))
|
103 |
|
|
->orderBy('age');
|
104 |
|
|
|
105 |
|
|
// This should result in 2 pages of results.
|
106 |
|
|
$query = $query->extend('PagerDefault')->limit($limit);
|
107 |
|
|
|
108 |
|
|
$names = $query->execute()->fetchCol();
|
109 |
|
|
|
110 |
|
|
drupal_json_output(array(
|
111 |
|
|
'names' => $names,
|
112 |
|
|
));
|
113 |
|
|
exit;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
/**
|
117 |
|
|
* Run a pager query and return the results.
|
118 |
|
|
*
|
119 |
|
|
* This function does care about the page GET parameter, as set by the
|
120 |
|
|
* simpletest HTTP call.
|
121 |
|
|
*/
|
122 |
|
|
function database_test_odd_pager_query($limit) {
|
123 |
|
|
|
124 |
|
|
$query = db_select('test_task', 't');
|
125 |
|
|
$query
|
126 |
|
|
->fields('t', array('task'))
|
127 |
|
|
->orderBy('pid');
|
128 |
|
|
|
129 |
|
|
// This should result in 4 pages of results.
|
130 |
|
|
$query = $query->extend('PagerDefault')->limit($limit);
|
131 |
|
|
|
132 |
|
|
$names = $query->execute()->fetchCol();
|
133 |
|
|
|
134 |
|
|
drupal_json_output(array(
|
135 |
|
|
'names' => $names,
|
136 |
|
|
));
|
137 |
|
|
exit;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
|
|
* Run a tablesort query and return the results.
|
142 |
|
|
*
|
143 |
|
|
* This function does care about the page GET parameter, as set by the
|
144 |
|
|
* simpletest HTTP call.
|
145 |
|
|
*/
|
146 |
|
|
function database_test_tablesort() {
|
147 |
|
|
$header = array(
|
148 |
|
|
'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
|
149 |
|
|
'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
|
150 |
|
|
'task' => array('data' => t('Task'), 'field' => 'task'),
|
151 |
|
|
'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
|
152 |
|
|
);
|
153 |
|
|
|
154 |
|
|
$query = db_select('test_task', 't');
|
155 |
|
|
$query
|
156 |
|
|
->fields('t', array('tid', 'pid', 'task', 'priority'));
|
157 |
|
|
|
158 |
|
|
$query = $query->extend('TableSort')->orderByHeader($header);
|
159 |
|
|
|
160 |
|
|
// We need all the results at once to check the sort.
|
161 |
|
|
$tasks = $query->execute()->fetchAll();
|
162 |
|
|
|
163 |
|
|
drupal_json_output(array(
|
164 |
|
|
'tasks' => $tasks,
|
165 |
|
|
));
|
166 |
|
|
exit;
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* Run a tablesort query with a second order_by after and return the results.
|
171 |
|
|
*
|
172 |
|
|
* This function does care about the page GET parameter, as set by the
|
173 |
|
|
* simpletest HTTP call.
|
174 |
|
|
*/
|
175 |
|
|
function database_test_tablesort_first() {
|
176 |
|
|
$header = array(
|
177 |
|
|
'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
|
178 |
|
|
'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
|
179 |
|
|
'task' => array('data' => t('Task'), 'field' => 'task'),
|
180 |
|
|
'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
|
181 |
|
|
);
|
182 |
|
|
|
183 |
|
|
$query = db_select('test_task', 't');
|
184 |
|
|
$query
|
185 |
|
|
->fields('t', array('tid', 'pid', 'task', 'priority'));
|
186 |
|
|
|
187 |
|
|
$query = $query->extend('TableSort')->orderByHeader($header)->orderBy('priority');
|
188 |
|
|
|
189 |
|
|
// We need all the results at once to check the sort.
|
190 |
|
|
$tasks = $query->execute()->fetchAll();
|
191 |
|
|
|
192 |
|
|
drupal_json_output(array(
|
193 |
|
|
'tasks' => $tasks,
|
194 |
|
|
));
|
195 |
|
|
exit;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/**
|
199 |
|
|
* Output a form without setting a header sort.
|
200 |
|
|
*/
|
201 |
|
|
function database_test_theme_tablesort($form, &$form_state) {
|
202 |
|
|
$header = array(
|
203 |
|
|
'username' => array('data' => t('Username'), 'field' => 'u.name'),
|
204 |
|
|
'status' => array('data' => t('Status'), 'field' => 'u.status'),
|
205 |
|
|
);
|
206 |
|
|
|
207 |
|
|
$query = db_select('users', 'u');
|
208 |
|
|
$query->condition('u.uid', 0, '<>');
|
209 |
|
|
user_build_filter_query($query);
|
210 |
|
|
|
211 |
|
|
$count_query = clone $query;
|
212 |
|
|
$count_query->addExpression('COUNT(u.uid)');
|
213 |
|
|
|
214 |
|
|
$query = $query->extend('PagerDefault')->extend('TableSort');
|
215 |
|
|
$query
|
216 |
|
|
->fields('u', array('uid', 'name', 'status', 'created', 'access'))
|
217 |
|
|
->limit(50)
|
218 |
|
|
->orderByHeader($header)
|
219 |
|
|
->setCountQuery($count_query);
|
220 |
|
|
$result = $query->execute();
|
221 |
|
|
|
222 |
|
|
$options = array();
|
223 |
|
|
|
224 |
|
|
$status = array(t('blocked'), t('active'));
|
225 |
|
|
$accounts = array();
|
226 |
|
|
foreach ($result as $account) {
|
227 |
|
|
$options[$account->uid] = array(
|
228 |
|
|
'username' => check_plain($account->name),
|
229 |
|
|
'status' => $status[$account->status],
|
230 |
|
|
);
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
$form['accounts'] = array(
|
234 |
|
|
'#type' => 'tableselect',
|
235 |
|
|
'#header' => $header,
|
236 |
|
|
'#options' => $options,
|
237 |
|
|
'#empty' => t('No people available.'),
|
238 |
|
|
);
|
239 |
|
|
|
240 |
|
|
return $form;
|
241 |
|
|
} |