<commit_before><?php

namespace Neptune\Database\Entity;

use Neptune\Database\Entity\EntityCollection;
use Neptune\Database\Driver\DatabaseDriverInterface;
use Neptune\Database\DatabaseFactory;
use Neptune\Database\Entity\AbstractEntity;
use Neptune\Database\Query\AbstractQuery;

/**
 * Entity
 * @author <NAME> <<EMAIL>>
 */
class Entity extends AbstractEntity {

	protected static $table;
	protected static $fields = array();
	protected static $primary_key = 'id';
	protected static $relations = array();
	protected $current_index;

	/**
	 * Create a new Entity instance.
	 *
	 * @param string $database The name of the database instance to use.
	 * @param array $values Initial values to give the instance. Note
	 * that no set<Key> methods will be called. To apply these
	 * methods, call setValues($data) after creation.
	 */
	public function __construct(DatabaseDriverInterface $database, array $values = array()) {
		$this->database = $database;
        foreach ($values as $key => $value) {
            //don't call setRaw as we don't want the modified flag to be
            //set.
            $this->values[$key] = $value;
        }
	}

    public function __sleep()
    {
        return array('values', 'modified', 'stored', 'current_index', 'relation_objects', 'relation_keys');
    }

    public function setRaw($key, $value)
    {
		if (isset(static::$relations[$key])) {
			return $this->setRelation($key, $value);
		}
		//if attempting to update the primary key, keep a copy of the current
		if($key === static::$primary_key && isset($this->values[$key])) {
			$this->current_index = $this->values[$key];
		}
		//maybe apply the modified flag
		if (in_array($key, static::$fields) && !in_array($key, $this->modified)) {
			$this->modified [] = $key;
		}
		$this->values[$key] = $value;
    }

	public function getRaw($key) {
		if (isset(static::$relations[$key])) {
			return $this->getRelation($key);
		}
		if (isset($this->values[$key])) {
			return $this->values[$key];
		}
		return null;
	}

	public function save() {
		if ($this->stored) {
            return $this->update();
		}
        return $this->insert();
	}

	public function delete() {
		if (!isset(static::$primary_key)) {
			throw new \Exception('Can\'t delete with no index key');
		}
		$q = $this->database->delete();
		$q->from(static::$table);
		if (!isset($this->values[static::$primary_key])) {
			throw new \Exception('Can\'t delete with no index key');
		}
		$q->where(static::$primary_key. " =", '?');
		$stmt = $q->prepare();
		if($this->current_index) {
			$index = $this->current_index;
		} else {
			$index = $this->values[static::$primary_key];
		}
		if ($stmt->execute(array($index))) {
			return true;
		}
		return false;
	}

	public function update() {
		if (empty($this->modified)) {
            return true;
        }
        if (!isset($this->values[static::$primary_key])) {
            throw new \Exception('Can\'t update with no index key');
        }
        $q = $this->database->update();
        $q->tables(static::$table)
          ->fields($this->modified)
          ->where(static::$primary_key . ' = ?');
        $stmt = $q->prepare();
        $values = array();
        foreach ($this->modified as $modified) {
            $values[] = $this->values[$modified];
        }
        if($this->current_index) {
            $index = $this->current_index;
        } else {
            $index = $this->values[static::$primary_key];<commit_msg>Fix EntityTest using a mock Query.<commit_after><?php

namespace Neptune\Database\Entity;

use Neptune\Database\Entity\EntityCollection;
use Neptune\Database\Driver\DatabaseDriverInterface;
use Neptune\Database\DatabaseFactory;
use Neptune\Database\Entity\AbstractEntity;
use Neptune\Database\Query\AbstractQuery;

/**
 * Entity
 * @author <NAME> <<EMAIL>>
 */
class Entity extends AbstractEntity {

	protected static $table;
	protected static $fields = array();
	protected static $primary_key = 'id';
	protected static $relations = array();
	protected $current_index;

	/**
	 * Create a new Entity instance.
	 *
	 * @param string $database The name of the database instance to use.
	 * @param array $values Initial values to give the instance. Note
	 * that no set<Key> methods will be called. To apply these
	 * methods, call setValues($data) after creation.
	 */
	public function __construct(DatabaseDriverInterface $database, array $values = array()) {
		$this->database = $database;
        foreach ($values as $key => $value) {
            //don't call setRaw as we don't want the modified flag to be
            //set.
            $this->values[$key] = $value;
        }
	}

    public function __sleep()
    {
        return array('values', 'modified', 'stored', 'current_index', 'relation_objects', 'relation_keys');
    }

    public function setRaw($key, $value)
    {
		if (isset(static::$relations[$key])) {
			return $this->setRelation($key, $value);
		}
		//if attempting to update the primary key, keep a copy of the current
		if($key === static::$primary_key && isset($this->values[$key])) {
			$this->current_index = $this->values[$key];
		}
		//maybe apply the modified flag
		if (in_array($key, static::$fields) && !in_array($key, $this->modified)) {
			$this->modified [] = $key;
		}
		$this->values[$key] = $value;
    }

	public function getRaw($key) {
		if (isset(static::$relations[$key])) {
			return $this->getRelation($key);
		}
		if (isset($this->values[$key])) {
			return $this->values[$key];
		}
		return null;
	}

	public function save() {
		if ($this->stored) {
            return $this->update();
		}
        return $this->insert();
	}

	public function delete() {
		if (!isset(static::$primary_key)) {
			throw new \Exception('Can\'t delete with no index key');
		}
		$q = $this->database->delete();
		$q->from(static::$table);
		if (!isset($this->values[static::$primary_key])) {
			throw new \Exception('Can\'t delete with no index key');
		}
		$q->where(static::$primary_key. " =", '?');
		$stmt = $q->prepare();
		if($this->current_index) {
			$index = $this->current_index;
		} else {
			$index = $this->values[static::$primary_key];
		}
		if ($stmt->execute(array($index))) {
			return true;
		}
		return false;
	}

	public function update() {
		if (empty($this->modified)) {
            return true;
        }
        if (!isset($this->values[static::$primary_key])) {
            throw new \Exception('Can\'t update with no index key');
        }
        $q = $this->database->update();
        $q->tables(static::$table);
        $q->fields($this->modified);
        $q->where(static::$primary_key . ' = ?');
        $stmt = $q->prepare();
        $values = array();
        foreach ($this->modified as $modified) {
            $values[] = $this->values[$modified];
        }
        if($this->current_index) {
            $index = $this->current_index;
        } else {
            $index = $this->values[static::$primary_key];
{'fraction_non_alphanumeric': 0.16885743174924167, 'fraction_numerical': 0.0, 'mean_word_length': 3.1189767995240927, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 91, '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': '5093182', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2118, 'n_words': 524}