<commit_before><?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage addon
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 * @version    SVN: $Id$
 */
class sfPropelDatabaseSchema
{
  protected $connection_name = '';
  protected $database        = array();

  public function asArray()
  {
    return array($this->connection_name => $this->database);
  }
  
  public function loadYAML($file)
  {
    $schema = sfYaml::load($file);

    if (count($schema) > 1)
    {
      throw new sfException('A schema.yml must only contain 1 database entry.');
    }

    $this->connection_name = array_shift(array_keys($schema));
    $this->database = $schema[$this->connection_name];
        
    $this->fixYAMLDatabase();
    $this->fixYAMLI18n();
    $this->fixYAMLColumns();
  }

  public function asXML()
  {
    $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

    $xml .= "<database name=\"$this->connection_name\"".$this->getAttributesFor($this->database).">\n";

    // tables
    foreach ($this->getChildren($this->database) as $tb_name => $table)
    {
      $xml .= "\n  <table name=\"$tb_name\"".$this->getAttributesFor($table).">\n";

      // columns
      foreach ($this->getChildren($table) as $col_name => $column)
      {
        $xml .= "    <column name=\"$col_name\"".$this->getAttributesForColumn($col_name, $column);
      }

      // indexes
      if (isset($table['_indexes']))
      {
        foreach ($table['_indexes'] as $index_name => $index)
        {
          $xml .= "    <index name=\"$index_name\">\n";
          foreach ($index as $index_column)
          {
            $xml .= "      <index-column name=\"$index_column\" />\n";
          }
          $xml .= "    </index>\n";
        }
      }

      // uniques
      if (isset($table['_uniques']))
      {
        foreach ($table['_uniques'] as $unique_name => $index)
        {
          $xml .= "    <unique name=\"$unique_name\">\n";
          foreach ($index as $unique_column)
          {
            $xml .= "      <unique-column name=\"$unique_column\" />\n";
          }
          $xml .= "    </unique>\n";
        }
      }

      // foreign-keys
      if (isset($table['_foreign_keys']))
      {
        foreach ($table['_foreign_keys'] as $fkey_name => $fkey)
        {
          $xml .= "    <foreign-key foreignTable=\"$fkey[foreign_table]\"";
          // foreign key name
          if(!is_numeric($fkey_name))
          {
            $xml .= " name=\"$fkey_name\"";
          }
          // onDelete
          if(isset($fkey['on_delete']))
          {
            $xml .= " onDelete=\"$fkey[on_delete]\"";
          }
          $xml .= ">\n";

          // references
          if (isset($fkey['references']))
          {
            foreach ($fkey['references'] as $reference)
            {
              $xml .= "      <reference local=\"$reference[local]\" foreign=\"$reference[foreign]\" />\n";
            }
          }
          $xml .= "    </foreign-key>\n";
        }
      }

      $xml .= "  </table>\n";
    }
    $xml .= "\n</database>\n";

    return $xml;
  }
  
  private function fixYAMLDatabase()
  {
    if (!isset($this->database['_attributes']))
    {
      $this->database['_attributes'] = array();
    }
    // conventions for database attributes
    $this->setIfNotSet($this->database['_attributes'], 'defaultIdMethod', 'native');
    $this->setIfNotSet($this->database['_attributes'], 'noXsd', true);
  }

  private function fixYAMLI18n()
  {
    foreach ($this->getTables() as $i18n_table => $columns)
    {
      $pos = strpos($i18n_table, '_i18n');
      
      $has_primary_key = false;
      foreach ($columns as $column => $attributes)
      {
        if (is_array($attributes) && array_key_exists('primaryKey', $attributes))
        {
           $has_primary_key = true;
        }
      }

      if ($pos > 0 && $pos == strlen($i18n_table) - 5 && !$has_primary_key)
      {
        // i18n table without primary key
        $main_table = $this->findTable(substr($i18n_table, 0, $pos));
        
        if ($main_table)
        {
          // set i18n attributes for main table
          $this->setIfNotSet($this->database[$main_table]['_attributes'], 'isI18N', 1);
          $this->setIfNotSet($this->database[$main_table]['_attributes'], 'i18nTable', $i18n_table);

          // set id and culture columns for i18n table
          $this->setIfNotSet($this->database[$i18n_table], 'id', array (
            'type'             => 'integer',
            'required'         => true, 
            'primaryKey'       => true,
            'foreignTable'     => $main_table,
            'foreignReference' => 'id'
          ));
          $this->setIfNotSet($this->database[$i18n_table], 'culture', array(
            'isCulture'  => 'true',
            'type'       => 'varchar',
            'size'       => '7',
            'required'   => true,
            'primaryKey' => true
          ));
        }
        else
        {
          throw new sfException(sprintf('Missing main table for internationalized table "%s".', $i18n_table));
        }
      }
    }
  }

