1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Default implementation of the field storage API.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_help().
|
10 |
|
|
*/
|
11 |
|
|
function field_sql_storage_help($path, $arg) {
|
12 |
|
|
switch ($path) {
|
13 |
|
|
case 'admin/help#field_sql_storage':
|
14 |
|
|
$output = '';
|
15 |
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
16 |
|
|
$output .= '<p>' . t('The Field SQL storage module stores field data in the database. It is the default field storage module; other field storage mechanisms may be available as contributed modules. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
|
17 |
|
|
return $output;
|
18 |
|
|
}
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
/**
|
22 |
|
|
* Implements hook_field_storage_info().
|
23 |
|
|
*/
|
24 |
|
|
function field_sql_storage_field_storage_info() {
|
25 |
|
|
return array(
|
26 |
|
|
'field_sql_storage' => array(
|
27 |
|
|
'label' => t('Default SQL storage'),
|
28 |
|
|
'description' => t('Stores fields in the local SQL database, using per-field tables.'),
|
29 |
|
|
),
|
30 |
|
|
);
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
|
|
* Generate a table name for a field data table.
|
35 |
|
|
*
|
36 |
|
|
* @param $field
|
37 |
|
|
* The field structure.
|
38 |
|
|
* @return
|
39 |
|
|
* A string containing the generated name for the database table
|
40 |
|
|
*/
|
41 |
|
|
function _field_sql_storage_tablename($field) {
|
42 |
|
|
if ($field['deleted']) {
|
43 |
|
|
return "field_deleted_data_{$field['id']}";
|
44 |
|
|
}
|
45 |
|
|
else {
|
46 |
|
|
return "field_data_{$field['field_name']}";
|
47 |
|
|
}
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* Generate a table name for a field revision archive table.
|
52 |
|
|
*
|
53 |
|
|
* @param $name
|
54 |
|
|
* The field structure.
|
55 |
|
|
* @return
|
56 |
|
|
* A string containing the generated name for the database table
|
57 |
|
|
*/
|
58 |
|
|
function _field_sql_storage_revision_tablename($field) {
|
59 |
|
|
if ($field['deleted']) {
|
60 |
|
|
return "field_deleted_revision_{$field['id']}";
|
61 |
|
|
}
|
62 |
|
|
else {
|
63 |
|
|
return "field_revision_{$field['field_name']}";
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* Generate a column name for a field data table.
|
69 |
|
|
*
|
70 |
|
|
* @param $name
|
71 |
|
|
* The name of the field
|
72 |
|
|
* @param $column
|
73 |
|
|
* The name of the column
|
74 |
|
|
* @return
|
75 |
|
|
* A string containing a generated column name for a field data
|
76 |
|
|
* table that is unique among all other fields.
|
77 |
|
|
*/
|
78 |
|
|
function _field_sql_storage_columnname($name, $column) {
|
79 |
|
|
return $name . '_' . $column;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Generate an index name for a field data table.
|
84 |
|
|
*
|
85 |
|
|
* @param $name
|
86 |
|
|
* The name of the field
|
87 |
|
|
* @param $column
|
88 |
|
|
* The name of the index
|
89 |
|
|
* @return
|
90 |
|
|
* A string containing a generated index name for a field data
|
91 |
|
|
* table that is unique among all other fields.
|
92 |
|
|
*/
|
93 |
|
|
function _field_sql_storage_indexname($name, $index) {
|
94 |
|
|
return $name . '_' . $index;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Return the database schema for a field. This may contain one or
|
99 |
|
|
* more tables. Each table will contain the columns relevant for the
|
100 |
|
|
* specified field. Leave the $field's 'columns' and 'indexes' keys
|
101 |
|
|
* empty to get only the base schema.
|
102 |
|
|
*
|
103 |
|
|
* @param $field
|
104 |
|
|
* The field structure for which to generate a database schema.
|
105 |
|
|
* @return
|
106 |
|
|
* One or more tables representing the schema for the field.
|
107 |
|
|
*/
|
108 |
|
|
function _field_sql_storage_schema($field) {
|
109 |
|
|
$deleted = $field['deleted'] ? 'deleted ' : '';
|
110 |
|
|
$current = array(
|
111 |
|
|
'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})",
|
112 |
|
|
'fields' => array(
|
113 |
|
|
'entity_type' => array(
|
114 |
|
|
'type' => 'varchar',
|
115 |
|
|
'length' => 128,
|
116 |
|
|
'not null' => TRUE,
|
117 |
|
|
'default' => '',
|
118 |
|
|
'description' => 'The entity type this data is attached to',
|
119 |
|
|
),
|
120 |
|
|
'bundle' => array(
|
121 |
|
|
'type' => 'varchar',
|
122 |
|
|
'length' => 128,
|
123 |
|
|
'not null' => TRUE,
|
124 |
|
|
'default' => '',
|
125 |
|
|
'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
|
126 |
|
|
),
|
127 |
|
|
'deleted' => array(
|
128 |
|
|
'type' => 'int',
|
129 |
|
|
'size' => 'tiny',
|
130 |
|
|
'not null' => TRUE,
|
131 |
|
|
'default' => 0,
|
132 |
|
|
'description' => 'A boolean indicating whether this data item has been deleted'
|
133 |
|
|
),
|
134 |
|
|
'entity_id' => array(
|
135 |
|
|
'type' => 'int',
|
136 |
|
|
'unsigned' => TRUE,
|
137 |
|
|
'not null' => TRUE,
|
138 |
|
|
'description' => 'The entity id this data is attached to',
|
139 |
|
|
),
|
140 |
|
|
'revision_id' => array(
|
141 |
|
|
'type' => 'int',
|
142 |
|
|
'unsigned' => TRUE,
|
143 |
|
|
'not null' => FALSE,
|
144 |
|
|
'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
|
145 |
|
|
),
|
146 |
|
|
// @todo Consider storing language as integer.
|
147 |
|
|
'language' => array(
|
148 |
|
|
'type' => 'varchar',
|
149 |
|
|
'length' => 32,
|
150 |
|
|
'not null' => TRUE,
|
151 |
|
|
'default' => '',
|
152 |
|
|
'description' => 'The language for this data item.',
|
153 |
|
|
),
|
154 |
|
|
'delta' => array(
|
155 |
|
|
'type' => 'int',
|
156 |
|
|
'unsigned' => TRUE,
|
157 |
|
|
'not null' => TRUE,
|
158 |
|
|
'description' => 'The sequence number for this data item, used for multi-value fields',
|
159 |
|
|
),
|
160 |
|
|
),
|
161 |
|
|
'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
|
162 |
|
|
'indexes' => array(
|
163 |
|
|
'entity_type' => array('entity_type'),
|
164 |
|
|
'bundle' => array('bundle'),
|
165 |
|
|
'deleted' => array('deleted'),
|
166 |
|
|
'entity_id' => array('entity_id'),
|
167 |
|
|
'revision_id' => array('revision_id'),
|
168 |
|
|
'language' => array('language'),
|
169 |
|
|
),
|
170 |
|
|
);
|
171 |
|
|
|
172 |
|
|
$field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
|
173 |
|
|
// Add field columns.
|
174 |
|
|
foreach ($field['columns'] as $column_name => $attributes) {
|
175 |
|
|
$real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
|
176 |
|
|
$current['fields'][$real_name] = $attributes;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
// Add indexes.
|
180 |
|
|
foreach ($field['indexes'] as $index_name => $columns) {
|
181 |
|
|
$real_name = _field_sql_storage_indexname($field['field_name'], $index_name);
|
182 |
|
|
foreach ($columns as $column_name) {
|
183 |
|
|
$current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name);
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
// Add foreign keys.
|
188 |
|
|
foreach ($field['foreign keys'] as $specifier => $specification) {
|
189 |
|
|
$real_name = _field_sql_storage_indexname($field['field_name'], $specifier);
|
190 |
|
|
$current['foreign keys'][$real_name]['table'] = $specification['table'];
|
191 |
|
|
foreach ($specification['columns'] as $column_name => $referenced) {
|
192 |
|
|
$sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name);
|
193 |
|
|
$current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
// Construct the revision table.
|
198 |
|
|
$revision = $current;
|
199 |
|
|
$revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})";
|
200 |
|
|
$revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language');
|
201 |
|
|
$revision['fields']['revision_id']['not null'] = TRUE;
|
202 |
|
|
$revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
|
203 |
|
|
|
204 |
|
|
return array(
|
205 |
|
|
_field_sql_storage_tablename($field) => $current,
|
206 |
|
|
_field_sql_storage_revision_tablename($field) => $revision,
|
207 |
|
|
);
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
|
|
* Implements hook_field_storage_create_field().
|
212 |
|
|
*/
|
213 |
|
|
function field_sql_storage_field_storage_create_field($field) {
|
214 |
|
|
$schema = _field_sql_storage_schema($field);
|
215 |
|
|
foreach ($schema as $name => $table) {
|
216 |
|
|
db_create_table($name, $table);
|
217 |
|
|
}
|
218 |
|
|
drupal_get_schema(NULL, TRUE);
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
/**
|
222 |
|
|
* Implements hook_field_update_forbid().
|
223 |
|
|
*
|
224 |
|
|
* Forbid any field update that changes column definitions if there is
|
225 |
|
|
* any data.
|
226 |
|
|
*/
|
227 |
|
|
function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) {
|
228 |
|
|
if ($has_data && $field['columns'] != $prior_field['columns']) {
|
229 |
|
|
throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data.");
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/**
|
234 |
|
|
* Implements hook_field_storage_update_field().
|
235 |
|
|
*/
|
236 |
|
|
function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) {
|
237 |
|
|
if (! $has_data) {
|
238 |
|
|
// There is no data. Re-create the tables completely.
|
239 |
|
|
|
240 |
|
|
if (Database::getConnection()->supportsTransactionalDDL()) {
|
241 |
|
|
// If the database supports transactional DDL, we can go ahead and rely
|
242 |
|
|
// on it. If not, we will have to rollback manually if something fails.
|
243 |
|
|
$transaction = db_transaction();
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
try {
|
247 |
|
|
$prior_schema = _field_sql_storage_schema($prior_field);
|
248 |
|
|
foreach ($prior_schema as $name => $table) {
|
249 |
|
|
db_drop_table($name, $table);
|
250 |
|
|
}
|
251 |
|
|
$schema = _field_sql_storage_schema($field);
|
252 |
|
|
foreach ($schema as $name => $table) {
|
253 |
|
|
db_create_table($name, $table);
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
catch (Exception $e) {
|
257 |
|
|
if (Database::getConnection()->supportsTransactionalDDL()) {
|
258 |
|
|
$transaction->rollback();
|
259 |
|
|
}
|
260 |
|
|
else {
|
261 |
|
|
// Recreate tables.
|
262 |
|
|
$prior_schema = _field_sql_storage_schema($prior_field);
|
263 |
|
|
foreach ($prior_schema as $name => $table) {
|
264 |
|
|
if (!db_table_exists($name)) {
|
265 |
|
|
db_create_table($name, $table);
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
}
|
269 |
|
|
throw $e;
|
270 |
|
|
}
|
271 |
|
|
}
|
272 |
|
|
else {
|
273 |
|
|
// There is data, so there are no column changes. Drop all the
|
274 |
|
|
// prior indexes and create all the new ones, except for all the
|
275 |
|
|
// priors that exist unchanged.
|
276 |
|
|
$table = _field_sql_storage_tablename($prior_field);
|
277 |
|
|
$revision_table = _field_sql_storage_revision_tablename($prior_field);
|
278 |
|
|
foreach ($prior_field['indexes'] as $name => $columns) {
|
279 |
|
|
if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
|
280 |
|
|
$real_name = _field_sql_storage_indexname($field['field_name'], $name);
|
281 |
|
|
db_drop_index($table, $real_name);
|
282 |
|
|
db_drop_index($revision_table, $real_name);
|
283 |
|
|
}
|
284 |
|
|
}
|
285 |
|
|
$table = _field_sql_storage_tablename($field);
|
286 |
|
|
$revision_table = _field_sql_storage_revision_tablename($field);
|
287 |
|
|
foreach ($field['indexes'] as $name => $columns) {
|
288 |
|
|
if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) {
|
289 |
|
|
$real_name = _field_sql_storage_indexname($field['field_name'], $name);
|
290 |
|
|
$real_columns = array();
|
291 |
|
|
foreach ($columns as $column_name) {
|
292 |
|
|
$real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
|
293 |
|
|
}
|
294 |
|
|
db_add_index($table, $real_name, $real_columns);
|
295 |
|
|
db_add_index($revision_table, $real_name, $real_columns);
|
296 |
|
|
}
|
297 |
|
|
}
|
298 |
|
|
}
|
299 |
|
|
drupal_get_schema(NULL, TRUE);
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
/**
|
303 |
|
|
* Implements hook_field_storage_delete_field().
|
304 |
|
|
*/
|
305 |
|
|
function field_sql_storage_field_storage_delete_field($field) {
|
306 |
|
|
// Mark all data associated with the field for deletion.
|
307 |
|
|
$field['deleted'] = 0;
|
308 |
|
|
$table = _field_sql_storage_tablename($field);
|
309 |
|
|
$revision_table = _field_sql_storage_revision_tablename($field);
|
310 |
|
|
db_update($table)
|
311 |
|
|
->fields(array('deleted' => 1))
|
312 |
|
|
->execute();
|
313 |
|
|
|
314 |
|
|
// Move the table to a unique name while the table contents are being deleted.
|
315 |
|
|
$field['deleted'] = 1;
|
316 |
|
|
$new_table = _field_sql_storage_tablename($field);
|
317 |
|
|
$revision_new_table = _field_sql_storage_revision_tablename($field);
|
318 |
|
|
db_rename_table($table, $new_table);
|
319 |
|
|
db_rename_table($revision_table, $revision_new_table);
|
320 |
|
|
drupal_get_schema(NULL, TRUE);
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
/**
|
324 |
|
|
* Implements hook_field_storage_load().
|
325 |
|
|
*/
|
326 |
|
|
function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
|
327 |
|
|
$load_current = $age == FIELD_LOAD_CURRENT;
|
328 |
|
|
|
329 |
|
|
foreach ($fields as $field_id => $ids) {
|
330 |
|
|
// By the time this hook runs, the relevant field definitions have been
|
331 |
|
|
// populated and cached in FieldInfo, so calling field_info_field_by_id()
|
332 |
|
|
// on each field individually is more efficient than loading all fields in
|
333 |
|
|
// memory upfront with field_info_field_by_ids().
|
334 |
|
|
$field = field_info_field_by_id($field_id);
|
335 |
|
|
$field_name = $field['field_name'];
|
336 |
|
|
$table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
|
337 |
|
|
|
338 |
|
|
$query = db_select($table, 't')
|
339 |
|
|
->fields('t')
|
340 |
|
|
->condition('entity_type', $entity_type)
|
341 |
|
|
->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
|
342 |
|
|
->condition('language', field_available_languages($entity_type, $field), 'IN')
|
343 |
|
|
->orderBy('delta');
|
344 |
|
|
|
345 |
|
|
if (empty($options['deleted'])) {
|
346 |
|
|
$query->condition('deleted', 0);
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
$results = $query->execute();
|
350 |
|
|
|
351 |
|
|
$delta_count = array();
|
352 |
|
|
foreach ($results as $row) {
|
353 |
|
|
if (!isset($delta_count[$row->entity_id][$row->language])) {
|
354 |
|
|
$delta_count[$row->entity_id][$row->language] = 0;
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
|
358 |
|
|
$item = array();
|
359 |
|
|
// For each column declared by the field, populate the item
|
360 |
|
|
// from the prefixed database column.
|
361 |
|
|
foreach ($field['columns'] as $column => $attributes) {
|
362 |
|
|
$column_name = _field_sql_storage_columnname($field_name, $column);
|
363 |
|
|
$item[$column] = $row->$column_name;
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
// Add the item to the field values for the entity.
|
367 |
|
|
$entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
|
368 |
|
|
$delta_count[$row->entity_id][$row->language]++;
|
369 |
|
|
}
|
370 |
|
|
}
|
371 |
|
|
}
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
/**
|
375 |
|
|
* Implements hook_field_storage_write().
|
376 |
|
|
*/
|
377 |
|
|
function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
|
378 |
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
379 |
|
|
if (!isset($vid)) {
|
380 |
|
|
$vid = $id;
|
381 |
|
|
}
|
382 |
|
|
|
383 |
|
|
foreach ($fields as $field_id) {
|
384 |
|
|
$field = field_info_field_by_id($field_id);
|
385 |
|
|
$field_name = $field['field_name'];
|
386 |
|
|
$table_name = _field_sql_storage_tablename($field);
|
387 |
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
388 |
|
|
|
389 |
|
|
$all_languages = field_available_languages($entity_type, $field);
|
390 |
|
|
$field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
|
391 |
|
|
|
392 |
|
|
// Delete and insert, rather than update, in case a value was added.
|
393 |
|
|
if ($op == FIELD_STORAGE_UPDATE) {
|
394 |
|
|
// Delete languages present in the incoming $entity->$field_name.
|
395 |
|
|
// Delete all languages if $entity->$field_name is empty.
|
396 |
|
|
$languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
|
397 |
|
|
if ($languages) {
|
398 |
|
|
db_delete($table_name)
|
399 |
|
|
->condition('entity_type', $entity_type)
|
400 |
|
|
->condition('entity_id', $id)
|
401 |
|
|
->condition('language', $languages, 'IN')
|
402 |
|
|
->execute();
|
403 |
|
|
db_delete($revision_name)
|
404 |
|
|
->condition('entity_type', $entity_type)
|
405 |
|
|
->condition('entity_id', $id)
|
406 |
|
|
->condition('revision_id', $vid)
|
407 |
|
|
->condition('language', $languages, 'IN')
|
408 |
|
|
->execute();
|
409 |
|
|
}
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
// Prepare the multi-insert query.
|
413 |
|
|
$do_insert = FALSE;
|
414 |
|
|
$columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
|
415 |
|
|
foreach ($field['columns'] as $column => $attributes) {
|
416 |
|
|
$columns[] = _field_sql_storage_columnname($field_name, $column);
|
417 |
|
|
}
|
418 |
|
|
$query = db_insert($table_name)->fields($columns);
|
419 |
|
|
$revision_query = db_insert($revision_name)->fields($columns);
|
420 |
|
|
|
421 |
|
|
foreach ($field_languages as $langcode) {
|
422 |
|
|
$items = (array) $entity->{$field_name}[$langcode];
|
423 |
|
|
$delta_count = 0;
|
424 |
|
|
foreach ($items as $delta => $item) {
|
425 |
|
|
// We now know we have someting to insert.
|
426 |
|
|
$do_insert = TRUE;
|
427 |
|
|
$record = array(
|
428 |
|
|
'entity_type' => $entity_type,
|
429 |
|
|
'entity_id' => $id,
|
430 |
|
|
'revision_id' => $vid,
|
431 |
|
|
'bundle' => $bundle,
|
432 |
|
|
'delta' => $delta,
|
433 |
|
|
'language' => $langcode,
|
434 |
|
|
);
|
435 |
|
|
foreach ($field['columns'] as $column => $attributes) {
|
436 |
|
|
$record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
|
437 |
|
|
}
|
438 |
|
|
$query->values($record);
|
439 |
|
|
if (isset($vid)) {
|
440 |
|
|
$revision_query->values($record);
|
441 |
|
|
}
|
442 |
|
|
|
443 |
|
|
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
|
444 |
|
|
break;
|
445 |
|
|
}
|
446 |
|
|
}
|
447 |
|
|
}
|
448 |
|
|
|
449 |
|
|
// Execute the query if we have values to insert.
|
450 |
|
|
if ($do_insert) {
|
451 |
|
|
$query->execute();
|
452 |
|
|
$revision_query->execute();
|
453 |
|
|
}
|
454 |
|
|
}
|
455 |
|
|
}
|
456 |
|
|
|
457 |
|
|
/**
|
458 |
|
|
* Implements hook_field_storage_delete().
|
459 |
|
|
*
|
460 |
|
|
* This function deletes data for all fields for an entity from the database.
|
461 |
|
|
*/
|
462 |
|
|
function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
|
463 |
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
464 |
|
|
|
465 |
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
466 |
|
|
if (isset($fields[$instance['field_id']])) {
|
467 |
|
|
$field = field_info_field_by_id($instance['field_id']);
|
468 |
|
|
field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
|
469 |
|
|
}
|
470 |
|
|
}
|
471 |
|
|
}
|
472 |
|
|
|
473 |
|
|
/**
|
474 |
|
|
* Implements hook_field_storage_purge().
|
475 |
|
|
*
|
476 |
|
|
* This function deletes data from the database for a single field on
|
477 |
|
|
* an entity.
|
478 |
|
|
*/
|
479 |
|
|
function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
|
480 |
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
481 |
|
|
|
482 |
|
|
$table_name = _field_sql_storage_tablename($field);
|
483 |
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
484 |
|
|
db_delete($table_name)
|
485 |
|
|
->condition('entity_type', $entity_type)
|
486 |
|
|
->condition('entity_id', $id)
|
487 |
|
|
->execute();
|
488 |
|
|
db_delete($revision_name)
|
489 |
|
|
->condition('entity_type', $entity_type)
|
490 |
|
|
->condition('entity_id', $id)
|
491 |
|
|
->execute();
|
492 |
|
|
}
|
493 |
|
|
|
494 |
|
|
/**
|
495 |
|
|
* Implements hook_field_storage_query().
|
496 |
|
|
*/
|
497 |
|
|
function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
|
498 |
|
|
if ($query->age == FIELD_LOAD_CURRENT) {
|
499 |
|
|
$tablename_function = '_field_sql_storage_tablename';
|
500 |
|
|
$id_key = 'entity_id';
|
501 |
|
|
}
|
502 |
|
|
else {
|
503 |
|
|
$tablename_function = '_field_sql_storage_revision_tablename';
|
504 |
|
|
$id_key = 'revision_id';
|
505 |
|
|
}
|
506 |
|
|
$table_aliases = array();
|
507 |
|
|
// Add tables for the fields used.
|
508 |
|
|
foreach ($query->fields as $key => $field) {
|
509 |
|
|
$tablename = $tablename_function($field);
|
510 |
|
|
// Every field needs a new table.
|
511 |
|
|
$table_alias = $tablename . $key;
|
512 |
|
|
$table_aliases[$key] = $table_alias;
|
513 |
|
|
if ($key) {
|
514 |
|
|
$select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key");
|
515 |
|
|
}
|
516 |
|
|
else {
|
517 |
|
|
$select_query = db_select($tablename, $table_alias);
|
518 |
|
|
// Allow queries internal to the Field API to opt out of the access
|
519 |
|
|
// check, for situations where the query's results should not depend on
|
520 |
|
|
// the access grants for the current user.
|
521 |
|
|
if (!isset($query->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
|
522 |
|
|
$select_query->addTag('entity_field_access');
|
523 |
|
|
}
|
524 |
|
|
$select_query->addMetaData('base_table', $tablename);
|
525 |
|
|
$select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
|
526 |
|
|
$field_base_table = $table_alias;
|
527 |
|
|
}
|
528 |
|
|
if ($field['cardinality'] != 1 || $field['translatable']) {
|
529 |
|
|
$select_query->distinct();
|
530 |
|
|
}
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
// Add field conditions. We need a fresh grouping cache.
|
534 |
|
|
drupal_static_reset('_field_sql_storage_query_field_conditions');
|
535 |
|
|
_field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
|
536 |
|
|
|
537 |
|
|
// Add field meta conditions.
|
538 |
|
|
_field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
|
539 |
|
|
|
540 |
|
|
if (isset($query->deleted)) {
|
541 |
|
|
$select_query->condition("$field_base_table.deleted", (int) $query->deleted);
|
542 |
|
|
}
|
543 |
|
|
|
544 |
|
|
// Is there a need to sort the query by property?
|
545 |
|
|
$has_property_order = FALSE;
|
546 |
|
|
foreach ($query->order as $order) {
|
547 |
|
|
if ($order['type'] == 'property') {
|
548 |
|
|
$has_property_order = TRUE;
|
549 |
|
|
}
|
550 |
|
|
}
|
551 |
|
|
|
552 |
|
|
if ($query->propertyConditions || $has_property_order) {
|
553 |
|
|
if (empty($query->entityConditions['entity_type']['value'])) {
|
554 |
|
|
throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
|
555 |
|
|
}
|
556 |
|
|
$entity_type = $query->entityConditions['entity_type']['value'];
|
557 |
|
|
$entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
|
558 |
|
|
$query->entityConditions['entity_type']['operator'] = '=';
|
559 |
|
|
foreach ($query->propertyConditions as $property_condition) {
|
560 |
|
|
$query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
|
561 |
|
|
}
|
562 |
|
|
}
|
563 |
|
|
foreach ($query->entityConditions as $key => $condition) {
|
564 |
|
|
$query->addCondition($select_query, "$field_base_table.$key", $condition);
|
565 |
|
|
}
|
566 |
|
|
|
567 |
|
|
// Order the query.
|
568 |
|
|
foreach ($query->order as $order) {
|
569 |
|
|
if ($order['type'] == 'entity') {
|
570 |
|
|
$key = $order['specifier'];
|
571 |
|
|
$select_query->orderBy("$field_base_table.$key", $order['direction']);
|
572 |
|
|
}
|
573 |
|
|
elseif ($order['type'] == 'field') {
|
574 |
|
|
$specifier = $order['specifier'];
|
575 |
|
|
$field = $specifier['field'];
|
576 |
|
|
$table_alias = $table_aliases[$specifier['index']];
|
577 |
|
|
$sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
|
578 |
|
|
$select_query->orderBy($sql_field, $order['direction']);
|
579 |
|
|
}
|
580 |
|
|
elseif ($order['type'] == 'property') {
|
581 |
|
|
$select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
|
582 |
|
|
}
|
583 |
|
|
}
|
584 |
|
|
|
585 |
|
|
return $query->finishQuery($select_query, $id_key);
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
/**
|
589 |
|
|
* Adds the base entity table to a field query object.
|
590 |
|
|
*
|
591 |
|
|
* @param SelectQuery $select_query
|
592 |
|
|
* A SelectQuery containing at least one table as specified by
|
593 |
|
|
* _field_sql_storage_tablename().
|
594 |
|
|
* @param $entity_type
|
595 |
|
|
* The entity type for which the base table should be joined.
|
596 |
|
|
* @param $field_base_table
|
597 |
|
|
* Name of a table in $select_query. As only INNER JOINs are used, it does
|
598 |
|
|
* not matter which.
|
599 |
|
|
*
|
600 |
|
|
* @return
|
601 |
|
|
* The name of the entity base table joined in.
|
602 |
|
|
*/
|
603 |
|
|
function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
|
604 |
|
|
$entity_info = entity_get_info($entity_type);
|
605 |
|
|
$entity_base_table = $entity_info['base table'];
|
606 |
|
|
$entity_field = $entity_info['entity keys']['id'];
|
607 |
|
|
$select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
|
608 |
|
|
return $entity_base_table;
|
609 |
|
|
}
|
610 |
|
|
|
611 |
|
|
/**
|
612 |
|
|
* Adds field (meta) conditions to the given query objects respecting groupings.
|
613 |
|
|
*
|
614 |
|
|
* @param EntityFieldQuery $query
|
615 |
|
|
* The field query object to be processed.
|
616 |
|
|
* @param SelectQuery $select_query
|
617 |
|
|
* The SelectQuery that should get grouping conditions.
|
618 |
|
|
* @param condtions
|
619 |
|
|
* The conditions to be added.
|
620 |
|
|
* @param $table_aliases
|
621 |
|
|
* An associative array of table aliases keyed by field index.
|
622 |
|
|
* @param $column_callback
|
623 |
|
|
* A callback that should return the column name to be used for the field
|
624 |
|
|
* conditions. Accepts a field name and a field column name as parameters.
|
625 |
|
|
*/
|
626 |
|
|
function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
|
627 |
|
|
$groups = &drupal_static(__FUNCTION__, array());
|
628 |
|
|
foreach ($conditions as $key => $condition) {
|
629 |
|
|
$table_alias = $table_aliases[$key];
|
630 |
|
|
$field = $condition['field'];
|
631 |
|
|
// Add the specified condition.
|
632 |
|
|
$sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
|
633 |
|
|
$query->addCondition($select_query, $sql_field, $condition);
|
634 |
|
|
// Add delta / language group conditions.
|
635 |
|
|
foreach (array('delta', 'language') as $column) {
|
636 |
|
|
if (isset($condition[$column . '_group'])) {
|
637 |
|
|
$group_name = $condition[$column . '_group'];
|
638 |
|
|
if (!isset($groups[$column][$group_name])) {
|
639 |
|
|
$groups[$column][$group_name] = $table_alias;
|
640 |
|
|
}
|
641 |
|
|
else {
|
642 |
|
|
$select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
|
643 |
|
|
}
|
644 |
|
|
}
|
645 |
|
|
}
|
646 |
|
|
}
|
647 |
|
|
}
|
648 |
|
|
|
649 |
|
|
/**
|
650 |
|
|
* Field meta condition column callback.
|
651 |
|
|
*/
|
652 |
|
|
function _field_sql_storage_query_columnname($field_name, $column) {
|
653 |
|
|
return $column;
|
654 |
|
|
}
|
655 |
|
|
|
656 |
|
|
/**
|
657 |
|
|
* Implements hook_field_storage_delete_revision().
|
658 |
|
|
*
|
659 |
|
|
* This function actually deletes the data from the database.
|
660 |
|
|
*/
|
661 |
|
|
function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
|
662 |
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
663 |
|
|
|
664 |
|
|
if (isset($vid)) {
|
665 |
|
|
foreach ($fields as $field_id) {
|
666 |
|
|
$field = field_info_field_by_id($field_id);
|
667 |
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
668 |
|
|
db_delete($revision_name)
|
669 |
|
|
->condition('entity_type', $entity_type)
|
670 |
|
|
->condition('entity_id', $id)
|
671 |
|
|
->condition('revision_id', $vid)
|
672 |
|
|
->execute();
|
673 |
|
|
}
|
674 |
|
|
}
|
675 |
|
|
}
|
676 |
|
|
|
677 |
|
|
/**
|
678 |
|
|
* Implements hook_field_storage_delete_instance().
|
679 |
|
|
*
|
680 |
|
|
* This function simply marks for deletion all data associated with the field.
|
681 |
|
|
*/
|
682 |
|
|
function field_sql_storage_field_storage_delete_instance($instance) {
|
683 |
|
|
$field = field_info_field($instance['field_name']);
|
684 |
|
|
$table_name = _field_sql_storage_tablename($field);
|
685 |
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
686 |
|
|
db_update($table_name)
|
687 |
|
|
->fields(array('deleted' => 1))
|
688 |
|
|
->condition('entity_type', $instance['entity_type'])
|
689 |
|
|
->condition('bundle', $instance['bundle'])
|
690 |
|
|
->execute();
|
691 |
|
|
db_update($revision_name)
|
692 |
|
|
->fields(array('deleted' => 1))
|
693 |
|
|
->condition('entity_type', $instance['entity_type'])
|
694 |
|
|
->condition('bundle', $instance['bundle'])
|
695 |
|
|
->execute();
|
696 |
|
|
}
|
697 |
|
|
|
698 |
|
|
/**
|
699 |
|
|
* Implements hook_field_attach_rename_bundle().
|
700 |
|
|
*/
|
701 |
|
|
function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
|
702 |
|
|
// We need to account for deleted or inactive fields and instances.
|
703 |
|
|
$instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
|
704 |
|
|
foreach ($instances as $instance) {
|
705 |
|
|
$field = field_info_field_by_id($instance['field_id']);
|
706 |
|
|
if ($field['storage']['type'] == 'field_sql_storage') {
|
707 |
|
|
$table_name = _field_sql_storage_tablename($field);
|
708 |
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
709 |
|
|
db_update($table_name)
|
710 |
|
|
->fields(array('bundle' => $bundle_new))
|
711 |
|
|
->condition('entity_type', $entity_type)
|
712 |
|
|
->condition('bundle', $bundle_old)
|
713 |
|
|
->execute();
|
714 |
|
|
db_update($revision_name)
|
715 |
|
|
->fields(array('bundle' => $bundle_new))
|
716 |
|
|
->condition('entity_type', $entity_type)
|
717 |
|
|
->condition('bundle', $bundle_old)
|
718 |
|
|
->execute();
|
719 |
|
|
}
|
720 |
|
|
}
|
721 |
|
|
}
|
722 |
|
|
|
723 |
|
|
/**
|
724 |
|
|
* Implements hook_field_storage_purge_field().
|
725 |
|
|
*
|
726 |
|
|
* All field data items and instances have already been purged, so all
|
727 |
|
|
* that is left is to delete the table.
|
728 |
|
|
*/
|
729 |
|
|
function field_sql_storage_field_storage_purge_field($field) {
|
730 |
|
|
$table_name = _field_sql_storage_tablename($field);
|
731 |
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
732 |
|
|
db_drop_table($table_name);
|
733 |
|
|
db_drop_table($revision_name);
|
734 |
|
|
}
|
735 |
|
|
|
736 |
|
|
/**
|
737 |
|
|
* Implements hook_field_storage_details().
|
738 |
|
|
*/
|
739 |
|
|
function field_sql_storage_field_storage_details($field) {
|
740 |
|
|
$details = array();
|
741 |
|
|
if (!empty($field['columns'])) {
|
742 |
|
|
// Add field columns.
|
743 |
|
|
foreach ($field['columns'] as $column_name => $attributes) {
|
744 |
|
|
$real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
|
745 |
|
|
$columns[$column_name] = $real_name;
|
746 |
|
|
}
|
747 |
|
|
return array(
|
748 |
|
|
'sql' => array(
|
749 |
|
|
FIELD_LOAD_CURRENT => array(
|
750 |
|
|
_field_sql_storage_tablename($field) => $columns,
|
751 |
|
|
),
|
752 |
|
|
FIELD_LOAD_REVISION => array(
|
753 |
|
|
_field_sql_storage_revision_tablename($field) => $columns,
|
754 |
|
|
),
|
755 |
|
|
),
|
756 |
|
|
);
|
757 |
|
|
}
|
758 |
|
|
} |