<commit_before><?php

namespace Lorry\Service;

use Analog;
use PDO;
use RuntimeException;
use InvalidArgumentException;
use PDOException;
use Lorry\Model;
use Aura\SqlQuery\QueryFactory;

class PersistenceService {

	/**
	 *
	 * @var ConfigService
	 */
	protected $config;
	protected $cache;

	public function setConfigService(ConfigService $config) {
		$this->config = $config;
		$this->cache = array();
	}

	/**
	 *
	 * @var PDO
	 */
	private $connection = null;

	/**
	 *
	 * @var QueryFactory
	 */
	private $factory = null;

	public function ensureConnected() {
		if($this->connection !== null) {
			return true;
		}
		try {
			$dsn = $this->config->get('persistence/dsn');
			$this->connection = new PDO($dsn, $this->config->get('persistence/username'), $this->config->get('persistence/password'));
			$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$this->factory = new QueryFactory(strstr($dsn, ':', true));
		} catch(PDOException $ex) {
			// catch the pdo exception to prevent credential leaking
			throw new RuntimeException('could not connect to database ('.$ex->getMessage().')');
		}
	}

	/**
	 * 
	 * @param Model $model
	 * @param array $pairs
	 * @param array $order
	 * @param int $offset
	 * @param int $limit
	 * @return array
	 * @throws InvalidArgumentException
	 */
	public function loadAll(Model $model, $pairs, $order, $offset, $limit) {
		$this->ensureConnected();
		$query = $this->factory->newSelect();

		$query->cols(array('*'));
		$query->from($model->getTable());

		foreach($pairs as $row => $parameter) {
			$operator = '=';
			if(is_array($parameter)) {
				$operator = $parameter[0];
				$value = $parameter[1];
			} else {
				$value = $parameter;
			}
			if($value === null) {
				switch($operator) {
					case '=':
						$operator = 'IS';
						break;
					case '!=':
						$operator = 'IS NOT';
						break;
				}
				$query->where($row.' '.$operator.' null');
			} else {
				$query->where($row.' '.$operator.' :'.$row);
				$query->bindValue($row, $value);
			}
		}

		$query->orderBy($order);
		$query->offset($offset);
		$query->limit($limit);

		$statement = $this->connection->prepare($query->__toString());
		$statement->execute($query->getBindValues());

		if($statement->errorCode() != PDO::ERR_NONE) {
			$errorinfo = $statement->errorInfo();
			throw new RuntimeException($errorinfo[1].': '.$errorinfo[2].' (sql error '.$errorinfo[0].' for query "'.$query.'")');
		}
		$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

		return $rows;
	}

	/**
	 * 
	 * @param Model $model
	 * @param array $pairs
	 * @param array $order
	 * @param int $offset
	 * @param int $limit
	 * @return Model
	 * @throws Exception
	 */
	public function load(Model $model, $pairs, $order, $offset, $limit) {
		$rows = $this->loadAll($model, $pairs, $order, $offset, $limit);
		if(count($rows) > 1 && $limit === null && $offset === null) {
			throw new Exception('result ambiguity: expected unique identifier');
		} else if(count($rows) == 1) {
			return $rows[0];
		}

		return null;
	}

	/**
	 * 
	 * @param Model $model
	 * @param array $changes
	 * @return bool
	 * @throws Exception
	 */
	public function update(Model $model, $changes) {
		$this->ensureConnected();
		$model->ensureLoaded();<commit_msg>fix wrong exception class thrown in PersistenceService<commit_after><?php

namespace Lorry\Service;

use Analog;
use PDO;
use RuntimeException;
use InvalidArgumentException;
use PDOException;
use Lorry\Model;
use Aura\SqlQuery\QueryFactory;

class PersistenceService {

	/**
	 *
	 * @var ConfigService
	 */
	protected $config;
	protected $cache;

	public function setConfigService(ConfigService $config) {
		$this->config = $config;
		$this->cache = array();
	}

	/**
	 *
	 * @var PDO
	 */
	private $connection = null;

	/**
	 *
	 * @var QueryFactory
	 */
	private $factory = null;

	public function ensureConnected() {
		if($this->connection !== null) {
			return true;
		}
		try {
			$dsn = $this->config->get('persistence/dsn');
			$this->connection = new PDO($dsn, $this->config->get('persistence/username'), $this->config->get('persistence/password'));
			$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$this->factory = new QueryFactory(strstr($dsn, ':', true));
		} catch(PDOException $ex) {
			// catch the pdo exception to prevent credential leaking
			throw new RuntimeException('could not connect to database ('.$ex->getMessage().')');
		}
	}

	/**
	 * 
	 * @param Model $model
	 * @param array $pairs
	 * @param array $order
	 * @param int $offset
	 * @param int $limit
	 * @return array
	 * @throws InvalidArgumentException
	 */
	public function loadAll(Model $model, $pairs, $order, $offset, $limit) {
		$this->ensureConnected();
		$query = $this->factory->newSelect();

		$query->cols(array('*'));
		$query->from($model->getTable());

		foreach($pairs as $row => $parameter) {
			$operator = '=';
			if(is_array($parameter)) {
				$operator = $parameter[0];
				$value = $parameter[1];
			} else {
				$value = $parameter;
			}
			if($value === null) {
				switch($operator) {
					case '=':
						$operator = 'IS';
						break;
					case '!=':
						$operator = 'IS NOT';
						break;
				}
				$query->where($row.' '.$operator.' null');
			} else {
				$query->where($row.' '.$operator.' :'.$row);
				$query->bindValue($row, $value);
			}
		}

		$query->orderBy($order);
		$query->offset($offset);
		$query->limit($limit);

		$statement = $this->connection->prepare($query->__toString());
		$statement->execute($query->getBindValues());

		if($statement->errorCode() != PDO::ERR_NONE) {
			$errorinfo = $statement->errorInfo();
			throw new RuntimeException($errorinfo[1].': '.$errorinfo[2].' (sql error '.$errorinfo[0].' for query "'.$query.'")');
		}
		$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

		return $rows;
	}

	/**
	 * 
	 * @param Model $model
	 * @param array $pairs
	 * @param array $order
	 * @param int $offset
	 * @param int $limit
	 * @return Model
	 * @throws Exception
	 */
	public function load(Model $model, $pairs, $order, $offset, $limit) {
		$rows = $this->loadAll($model, $pairs, $order, $offset, $limit);
		if(count($rows) > 1 && $limit === null && $offset === null) {
			throw new RuntimeException('result ambiguity: expected unique identifier');
		} else if(count($rows) == 1) {
			return $rows[0];
		}

		return null;
	}

	/**
	 * 
	 * @param Model $model
	 * @param array $changes
	 * @return bool
	 * @throws Exception
	 */
	public function update(Model $model, $changes) {
		$this->ensureConnected();
		$model->ensureLoaded();
{'fraction_non_alphanumeric': 0.18776628119293975, 'fraction_numerical': 0.002434570906877663, 'mean_word_length': 3.6386732533521524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, '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': '4209389', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2133, 'n_words': 489}