<commit_before><?php

namespace db;

use \PDO as PDO;
use \Database as Database;
use \DataObject as DataObject;
use \Localized as Localized;
use \Logger as Logger;
use \Model as Model;
use \SQL as SQL;

class SelectSQL extends SQL
{
	public $model;
	public $columns;
	public $order;

    public function __construct(Model $model, array $columns = array())
    {
    	parent::__construct();
    	if ( is_null($model) ) {
    		throw new \Exception( "Must specfify the model to selct from .. " );
    	}

    	if ( count($columns) == 0 ) {
    		$columns = $model->allColumnNames();
    	}

    	$this->model = $model;
    	$this->columns = $columns;
    	return $this;
    }

	public function orderby(array $order = null) {
		$this->order = $order;
	}

	public function sqlParameters()
	{
		return (isset($this->qualifier) ? $this->qualifier->sqlParameters() : null);
	}

	public function sqlStatement()
	{
		$components = array(
			SQL::CMD_SELECT,
			implode(",", $this->columns),
			SQL::SQL_FROM,
			$this->model->tableName(),
		);

		if ( isset($this->qualifier) ) {
			$components[] = SQL::SQL_WHERE;
			$components[] = $this->qualifier->sqlStatement();
		}

		if ( isset($this->order) ) {
			$components[] = SQL::SQL_ORDER;
			$orderClauses = array();
			foreach( $this->order as $clause ) {
				if ( is_array($clause) ) {
					if ( count($clause) != 1 ) {
						throw new \Exception( "Unable to parse order clause " . var_export($clause, true) );
					}
					foreach( $clause as $direction => $col ) {
						if ( is_int($direction) || strtoupper($direction) === SQL::SQL_ORDER_ASC) {
							$orderClauses[] = $col;
						}
						else if ( strtoupper($direction) === SQL::SQL_ORDER_DESC ) {
							$orderClauses[] = $col . ' ' . SQL::SQL_ORDER_DESC;
						}
						else {
							throw new \Exception( "Unable to order '$direction' on column $col" );
						}
					}
				}
				else if ( is_string($clause) ) {
					$orderClauses[] = $clause;
				}
				else {
					throw new \Exception( "Unable to order '$clause'" );
				}
			}
			$components[] = implode(",", $orderClauses);
		}

		return implode(" ", $components );
	}

	public function fetch()
	{
		$sql = $this->sqlStatement();
		$params = $this->sqlParameters();

		$statement = Database::instance()->prepare($sql);
		if ($statement && $statement->execute($params)) {
			try {
				if ( isset($this->model) ) {
					$dboClassName = DataObject::NameForModel($this->model);
					if (class_exists($dboClassName)) {
						return $statement->fetchObject($dboClassName);
					}
				}
				else {
					return $statement->fetch();
				}
			}
			catch ( \ClassNotFoundException $e ) {
				Logger::logException( $e );
				return $statement->fetch();
			}
		}

		$caller = callerClassAndMethod('fetch');
		$errPoint = ($statement ? $statement : Database::instance());
		$pdoError = $errPoint->errorInfo()[1] . ':' . $errPoint->errorInfo()[2];
		$this->reportSQLError($caller['class'], $caller['function'], $errPoint->errorCode(), $pdoError, $sql, $params);

		return false;
	}

	public function fetchAll()
	{
		$sql = $this->sqlStatement();
		$params = $this->sqlParameters();

		$statement = Database::instance()->prepare($sql);
		if ($statement && $statement->execute($params)) {
			try {
				if ( isset($this->model) ) {
					$dboClassName = DataObject::NameForModel($this->model);
					if (class_exists($dboClassName)) {
						return $statement->fetchAll(PDO::FETCH_CLASS, $dboClassName);
					}
					else {
						return $statement->fetchAll();
					}
				}
				else {
					return $statement->fetchAll();
				}
			}
			catch ( \ClassNotFoundException $e ) {
				Logger::logException( $e );
				return $statement->fetchAll();
			}
		}

		$caller = callerClassAndMethod('fetchAll');
		$errPoint = ($statement ? $statement : Database::instance());
		$pdoError = $errPoint->errorInfo()[1] . ':' . $errPoint->errorInfo()[2];
		$this->reportSQLError($caller['class'], $caller['function'], $errPoint->errorCode(), $pdoError, $sql, $params);

		return false;
	}
}
<commit_msg>Test if select matched the model'd DataObject, otherwise fetch raw rows<commit_after><?php

namespace db;

use \PDO as PDO;
use \Database as Database;
use \DataObject as DataObject;
use \Localized as Localized;
use \Logger as Logger;
use \Model as Model;
use \SQL as SQL;

class SelectSQL extends SQL
{
	public $model;
	public $columns;
	public $order;

