<commit_before><?php

namespace neptune\model;

use \Iterator;
use \ArrayAccess;
use \Countable;
use neptune\model\DatabaseModel;

/**
 * ModelGroup
 * @author <NAME> <EMAIL>
 * */
class ModelGroup implements Iterator, ArrayAccess, Countable {

	protected $database;
	protected $table;
	protected $primary_key;
	protected $fields = array();
	protected $values = array();
	protected $objects = array();
	protected $modified = array();
	protected $stored = false;
	protected $position = 0;

	public function __construct($database, $table, array $dbobjects = null) {
		$this->database = $database;
		$this->table = $table;
		if ($dbobjects) {
			$this->objects = $dbobjects;
			$this->stored = true;
		}
	}

	public function save($recursive = false) {
		if ($this->stored) {
			if (!empty($this->modified)) {
				$pass = $this->update();
			} else {
				$pass = true;
			}
		} else {
			if($recursive) {
				foreach ($this->objects as $obj) {
					foreach($this->modified as $value) {
						$obj->set($value, $this->values[$value], false);
					}
				}
				$pass = true;
			} else {
				$pass = $this->insert();
			}
		}
		if ($recursive) {
			foreach ($this->objects as $obj) {
				$obj->save();
			}
		}
		return $pass;
	}

	protected function insert() {
		if (!empty($this->modified)) {
			$q = SQLQuery::insert($this->database);
			$q->into($this->table);
			$q->fields($this->modified);
			$values = array();
			foreach ($this->modified as $modified) {
				$values[] = $this->values[$modified];
			}
			$stmt = $q->prepare();
			foreach ($this->objects as $obj) {
				if (!$stmt->execute($values)) {
					return false;
				}
			}
			$this->modified = array();
				$this->modified = array();
				$this->stored = true;
				return true;
		}
		return false;
	}

	protected function update() {
		if (!isset($this->primary_key)) {
			throw new \Exception('Can\'t update with no index key');
		}
		if (!empty($this->modified)) {
			$q = SQLQuery::update($this->database);
			$q->tables($this->table);
			$q->fields($this->modified);
			$values = array();
			foreach ($this->modified as $modified) {
				$values[] = $this->values[$modified];
			}
			$q->where("$this->primary_key =", '?');
			$stmt = $q->prepare();
			foreach ($this->objects as $obj) {
				$key = $this->primary_key;
				if (!isset($obj->$key)) {
					continue;
				}
				$params = $values;
				$params[] = $obj->$key;
				if (!$stmt->execute($params)) {
					return true;
				}
			}
			$this->modified = array();
		}
		return false;
	}

	public function __get($key) {
		return $this->get($key);
	}

	public function get($key, $value) {
		$values = array();
		foreach ($this->objects as $obj) {
			$values[] = $obj->$key;
		}
		return $values;
	}

	public function __set($key, $value) {
		return $this->set($key, $value);
	}

	public function set($key, $value) {
		$this->values[$key] = $value;
		if (in_array($key, $this->fields) && !in_array($key, $this->modified)) {
			$this->modified [] = $key;
		}
	}

	public function setFields(array $fields) {
		$this->fields = $fields;
	}

	public function setPrimaryKey($columnname) {
		$this->primary_key = $columnname;
	}

	public function getValues() {
		$values = array();
		foreach ($this->objects as $obj) {
			$values[] = $obj->getValues();
		}
		return $values;
	}

	public function getTable() {
		return $this->table;
	}

	public function rewind() {
		$this->position = 0;
	}

	public function current() {
		return $this->objects[$this->position];
	}

	public function key() {
		return $this->position;
	}

	public function next() {
		$this->position++;
	}

	public function valid() {
		return isset($this->objects[$this->position]);
	}

	public function offsetGet($offset) {
		return isset($this->objects[$offset]) ? $this->objects[$offset] : null;
	}

	public function offsetSet($offset, $value) {
		if (!$value instanceof DatabaseModel) {
			return false;
		}
		if (is_null($offset)) {
			$this->objects[] = $value;
		} else {
			$this->objects[$offset] = $value;
		}
	}

	public function offsetExists($offset) {
		return isset($this->objects[$offset]);
	}

	public function offsetUnset($offset) {
		unset($this->objects[$offset]);
	}

	public function count() {
		return count($this->objects);
	}

}

?>
<commit_msg>Removing redundant code in ModelGroup.<commit_after><?php

namespace neptune\model;

