Exemplo n.º 1
0
 public function add_cols($objects, $table = null)
 {
     if (is_null($table)) {
         $table = first_key($this->tables);
     } elseif ($table === false) {
         $table = self::NO_TABLE;
     }
     if (is_array($objects) && is_array(reset($objects))) {
         foreach ($objects as $obj) {
             $this->add_cols($obj, $table);
         }
     } else {
         $this->add_objects($objects, $this->cols[$table]);
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Bring the table up to the current definition
  */
 protected function updateTable()
 {
     $customDefs = $this->table_defs;
     $tableName = first_key($customDefs);
     if (!isset($customDefs[$tableName])) {
         return FALSE;
     }
     $wanteddef = $customDefs[$tableName];
     $actualdef = $this->getActualTableDefs();
     $sqlConnection = $this->getSQLConnection();
     // Set the primary keys
     $wantedKey = $this->findKey($wanteddef);
     $actualKey = $this->findKey($actualdef);
     if ($wantedKey != $actualKey) {
         if ($actualKey) {
             $query = "ALTER TABLE " . $this->sql_prfx . $tableName . "\n";
             $query .= "  DROP PRIMARY KEY";
             dbm_debug("server query", $query);
             if (!amtz_query($query, $sqlConnection)) {
                 return FALSE;
             }
         }
         if ($wantedKey) {
             $query = "ALTER TABLE " . $this->sql_prfx . $tableName . "\n";
             $query .= "  ADD PRIMARY KEY (" . $wantedKey . ")";
             dbm_debug("server query", $query);
             if (!amtz_query($query, $sqlConnection)) {
                 return FALSE;
             }
         }
     }
     // Run through the wanted definition for what needs changing
     $location = "FIRST";
     foreach ($wanteddef as $name => $options) {
         $creationDef = $this->getCreationDefinition($name, $options);
         // Find a column that needs creating
         if (!isset($actualdef[$name])) {
             $query = "ALTER TABLE " . $this->sql_prfx . $tableName . "\n";
             $query .= "  ADD COLUMN " . $creationDef . " " . $location;
             dbm_debug("server query", $query);
             if (!amtz_query($query, $sqlConnection)) {
                 return FALSE;
             }
         } else {
             if ($wanteddef[$name] != $actualdef[$name]) {
                 $query = "ALTER TABLE " . $this->sql_prfx . $tableName . "\n";
                 $query .= "  MODIFY COLUMN " . $creationDef . " " . $location;
                 dbm_debug("server query", $query);
                 if (!amtz_query($query, $sqlConnection)) {
                     return FALSE;
                 }
             }
         }
         // Change location so it will be set properly for the next iteration
         $location = "AFTER " . $name;
     }
     // SCARY
     // Run through the actual definition for what needs dropping
     foreach ($actualdef as $name => $options) {
         // Find a column that needs deleting
         if (!isset($wanteddef[$name]) && DBM_AUTODROP) {
             $query = "ALTER TABLE " . $this->sql_prfx . $tableName . "\n";
             $query .= "  DROP COLUMN " . $name;
             dbm_debug("server query", $query);
             if (!amtz_query($query, $sqlConnection)) {
                 return FALSE;
             }
         }
     }
     return TRUE;
 }