예제 #1
0
 function test_isMetadataTable()
 {
     $mt = new Dataface_MetadataTool(null);
     $this->assertTrue($mt->isMetadataTable('foo__metadata'));
     $this->assertTrue(!$mt->isMetadataTable('foo__metadata2'));
     $this->assertTrue($mt->isMetadataTable('bar_metadata__metadata'));
 }
예제 #2
0
 /**
  * Refreshes the metadata table for a given table.  This means that missing
  * columns and keys are created so that the schema matches the schema of
  * the current table structure.
  *
  * @param string $tablename The name of the table for which the metadata is being
  *					 stored.
  */
 function refreshMetadataTable($tablename = null)
 {
     if (!isset($tablename)) {
         $tablename = $this->tablename;
     }
     if (Dataface_MetadataTool::isMetadataTable($tablename)) {
         return false;
     }
     $app =& Dataface_Application::getInstance();
     $table =& Dataface_Table::loadTable($tablename);
     $md_tablename = $tablename . '__metadata';
     if (!Dataface_Table::tableExists($md_tablename, false)) {
         if ($this->createMetadataTable($tablename)) {
             return true;
         }
     }
     $cols =& $this->getColumns($tablename, false);
     // First we have to go through all of the key fields of the subject table
     // and make sure that they appear in the metadata table.
     $updatePrimaryKey = false;
     foreach ($table->keys() as $field) {
         if (!isset($cols[$field['Field']])) {
             $updatePrimaryKey = true;
             $default = @$field['Default'] ? " DEFAULT {$field['Default']}" : '';
             $sql = "alter table `{$md_tablename}` add column `{$field['Field']}` {$field['Type']}{$default}";
             $res = xf_db_query($sql, $app->db());
             if (!$res) {
                 trigger_error(xf_db_error($app->db()), E_USER_ERROR);
             }
         }
     }
     $table_keys =& $table->keys();
     //Next we have to go through all of the key fields in the metadata table ane make sure that they
     // appear in the subject table primary keys.
     foreach ($this->getKeyColumns($tablename, false) as $field) {
         if (!isset($table_keys[$field['Field']])) {
             $updatePrimaryKey = true;
             $sql = "alter table `{$md_tablename}` drop column `{$field['Field']}`";
             $res = xf_db_query($sql, $app->db());
             if (!$res) {
                 trigger_error(xf_db_error($app->db()), E_USER_ERROR);
             }
         }
     }
     // If the primary key needed to be updated, we will update it now.
     if ($updatePrimaryKey) {
         // The primary key needs to be updated
         $sql = "drop primary key";
         @xf_db_query($sql, $app->db());
         $sql = "alter table `{$md_tablename}` add primary key (`" . implode('`,`', array_keys($table->keys())) . "`)";
         $res = xf_db_query($sql, $app->db());
         if (!$res) {
             trigger_error(xf_db_error($app->db()), E_USER_ERROR);
         }
     }
     // Now we need to make sure that all of the prescribed meta fields are
     // in the metadata field.
     $fielddefs = $this->loadMetadataFieldDefs($tablename);
     $cols = $this->getColumns($tablename, false);
     foreach ($fielddefs as $field) {
         if (!isset($cols[$field['Field']])) {
             $default = @$field['Default'] ? " DEFAULT {$field['Default']}" : '';
             $sql = "alter table `{$md_tablename}` add column `{$field['Field']}` {$field['Type']}{$default}";
             $res = xf_db_query($sql, $app->db());
             if (!$res) {
                 trigger_error(xf_db_error($app->db()), E_USER_ERROR);
             }
         }
     }
     return true;
 }