Ejemplo n.º 1
0
 public function __construct($schema, $db = null)
 {
     $this->schema = $schema;
     $this->alias = null;
     $this->className = "\\stdClass";
     $this->attributes = array();
     $this->joins = array();
     $this->where = array();
     $this->orderBy = array();
     $this->groupBy = array();
     $this->having = array();
     $this->limit = null;
     $this->offset = null;
     $this->page = null;
     $this->db = $db === null ? Db::connect($this->schema->getDb()) : $db;
     $this->hasOneElement = false;
     $this->iterator = null;
     $this->filters = array();
     $this->pile = array();
     $this->maxPileSize = 5000;
     $this->insertCount = 0;
     $this->updateValues = array();
 }
Ejemplo n.º 2
0
 public function alterTo($targetSchema)
 {
     $db = Db::connect($this->db);
     foreach ($this->attributes as $attributeName => $attributeData) {
         if (!isset($targetSchema->attributes[$attributeName])) {
             //dump("ALTER TABLE `$this->table` DROP `$attributeName`");
             $db->query("ALTER TABLE `{$this->table}` DROP `{$attributeName}`");
         }
     }
     $previousAttribute = null;
     foreach ($targetSchema->attributes as $attributeName => $attributeData) {
         $typeString = $attributeData["type"] . (isset($attributeData["length"]) ? "({$attributeData["length"]})" : "");
         $unsignedString = isset($attributeData["unsigned"]) && $attributeData["unsigned"] ? "unsigned" : null;
         $nullString = isset($attributeData["acceptNull"]) && $attributeData["acceptNull"] ? "NULL" : "NOT NULL";
         $incrementString = isset($attributeData["autoIncrement"]) && $attributeData["autoIncrement"] ? "AUTO_INCREMENT" : null;
         if (array_key_exists("default", $attributeData)) {
             $defaultValue = is_null($attributeData["default"]) ? 'NULL' : "'" . $attributeData["default"] . "'";
             $defaultString = "DEFAULT {$defaultValue}";
         } else {
             $defaultString = null;
         }
         if (!isset($this->attributes[$attributeName])) {
             $positionString = $previousAttribute == null ? "FIRST" : "AFTER `{$previousAttribute}`";
             //dump("ALTER TABLE `$this->table` ADD `$attributeName` $typeString $unsignedString $nullString $defaultString $incrementString $positionString;");
             $db->query("ALTER TABLE `{$this->table}` ADD `{$attributeName}` {$typeString} {$unsignedString} {$nullString} {$defaultString} {$incrementString} {$positionString};");
             break;
         }
         $isDifferent = false;
         foreach ($attributeData as $property => $value) {
             if ($property == "type" && $value == "integer") {
                 $value = "int";
             }
             if ($property == "length" && $value == null) {
                 continue;
             }
             if (isset($this->attributes[$attributeName][$property]) && $this->attributes[$attributeName][$property] != $value) {
                 //dump("$attributeName:$property is {$this->attributes[$attributeName][$property]} but expected $value");
                 $isDifferent = true;
                 break;
             }
         }
         if ($isDifferent) {
             //dump("ALTER TABLE `$this->table` CHANGE `$attributeName` `$attributeName` $typeString $unsignedString $nullString $defaultString $incrementString;");
             $db->query("ALTER TABLE `{$this->table}` CHANGE `{$attributeName}` `{$attributeName}` {$typeString} {$unsignedString} {$nullString} {$defaultString} {$incrementString};");
         }
         $previousAttribute = $attributeName;
     }
 }