1 | <?php declare(strict_types=1); |
||
16 | class Database implements DatabaseInterface |
||
17 | { |
||
18 | /** Table name prefix */ |
||
19 | public static $prefix = ''; |
||
20 | |||
21 | /** @var \PDO Database driver */ |
||
22 | protected $driver; |
||
23 | |||
24 | /** @var SQLBuilder */ |
||
25 | protected $sqlBuilder; |
||
26 | |||
27 | /** |
||
28 | * Database constructor. |
||
29 | * |
||
30 | * @param \PDO $driver |
||
31 | * |
||
32 | * @\samsonframework\containerannotation\InjectArgument(driver="\PDO") |
||
33 | * @\samsonframework\containerannotation\InjectArgument(sqlBuilder="\samsonframework\orm\SQLBuilder") |
||
34 | */ |
||
35 | public function __construct(\PDO $driver, SQLBuilder $sqlBuilder) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function execute(string $sql) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function insert(TableMetadata $tableMetadata, array $columnValues) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function update(TableMetadata $tableMetadata, array $columnValues, Condition $condition) |
||
72 | |||
73 | /** |
||
74 | * Execute SQL query. |
||
75 | * |
||
76 | * @param string $sql SQL statement |
||
77 | * |
||
78 | * @deprecated Use self::execute() |
||
79 | * @return mixed Driver result |
||
80 | */ |
||
81 | public function fetch(string $sql) |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function fetchArray(string $sql) : array |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function database() : string |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function count(string $sql) : int |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function fetchObjects(string $sql, string $className, string $primaryField) : array |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function fetchColumn(string $sql, int $columnIndex) : array |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function fetchObjectsWithJoin(string $sql, TableMetadata $metadata, array $joinedMetadata) : array |
||
145 | |||
146 | /** |
||
147 | * Create entity instances and its joined entities. |
||
148 | * |
||
149 | * @param array $rows |
||
150 | * @param TableMetadata $metadata |
||
151 | * @param TableMetadata[] $joinedMetadata |
||
152 | * |
||
153 | * @return array |
||
154 | * @throws \InvalidArgumentException |
||
155 | */ |
||
156 | protected function createEntities(array $rows, TableMetadata $metadata, array $joinedMetadata = []) |
||
192 | |||
193 | /** |
||
194 | * Regroup database rows by primary field value. |
||
195 | * |
||
196 | * @param array $rows Collection of records received from database |
||
197 | * @param string $primaryField Primary field name for grouping |
||
198 | * |
||
199 | * @return array Grouped rows by primary field value |
||
200 | */ |
||
201 | protected function groupResults(array $rows, string $primaryField) : array |
||
216 | |||
217 | /** |
||
218 | * Fill entity instance fields from row column values according to entity value attributes. |
||
219 | * |
||
220 | * @param mixed $instance Entity instance |
||
221 | * @param array $attributes Metadata entity attributes |
||
222 | * @param array $row Database results row |
||
223 | */ |
||
224 | protected function fillEntityFieldValues($instance, array $attributes, array $row) |
||
238 | |||
239 | /** |
||
240 | * Quote variable for security reasons. |
||
241 | * |
||
242 | * @param string $value |
||
243 | * |
||
244 | * @return string Quoted value |
||
245 | */ |
||
246 | protected function quote($value) |
||
250 | } |
||
251 |