/**
  * Returns an array with SQL DDL statements that creates the database definition in $dbSchema
  *
  * Converts the schema definition contained in $dbSchema to DDL SQL. This
  * SQL can be used to create tables in an existing database according to
  * the definition.  The SQL queries are returned as an array.
  * 
  * @param ezcDbSchema $dbSchema
  * @return array(string)
  */
 public function convertToDDL(ezcDbSchema $dbSchema)
 {
     $this->schema = $dbSchema->getSchema();
     // reset queries
     $this->queries = array();
     $this->context = array();
     $this->generateSchemaAsSql();
     return $this->queries;
 }
Esempio n. 2
0
 /**
  * Saves the schema definition in $schema to the file $file.
  * @todo throw exception when file can not be opened
  *
  * @param string      $file
  * @param ezcDbSchema $dbSchema
  */
 public function saveToFile($file, ezcDbSchema $dbSchema)
 {
     $schema = $dbSchema->getSchema();
     $data = $dbSchema->getData();
     $fileData = '<?php return ' . var_export(array($schema, $data), true) . '; ?>';
     if (!@file_put_contents($file, (string) $fileData)) {
         throw new ezcBaseFilePermissionException($file, ezcBaseFileException::WRITE);
     }
 }
Esempio n. 3
0
 /**
  * Writes the schema definition in $dbSchema to files located in $dir.
  * This method dumps the given schema to PersistentObject definitions, which
  * will be located in the given directory.
  *
  * @param string $dir           The directory to store definitions in.
  * @param ezcDbSchema $dbSchema The schema object to create defs for.
  *
  * @throws ezcBaseFileNotFoundException If the given directory could not be
  *                                      found.
  * @throws ezcBaseFilePermissionException If the given directory is not 
  *                                        writable.
  */
 public function saveToFile($dir, ezcDbSchema $dbSchema)
 {
     if (!is_dir($dir)) {
         throw new ezcBaseFileNotFoundException($dir, 'directory');
     }
     if (!is_writable($dir)) {
         throw new ezcBaseFilePermissionException($dir, ezcBaseFileException::WRITE);
     }
     $schema = $dbSchema->getSchema();
     foreach ($schema as $tableName => $table) {
         $this->writeTable($dir, $tableName, $table);
     }
 }
Esempio n. 4
0
 /**
  * Validates if all the types used in the $schema are supported.
  *
  * This method loops over all the fields in a table and checks whether the
  * type that is used for each field is supported. It will return an array
  * containing error strings for each non-supported type that it finds.
  *
  * @param ezcDbSchema $schema
  * @return array(string)
  */
 public static function validate(ezcDbSchema $schema)
 {
     $errors = array();
     /* For each table we check all field's types. */
     foreach ($schema->getSchema() as $tableName => $table) {
         foreach ($table->fields as $fieldName => $field) {
             if (!in_array($field->type, ezcDbSchema::$supportedTypes)) {
                 $errors[] = "Field '{$tableName}:{$fieldName}' uses the unsupported type '{$field->type}'.";
             }
         }
     }
     return $errors;
 }
