<commit_before><?php

namespace Besearcher;

class Db {
	private $mPDO;

	public function __construct($theDatabasePath, $theCreateIfNonExistent = true) {
		$this->mPDO = new \PDO('sqlite:' . $theDatabasePath);
		$this->mPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

		if($theCreateIfNonExistent && !$this->hasTables()) {
			$this->createTables();
		}
	}

	public function begin() {
		// TODO: keep track of being in a transaction
		$this->mPDO->beginTransaction();
	}

	public function rollback() {
		$this->mPDO->rollBack();
	}

	public function commit() {
		$this->mPDO->commit();
	}

	public function shutdown() {
		$mPDO = null;
	}

	public function destroy() {
		$this->mPDO->exec("DROP TABLE tasks");
		$this->mPDO->exec("DROP TABLE results");
		$this->mPDO->exec("DROP TABLE context");
		$this->mPDO->exec("DROP TABLE analytics");
		$this->createTables();
	}

	public function hasTables() {
		$aStmt = $this->mPDO->prepare("SELECT COUNT(*) AS num FROM sqlite_master WHERE type='table' AND name='context'");
		$aStmt->execute();
		$aRow = $aStmt->fetch(\PDO::FETCH_ASSOC);

		return $aRow['num'] > 0;
	}

	public function createTables() {
		$aSqlFile = dirname(__FILE__) . '/resources/db.sql';
		$aStructure = file_get_contents($aSqlFile);

		if($aStructure === false) {
			throw new \Exception('Unable to load SQL file: ' . $aSqlFile);
		}

		$aParts = explode('--split', $aStructure);

		foreach($aParts as $aCommand) {
			$this->mPDO->exec($aCommand);
		}
	}

	private function getIdParts(array $theIdInfo) {
		$aRet = array('name' => '', 'value' => '');

		if(count($theIdInfo) != 1) {
			return $aRet;
		}

		$aRet['name'] = array_keys($theIdInfo)[0];
		$aRet['value'] = array_values($theIdInfo)[0];

		return $aRet;
	}

	public function update($theTable, array $theKeyValuePairs, array $theIdInfo = array()) {
		$aFields = array_keys($theKeyValuePairs);
		$aParts = array();

		foreach($aFields as $aField) {
			$aParts[] = $aField . ' = ' . ':' . $aField;
		}

		$aWhere = "1";
		$aIdParts = $this->getIdParts($theIdInfo);
		$aHasId = $aIdParts['name'] != '';

		if($aHasId) {
			$aWhere = $aIdParts['name']." = :" . $aIdParts['name'];
		}

		$aSql = "UPDATE ".$theTable." SET ".implode(', ', $aParts)." WHERE ".$aWhere;
		$aStmt = $this->mPDO->prepare($aSql);

		foreach($aFields as $aField) {
			$aStmt->bindParam(':' . $aField, $theKeyValuePairs[$aField]);
		}

		if($aHasId) {
			$aStmt->bindParam(':' . $aIdParts['name'], $aIdParts['value']);
		}

		$aOk = $aStmt->execute();

		return $aOk;
	}

	public function getPDO() {
		return $this->mPDO;
	}
}

?>
<commit_msg>Simplify things<commit_after><?php

namespace Besearcher;

class Db {
	private $mPDO;

	public function __construct($theDatabasePath, $theCreateIfNonExistent = true) {
		$this->mPDO = new \PDO('sqlite:' . $theDatabasePath);
		$this->mPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

		if($theCreateIfNonExistent && !$this->hasTables()) {
			$this->createTables();
		}
	}

	public function begin() {
		// TODO: keep track of being in a transaction
		$this->mPDO->beginTransaction();
	}

	public function rollback() {
		$this->mPDO->rollBack();
	}

	public function commit() {
		$this->mPDO->commit();
	}

	public function shutdown() {
		$mPDO = null;
	}

	public function destroy() {
		$this->mPDO->exec("DROP TABLE tasks");
		$this->mPDO->exec("DROP TABLE results");
		$this->mPDO->exec("DROP TABLE context");
		$this->mPDO->exec("DROP TABLE analytics");
		$this->createTables();
	}

	public function hasTables() {
		$aStmt = $this->mPDO->prepare("SELECT COUNT(*) AS num FROM sqlite_master WHERE type='table' AND name='context'");
		$aStmt->execute();
		$aRow = $aStmt->fetch(\PDO::FETCH_ASSOC);

		return $aRow['num'] > 0;
	}

	public function createTables() {
		$aSqlFile = dirname(__FILE__) . '/resources/db.sql';
		$aStructure = file_get_contents($aSqlFile);

		if($aStructure === false) {
			throw new \Exception('Unable to load SQL file: ' . $aSqlFile);
		}

		$aParts = explode('--split', $aStructure);

		foreach($aParts as $aCommand) {
			$this->mPDO->exec($aCommand);
		}
	}

	public function update($theTable, array $theKeyValuePairs, array $theIdInfo = array()) {
		$aFields = array_keys($theKeyValuePairs);
		$aParts = array();

		foreach($aFields as $aField) {
			$aParts[] = $aField . ' = ' . ':' . $aField;
		}

		$aWhere = "1";
		$aIdFieldName = null;
		$aIdFieldValue = null;
		$aHasIdValue = count($theIdInfo) == 1;

		if($aHasIdValue) {
			$aIdFieldName = array_keys($theIdInfo)[0];
			$aIdFieldValue = array_values($theIdInfo)[0];
			$aWhere = $aIdFieldName." = :" . $aIdFieldName;
		}

		$aSql = "UPDATE ".$theTable." SET ".implode(', ', $aParts)." WHERE ".$aWhere;
		$aStmt = $this->mPDO->prepare($aSql);

		foreach($aFields as $aField) {
			$aStmt->bindParam(':' . $aField, $theKeyValuePairs[$aField]);
		}

		if($aHasIdValue) {
			$aStmt->bindParam(':' . $aIdFieldName, $aIdFieldValue);
		}

		$aOk = $aStmt->execute();

		return $aOk;
	}

	public function getPDO() {
		return $this->mPDO;
	}
}

?>

{'fraction_non_alphanumeric': 0.1972640761300555, 'fraction_numerical': 0.0019825535289452814, 'mean_word_length': 4.137474541751527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, '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': '4464174', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1898, 'n_words': 353}