  private function fixYAMLColumns()
  {
    foreach ($this->getTables() as $table => $columns)
    {
      foreach ($columns as $column => $attributes)
      {
        if ($attributes == null)
        {
          // conventions for null attributes  
          if ($column == 'created_at' || $column == 'updated_at')<commit_msg>added default cascade deletion behavior for i18n tables primary keys in schema.yml (closes #826)<commit_after><?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage addon
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 * @version    SVN: $Id$
 */
class sfPropelDatabaseSchema
{
  protected $connection_name = '';
  protected $database        = array();

  public function asArray()
  {
    return array($this->connection_name => $this->database);
  }
  
  public function loadYAML($file)
  {
    $schema = sfYaml::load($file);

    if (count($schema) > 1)
    {
      throw new sfException('A schema.yml must only contain 1 database entry.');
    }

    $this->connection_name = array_shift(array_keys($schema));
    $this->database = $schema[$this->connection_name];
        
    $this->fixYAMLDatabase();
    $this->fixYAMLI18n();
    $this->fixYAMLColumns();
  }

  public function asXML()
  {
    $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

    $xml .= "<database name=\"$this->connection_name\"".$this->getAttributesFor($this->database).">\n";

    // tables
    foreach ($this->getChildren($this->database) as $tb_name => $table)
    {
      $xml .= "\n  <table name=\"$tb_name\"".$this->getAttributesFor($table).">\n";

      // columns
      foreach ($this->getChildren($table) as $col_name => $column)
      {
        $xml .= "    <column name=\"$col_name\"".$this->getAttributesForColumn($col_name, $column);
      }

      // indexes
      if (isset($table['_indexes']))
      {
        foreach ($table['_indexes'] as $index_name => $index)
        {
          $xml .= "    <index name=\"$index_name\">\n";
          foreach ($index as $index_column)
          {
            $xml .= "      <index-column name=\"$index_column\" />\n";
          }
          $xml .= "    </index>\n";
        }
      }

      // uniques
      if (isset($table['_uniques']))
      {
        foreach ($table['_uniques'] as $unique_name => $index)
        {
          $xml .= "    <unique name=\"$unique_name\">\n";
          foreach ($index as $unique_column)
          {
            $xml .= "      <unique-column name=\"$unique_column\" />\n";
          }
          $xml .= "    </unique>\n";
        }
      }

      // foreign-keys
      if (isset($table['_foreign_keys']))
      {
        foreach ($table['_foreign_keys'] as $fkey_name => $fkey)
        {
          $xml .= "    <foreign-key foreignTable=\"$fkey[foreign_table]\"";
          // foreign key name
          if(!is_numeric($fkey_name))
          {
            $xml .= " name=\"$fkey_name\"";
          }
          // onDelete
          if(isset($fkey['on_delete']))
          {
            $xml .= " onDelete=\"$fkey[on_delete]\"";
          }
          $xml .= ">\n";

          // references
          if (isset($fkey['references']))
          {
            foreach ($fkey['references'] as $reference)
            {
              $xml .= "      <reference local=\"$reference[local]\" foreign=\"$reference[foreign]\" />\n";
            }
          }
          $xml .= "    </foreign-key>\n";
        }
      }

      $xml .= "  </table>\n";
    }
    $xml .= "\n</database>\n";

    return $xml;
  }
  
  private function fixYAMLDatabase()
  {
    if (!isset($this->database['_attributes']))
    {
      $this->database['_attributes'] = array();
    }
    // conventions for database attributes
    $this->setIfNotSet($this->database['_attributes'], 'defaultIdMethod', 'native');
    $this->setIfNotSet($this->database['_attributes'], 'noXsd', true);
  }

  private function fixYAMLI18n()
  {
    foreach ($this->getTables() as $i18n_table => $columns)
    {
      $pos = strpos($i18n_table, '_i18n');
      
      $has_primary_key = false;
      foreach ($columns as $column => $attributes)
      {
        if (is_array($attributes) && array_key_exists('primaryKey', $attributes))
        {
           $has_primary_key = true;
        }
      }

      if ($pos > 0 && $pos == strlen($i18n_table) - 5 && !$has_primary_key)
      {
        // i18n table without primary key
        $main_table = $this->findTable(substr($i18n_table, 0, $pos));
        
        if ($main_table)
        {
          // set i18n attributes for main table
          $this->setIfNotSet($this->database[$main_table]['_attributes'], 'isI18N', 1);
          $this->setIfNotSet($this->database[$main_table]['_attributes'], 'i18nTable', $i18n_table);

          // set id and culture columns for i18n table
          $this->setIfNotSet($this->database[$i18n_table], 'id', array (
            'type'             => 'integer',
            'required'         => true, 
            'primaryKey'       => true,
            'foreignTable'     => $main_table,
            'foreignReference' => 'id',
            'onDelete'         => 'cascade'
          ));
          $this->setIfNotSet($this->database[$i18n_table], 'culture', array(
            'isCulture'  => 'true',
            'type'       => 'varchar',
            'size'       => '7',
            'required'   => true,
            'primaryKey' => true
          ));
        }
        else
        {
          throw new sfException(sprintf('Missing main table for internationalized table "%s".', $i18n_table));
        }
      }
    }
  }

  private function fixYAMLColumns()
  {
    foreach ($this->getTables() as $table => $columns)
    {
      foreach ($columns as $column => $attributes)
      {
        if ($attributes == null)
        {
          // conventions for null attributes  
          if ($column == 'created_at' || $column == 'updated_at')
{'fraction_non_alphanumeric': 0.17258572428271518, 'fraction_numerical': 0.009184744576627012, 'mean_word_length': 1.9345482546201231, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 2, '>': 160, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219653', 'n_tokens_mistral': 3609, 'n_tokens_neox': 3487, 'n_words': 692}