<commit_before><?


namespace chimera;


class Document
{
	protected $_model;
	protected $_id = false;
	protected $_data = array();

	public function __construct($model) {
		$this->_model = $model;
		$this->_data = $this->_model->schema()->defaults();
	}

	public function __set($key, $val) {
		if ($key == 'id') $this->_id = $val;
		else if ($this->_model->schema()->has_virtual($key))
			$this->_model->schema()->virtual($key)->execute_set($this, $val);
		else {
			$this->_model->schema()->validate($key, $val);
			$this->_data[$key] = $val;
		}
	}

	public function __get($key) {
		if ($key == 'id') return $this->_id;
		if (!isset($this->_data[$key])) {
			if ($this->_model->schema()->has_virtual($key)) return $this->_model->schema()->virtual($key)->execute_get($this);
			return false;
		}
		return $this->_data[$key];
	}

	public function save() {
		$this->_model->schema()->validate($this);

		$fields = $this->_model->schema()->definition();
		$field_names = array_keys($fields);
		$data = $this->data();
		$values = array(':id' => 'NULL', ':_data' => '');
		foreach ($field_names as $key => $name) {
			$values[':' . $name] = $this->$name;
			unset($data[$name]);
		}
		$values[':_data'] = json_encode($data);

		if ($this->id) {
			unset($values['id']);
	#TODO "updates need to be moved into storage classes";
			$update = 'UPDATE ' . $this->_model->source() . ' SET _data=:data';
			foreach($fields as $name => $info) {
				switch ($info['type']) {
					case Schema::Date:
					case Schema::DateTime:
					case Schema::TimeStamp:
						if (strpos($this->$name, ')')) {
							$update .= ", $name=" . $this->$name;
							unset($values[$name]);
						}
						else $update .= ", $name=:$name";
						break;

					default:
						$update .= ", $name=:$name";
						break;
				}
			}
			$update .= ' WHERE id=:id';
			$query = $this->_model->owner()->prepare($update);

			$ok = $query->execute(array_values($values));
			if (!$ok) {
				$e = $query->errorInfo();<commit_msg>more insert fixes<commit_after><?


namespace chimera;


class Document
{
	protected $_model;
	protected $_id = false;
	protected $_data = array();

	public function __construct($model) {
		$this->_model = $model;
		$this->_data = $this->_model->schema()->defaults();
	}

	public function __set($key, $val) {
		if ($key == 'id') $this->_id = $val;
		else if ($this->_model->schema()->has_virtual($key))
			$this->_model->schema()->virtual($key)->execute_set($this, $val);
		else {
			$this->_model->schema()->validate($key, $val);
			$this->_data[$key] = $val;
		}
	}

	public function __get($key) {
		if ($key == 'id') return $this->_id;
		if (!isset($this->_data[$key])) {
			if ($this->_model->schema()->has_virtual($key)) return $this->_model->schema()->virtual($key)->execute_get($this);
			return false;
		}
		return $this->_data[$key];
	}

	public function save() {
		$this->_model->schema()->validate($this);

		$fields = $this->_model->schema()->definition();
		$field_names = array_keys($fields);
		$data = $this->data();
		$values = array(':id' => 'NULL', ':_data' => '');
		foreach ($field_names as $key => $name) {
			$values[':' . $name] = $this->$name;
			unset($data[$name]);
		}
		$values[':_data'] = json_encode($data);

		if ($this->id) {
			$values[':id'] = $this->id;
			unset($values['id']);
	#TODO "updates need to be moved into storage classes";
			$update = 'UPDATE ' . $this->_model->source() . ' SET _data=:_data';
			foreach($fields as $name => $info) {
				switch ($info['type']) {
					case Schema::Date:
					case Schema::DateTime:
					case Schema::TimeStamp:
						if (strpos($this->$name, ')')) {
							$update .= ", $name=" . $this->$name;
							unset($values[':'.$name]);
						}
						else $update .= ", $name=:$name";
						break;

					default:
						$update .= ", $name=:$name";
						break;
				}
			}
			$update .= ' WHERE id=:id';
			$query = $this->_model->owner()->prepare($update);

			$ok = $query->execute($values);
			if (!$ok) {
				$e = $query->errorInfo();
{'fraction_non_alphanumeric': 0.23760640961442164, 'fraction_numerical': 0.0, 'mean_word_length': 3.524348810872027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 102, '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': '2599848', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1363, 'n_words': 275}