function node_gallery_update_6305 in Node Gallery 6.3
#1226982: add gallery image counts with indexes and create initial count values.
File
- ./
node_gallery.install, line 916 - Install, update and uninstall functions for the node_gallery module.
Code
function node_gallery_update_6305() {
$ret = array();
// Create count fields.
$fields = array(
'img_count' => array(
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('Gallery images count.'),
),
'pub_img_count' => array(
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('Gallery published images count.'),
),
);
foreach ($fields as $field => $spec) {
db_add_field($ret, 'node_gallery_galleries', $field, $spec);
}
// Create indexes for searching and filtering using counts.
$indexes = array(
'img_count' => array(
'img_count',
),
'pub_img_count' => array(
'pub_img_count',
),
);
foreach ($indexes as $name => $index) {
db_add_index($ret, 'node_gallery_galleries', $name, $index);
}
// Create initial values for the counts.
$counts = array();
$res = db_query("SELECT ngi.gid AS gid, n.status AS status, COUNT(*) AS num\n FROM {node_gallery_images} ngi\n INNER JOIN {node} n ON ngi.nid = n.nid\n GROUP BY ngi.gid, n.status");
while ($row = db_fetch_array($res)) {
$row = array_map('intval', $row);
if (!isset($counts[$row['gid']])) {
$counts[$row['gid']] = array(
0 => 0,
1 => 0,
);
}
$counts[$row['gid']][$row['status']] = $row['num'];
}
foreach ($counts as $gid => $c) {
db_query("UPDATE {node_gallery_galleries} \n SET img_count = %d, pub_img_count = %d\n WHERE gid = %d", $c[0] + $c[1], $c[1], $gid);
}
$processed_num = count($counts);
$ret[] = array(
'success' => TRUE,
'query' => format_plural($processed_num, 'Created initial image count values for 1 gallery.', 'Created initial image count values for !count galleries.', array(
'!count' => $processed_num,
)),
);
drupal_flush_all_caches();
return $ret;
}