<commit_before><?php

/**
 *    DABL (Database ABstraction Layer)
 *    	By <NAME>
 *    		Inspired by Propel
 *    			Last Modified July 25th 2009
 */

class DABLGenerator{

	/**
	 * @var String
	 */
	private $connection_name;

	/**
	 * @var DOMDocument
	 */
	private $db_schema;

	/**
	 * Constructor function
	 * @param $conn_name String
	 * @param $schema DOMDocument
	 */
	function __construct($conn_name = null, DOMDocument $schema = null){
		if($conn_name)$this->setConnectionName($conn_name);
		if($schema)$this->setSchema($schema);
	}

	/**
	 * @param DOMDocument $schema
	 */
	function setSchema(DOMDocument $schema){
		$this->db_schema = $schema;
	}

	/**
	 * @return DOMDocument
	 */
	function getSchema(){
		return $this->db_schema;
	}

	function getTableNames(){
		$table_names = array();
		$database = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database->getElementsByTagName('table') as $table)
			$table_names[] = $table->getAttribute("name");
		return $table_names;
	}

	function getColumns($table_name){
		$columns = array();
		$database_node = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database_node->getElementsByTagName('table') as $table_node){
			if($table_node->getAttribute("name")!==$table_name)continue;
			foreach($table_node->getElementsByTagName('column') as $column_node){
				$column = new Column($column_node->getAttribute('name'));

				if($column_node->hasAttribute('size'))
					$column->setSize($column_node->getAttribute('size'));

				$column->setType($column_node->getAttribute('type'));
				$column->setPrimaryKey(($column_node->getAttribute('primaryKey')=="true"));
				$column->setAutoIncrement(($column_node->getAttribute('autoIncrement=')=="true"));

				if($column_node->hasAttribute('default'))
					$column->setDefaultValue($column_node->getAttribute('default'));

				$column->setNotNull(($column_node->getAttribute('required')=='true'));

				$columns[] = $column;
			}
		}
		return $columns;
	}

	function getForeignKeysFromTable($table_name){
		$references = array();

		$database_node = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database_node->getElementsByTagName('table') as $table_node){
			if($table_node->getAttribute("name")!==$table_name)continue;
			foreach($table_node->getElementsByTagName('foreign-key') as $fk_node){
				foreach($fk_node->getElementsByTagName('reference') as $reference_node){
					$references[] = array(
						'to_table' => $fk_node->getAttribute('foreignTable'),
						'to_column' => $reference_node->getAttribute('foreign'),
						'from_column' => $reference_node->getAttribute('local'),
					);
				}
			}
		}
		return $references;
	}

	function getForeignKeysToTable($table_name){
		$references = array();

		$database_node = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database_node->getElementsByTagName('table') as $table_node){
			foreach($table_node->getElementsByTagName('foreign-key') as $fk_node){
				foreach($fk_node->getElementsByTagName('reference') as $reference_node){
					if($fk_node->getAttribute('foreignTable')!=$table_name)continue;
					$references[] = array(
						'from_table' => $table_node->getAttribute('name'),
						'to_column' => $reference_node->getAttribute('foreign'),
						'from_column' => $reference_node->getAttribute('local'),
					);
				}
			}
		}
		return $references;
	}

	/**
	 * @return String
	 */
	private function getDBName(){
		foreach($this->getSchema()->getElementsByTagName('database') as $database)
			return $database->getAttribute("name");
	}

	/**
	 * @param String $conn_name
	 */
	function setConnectionName($conn_name){
		$this->connection_name = $conn_name;
	}

	/**
	 * @return String
	 */
	private function getConnectionName(){
		return $this->connection_name;
	}

	/**
	 * Generates a String with the contents of the Base class
	 * @param String $tableName
	 * @param String $className
	 * @param Array $options
	 * @return String
	 */
	private function getBaseClass($table, $className, $options){

		//Gather all the information about the table's columns from the database
		$PK = null;
		$numeric=array();
		$null = array();
		$PKs = array();
		$fields = $this->getColumns($table);
		foreach($fields as $field)
			if($field->isPrimaryKey()) $PKs[] = $field->getName();

		if(count($PKs)==1)
			$PK = $PKs[0];

		$class = "<?php
/**
 *	Created by <NAME>'s Database->Object Mapper
 *		             Based on Propel
 *
 *		Do not alter base files, as they will be overwritten.
 *		To alter the objects, alter the extended clases in
 *		the 'tables' folder.
 *
 */

abstract class base$className extends BaseTable{
";

		$class .= '
	/**
	 * Name of the table
	 */
	protected static $_tableName = "'.$table.'";

	/**
	 * Array of all primary keys
	 */
	protected static $_primaryKeys = array(';
		if($PKs)
			foreach($PKs as $thePK){
	 		$class .= '
			"'.$thePK.'",';
		}
		$class .= '
	);

	/**
	 * Primary Key
	 */
	 protected static $_primaryKey = "'.$PK.'";

	/**
	 * Array of all column names
	 */
	protected static $_columnNames = array(';
		foreach($fields as $key=>$field){
			$class .= "
		'{$field->getName()}'";
			if($key!=(count($fields)-1)) $class .= ",";
		}
$class .= '
	);
';
		foreach($fields as $key=>$field){
			$default = $field->getDefaultValue();
			$class .= '	protected $'.$field->getName();
			if(is_numeric($default))
				$class .= ' = '.$default;
			elseif($default!==NULL)
				$class .= ' = "'.$default.'"';
			$class .= ';
';
		}

		$class .='
	/**
	 * Column Accessors and Mutators
	 */
';
		foreach($fields as $key=>$field){
			$default = $field->getDefaultValue();
			$class .= '
	function get'.($options['cap_method_names'] ? ucfirst($field->getName()) : $field->getName()).'('.($field->isTemporal() ? '$format = null' : '').'){';
			if($field->isTemporal()){
				$class .= '
		if($this->'.$field->getName().'===null || !$format)
			return $this->'.$field->getName().';
		$dateTime = new DateTime($this->'.$field->getName().');
		return $dateTime->format($format);';
			}
			else{
			$class .='
		return $this->'.$field->getName().';';
			}
			$class .='
	}
	function set'.($options['cap_method_names'] ? ucfirst($field->getName()) : $field->getName()).'($theValue){';
			if($field->isNumeric()){
				if($options['empty_string_zero'] && $field->getName()!=$PK){
					$class .= '
		if($theValue==="")
			$theValue = 0;';
				}
				else{
					$class .= '
		if($theValue==="")
			$theValue = null;';
				}
			}

			if($options['protect_not_null'] && $field->getName()!=$PK && $field->isNotNull()){
				$class .= '
		if($theValue===null){';
				if($default){
					$class .='
			$pk = $this->getPrimaryKey();
			if($pk && $this->$pk===null)
				$theValue='.(is_numeric($default) ? $default : '"'.$default.'"').';
			else{';
				}

				if($field->isNumeric())
					$class .= '
			$theValue = 0;';
				else<commit_msg>fix to datetime columns<commit_after><?php

/**
 *    DABL (Database ABstraction Layer)
 *    	By <NAME>
 *    		Inspired by Propel
 *    			Last Modified July 25th 2009
 */

class DABLGenerator{

	/**
	 * @var String
	 */
	private $connection_name;

	/**
	 * @var DOMDocument
	 */
	private $db_schema;

	/**
	 * Constructor function
	 * @param $conn_name String
	 * @param $schema DOMDocument
	 */
	function __construct($conn_name = null, DOMDocument $schema = null){
		if($conn_name)$this->setConnectionName($conn_name);
		if($schema)$this->setSchema($schema);
	}

	/**
	 * @param DOMDocument $schema
	 */
	function setSchema(DOMDocument $schema){
		$this->db_schema = $schema;
	}

	/**
	 * @return DOMDocument
	 */
	function getSchema(){
		return $this->db_schema;
	}

	function getTableNames(){
		$table_names = array();
		$database = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database->getElementsByTagName('table') as $table)
			$table_names[] = $table->getAttribute("name");
		return $table_names;
	}

	function getColumns($table_name){
		$columns = array();
		$database_node = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database_node->getElementsByTagName('table') as $table_node){
			if($table_node->getAttribute("name")!==$table_name)continue;
			foreach($table_node->getElementsByTagName('column') as $column_node){
				$column = new Column($column_node->getAttribute('name'));

				if($column_node->hasAttribute('size'))
					$column->setSize($column_node->getAttribute('size'));

				$column->setType($column_node->getAttribute('type'));
				$column->setPrimaryKey(($column_node->getAttribute('primaryKey')=="true"));
				$column->setAutoIncrement(($column_node->getAttribute('autoIncrement=')=="true"));

				if($column_node->hasAttribute('default'))
					$column->setDefaultValue($column_node->getAttribute('default'));

				$column->setNotNull(($column_node->getAttribute('required')=='true'));

				$columns[] = $column;
			}
		}
		return $columns;
	}

	function getForeignKeysFromTable($table_name){
		$references = array();

		$database_node = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database_node->getElementsByTagName('table') as $table_node){
			if($table_node->getAttribute("name")!==$table_name)continue;
			foreach($table_node->getElementsByTagName('foreign-key') as $fk_node){
				foreach($fk_node->getElementsByTagName('reference') as $reference_node){
					$references[] = array(
						'to_table' => $fk_node->getAttribute('foreignTable'),
						'to_column' => $reference_node->getAttribute('foreign'),
						'from_column' => $reference_node->getAttribute('local'),
					);
				}
			}
		}
		return $references;
	}

	function getForeignKeysToTable($table_name){
		$references = array();

		$database_node = $this->getSchema()->getElementsByTagName('database')->item(0);
		foreach($database_node->getElementsByTagName('table') as $table_node){
			foreach($table_node->getElementsByTagName('foreign-key') as $fk_node){
				foreach($fk_node->getElementsByTagName('reference') as $reference_node){
					if($fk_node->getAttribute('foreignTable')!=$table_name)continue;
					$references[] = array(
						'from_table' => $table_node->getAttribute('name'),
						'to_column' => $reference_node->getAttribute('foreign'),
						'from_column' => $reference_node->getAttribute('local'),
					);
				}
			}
		}
		return $references;
	}

	/**
	 * @return String
	 */
	private function getDBName(){
		foreach($this->getSchema()->getElementsByTagName('database') as $database)
			return $database->getAttribute("name");
	}

	/**
	 * @param String $conn_name
	 */
	function setConnectionName($conn_name){
		$this->connection_name = $conn_name;
	}

	/**
	 * @return String
	 */
	private function getConnectionName(){
		return $this->connection_name;
	}

	/**
	 * Generates a String with the contents of the Base class
	 * @param String $tableName
	 * @param String $className
	 * @param Array $options
	 * @return String
	 */
	private function getBaseClass($table, $className, $options){

		//Gather all the information about the table's columns from the database
		$PK = null;
		$numeric=array();
		$null = array();
		$PKs = array();
		$fields = $this->getColumns($table);
		foreach($fields as $field)
			if($field->isPrimaryKey()) $PKs[] = $field->getName();

		if(count($PKs)==1)
			$PK = $PKs[0];

		$class = "<?php
/**
 *	Created by <NAME>'s Database->Object Mapper
 *		             Based on Propel
 *
 *		Do not alter base files, as they will be overwritten.
 *		To alter the objects, alter the extended clases in
 *		the 'tables' folder.
 *
 */

abstract class base$className extends BaseTable{
";

		$class .= '
	/**
	 * Name of the table
	 */
	protected static $_tableName = "'.$table.'";

	/**
	 * Array of all primary keys
	 */
	protected static $_primaryKeys = array(';
		if($PKs)
			foreach($PKs as $thePK){
	 		$class .= '
			"'.$thePK.'",';
		}
		$class .= '
	);

	/**
	 * Primary Key
	 */
	 protected static $_primaryKey = "'.$PK.'";

	/**
	 * Array of all column names
	 */
	protected static $_columnNames = array(';
		foreach($fields as $key=>$field){
			$class .= "
		'{$field->getName()}'";
			if($key!=(count($fields)-1)) $class .= ",";
		}
$class .= '
	);
';
		foreach($fields as $key=>$field){
			$default = $field->getDefaultValue();
			$class .= '	protected $'.$field->getName();
			if(is_numeric($default))
				$class .= ' = '.$default;
			elseif($default!==NULL)
				$class .= ' = "'.$default.'"';
			$class .= ';
';
		}

		$class .='
	/**
	 * Column Accessors and Mutators
	 */
';
		foreach($fields as $key=>$field){
			$default = $field->getDefaultValue();
			$class .= '
	function get'.($options['cap_method_names'] ? ucfirst($field->getName()) : $field->getName()).'('.($field->isTemporal() ? '$format = null' : '').'){';
			if($field->isTemporal()){
				$class .= '
		if($this->'.$field->getName().'===null || !$format)
			return $this->'.$field->getName().';
		$dateTime = new DateTime($this->'.$field->getName().');
		return $dateTime->format($format);';
			}
			else{
			$class .='
		return $this->'.$field->getName().';';
			}
			$class .='
	}
	function set'.($options['cap_method_names'] ? ucfirst($field->getName()) : $field->getName()).'($theValue){';
			if($field->isNumeric() || $field->isTemporal()){
				if($field->isNumeric() && $options['empty_string_zero'] && $field->getName()!=$PK){
					$class .= '
		if($theValue==="")
			$theValue = 0;';
				}
				else{
					$class .= '
		if($theValue==="")
			$theValue = null;';
				}
			}

			if($options['protect_not_null'] && $field->getName()!=$PK && $field->isNotNull()){
				$class .= '
		if($theValue===null){';
				if($default){
					$class .='
			$pk = $this->getPrimaryKey();
			if($pk && $this->$pk===null)
				$theValue='.(is_numeric($default) ? $default : '"'.$default.'"').';
			else{';
				}

				if($field->isNumeric())
					$class .= '
			$theValue = 0;';
				else
{'fraction_non_alphanumeric': 0.20142538334173204, 'fraction_numerical': 0.0021596717298970557, 'mean_word_length': 4.0369833212472805, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 197, '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': '6176159', 'n_tokens_mistral': 5229, 'n_tokens_neox': 4547, 'n_words': 796}