Esempio n. 5
0
 /**
  * Validates if all the fields used in all indexes exist.
  *
  * This method loops over all the fields in the indexes of each table and
  * checks whether the fields that is used in an index is also defined in
  * the table definition. It will return an array containing error strings
  * for each non-supported type that it finds.
  *
  * @param ezcDbSchema $schema
  * @return array(string)
  */
 public static function validate(ezcDbSchema $schema)
 {
     $errors = array();
     /* For each table we first retrieve all the field names, and then check
      * per index whether the fields it references exist */
     foreach ($schema->getSchema() as $tableName => $table) {
         $fields = array_keys($table->fields);
         foreach ($table->indexes as $indexName => $index) {
             foreach ($index->indexFields as $indexFieldName => $dummy) {
                 if (!in_array($indexFieldName, $fields)) {
                     $errors[] = "Index '{$tableName}:{$indexName}' references unknown field name '{$tableName}:{$indexFieldName}'.";
                 }
             }
         }
     }
     return $errors;
 }
 /**
  * Validates if all the index names used are unique accross the schema.
  *
  * This method loops over all the indexes in all tables and checks whether
  * they have been used before.
  *
  * @param ezcDbSchema $schema
  * @return array(string)
  */
 public static function validate(ezcDbSchema $schema)
 {
     $indexes = array();
     $errors = array();
     /* For each table we check all auto increment fields. */
     foreach ($schema->getSchema() as $tableName => $table) {
         foreach ($table->indexes as $indexName => $dummy) {
             $indexes[$indexName][] = $tableName;
         }
     }
     foreach ($indexes as $indexName => $tableList) {
         if (count($tableList) > 1) {
             $errors[] = "The index name '{$indexName}' is not unique. It exists for the tables: '" . join("', '", $tableList) . "'.";
         }
     }
     return $errors;
 }
 /**
  * Writes the given $schema to $dir using $template.
  *
  * Iterates through all tables in $schema, sends each of them to a {@link
  * ezcTemplate} with $template and writes the result to $dir with the file
  * name returned by the template.
  * 
  * @param ezcDbSchema $schema 
  * @param string $template
  * @param mixed $dir 
  */
 public function write(ezcDbSchema $schema, $template, $dir)
 {
     $tplConf = ezcTemplateConfiguration::getInstance();
     $tplConf->templatePath = $this->properties['options']->templatePath;
     $tplConf->compilePath = $this->properties['options']->templateCompilePath;
     $tpl = new ezcTemplate();
     $tpl->send->classPrefix = $this->properties['options']->classPrefix;
     foreach ($schema->getSchema() as $tableName => $tableSchema) {
         $tpl->send->schema = $tableSchema;
         $tpl->send->tableName = $tableName;
         $content = $tpl->process($template);
         $fileName = $dir . '/' . $tpl->receive->fileName;
         if (!$this->properties['options']->overwrite && file_exists($fileName)) {
             throw new ezcPersistentObjectSchemaOverwriteException($fileName);
         }
         file_put_contents($fileName, $content);
     }
 }
 /**
  * Validates if all the types used in the $schema are supported.
  *
  * This method loops over all the fields in a table and checks whether the
  * type that is used for each field is supported. It will return an array
  * containing error strings for each non-supported type that it finds.
  *
  * @param ezcDbSchema $schema
  * @return array(string)
  */
 public static function validate(ezcDbSchema $schema)
 {
     $errors = array();
     /* For each table we check all auto increment fields. */
     foreach ($schema->getSchema() as $tableName => $table) {
         foreach ($table->fields as $fieldName => $field) {
             if ($field->autoIncrement === true) {
                 $found = false;
                 // Loop over de indexes to see if there is a primary
                 foreach ($table->indexes as $indexName => $index) {
                     if ($index->primary === true) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     $errors[] = "Field '{$tableName}:{$fieldName}' is auto increment but there is no primary index defined.";
                 }
             }
         }
     }
     return $errors;
 }
Esempio n. 9
0
 /**
  * Writes the schema definition in $dbSchema to the file $file.
  *
  * @param string      $file
  * @param ezcDbSchema $dbSchema
  * @todo throw exception when file can not be opened
  */
 public function saveToFile($file, ezcDbSchema $dbSchema)
 {
     $schema = $dbSchema->getSchema();
     $data = $dbSchema->getData();
     $this->writer = new XMLWriter();
     if (!@$this->writer->openUri($file)) {
         throw new ezcBaseFilePermissionException($file, ezcBaseFileException::WRITE);
     }
     $this->writer->startDocument('1.0', 'utf-8');
     $this->writer->setIndent(true);
     $this->writer->startElement('database');
     foreach ($schema as $tableName => $table) {
         $this->writer->flush();
         $this->writeTable($tableName, $table);
     }
     $this->writer->endElement();
     $this->writer->endDocument();
 }
Esempio n. 10
0
 /**
  * Filter properties that accept a null value.
  *
  * Note that ToOne relations involve a *virtual* property that
  * accepts a NULL value; except if the ToOne is mapped to a local
  * *table* column that does not accept null values.
  *
  * The $out parameter specifies if the filtered properties should be
  * deleted from the list, or if it should be the only properties left.
  * If $out is *true*, the filtered properties are removed from the list
  * and if it's false, all the non-filtered properties are removed.
  *
  * <code>
  * <?php
  * // Remove all properties accepting a null value :
  * $list->filterNull();
  *
  * // Remove all properties that don't accept a null value :
  * $list->filterNull( false );
  * ?>
  * </code>
  *
  * @param $out bool True to filter out all the properties accepting
  *                  null value; false to remove all other.
  * @return aiiPersistentObjectFilterablePropertyList $this
  */
 public function filterNull($out = true)
 {
     $schema = $this->schema->getSchema();
     foreach ($this->properties as $name => $def) {
         if (array_key_exists($name, $schema[$this->def->table]->fields)) {
             $notNull = $schema[$this->def->table]->fields[$name]->notNull;
         } else {
             $notNull = $def->propertyType != ezcPersistentObjectProperty::PHP_TYPE_OBJECT;
         }
         if ($out != $notNull) {
             if (array_key_exists($name, $this)) {
                 unset($this[$name]);
             }
         }
     }
     return $this;
 }