/**
  * instance method
  *
  * Get singleton instance of self (Ai1ec_Database).
  *
  * @return Ai1ec_Database Initialized instance of self
  */
 public static function instance()
 {
     if (!self::$_instance instanceof Ai1ec_Database) {
         self::$_instance = new Ai1ec_Database();
     }
     return self::$_instance;
 }
 /**
  * instance method
  *
  * Get singleton instance of self (Ai1ec_Database).
  *
  * @return Ai1ec_Database Initialized instance of self
  */
 public static function instance()
 {
     if (!self::$_instance instanceof Ai1ec_Database) {
         global $wpdb;
         self::$_instance = new Ai1ec_Database($wpdb);
     }
     return self::$_instance;
 }
 /**
  * Handles all the required steps to install / update the schema
  */
 private function install_schema()
 {
     // If existing DB version is not consistent with current plugin's version,
     // or does not exist, then create/update table structure using dbDelta().
     if (Ai1ec_Meta::get_option(self::ICS_OPTION_DB_VERSION) != self::ICS_DB_VERSION) {
         global $wpdb;
         // ======================
         // = Create table feeds =
         // ======================
         $table_name = $wpdb->prefix . 'ai1ec_event_feeds';
         $sql = "CREATE TABLE {$table_name} (\r\n\t\t\t\tfeed_id bigint(20) NOT NULL AUTO_INCREMENT,\r\n\t\t\t\tfeed_url varchar(255) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,\r\n\t\t\t\tfeed_category bigint(20) NOT NULL,\r\n\t\t\t\tfeed_tags varchar(255) NOT NULL,\r\n\t\t\t\tcomments_enabled tinyint(1) NOT NULL DEFAULT '1',\r\n\t\t\t\tmap_display_enabled tinyint(1) NOT NULL DEFAULT '0',\r\n\t\t\t\tPRIMARY KEY  (feed_id),\r\n\t\t\t\tUNIQUE KEY feed (feed_url)\r\n\t\t\t) CHARACTER SET utf8;";
         if (Ai1ec_Database::instance()->apply_delta($sql)) {
             update_option(self::ICS_OPTION_DB_VERSION, self::ICS_DB_VERSION);
         } else {
             trigger_error('Failed to upgrade ICS DB schema', E_USER_WARNING);
         }
     }
 }
 /**
  * Handles all the required steps to install / update the schema
  */
 private function install_schema()
 {
     // If existing DB version is not consistent with current plugin's version,
     // or does not exist, then create/update table structure using dbDelta().
     if (Ai1ec_Meta::get_option(self::FB_OPTION_DB_VERSION) != self::FB_DB_VERSION) {
         $table_name = Ai1ec_Facebook_Factory::get_plugin_table();
         $sql = "CREATE TABLE {$table_name} (\n\t\t\t\tuser_id bigint(20) NOT NULL,\n\t\t\t\tuser_name varchar(255) NOT NULL,\n\t\t\t\tuser_pic varchar(255) NOT NULL,\n\t\t\t\tsubscribed tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\ttype varchar(20) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,\n\t\t\t\ttag varchar(255) NOT NULL DEFAULT '',\n\t\t\t\tcategory int(11) NOT NULL DEFAULT '0',\n\t\t\t\tcomments_enabled tinyint(1) NOT NULL DEFAULT '1',\n\t\t\t\tmap_display_enabled tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\tlast_synced timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,\n\t\t\t\tPRIMARY KEY  (user_id),\n\t\t\t\tKEY subscribers (type(2),subscribed)\n\t\t\t) DEFAULT CHARSET=utf8;";
         $table_users_events = Ai1ec_Facebook_Factory::get_user_events_table();
         $sql .= "CREATE TABLE {$table_users_events} (\n\t\t\t\tuser_id bigint(20) unsigned NOT NULL,\n\t\t\t\teid bigint(20) unsigned NOT NULL,\n\t\t\t\tstart datetime NOT NULL,\n\t\t\t\tPRIMARY KEY  (user_id,eid)\n\t\t\t) DEFAULT CHARSET=utf8;";
         if (Ai1ec_Database::instance()->apply_delta($sql)) {
             update_option(self::FB_OPTION_DB_VERSION, self::FB_DB_VERSION);
         } else {
             trigger_error('Failed to upgrade Facebook DB schema', E_USER_WARNING);
         }
     }
 }
 /**
  * install_schema function
  *
  * This function sets up the database, and upgrades it if it is out of date.
  *
  * @return void
  **/
 function install_schema()
 {
     global $wpdb;
     // If existing DB version is not consistent with current plugin's version,
     // or does not exist, then create/update table structure using dbDelta().
     if (Ai1ec_Meta::get_option('ai1ec_db_version') != AI1EC_DB_VERSION) {
         // =======================
         // = Create table events =
         // =======================
         $table_name = $wpdb->prefix . 'ai1ec_events';
         $sql = "CREATE TABLE {$table_name} (\n\t\t\t\tpost_id bigint(20) NOT NULL,\n\t\t\t\tstart datetime NOT NULL,\n\t\t\t\tend datetime,\n\t\t\t\tallday tinyint(1) NOT NULL,\n\t\t\t\tinstant_event tinyint(1) NOT NULL DEFAULT 0,\n\t\t\t\trecurrence_rules longtext,\n\t\t\t\texception_rules longtext,\n\t\t\t\trecurrence_dates longtext,\n\t\t\t\texception_dates longtext,\n\t\t\t\tvenue varchar(255),\n\t\t\t\tcountry varchar(255),\n\t\t\t\taddress varchar(255),\n\t\t\t\tcity varchar(255),\n\t\t\t\tprovince varchar(255),\n\t\t\t\tpostal_code varchar(32),\n\t\t\t\tshow_map tinyint(1),\n\t\t\t\tcontact_name varchar(255),\n\t\t\t\tcontact_phone varchar(32),\n\t\t\t\tcontact_email varchar(128),\n\t\t\t\tcontact_url varchar(255),\n\t\t\t\tcost varchar(255),\n\t\t\t\tticket_url varchar(255),\n\t\t\t\tical_feed_url varchar(255) CHARACTER SET ascii COLLATE ascii_general_ci,\n\t\t\t\tical_source_url varchar(255) CHARACTER SET ascii COLLATE ascii_general_ci,\n\t\t\t\tical_organizer varchar(255),\n\t\t\t\tical_contact varchar(255),\n\t\t\t\tical_uid varchar(255),\n\t\t\t\tshow_coordinates tinyint(1),\n\t\t\t\tlatitude decimal(20,15),\n\t\t\t\tlongitude decimal(20,15),\n\t\t\t\tfacebook_eid bigint(20),\n\t\t\t\tfacebook_user bigint(20),\n\t\t\t\tfacebook_status varchar(1) NOT NULL DEFAULT '',\n\t\t\t\tPRIMARY KEY  (post_id),\n\t\t\t\tKEY feed_source (ical_feed_url)\n\t\t\t) CHARACTER SET utf8;";
         // ==========================
         // = Create table instances =
         // ==========================
         $table_name = $wpdb->prefix . 'ai1ec_event_instances';
         $sql .= "CREATE TABLE {$table_name} (\n\t\t\t\tid bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t\tpost_id bigint(20) NOT NULL,\n\t\t\t\tstart datetime NOT NULL,\n\t\t\t\tend datetime NOT NULL,\n\t\t\t\tPRIMARY KEY  (id),\n\t\t\t\tUNIQUE KEY evt_instance (post_id,start)\n\t\t\t) CHARACTER SET utf8;";
         // ================================
         // = Create table category colors =
         // ================================
         $table_name = $wpdb->prefix . 'ai1ec_event_category_colors';
         $sql .= "CREATE TABLE {$table_name} (\n\t\t\t\tterm_id bigint(20) NOT NULL,\n\t\t\t\tterm_color varchar(255) NOT NULL,\n\t\t\t\tPRIMARY KEY  (term_id)\n\t\t\t) CHARACTER SET utf8;";
         if (Ai1ec_Database::instance()->apply_delta($sql)) {
             update_option('ai1ec_db_version', AI1EC_DB_VERSION);
         } else {
             trigger_error('Failed to upgrade DB schema', E_USER_WARNING);
         }
     }
 }
 /**
  * Constructor
  *
  * Initialize object, by storing instance of `wpdb` in local variable
  *
  * @return void Constructor does not return
  */
 protected function __construct()
 {
     global $wpdb;
     $this->_db = $wpdb;
     $this->_database = Ai1ec_Database::instance();
 }
    /**
     * _create_table method
     *
     * Method check current DB version, and updates table, if necessary.
     *
     * @return bool Success / validity
     */
    protected function _create_table()
    {
        $success = true;
        $option = 'ai1ec_log_db_ver';
        if (Ai1ec_Meta::get_option($option) != self::DB_VERSION) {
            $sql_query = '
				CREATE TABLE ' . $this->_table() . ' (
					id          BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
					remote_addr VARCHAR(39),
					the_time    INT(10) UNSIGNED NOT NULL,
					http_method VARCHAR(10) NOT NULL,
					srv_host    VARCHAR(200) NOT NULL,
					request_uri VARCHAR(255) NOT NULL,
					logger      VARCHAR(255) NOT NULL,
					err_level   VARCHAR(32) NOT NULL,
					message     VARCHAR(4000) NOT NULL,
					thread      INT(10) UNSIGNED NOT NULL,
					last_file   VARCHAR(255) NOT NULL,
					last_line   SMALLINT(5) UNSIGNED NOT NULL,
					PRIMARY KEY (id)
				) ENGINE=InnoDB CHARACTER SET utf8;
			';
            if (Ai1ec_Database::instance()->apply_delta($sql_query)) {
                update_option($option, self::DB_VERSION);
            } else {
                trigger_error('Failed to upgrade/install Logger database', E_USER_WARNING);
                $success = false;
            }
        }
        return $success;
    }
 /**
  * Constructor
  *
  * @return void Constructor does not return
  */
 public function __construct()
 {
     global $wpdb;
     $this->_db = $wpdb;
     $this->_ai1ec_db = Ai1ec_Database::instance();
 }