QueryStatement::getColumnMeta()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 1
nop 1
1
<?php
2
namespace Kir\MySQL\Builder;
3
4
use Kir\MySQL\Database\DatabaseStatement;
5
use Kir\MySQL\Databases\MySQL\MySQLExceptionInterpreter;
6
use Kir\MySQL\Exceptions\SqlException;
7
use Kir\MySQL\QueryLogger\QueryLoggers;
8
use PDO;
9
use PDOException;
10
use PDOStatement;
11
12
class QueryStatement implements DatabaseStatement {
13
	/** @var PDOStatement<mixed> */
14
	private $statement;
15
	/** @var QueryLoggers */
16
	private $queryLoggers;
17
	/** @var string */
18
	private $query;
19
	/** @var MySQLExceptionInterpreter */
20
	private $exceptionInterpreter;
21
22
	/**
23
	 * @param PDOStatement<mixed> $stmt
24
	 * @param string $query
25
	 * @param MySQLExceptionInterpreter $exceptionInterpreter
26
	 * @param QueryLoggers $queryLoggers
27
	 */
28
	public function __construct(PDOStatement $stmt, string $query, MySQLExceptionInterpreter $exceptionInterpreter, QueryLoggers $queryLoggers) {
29
		$this->statement = $stmt;
30
		$this->queryLoggers = $queryLoggers;
31
		$this->query = $query;
32
		$this->exceptionInterpreter = $exceptionInterpreter;
33
	}
34
35
	/**
36
	 * @return PDOStatement<mixed>
37
	 */
38
	public function getStatement(): PDOStatement {
39
		return $this->statement;
40
	}
41
42
	/**
43
	 * @param int $mode
44
	 * @param mixed $arg0
45
	 * @param array<mixed, mixed>|null $arg1
46
	 * @return $this
47
	 */
48
	public function setFetchMode(int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?array $arg1 = null) {
49
		$args = [$mode];
50
		if($arg0 !== null) {
51
			$args[] = $arg0;
52
		}
53
		if($arg1 !== null) {
54
			$args[] = $arg1;
55
		}
56
		$this->statement->setFetchMode(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $mode of PDOStatement::setFetchMode() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
		$this->statement->setFetchMode(/** @scrutinizer ignore-type */ ...$args);
Loading history...
57
		return $this;
58
	}
59
60
	/**
61
	 * @param array<string, mixed> $params
62
	 * @throws SqlException
63
	 * @return $this
64
	 */
65
	public function execute(array $params = []) {
66
		$this->exceptionHandler(function() use ($params) {
67
			$this->queryLoggers->logRegion($this->query, function() use ($params) {
68
				$response = $this->statement->execute($params);
69
				if(!$response) {
70
					throw new SqlException('Execution returned with "false".');
71
				}
72
			});
73
		});
74
		return $this;
75
	}
76
77
	/**
78
	 * @param int $fetchStyle
79
	 * @param mixed|null $fetchArgument
80
	 * @param mixed[] $ctorArgs
81
	 * @return array<mixed, mixed>
82
	 */
83
	public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null, array $ctorArgs = []): array {
84
		$result = $x = $this->exceptionHandler(function() use ($fetchStyle, $fetchArgument, $ctorArgs) {
85
			if($fetchArgument !== null) {
86
				return $this->statement->fetchAll($fetchStyle, $fetchArgument, ...$ctorArgs);
87
			}
88
			return $this->statement->fetchAll($fetchStyle);
89
		});
90
		/** @var array<mixed, mixed>|false $x */
91
		if($x === false) {
92
			return [];
93
		}
94
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return array.
Loading history...
95
	}
96
97
	/**
98
	 * @param int $fetchStyle
99
	 * @param int $cursorOrientation
100
	 * @param int $cursorOffset
101
	 * @return mixed
102
	 */
103
	public function fetch($fetchStyle = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
104
		return $this->exceptionHandler(fn() => $this->statement->fetch($fetchStyle, $cursorOrientation, $cursorOffset));
105
	}
106
107
	/**
108
	 * @param int $columnNo
109
	 * @return mixed
110
	 */
111
	public function fetchColumn($columnNo = 0) {
112
		return $this->exceptionHandler(fn() => $this->statement->fetchColumn($columnNo));
113
	}
114
115
	/**
116
	 * @return bool
117
	 */
118
	public function closeCursor(): bool {
119
		return $this->exceptionHandler(fn() => $this->statement->closeCursor());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptionH...ion(...) { /* ... */ }) returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return boolean.
Loading history...
120
	}
121
122
	/**
123
	 * @return int
124
	 */
125
	public function columnCount(): int {
126
		return $this->exceptionHandler(fn() => $this->statement->columnCount());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptionH...ion(...) { /* ... */ }) returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return integer.
Loading history...
127
	}
128
129
	/**
130
	 * @param int $columnNo
131
	 * @return null|array<string, mixed>
132
	 */
133
	public function getColumnMeta(int $columnNo): ?array {
134
		return $this->exceptionHandler(function() use ($columnNo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptionH...ion(...) { /* ... */ }) returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return array|null.
Loading history...
135
			$columnMeta = $this->statement->getColumnMeta($columnNo);
136
			if($columnMeta === false) {
137
				return null;
138
			}
139
			return $columnMeta;
140
		});
141
	}
142
143
	/**
144
	 * @template T
145
	 * @param callable(): T $fn
146
	 * @return T
147
	 */
148
	private function exceptionHandler(callable $fn) {
149
		try {
150
			return $fn();
151
		} catch (PDOException $exception) {
152
			throw $this->exceptionInterpreter->getMoreConcreteException($exception);
153
		}
154
	}
155
}
156