    public function __construct(Model $model, array $columns = array())
    {
    	parent::__construct();
    	if ( is_null($model) ) {
    		throw new \Exception( "Must specfify the model to selct from .. " );
    	}

    	if ( count($columns) == 0 ) {
    		$columns = $model->allColumnNames();
    	}

    	$this->model = $model;
    	$this->columns = $columns;
    	return $this;
    }

    public function objectBasedSelect()
    {
		if ( isset($this->columns) ) {
			$modelColumns = $this->model->allColumnNames();
			if ( count($this->columns) == count($modelColumns) ) {
				$diff = array_diff($this->columns, $modelColumns);
				return (count($diff) == 0);
			}
		}
		return false;
    }

	public function orderby(array $order = null)
	{
		$this->order = $order;
	}

	public function sqlParameters()
	{
		return (isset($this->qualifier) ? $this->qualifier->sqlParameters() : null);
	}

	public function sqlStatement()
	{
		$components = array(
			SQL::CMD_SELECT,
			implode(",", $this->columns),
			SQL::SQL_FROM,
			$this->model->tableName(),
		);

		if ( isset($this->qualifier) ) {
			$components[] = SQL::SQL_WHERE;
			$components[] = $this->qualifier->sqlStatement();
		}

		if ( isset($this->order) ) {
			$components[] = SQL::SQL_ORDER;
			$orderClauses = array();
			foreach( $this->order as $clause ) {
				if ( is_array($clause) ) {
					if ( count($clause) != 1 ) {
						throw new \Exception( "Unable to parse order clause " . var_export($clause, true) );
					}
					foreach( $clause as $direction => $col ) {
						if ( is_int($direction) || strtoupper($direction) === SQL::SQL_ORDER_ASC) {
							$orderClauses[] = $col;
						}
						else if ( strtoupper($direction) === SQL::SQL_ORDER_DESC ) {
							$orderClauses[] = $col . ' ' . SQL::SQL_ORDER_DESC;
						}
						else {
							throw new \Exception( "Unable to order '$direction' on column $col" );
						}
					}
				}
				else if ( is_string($clause) ) {
					$orderClauses[] = $clause;
				}
				else {
					throw new \Exception( "Unable to order '$clause'" );
				}
			}
			$components[] = implode(",", $orderClauses);
		}

		return implode(" ", $components );
	}

	public function fetch()
	{
		$sql = $this->sqlStatement();
		$params = $this->sqlParameters();

		$statement = Database::instance()->prepare($sql);
		if ($statement && $statement->execute($params)) {
			try {
				if ( isset($this->model) && $this->objectBasedSelect() ) {
					$dboClassName = DataObject::NameForModel($this->model);
					if (class_exists($dboClassName)) {
						return $statement->fetchObject($dboClassName);
					}
				}
				else {
					return $statement->fetch();
				}
			}
			catch ( \ClassNotFoundException $e ) {
				Logger::logException( $e );
				return $statement->fetch();
			}
		}

		$caller = callerClassAndMethod('fetch');
		$errPoint = ($statement ? $statement : Database::instance());
		$pdoError = $errPoint->errorInfo()[1] . ':' . $errPoint->errorInfo()[2];
		$this->reportSQLError($caller['class'], $caller['function'], $errPoint->errorCode(), $pdoError, $sql, $params);

		return false;
	}

	public function fetchAll()
	{
		$sql = $this->sqlStatement();
		$params = $this->sqlParameters();

		$statement = Database::instance()->prepare($sql);
		if ($statement && $statement->execute($params)) {
			try {
				if ( isset($this->model) && $this->objectBasedSelect() ) {
					$dboClassName = DataObject::NameForModel($this->model);
					if (class_exists($dboClassName)) {
						return $statement->fetchAll(PDO::FETCH_CLASS, $dboClassName);
					}
					else {
						return $statement->fetchAll();
					}
				}
				else {
					return $statement->fetchAll();
				}
			}
			catch ( \ClassNotFoundException $e ) {
				Logger::logException( $e );
				return $statement->fetchAll();
			}
		}

		$caller = callerClassAndMethod('fetchAll');
		$errPoint = ($statement ? $statement : Database::instance());
		$pdoError = $errPoint->errorInfo()[1] . ':' . $errPoint->errorInfo()[2];
		$this->reportSQLError($caller['class'], $caller['function'], $errPoint->errorCode(), $pdoError, $sql, $params);

		return false;
	}
}

{'fraction_non_alphanumeric': 0.17801418439716313, 'fraction_numerical': 0.0015366430260047282, 'mean_word_length': 3.2624685138539045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 96, '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': '4101965', 'n_tokens_mistral': 3332, 'n_tokens_neox': 2772, 'n_words': 580}