<commit_before><?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Sprig migration driver.
 *
 * @package		Migration
 * @author		<NAME>
 * @uses		DBForge
 * @copyright	(c) 2009 <NAME>
 * @license		MIT
 */
class Migration_Sprig extends Migration {
	
	/**
	 * The sprig model object.
	 * 
	 * @var	Sprig
	 */
	protected $_model;
	
	protected function _model($model)
	{
		if (is_string($model))
		{
			return Sprig::factory($model);
		}
		elseif ($model instanceof Sprig)
		{
			return $model;
		}
		else
		{
			throw new Kohana_Exception('Invalid model :model given to sprig driver.', array(
				':model'	=> (string) $model
			));
		}
	}
	
	protected function _tables()
	{
		$tables = array();
		
		$table = new Database_Table($this->_model->table(), $this->_db);
		
		$model_pks = is_array($this->_model->pk()) ? $this->_model->pk() : array($this->_model->pk());
		
		foreach ($this->_model->fields() as $field)
		{
			if ($field->in_db)
			{
				if ($field->unique)
				{
					$table->add_constraint(
						new Database_Constraint_Unique($field->column)
					);
				}
				
				$column = current($this->_get_columns($field, $table));
				
				$table->add_column($column);
			}
			else
			{
				if ($field instanceof Sprig_Field_ManyToMany)
				{
					$pivot = new Database_Table($field->through, $this->_db);

					$columns = $this->_get_columns(
						new Sprig_Field_BelongsTo(array(
							'model'	=> $field->model
						)), $pivot);
						
					foreach ($columns as $column)
					{
						$pivot->add_column($column);
					}
					
					$columns = $this->_get_columns(
						new Sprig_Field_BelongsTo(array(
							'model'	=> inflector::singular($this->_model->table())
						)), $pivot);
						
					foreach ($columns as $column)
					{
						$pivot->add_column($column);
					}
					
					$pivot->add_constraint(new Database_Constraint_Primary(
						array_keys($pivot->columns()), $pivot->name
					));
						
					$tables[] = $pivot;
				}
			}
		}
		
		$table->add_constraint(
			new Database_Constraint_Primary($model_pks, $table->name)
		);
		
		$tables[] = $table;
		
		return $tables;
	}
	
	protected function _db()
	{
		return Database::instance($this->_model->db());
	}
	
	/**
	 * Gets the database columns associated with the field.
	 * 
	 * @param	Sprig_Field	The sprig field.
	 * @param	Database_Table	The parent database table.
	 * @return	array
	 */
	private function _get_columns(Sprig_Field $field, Database_Table $table)
	{
		if ($field instanceof Sprig_Field_BelongsTo)
		{
			$references = $this->_model($field->model);
			
			$pks = is_array($references->pk()) ? $references->pk() : array($references->pk());
			
			$columns = array();
			
			foreach($pks as $pk)
			{
				$foreign_field = $references->field($pk);
				
				$column = current($this->_get_columns($foreign_field, $table));
				
				$column->name = Inflector::singular($references->table()).'_'.$pk;
				
				if ($column instanceof Database_Column_Int)
				{
					$column->auto_increment = FALSE;
				}
				
				$columns[] = $column;
			}
			
			return $columns;
		}
		elseif ($field instanceof Sprig_Field_Char)
		{
			if ($field instanceof Sprig_Field_Text)
			{
				$column = Database_Column::factory('text');
			}
			else
			{
				$column = Database_Column::factory('varchar');
				$column->max_length = isset($column->max_length) ? $column->max_length : 45;
			}
		}
		elseif ($field instanceof Sprig_Field_Integer)
		{<commit_msg>Fixed bug where text type was given a max-type parameter.<commit_after><?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Sprig migration driver.
 *
 * @package		Migration
 * @author		<NAME>
 * @uses		DBForge
 * @copyright	(c) 2009 <NAME>
 * @license		MIT
 */
class Migration_Sprig extends Migration {
	
	/**
	 * The sprig model object.
	 * 
	 * @var	Sprig
	 */
	protected $_model;
	
	protected function _model($model)
	{
		if (is_string($model))
		{
			return Sprig::factory($model);
		}
		elseif ($model instanceof Sprig)
		{
			return $model;
		}
		else
		{
			throw new Kohana_Exception('Invalid model :model given to sprig driver.', array(
				':model'	=> (string) $model
			));
		}
	}
	
	protected function _tables()
	{
		$tables = array();
		
		$table = new Database_Table($this->_model->table(), $this->_db);
		
		$model_pks = is_array($this->_model->pk()) ? $this->_model->pk() : array($this->_model->pk());
		
		foreach ($this->_model->fields() as $field)
		{
			if ($field->in_db)
			{
				if ($field->unique)
				{
					$table->add_constraint(
						new Database_Constraint_Unique($field->column)
					);
				}
				
				$column = current($this->_get_columns($field, $table));
				
				$table->add_column($column);
			}
			else
			{
				if ($field instanceof Sprig_Field_ManyToMany)
				{
					$pivot = new Database_Table($field->through, $this->_db);

					$columns = $this->_get_columns(
						new Sprig_Field_BelongsTo(array(
							'model'	=> $field->model
						)), $pivot);
						
					foreach ($columns as $column)
					{
						$pivot->add_column($column);
					}
					
					$columns = $this->_get_columns(
						new Sprig_Field_BelongsTo(array(
							'model'	=> inflector::singular($this->_model->table())
						)), $pivot);
						
					foreach ($columns as $column)
					{
						$pivot->add_column($column);
					}
					
					$pivot->add_constraint(new Database_Constraint_Primary(
						array_keys($pivot->columns()), $pivot->name
					));
						
					$tables[] = $pivot;
				}
			}
		}
		
		$table->add_constraint(
			new Database_Constraint_Primary($model_pks, $table->name)
		);
		
		$tables[] = $table;
		
		return $tables;
	}
	
	protected function _db()
	{
		return Database::instance($this->_model->db());
	}
	
	/**
	 * Gets the database columns associated with the field.
	 * 
	 * @param	Sprig_Field	The sprig field.
	 * @param	Database_Table	The parent database table.
	 * @return	array
	 */
	private function _get_columns(Sprig_Field $field, Database_Table $table)
	{
		if ($field instanceof Sprig_Field_BelongsTo)
		{
			$references = $this->_model($field->model);
			
			$pks = is_array($references->pk()) ? $references->pk() : array($references->pk());
			
			$columns = array();
			
			foreach($pks as $pk)
			{
				$foreign_field = $references->field($pk);
				
				$column = current($this->_get_columns($foreign_field, $table));
				
				$column->name = Inflector::singular($references->table()).'_'.$pk;
				
				if ($column instanceof Database_Column_Int)
				{
					$column->auto_increment = FALSE;
				}
				
				$columns[] = $column;
			}
			
			return $columns;
		}
		elseif ($field instanceof Sprig_Field_Char)
		{
			if ($field instanceof Sprig_Field_Text)
			{
				$column = Database_Column::factory('text');
				unset($column->max_length);
			}
			else
			{
				$column = Database_Column::factory('varchar');
				$column->max_length = isset($column->max_length) ? $column->max_length : 45;
			}
		}
		elseif ($field instanceof Sprig_Field_Integer)
		{
{'fraction_non_alphanumeric': 0.15842438182863713, 'fraction_numerical': 0.0017251293847038527, 'mean_word_length': 2.9349547511312215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1752357', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2286, 'n_words': 452}