<commit_before><?php
/** 
 * @author noah
 * @date 2/26/11
 * @brief
 * 
*/

namespace Gacela\DataSource\Resource;

class Database extends Resource {

	/**
	 * @var PDO
	 */
	protected $_db;
	
	public function __construct(array $config)
	{
		$this->_db = $config['db'];
		unset($config['db']);

		parent::__construct($config);

		$method = '_load'.ucfirst($this->_config->dbtype);

		$this->$method();
	}

	private function _loadMysql()
	{
		$this->_meta['columns'] = array();
		$this->_meta['belongs_to'] = array();
		$this->_meta['has_many'] = array();
		$this->_meta['primary'] = array();

		// Setup Column meta information
		$stmt = $this->_db->prepare("DESCRIBE ".$this->_config->name);

		if(!$stmt->execute()) {
			throw new \Exception(
				'Error Code: '.
				$stmt->errorCode().
				'<br/>'.
				\Util::debug($stmt->errorInfo()).
				'Param Dump:'.
				\Util::debug($stmt->debugDumpParams())
			);
		}

		$columns = $stmt->fetchAll(\PDO::FETCH_OBJ);

		foreach($columns as $column) {
			$meta = array('length' => null, 'unsigned' => false, 'sequenced' => false);
			
			preg_match('/(?P<type>\w+)($|\((?P<length>(\d+|(.*)))\))/', $column->Type, $meta);

			$meta = array_merge(
						array(
							'length' => null,
							'unsigned' => false,
							'sequenced' => false,
							'values' => array()
						),
						$meta
					);

			if(stripos($column->Type, 'unsigned') !== false) {
				$meta['unsigned'] = true;
			}

			if($meta['type'] == 'enum') {
				$meta['values'] = explode(',', str_replace("'", "", $meta['length']));
				$meta['length'] = null;
			}

			switch($meta['type']) {
				case 'varchar':
				case 'char':
					$meta['type'] = 'string';
					break;
				case 'tinyint':
					if($meta['length'] == 1) {
						$meta['type'] = 'bool';
					}
					break;
			}<commit_msg>auto_increment in Extra field now used to determine $meta['sequenced'] value<commit_after><?php
/** 
 * @author noah
 * @date 2/26/11
 * @brief
 * 
*/

namespace Gacela\DataSource\Resource;

class Database extends Resource {

	/**
	 * @var PDO
	 */
	protected $_db;
	
	public function __construct(array $config)
	{
		$this->_db = $config['db'];
		unset($config['db']);

		parent::__construct($config);

		$method = '_load'.ucfirst($this->_config->dbtype);

		$this->$method();
	}

	private function _loadMysql()
	{
		$this->_meta['columns'] = array();
		$this->_meta['belongs_to'] = array();
		$this->_meta['has_many'] = array();
		$this->_meta['primary'] = array();

		// Setup Column meta information
		$stmt = $this->_db->prepare("DESCRIBE ".$this->_config->name);

		if(!$stmt->execute()) {
			throw new \Exception(
				'Error Code: '.
				$stmt->errorCode().
				'<br/>'.
				\Util::debug($stmt->errorInfo()).
				'Param Dump:'.
				\Util::debug($stmt->debugDumpParams())
			);
		}

		$columns = $stmt->fetchAll(\PDO::FETCH_OBJ);

		foreach($columns as $column) {
			$meta = array('length' => null, 'unsigned' => false, 'sequenced' => false);
			
			preg_match('/(?P<type>\w+)($|\((?P<length>(\d+|(.*)))\))/', $column->Type, $meta);

			$meta = array_merge(
						array(
							'length' => null,
							'unsigned' => false,
							'sequenced' => false,
							'values' => array()
						),
						$meta
					);

			if(stripos($column->Type, 'unsigned') !== false) {
				$meta['unsigned'] = true;
			}

			if(stripos($column->Extra, 'auto_increment') !== false) {
				$meta['sequenced'] = true;
			}

			if($meta['type'] == 'enum') {
				$meta['values'] = explode(',', str_replace("'", "", $meta['length']));
				$meta['length'] = null;
			}

			switch($meta['type']) {
				case 'varchar':
				case 'char':
					$meta['type'] = 'string';
					break;
				case 'tinyint':
					if($meta['length'] == 1) {
						$meta['type'] = 'bool';
					}
					break;
			}
{'fraction_non_alphanumeric': 0.23527844391153743, 'fraction_numerical': 0.0031974420463629096, 'mean_word_length': 3.0760043431053203, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 62, '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': '1362359', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1315, 'n_words': 238}