<?php

/**
 * @file
 * Install file for the picasa_node_album module.
 */

/**
 * Implements hook_schema().
 */
function picasa_node_album_schema() {
  // This table stores the relation of the albums to a node.
  $schema['picasa_node_album'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'google_user' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => ''
      ),
      'album_id' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => ''
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'added' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => '0'
      ),
      'updated' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => '0'
      ),
      'images' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
    ),
    'indexes' => array(
      'nid'       => array('nid'),
      'album_id'  => array('album_id'),
      'added'     => array('added'),
    ),
  );

  // This table stores the user accounts.
  $schema['picasa_node_album_users'] = array(
    'fields' => array(
      'uid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'google_user' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => ''
      ),
    ),
    'primary key' => array('uid'),
  );

  // Cache table for albums per google account.
  //$schema['cache_picasa_albums'] = drupal_get_schema_unprocessed('system', 'cache');

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function picasa_node_album_uninstall() {
  // Query for all system variables from this module.
  $keys = db_select('variable', 'v')
    ->fields('v', array('name'))
    ->condition('name', db_like('picasa_node_album_') . '%', 'LIKE')
    ->execute()
    ->fetchCol();

  // Delete each variable.
  foreach ($keys as $key) {
    variable_del($key);
  }
}