Example #1
0
 public function getSql()
 {
     $db = Connection::instance();
     $name = $this->schema->getName();
     $primary_key = $this->schema->getPrimaryKey();
     $columns = $this->schema->getColumns();
     $unique_keys = $this->schema->getUniqueKeys();
     $keys = $this->schema->getKeys();
     if (!$this->schema->validate()) {
         throw new RuntimeException("Invalid database table schema ('{$this->schema->getName()}')");
     }
     $charset_collate = '';
     if ($charset = $db->getCharset()) {
         $charset_collate = "DEFAULT CHARACTER SET {$charset}";
     }
     if ($collate = $db->getCollate()) {
         $charset_collate .= " COLLATE {$collate}";
     }
     $sql = "CREATE TABLE IF NOT EXISTS {$db->getTablePrefix()}{$name} (";
     foreach ($columns as $column => $settings) {
         $sql .= "\n  {$column} {$settings},";
     }
     // double space after pk
     $sql .= "\n  PRIMARY KEY  ({$primary_key}),";
     foreach ($unique_keys as $name => $key) {
         $sql .= "\n  UNIQUE KEY {$name} ({$key}),";
     }
     foreach ($keys as $name => $key) {
         $sql .= "\n  KEY {$name} ({$key}),";
     }
     $sql = trim($sql, ',') . "\n) {$charset_collate};";
     return $sql;
 }
Example #2
0
 /**
  * Fetches column metadata from the database.
  * 
  * @return array
  */
 protected function fetchColumns()
 {
     $cache_key = 'wpapp_db_col_' . get_current_blog_id() . $this->tableName;
     $cached = wp_cache_get($cache_key, 'dbmeta', false, $found);
     if ($found && $cached) {
         return $cached;
     }
     $results = (array) $this->connection->wpdb()->get_results("select C.COLUMN_NAME, C.COLUMN_DEFAULT, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH, " . "C.NUMERIC_PRECISION, C.IS_NULLABLE, C.COLUMN_KEY, C.EXTRA, C.COLLATION_NAME " . "from INFORMATION_SCHEMA.COLUMNS as C where C.TABLE_NAME = '{$this->tableName}'");
     $columns = array();
     foreach ($results as $data) {
         $column = new Table\Column($data);
         $columns[$column->name] = $column;
     }
     wp_cache_set($cache_key, $columns, 'dbmeta', 600);
     return $columns;
 }
Example #3
0
 public function install()
 {
     if (!$this->isInstalled()) {
         $command = new Command\Create($this);
         $command();
         if (Connection::instance()->isTableInstalled($this->tableName, true)) {
             $this->installed = true;
         }
     }
     return $this->installed;
 }