Add setFetchMode() to DB\AbstractStatement #9
This commit is contained in:
parent
2073aceed7
commit
0b5e9b77f0
3 changed files with 75 additions and 13 deletions
|
@ -24,11 +24,54 @@ abstract class AbstractStatement extends DBStatement
|
|||
*/
|
||||
protected $columnsType;
|
||||
|
||||
/**
|
||||
* Режим выборки установленный через setFetchMode()
|
||||
* @var int
|
||||
*/
|
||||
protected $fetchMode;
|
||||
|
||||
/**
|
||||
* colno, class или object из setFetchMode()
|
||||
* @var mixed
|
||||
*/
|
||||
protected $fetchArg;
|
||||
|
||||
/**
|
||||
* constructorArgs из setFetchMode()
|
||||
* @var array
|
||||
*/
|
||||
protected $ctorArgs;
|
||||
|
||||
abstract public function getColumnsType(): array;
|
||||
abstract protected function convToBoolean(/* mixed */ $value): bool;
|
||||
|
||||
protected function dbSetFetchMode(int $mode, ...$args): bool
|
||||
{
|
||||
$this->fetchMode = $mode;
|
||||
$this->fetchArg = null;
|
||||
$this->ctorArgs = null;
|
||||
|
||||
switch ($mode) {
|
||||
case PDO::FETCH_CLASS:
|
||||
$this->ctorArgs = $args[1] ?? null;
|
||||
case PDO::FETCH_COLUMN:
|
||||
case PDO::FETCH_INTO:
|
||||
$this->fetchArg = $args[0];
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::setFetchMode($mode, ...$args);
|
||||
}
|
||||
|
||||
protected function dbFetch(int $mode, int $cursorOrientation, int $cursorOffset) /* : mixed */
|
||||
{
|
||||
if (0 === $mode) {
|
||||
$mode = $this->fetchMode ?? 0;
|
||||
$colNum = $this->fetchArg ?? 0;
|
||||
} else {
|
||||
$colNum = 0;
|
||||
}
|
||||
|
||||
$data = parent::fetch(
|
||||
PDO::FETCH_COLUMN === $mode ? PDO::FETCH_NUM : $mode,
|
||||
$cursorOrientation,
|
||||
|
@ -66,7 +109,7 @@ abstract class AbstractStatement extends DBStatement
|
|||
unset($value);
|
||||
|
||||
if (PDO::FETCH_COLUMN === $mode) {
|
||||
$data = $data[0];
|
||||
$data = $data[$colNum];
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
|
|
@ -18,13 +18,18 @@ use PDO;
|
|||
*/
|
||||
class SqliteStatement extends AbstractSqliteStatement
|
||||
{
|
||||
public function fetch(int $mode = PDO::FETCH_DEFAULT, int $cursorOrientation = PDO::FETCH_ORI_NEXT, int $cursorOffset = 0): mixed
|
||||
public function fetch(int $mode = PDO::FETCH_DEFAULT, int $orientation = PDO::FETCH_ORI_NEXT, int $offset = 0): mixed
|
||||
{
|
||||
return $this->dbFetch($mode, $cursorOrientation, $cursorOffset);
|
||||
return $this->dbFetch($mode, $orientation, $offset);
|
||||
}
|
||||
|
||||
public function fetchAll(int $mode = PDO::FETCH_DEFAULT, ...$args): array
|
||||
{
|
||||
return $this->dbFetchAll($mode, ...$args);
|
||||
}
|
||||
|
||||
public function setFetchMode(int $mode, ...$args): bool
|
||||
{
|
||||
return $this->dbSetFetchMode($mode, ...$args);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,19 +27,33 @@ class SqliteStatement7 extends AbstractSqliteStatement
|
|||
return $this->dbFetch($mode, $orientation, $offset);
|
||||
}
|
||||
|
||||
public function fetchAll($mode = null, $fetch_argument = null, $ctor_args = null)
|
||||
public function fetchAll($mode = null, $fetchArg = null, $ctorArgs = null)
|
||||
{
|
||||
$mode = $mode ?? 0;
|
||||
$args = [];
|
||||
|
||||
if (isset($fetch_argument)) {
|
||||
$args[] = $fetch_argument;
|
||||
|
||||
if (isset($ctor_args)) {
|
||||
$args[] = $ctor_args;
|
||||
}
|
||||
}
|
||||
$args = $this->returnArgs($fetchArg, $ctorArgs);
|
||||
|
||||
return $this->dbFetchAll($mode, ...$args);
|
||||
}
|
||||
|
||||
public function setFetchMode($mode, $fetchArg = null, $ctorArgs = null): bool
|
||||
{
|
||||
$args = $this->returnArgs($fetchArg, $ctorArgs);
|
||||
|
||||
return $this->dbSetFetchMode($mode, ...$args);
|
||||
}
|
||||
|
||||
protected function returnArgs($fetchArg, $ctorArgs): array
|
||||
{
|
||||
$args = [];
|
||||
|
||||
if (isset($fetchArg)) {
|
||||
$args[] = $fetchArg;
|
||||
|
||||
if (isset($ctorArgs)) {
|
||||
$args[] = $ctorArgs;
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue