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 createDatabase($identifier = null)
 {
     $sourcesFolder = $this->path . "/" . Module::DIR_SOURCES;
     if (!is_dir($sourcesFolder)) {
         return;
     }
     $targetEntities = [];
     foreach (EntityReflection::getEntities($sourcesFolder) as $entityClassName) {
         if ($entityClassName::getSchema()->getDb() == $identifier) {
             $targetEntities[] = $entityClassName;
         }
     }
     if (!$targetEntities) {
         echo "No entities found (no tables to create)";
         return;
     }
     $databaseSettings = Db::getCredentials($identifier);
     $databaseString = $databaseSettings["username"] . ($databaseSettings["password"] ? ':****' : '') . '@' . $databaseSettings["host"] . '/' . $databaseSettings["database"];
     echo "Updating database {$databaseString}\n";
     Db::create($identifier);
     foreach ($targetEntities as $entityClassName) {
         echo "    patching table for {$entityClassName}\n";
         try {
             $entityClassName::getSchema()->patch();
         } catch (\Exception $e) {
             echo $entityClassName . ": " . $e->getMessage() . "\n";
         }
     }
     foreach ($targetEntities as $entityClassName) {
         try {
             echo "    checking triggers for {$entityClassName}\n";
             $entityClassName::getSchema()->createTriggers();
         } catch (\Exception $e) {
             echo $e->getMessage();
         }
     }
 }
Ejemplo n.º 3
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;
     }
 }
Ejemplo n.º 4
0
<?php

use Phidias\Utilities\Configuration;
use Phidias\Db\Db;
// Obtain DB connection identifiers from configuration
Db::configure(function ($identifier = null) {
    $base = $identifier === null ? "phidias.db" : "phidias.db.{$identifier}";
    if (!Configuration::get("{$base}.host")) {
        return null;
    }
    return array("host" => Configuration::get("{$base}.host"), "username" => Configuration::get("{$base}.username"), "password" => Configuration::get("{$base}.password"), "database" => Configuration::get("{$base}.database"), "charset" => Configuration::get("{$base}.charset"));
});