update idiorm

This commit is contained in:
Ibnu Maksum 2023-08-23 12:10:16 +07:00
parent cefc6c0365
commit 27a2938534
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5

View File

@ -36,6 +36,80 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*
* The methods documented below are magic methods that conform to PSR-1.
* This documentation exposes these methods to doc generators and IDEs.
* @see http://www.php-fig.org/psr/psr-1/
*
* @method static array|string getConfig($key = null, $connection_name = self::DEFAULT_CONNECTION)
* @method static null resetConfig()
* @method static \ORM forTable($table_name, $connection_name = self::DEFAULT_CONNECTION)
* @method static null setDb($db, $connection_name = self::DEFAULT_CONNECTION)
* @method static null resetDb()
* @method static null setupLimitClauseStyle($connection_name)
* @method static \PDO getDb($connection_name = self::DEFAULT_CONNECTION)
* @method static bool rawExecute($query, $parameters = array())
* @method static \PDOStatement getLastStatement()
* @method static string getLastQuery($connection_name = null)
* @method static array getQueryLog($connection_name = self::DEFAULT_CONNECTION)
* @method array getConnectionNames()
* @method $this useIdColumn($id_column)
* @method \ORM|bool findOne($id=null)
* @method array|\IdiormResultSet findMany()
* @method \IdiormResultSet findResultSet()
* @method array findArray()
* @method $this forceAllDirty()
* @method $this rawQuery($query, $parameters = array())
* @method $this tableAlias($alias)
* @method int countNullIdColumns()
* @method $this selectExpr($expr, $alias=null)
* @method \ORM selectMany($values)
* @method \ORM selectManyExpr($values)
* @method $this rawJoin($table, $constraint, $table_alias, $parameters = array())
* @method $this innerJoin($table, $constraint, $table_alias=null)
* @method $this leftOuterJoin($table, $constraint, $table_alias=null)
* @method $this rightOuterJoin($table, $constraint, $table_alias=null)
* @method $this fullOuterJoin($table, $constraint, $table_alias=null)
* @method $this whereEqual($column_name, $value=null)
* @method $this whereNotEqual($column_name, $value=null)
* @method $this whereIdIs($id)
* @method $this whereAnyIs($values, $operator='=')
* @method array|string whereIdIn($ids)
* @method $this whereLike($column_name, $value=null)
* @method $this whereNotLike($column_name, $value=null)
* @method $this whereGt($column_name, $value=null)
* @method $this whereLt($column_name, $value=null)
* @method $this whereGte($column_name, $value=null)
* @method $this whereLte($column_name, $value=null)
* @method $this whereIn($column_name, $values)
* @method $this whereNotIn($column_name, $values)
* @method $this whereNull($column_name)
* @method $this whereNotNull($column_name)
* @method $this whereRaw($clause, $parameters=array())
* @method $this orderByDesc($column_name)
* @method $this orderByAsc($column_name)
* @method $this orderByExpr($clause)
* @method $this groupBy($column_name)
* @method $this groupByExpr($expr)
* @method $this havingEqual($column_name, $value=null)
* @method $this havingNotEqual($column_name, $value=null)
* @method $this havingIdIs($id)
* @method $this havingLike($column_name, $value=null)
* @method $this havingNotLike($column_name, $value=null)
* @method $this havingGt($column_name, $value=null)
* @method $this havingLt($column_name, $value=null)
* @method $this havingGte($column_name, $value=null)
* @method $this havingLte($column_name, $value=null)
* @method $this havingIn($column_name, $values=null)
* @method $this havingNotIn($column_name, $values=null)
* @method $this havingNull($column_name)
* @method $this havingNotNull($column_name)
* @method $this havingRaw($clause, $parameters=array())
* @method static this clearCache($table_name = null, $connection_name = self::DEFAULT_CONNECTION)
* @method array asArray()
* @method bool setExpr($key, $value = null)
* @method bool isDirty($key)
* @method bool isNew()
*/ */
class ORM implements ArrayAccess { class ORM implements ArrayAccess {
@ -179,7 +253,7 @@ class ORM implements ArrayAccess {
* required to use Idiorm). If you have more than one setting * required to use Idiorm). If you have more than one setting
* you wish to configure, another shortcut is to pass an array * you wish to configure, another shortcut is to pass an array
* of settings (and omit the second argument). * of settings (and omit the second argument).
* @param string $key * @param string|array $key
* @param mixed $value * @param mixed $value
* @param string $connection_name Which connection to use * @param string $connection_name Which connection to use
*/ */
@ -238,12 +312,6 @@ class ORM implements ArrayAccess {
return new self($table_name, array(), $connection_name); return new self($table_name, array(), $connection_name);
} }
public static function execute($query) {
self::_setup_db(self::DEFAULT_CONNECTION);
return self::_execute($query);
}
/** /**
* Set up the database connection used by the class * Set up the database connection used by the class
* @param string $connection_name Which connection to use * @param string $connection_name Which connection to use
@ -293,9 +361,11 @@ class ORM implements ArrayAccess {
} }
/** /**
* Delete all registered PDO objects in _db array. * Close and delete all registered PDO objects in _db array.
*/ */
public static function reset_db() { public static function reset_db() {
self::$_db = null;
self::$_db = array(); self::$_db = array();
} }
@ -462,15 +532,14 @@ class ORM implements ArrayAccess {
self::$_query_log[$connection_name] = array(); self::$_query_log[$connection_name] = array();
} }
// Strip out any non-integer indexes from the parameters if (empty($parameters)) {
foreach($parameters as $key => $value) { $bound_query = $query;
if (!is_int($key)) unset($parameters[$key]); } else {
}
if (count($parameters) > 0) {
// Escape the parameters // Escape the parameters
$parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters); $parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters);
if (array_values($parameters) === $parameters) {
// ? placeholders
// Avoid %format collision for vsprintf // Avoid %format collision for vsprintf
$query = str_replace("%", "%%", $query); $query = str_replace("%", "%%", $query);
@ -484,8 +553,13 @@ class ORM implements ArrayAccess {
// Replace the question marks in the query with the parameters // Replace the question marks in the query with the parameters
$bound_query = vsprintf($query, $parameters); $bound_query = vsprintf($query, $parameters);
} else { } else {
// named placeholders
foreach ($parameters as $key => $val) {
$query = str_replace($key, $val, $query);
}
$bound_query = $query; $bound_query = $query;
} }
}
self::$_last_query = $bound_query; self::$_last_query = $bound_query;
self::$_query_log[$connection_name][] = $bound_query; self::$_query_log[$connection_name][] = $bound_query;
@ -1261,14 +1335,14 @@ class ORM implements ArrayAccess {
$data = array(); $data = array();
$query = array("(("); $query = array("((");
$first = true; $first = true;
foreach ($values as $item) { foreach ($values as $value) {
if ($first) { if ($first) {
$first = false; $first = false;
} else { } else {
$query[] = ") OR ("; $query[] = ") OR (";
} }
$firstsub = true; $firstsub = true;
foreach($item as $key => $item) { foreach($value as $key => $item) {
$op = is_string($operator) ? $operator : (isset($operator[$key]) ? $operator[$key] : '='); $op = is_string($operator) ? $operator : (isset($operator[$key]) ? $operator[$key] : '=');
if ($firstsub) { if ($firstsub) {
$firstsub = false; $firstsub = false;
@ -1281,7 +1355,7 @@ class ORM implements ArrayAccess {
} }
} }
$query[] = "))"; $query[] = "))";
return $this->where_raw(join($query, ' '), $data); return $this->where_raw(join(' ', $query), $data);
} }
/** /**
@ -1475,7 +1549,7 @@ class ORM implements ArrayAccess {
*/ */
public function having_id_is($id) { public function having_id_is($id) {
return (is_array($this->_get_id_column_name())) ? return (is_array($this->_get_id_column_name())) ?
$this->having($this->_get_compound_id_column_values($value)) : $this->having($this->_get_compound_id_column_values($id), null) :
$this->having($this->_get_id_column_name(), $id); $this->having($this->_get_id_column_name(), $id);
} }
@ -1827,6 +1901,7 @@ class ORM implements ArrayAccess {
$cached_result = self::_check_query_cache($cache_key, $this->_table_name, $this->_connection_name); $cached_result = self::_check_query_cache($cache_key, $this->_table_name, $this->_connection_name);
if ($cached_result !== false) { if ($cached_result !== false) {
$this->_reset_idiorm_state();
return $cached_result; return $cached_result;
} }
} }
@ -1843,12 +1918,17 @@ class ORM implements ArrayAccess {
self::_cache_query_result($cache_key, $rows, $this->_table_name, $this->_connection_name); self::_cache_query_result($cache_key, $rows, $this->_table_name, $this->_connection_name);
} }
// reset Idiorm after executing the query $this->_reset_idiorm_state();
return $rows;
}
/**
* Reset the Idiorm instance state
*/
private function _reset_idiorm_state() {
$this->_values = array(); $this->_values = array();
$this->_result_columns = array('*'); $this->_result_columns = array('*');
$this->_using_default_result_columns = true; $this->_using_default_result_columns = true;
return $rows;
} }
/** /**
@ -1970,7 +2050,7 @@ class ORM implements ArrayAccess {
* object was saved. * object was saved.
*/ */
public function is_dirty($key) { public function is_dirty($key) {
return isset($this->_dirty_fields[$key]); return array_key_exists($key, $this->_dirty_fields);
} }
/** /**
@ -2029,7 +2109,7 @@ class ORM implements ArrayAccess {
// if the primary key is compound, assign the last inserted id // if the primary key is compound, assign the last inserted id
// to the first column // to the first column
if (is_array($column)) { if (is_array($column)) {
$column = array_slice($column, 0, 1); $column = reset($column);
} }
$this->_data[$column] = $db->lastInsertId(); $this->_data[$column] = $db->lastInsertId();
} }
@ -2129,14 +2209,17 @@ class ORM implements ArrayAccess {
// --- ArrayAccess --- // // --- ArrayAccess --- //
// --------------------- // // --------------------- //
#[\ReturnTypeWillChange]
public function offsetExists($key) { public function offsetExists($key) {
return array_key_exists($key, $this->_data); return array_key_exists($key, $this->_data);
} }
#[\ReturnTypeWillChange]
public function offsetGet($key) { public function offsetGet($key) {
return $this->get($key); return $this->get($key);
} }
#[\ReturnTypeWillChange]
public function offsetSet($key, $value) { public function offsetSet($key, $value) {
if(is_null($key)) { if(is_null($key)) {
throw new InvalidArgumentException('You must specify a key/array index.'); throw new InvalidArgumentException('You must specify a key/array index.');
@ -2144,6 +2227,7 @@ class ORM implements ArrayAccess {
$this->set($key, $value); $this->set($key, $value);
} }
#[\ReturnTypeWillChange]
public function offsetUnset($key) { public function offsetUnset($key) {
unset($this->_data[$key]); unset($this->_data[$key]);
unset($this->_dirty_fields[$key]); unset($this->_dirty_fields[$key]);
@ -2319,6 +2403,8 @@ class IdiormString {
/** /**
* A result set class for working with collections of model instances * A result set class for working with collections of model instances
* @author Simon Holywell <treffynnon@php.net> * @author Simon Holywell <treffynnon@php.net>
* @method null setResults(array $results)
* @method array getResults()
*/ */
class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Serializable { class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Serializable {
/** /**
@ -2363,6 +2449,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
* Get the number of records in the result set * Get the number of records in the result set
* @return int * @return int
*/ */
#[\ReturnTypeWillChange]
public function count() { public function count() {
return count($this->_results); return count($this->_results);
} }
@ -2372,6 +2459,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
* over the result set. * over the result set.
* @return \ArrayIterator * @return \ArrayIterator
*/ */
#[\ReturnTypeWillChange]
public function getIterator() { public function getIterator() {
return new ArrayIterator($this->_results); return new ArrayIterator($this->_results);
} }
@ -2381,6 +2469,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
* @param int|string $offset * @param int|string $offset
* @return bool * @return bool
*/ */
#[\ReturnTypeWillChange]
public function offsetExists($offset) { public function offsetExists($offset) {
return isset($this->_results[$offset]); return isset($this->_results[$offset]);
} }
@ -2390,6 +2479,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
* @param int|string $offset * @param int|string $offset
* @return mixed * @return mixed
*/ */
#[\ReturnTypeWillChange]
public function offsetGet($offset) { public function offsetGet($offset) {
return $this->_results[$offset]; return $this->_results[$offset];
} }
@ -2399,6 +2489,7 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
* @param int|string $offset * @param int|string $offset
* @param mixed $value * @param mixed $value
*/ */
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value) { public function offsetSet($offset, $value) {
$this->_results[$offset] = $value; $this->_results[$offset] = $value;
} }
@ -2407,10 +2498,19 @@ class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Seri
* ArrayAccess * ArrayAccess
* @param int|string $offset * @param int|string $offset
*/ */
#[\ReturnTypeWillChange]
public function offsetUnset($offset) { public function offsetUnset($offset) {
unset($this->_results[$offset]); unset($this->_results[$offset]);
} }
public function __serialize() {
return $this->serialize();
}
public function __unserialize($data) {
$this->unserialize($data);
}
/** /**
* Serializable * Serializable
* @return string * @return string