1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Install, update and uninstall functions for the field_sql_storage module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_schema().
|
10
|
*/
|
11
|
function field_sql_storage_schema() {
|
12
|
$schema = array();
|
13
|
|
14
|
// Dynamic (data) tables.
|
15
|
if (db_table_exists('field_config')) {
|
16
|
$fields = field_read_fields(array(), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
|
17
|
drupal_load('module', 'field_sql_storage');
|
18
|
foreach ($fields as $field) {
|
19
|
if ($field['storage']['type'] == 'field_sql_storage') {
|
20
|
$schema += _field_sql_storage_schema($field);
|
21
|
}
|
22
|
}
|
23
|
}
|
24
|
return $schema;
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* Utility function: write field data directly to SQL storage.
|
29
|
*
|
30
|
* This function can be used for databases whose schema is at field module
|
31
|
* version 7000 or higher.
|
32
|
*
|
33
|
* @ingroup update_api
|
34
|
*/
|
35
|
function _update_7000_field_sql_storage_write($entity_type, $bundle, $entity_id, $revision_id, $field_name, $data) {
|
36
|
$table_name = "field_data_{$field_name}";
|
37
|
$revision_name = "field_revision_{$field_name}";
|
38
|
|
39
|
db_delete($table_name)
|
40
|
->condition('entity_type', $entity_type)
|
41
|
->condition('entity_id', $entity_id)
|
42
|
->execute();
|
43
|
db_delete($revision_name)
|
44
|
->condition('entity_type', $entity_type)
|
45
|
->condition('entity_id', $entity_id)
|
46
|
->condition('revision_id', $revision_id)
|
47
|
->execute();
|
48
|
|
49
|
$columns = array();
|
50
|
foreach ($data as $langcode => $items) {
|
51
|
foreach ($items as $delta => $item) {
|
52
|
$record = array(
|
53
|
'entity_type' => $entity_type,
|
54
|
'entity_id' => $entity_id,
|
55
|
'revision_id' => $revision_id,
|
56
|
'bundle' => $bundle,
|
57
|
'delta' => $delta,
|
58
|
'language' => $langcode,
|
59
|
);
|
60
|
foreach ($item as $column => $value) {
|
61
|
$record[_field_sql_storage_columnname($field_name, $column)] = $value;
|
62
|
}
|
63
|
|
64
|
$records[] = $record;
|
65
|
// Record the columns used.
|
66
|
$columns += $record;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
if ($columns) {
|
71
|
$query = db_insert($table_name)->fields(array_keys($columns));
|
72
|
$revision_query = db_insert($revision_name)->fields(array_keys($columns));
|
73
|
foreach ($records as $record) {
|
74
|
$query->values($record);
|
75
|
if ($revision_id) {
|
76
|
$revision_query->values($record);
|
77
|
}
|
78
|
}
|
79
|
$query->execute();
|
80
|
$revision_query->execute();
|
81
|
}
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* @addtogroup updates-6.x-to-7.x
|
86
|
* @{
|
87
|
*/
|
88
|
|
89
|
/**
|
90
|
* Field SQL storage update version placeholder.
|
91
|
*/
|
92
|
function field_sql_storage_update_7000() {
|
93
|
// Some update helper functions (such as
|
94
|
// _update_7000_field_sql_storage_write()) modify the database directly. They
|
95
|
// can be used safely only if the database schema matches the field module
|
96
|
// schema established for Drupal 7.0 (i.e. version 7000). This function exists
|
97
|
// solely to set the schema version to 7000, so that update functions calling
|
98
|
// those helpers can do so safely by declaring a dependency on
|
99
|
// field_sql_storage_update_7000().
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Remove the field_config_entity_type table and store 'entity_type' strings.
|
104
|
*/
|
105
|
function field_sql_storage_update_7001(&$sandbox) {
|
106
|
if (!isset($sandbox['progress'])) {
|
107
|
// Collect current etids.
|
108
|
$sandbox['etids'] = db_query('SELECT etid, type FROM {field_config_entity_type}')->fetchAllKeyed();
|
109
|
|
110
|
// Collect affected tables: field data, field revision data, 'deleted'
|
111
|
// tables.
|
112
|
$sandbox['tables'] = array();
|
113
|
$results = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
|
114
|
->fields('fc')
|
115
|
->condition('storage_module', 'field_sql_storage')
|
116
|
->execute();
|
117
|
foreach ($results as $field) {
|
118
|
if ($field['deleted']) {
|
119
|
$sandbox['tables']["field_deleted_data_{$field['id']}"] = 'data';
|
120
|
$sandbox['tables']["field_deleted_revision_{$field['id']}"] = 'revision';
|
121
|
}
|
122
|
else {
|
123
|
$sandbox['tables']["field_data_{$field['field_name']}"] = 'data';
|
124
|
$sandbox['tables']["field_revision_{$field['field_name']}"] = 'revision';
|
125
|
}
|
126
|
}
|
127
|
reset($sandbox['tables']);
|
128
|
|
129
|
$sandbox['total'] = count($sandbox['tables']);
|
130
|
$sandbox['progress'] = 0;
|
131
|
}
|
132
|
|
133
|
if ($sandbox['tables']) {
|
134
|
// Grab the next table to process.
|
135
|
$table = key($sandbox['tables']);
|
136
|
$type = array_shift($sandbox['tables']);
|
137
|
|
138
|
if (db_table_exists($table)) {
|
139
|
// Add the 'entity_type' column.
|
140
|
if (!db_field_exists($table, 'entity_type')) {
|
141
|
$column = array(
|
142
|
'type' => 'varchar',
|
143
|
'length' => 128,
|
144
|
'not null' => TRUE,
|
145
|
'default' => '',
|
146
|
'description' => 'The entity type this data is attached to.',
|
147
|
);
|
148
|
db_add_field($table, 'entity_type', $column);
|
149
|
|
150
|
// Populate the 'entity_type' column based on the 'etid' column.
|
151
|
foreach ($sandbox['etids'] as $etid => $entity_type) {
|
152
|
db_update($table)
|
153
|
->fields(array('entity_type' => $entity_type))
|
154
|
->condition('etid', $etid)
|
155
|
->execute();
|
156
|
}
|
157
|
|
158
|
// Index the new column.
|
159
|
db_add_index($table, 'entity_type', array('entity_type'));
|
160
|
}
|
161
|
|
162
|
// Use the 'entity_type' column in the primary key.
|
163
|
db_drop_primary_key($table);
|
164
|
$primary_keys = array(
|
165
|
'data' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
|
166
|
'revision' => array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language'),
|
167
|
);
|
168
|
db_add_primary_key($table, $primary_keys[$type]);
|
169
|
|
170
|
// Drop the 'etid' column.
|
171
|
if (db_field_exists($table, 'etid')) {
|
172
|
db_drop_field($table, 'etid');
|
173
|
}
|
174
|
}
|
175
|
|
176
|
// Report progress.
|
177
|
$sandbox['progress']++;
|
178
|
$sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['total']);
|
179
|
}
|
180
|
else {
|
181
|
// No more tables left: drop the field_config_entity_type table.
|
182
|
db_drop_table('field_config_entity_type');
|
183
|
|
184
|
// Drop the previous 'field_sql_storage_ENTITYTYPE_etid' system variables.
|
185
|
foreach ($sandbox['etids'] as $etid => $entity_type) {
|
186
|
variable_del('field_sql_storage_' . $entity_type . '_etid');
|
187
|
}
|
188
|
|
189
|
// We're done.
|
190
|
$sandbox['#finished'] = 1;
|
191
|
}
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Fix primary keys in field revision data tables.
|
196
|
*/
|
197
|
function field_sql_storage_update_7002() {
|
198
|
$results = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
|
199
|
->fields('fc')
|
200
|
->condition('storage_module', 'field_sql_storage')
|
201
|
->execute();
|
202
|
foreach ($results as $field) {
|
203
|
// Revision tables of deleted fields do not need to be fixed, since no new
|
204
|
// data is written to them.
|
205
|
if (!$field['deleted']) {
|
206
|
$table = "field_revision_{$field['field_name']}";
|
207
|
db_drop_primary_key($table);
|
208
|
db_add_primary_key($table, array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language'));
|
209
|
}
|
210
|
}
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* @} End of "addtogroup updates-6.x-to-7.x".
|
215
|
*/
|