1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\ActiveRecord;
use ICanBoogie\Accessor\AccessorTrait;
/**
* A database statement.
*
* @property-read array $all An array with the matching records.
* @property-read array $pairs An array of key/value pairs, where _key_ is the value of the first
* column and _value_ the value of the second column.
* @property-read mixed $one The first matching record.
* @property-read string $rc The value of the first column of the first row.
*/
class Statement extends \PDOStatement
{
use AccessorTrait;
/**
* The database connection that created this statement.
*
* @var Connection
*/
public $connection;
/**
* Alias of {@link execute()}.
*
* The arguments can be provided as an array or a list of arguments:
*
* $statement(1, 2, 3);
* $statement([ 1, 2, 3 ]);
*/
public function __invoke()
{
$args = func_get_args();
if ($args && is_array($args[0]))
{
$args = $args[0];
}
return $this->execute($args);
}
/**
* Return the {@link queryString} property of the statement.
*/
public function __toString()
{
return $this->queryString;
}
/**
* Executes the statement.
*
* The connection queries count is incremented.
*
* @inheritdoc
*
* @throws StatementNotValid when the execution of the statement fails.
*/
public function execute($args = [])
{
$start = microtime(true);
if (!empty($this->connection))
{
$this->connection->queries_count++;
}
try
{
$this->connection->profiling[] = [ $start, microtime(true), $this->queryString . ' ' . json_encode($args) ];
return parent::execute($args);
}
catch (\PDOException $e)
{
throw new StatementNotValid([ $this, $args ], 500, $e);
}
}
/**
* Set the fetch mode for the statement.
*
* @param mixed $mode
*
* @return Statement Return the instance.
*
* @throws UnableToSetFetchMode if the mode cannot be set.
*
* @see http://www.php.net/manual/en/pdostatement.setfetchmode.php
*/
public function mode($mode)
{
$mode = func_get_args();
if (!call_user_func_array([ $this, 'setFetchMode' ], $mode))
{
throw new UnableToSetFetchMode($mode);
}
return $this;
}
/**
* Fetches the first row of the result set and closes the cursor.
*
* @param int $fetch_style
* @param int $cursor_orientation
* @param int $cursor_offset
*
* @return mixed
*
* @see PDOStatement::fetch()
*/
public function fetchAndClose($fetch_style = \PDO::FETCH_BOTH, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
{
$args = func_get_args();
$rc = call_user_func_array([ $this, 'fetch' ], $args);
$this->closeCursor();
return $rc;
}
/**
* Alias for `fetchAndClose()`.
*/
protected function get_one()
{
return $this->fetchAndClose();
}
/**
* Fetches a column of the first row of the result set and closes the cursor.
*
* @param int $column_number
*
* @return string
*
* @see PDOStatement::fetchColumn()
*/
public function fetchColumnAndClose($column_number = 0)
{
$rc = $this->fetchColumn($column_number);
$this->closeCursor();
return $rc;
}
/**
* Alias for `fetchColumnAndClose()`.
*/
protected function get_rc()
{
return $this->fetchColumnAndClose();
}
/**
* Alias for {@link \PDOStatement::fetchAll()}
*/
public function all($fetch_style=null, $column_index=null, array $ctor_args=null)
{
return call_user_func_array([ $this, 'fetchAll' ], func_get_args());
}
/**
* Alias for `all()`.
*/
protected function get_all()
{
return $this->fetchAll();
}
/**
* Alias for `all(\PDO::FETCH_KEY_PAIR`).
*
* @return array An array of key/value pairs, where _key_ is the value of the first
* column and _value_ the value of the second column.
*/
protected function get_pairs()
{
return $this->fetchAll(\PDO::FETCH_KEY_PAIR);
}
}