123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- function _serial_create_table(array $field, array $instance) {
- $table = _serial_get_field_table_name($field, $instance);
- if (!db_table_exists($table)) {
- db_create_table($table, _serial_get_table_schema());
- }
- }
- function _serial_drop_table(array $field, array $instance) {
- db_drop_table(_serial_get_field_table_name($field, $instance));
- }
- function _serial_rename_tables($entity_type, $bundle_old, $bundle_new) {
-
- $query = db_select('field_config', 'f')
- ->fields('f', array('field_name'));
- $query->join('field_config_instance', 'i', '(f.field_name = i.field_name)');
- $query->condition('f.type', SERIAL_FIELD_TYPE);
- $query->condition('i.entity_type', $entity_type);
- $query->condition('i.bundle', $bundle_new);
-
- foreach ($query->addTag('node_access')->execute() as $record) {
- db_rename_table(
- _serial_get_table_name($entity_type, $bundle_old, $record->field_name),
- _serial_get_table_name($entity_type, $bundle_new, $record->field_name)
- );
- }
- }
- function _serial_get_field_table_name(array $field, array $instance) {
- return _serial_get_table_name($instance['entity_type'], $instance['bundle'], $field['field_name']);
- }
- function _serial_get_table_name($entity_type, $bundle, $field_name) {
-
-
- return db_escape_table('serial_' . md5("{$entity_type}_{$bundle}_{$field_name}"));
- }
- function _serial_get_table_schema() {
- return array(
- 'fields' => array(
- 'sid' => array(
- 'type' => SERIAL_FIELD_TYPE,
- 'not null' => TRUE,
- 'unsigned' => TRUE,
- 'description' => 'The atomic serial field.',
- ),
- 'uniqid' => array(
- 'type' => 'varchar',
- 'length' => 23,
- 'default' => '',
- 'not null' => TRUE,
- 'description' => 'Unique temporary allocation Id.',
- ),
- ),
- 'primary key' => array('sid'),
- 'unique keys' => array(
- 'uniqid' => array('uniqid'),
- ),
- );
- }
- function _serial_generate_value($entity_type, $bundle, $field_name, $delete = TRUE) {
- $transaction = db_transaction();
- try {
-
- $table = _serial_get_table_name($entity_type, $bundle, $field_name);
-
- $uniqid = uniqid('', TRUE);
- $sid = db_insert($table)
- ->fields(array('uniqid' => $uniqid))
- ->execute();
-
- $sid = isset($sid) ? $sid : 0;
-
- if ($delete && $sid && ($sid % 10) == 0) {
- db_delete($table)
- ->condition('sid', $sid, '<')
- ->execute();
- }
-
- return $sid;
- }
- catch (Exception $e) {
- $transaction->rollback();
- watchdog_exception('serial', $e);
- throw $e;
- }
- }
- function _serial_init_old_entities($entity_type, $bundle, $field_name) {
- $query = new EntityFieldQuery();
- $query->entityCondition('entity_type', $entity_type)
- ->fieldCondition($field_name);
-
-
- if ('comment' !== $entity_type) {
- $query->entityCondition('bundle', $bundle);
- }
- $results = $query->execute();
- if (!empty($results[$entity_type])) {
- foreach ($results[$entity_type] as $entity) {
- list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
- $entity = entity_load_unchanged($entity_type, $id);
- $entity->{$field_name} = array(
- LANGUAGE_NONE => array(
- array(
- 'value' => _serial_generate_value($entity_type, $bundle, $field_name, FALSE),
- ),
- ),
- );
- field_attach_insert($entity_type, $entity);
- }
- return count($results[$entity_type]);
- }
- return 0;
- }
- function _serial_get_all_fields() {
- $query = db_select('field_config', 'f');
- $query->join('field_config_instance', 'i', 'i.field_name = f.field_name');
- return $query
- ->fields('i', array('entity_type', 'bundle', 'field_name'))
- ->condition('f.type', SERIAL_FIELD_TYPE)
- ->condition('i.deleted', 0)
- ->execute()
- ->fetchAll();
- }
|