use \Iterator;
use \ArrayAccess;
use \Countable;
use neptune\model\DatabaseModel;

/**
 * ModelGroup
 * @author <NAME> <EMAIL>
 * */
class ModelGroup implements Iterator, ArrayAccess, Countable {

	protected $database;
	protected $table;
	protected $primary_key;
	protected $fields = array();
	protected $values = array();
	protected $objects = array();
	protected $modified = array();
	protected $stored = false;
	protected $position = 0;

	public function __construct($database, $table, array $dbobjects = null) {
		$this->database = $database;
		$this->table = $table;
		if ($dbobjects) {
			$this->objects = $dbobjects;
			$this->stored = true;
		}
	}

	public function save($recursive = false) {
		if ($this->stored) {
			if (!empty($this->modified)) {
				$pass = $this->update();
			} else {
				$pass = true;
			}
		} else {
			if($recursive) {
				foreach ($this->objects as $obj) {
					foreach($this->modified as $value) {
						$obj->set($value, $this->values[$value], false);
					}
				}
				$pass = true;
			} else {
				$pass = $this->insert();
			}
		}
		if ($recursive) {
			foreach ($this->objects as $obj) {
				$obj->save();
			}
		}
		return $pass;
	}

	protected function insert() {
		if (!empty($this->modified)) {
			$q = SQLQuery::insert($this->database);
			$q->into($this->table);
			$q->fields($this->modified);
			$values = array();
			foreach ($this->modified as $modified) {
				$values[] = $this->values[$modified];
			}
			$stmt = $q->prepare();
			foreach ($this->objects as $obj) {
				if (!$stmt->execute($values)) {
					return false;
				}
			}
			$this->modified = array();
				$this->modified = array();
				$this->stored = true;
				return true;
		}
		return false;
	}

	protected function update() {
		if (!isset($this->primary_key)) {
			throw new \Exception('Can\'t update with no index key');
		}
		if (!empty($this->modified)) {
			$q = SQLQuery::update($this->database);
			$q->tables($this->table);
			$q->fields($this->modified);
			$values = array();
			foreach ($this->modified as $modified) {
				$values[] = $this->values[$modified];
			}
			$q->where("$this->primary_key =", '?');
			$stmt = $q->prepare();
			foreach ($this->objects as $obj) {
				$key = $this->primary_key;
				if (!isset($obj->$key)) {
					continue;
				}
				$params = $values;
				$params[] = $obj->$key;
				if (!$stmt->execute($params)) {
					return true;
				}
			}
			$this->modified = array();
		}
		return false;
	}

	public function __get($key) {
		return $this->get($key);
	}

	public function get($key) {
		$values = array();
		foreach ($this->objects as $obj) {
			$values[] = $obj->$key;
		}
		return $values;
	}

	public function __set($key, $value) {
		return $this->set($key, $value);
	}

	public function set($key, $value) {
		$this->values[$key] = $value;
		if (in_array($key, $this->fields) && !in_array($key, $this->modified)) {
			$this->modified [] = $key;
		}
	}

	public function setFields(array $fields) {
		$this->fields = $fields;
	}

	public function setPrimaryKey($columnname) {
		$this->primary_key = $columnname;
	}

	public function getValues() {
		$values = array();
		foreach ($this->objects as $obj) {
			$values[] = $obj->getValues();
		}
		return $values;
	}

	public function getTable() {
		return $this->table;
	}

	public function rewind() {
		$this->position = 0;
	}

	public function current() {
		return $this->objects[$this->position];
	}

	public function key() {
		return $this->position;
	}

	public function next() {
		$this->position++;
	}

	public function valid() {
		return isset($this->objects[$this->position]);
	}

	public function offsetGet($offset) {
		return isset($this->objects[$offset]) ? $this->objects[$offset] : null;
	}

	public function offsetSet($offset, $value) {
		if (!$value instanceof DatabaseModel) {
			return false;
		}
		$this->objects[$offset] = $value;
	}

	public function offsetExists($offset) {
		return isset($this->objects[$offset]);
	}

	public function offsetUnset($offset) {
		unset($this->objects[$offset]);
	}

	public function count() {
		return count($this->objects);
	}

}

?>

{'fraction_non_alphanumeric': 0.1910873440285205, 'fraction_numerical': 0.00047534165181224003, 'mean_word_length': 3.3970741901776385, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 154, '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': '2807881', 'n_tokens_mistral': 3318, 'n_tokens_neox': 2855, 'n_words': 636}