<commit_before><?php
/**
 * ORM/SimpleORM.php
 *
 * @author Jérémy 'Jejem' Desvages <<EMAIL>>
 * @copyright Jérémy 'Jejem' Desvages
 * @license The MIT License (MIT)
 * @version 1.1.0
**/

namespace Phyrexia\ORM;

use Phyrexia\SQL\SimpleSQL;

abstract class SimpleORM {
	protected static $table;

	public static function load($id) {
		$that = get_called_class();

		$SQL = SimpleSQL::getInstance();

		$SQL->doQuery('SELECT * FROM @1 WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $id);
		if ($SQL->numRows() == 0)
			return false;

		$row = $SQL->fetchResult();

		$object = new $that();
		foreach ($row as $k => $v)
			$object->$k = $v;

		return $object;
	}

	public function save() {
		$that = get_called_class();

		$SQL = SimpleSQL::getInstance();

		if (is_null($this->id) || ! $that::exists($this->id)) {
			$SQL->doQuery('INSERT INTO @1 (@2) VALUES (%3)', $that::$table, 'id', ((! is_null($this->id))?$this->id:''));
			if (is_null($this->id))
				$this->id = $SQL->insertID();
		}

		$SQL->doQuery('SHOW COLUMNS FROM @1', $that::$table);
		$rows = $SQL->fetchAllResults();

		$elements = array();
		foreach ($rows as $row)
			$elements[] = '`'.mysqli_real_escape_string($SQL->getLink(), $row['Field']).'`="'.mysqli_real_escape_string($SQL->getLink(), $this->$row['Field']).'"';

		$SQL->doQuery('UPDATE @1 SET '.implode(',', $elements).' WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $this->id);

		return $this;
	}

	public static function exists($id) {
		$that = get_called_class();

		$SQL = SimpleSQL::getInstance();

		$SQL->doQuery('SELECT 1 FROM @1 WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $id);
		if ($SQL->numRows() != 1)
			return false;

		return true;
	}

	public function delete() {
		$that = get_called_class();

		if (! $that::exists($this->id))
			return false;

		$SQL = SimpleSQL::getInstance();

		$SQL->doQuery('DELETE FROM @1 WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $this->id);

		return true;
	}
}<commit_msg>Adding Memcached support to provide caching (and reduce SQL queries :))<commit_after><?php
/**
 * ORM/SimpleORM.php
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME>' Desvages
 * @license The MIT License (MIT)
 * @version 1.1.0
**/

namespace Phyrexia\ORM;

use Phyrexia\SQL\SimpleSQL;
use StarterKit\Cache\Memcached;

abstract class SimpleORM {
	protected static $table;
	public static $mc;

	public static function load($id) {
		$that = get_called_class();

		if (isset(self::$mc) && is_object(self::$mc) && get_class(self::$mc) == 'Memcached') {
			$ret = self::$mc->get($that.'_'.$that::$table.'_'.$id);

			if ($ret && is_object($ret) && get_class($ret) == $that)
				return $ret;
		}

		$SQL = SimpleSQL::getInstance();

		$SQL->doQuery('SELECT * FROM @1 WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $id);
		if ($SQL->numRows() == 0)
			return false;

		$row = $SQL->fetchResult();

		$object = new $that();
		foreach ($row as $k => $v)
			$object->$k = $v;

		if (isset(self::$mc) && is_object(self::$mc) && get_class(self::$mc) == 'Memcached')
			self::$mc->add($that.'_'.$that::$table.'_'.$id, $object);

		return $object;
	}

	public function save() {
		$that = get_called_class();

		$SQL = SimpleSQL::getInstance();

		if (is_null($this->id) || ! $that::exists($this->id)) {
			$SQL->doQuery('INSERT INTO @1 (@2) VALUES (%3)', $that::$table, 'id', ((! is_null($this->id))?$this->id:''));
			if (is_null($this->id))
				$this->id = $SQL->insertID();
		}

		$SQL->doQuery('SHOW COLUMNS FROM @1', $that::$table);
		$rows = $SQL->fetchAllResults();

		$elements = array();
		foreach ($rows as $row)
			$elements[] = '`'.mysqli_real_escape_string($SQL->getLink(), $row['Field']).'`="'.mysqli_real_escape_string($SQL->getLink(), $this->$row['Field']).'"';

		$SQL->doQuery('UPDATE @1 SET '.implode(',', $elements).' WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $this->id);

		if (isset(self::$mc) && is_object(self::$mc) && get_class(self::$mc) == 'Memcached')
			self::$mc->set($that.'_'.$that::$table.'_'.$this->id, $this);

		return $this;
	}

	public static function exists($id) {
		$that = get_called_class();

		$SQL = SimpleSQL::getInstance();

		$SQL->doQuery('SELECT 1 FROM @1 WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $id);
		if ($SQL->numRows() != 1)
			return false;

		return true;
	}

	public function delete() {
		$that = get_called_class();

		if (! $that::exists($this->id))
			return false;

		$SQL = SimpleSQL::getInstance();

		$SQL->doQuery('DELETE FROM @1 WHERE @2 = %3 LIMIT 1', $that::$table, 'id', $this->id);

		if (isset(self::$mc) && is_object(self::$mc) && get_class(self::$mc) == 'Memcached')
			self::$mc->delete($that.'_'.$that::$table.'_'.$this->id);

		return true;
	}
}
{'fraction_non_alphanumeric': 0.23680823680823682, 'fraction_numerical': 0.011154011154011155, 'mean_word_length': 4.298863636363636, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, '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': '1154881', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1751, 'n_words': 390}