77use Cake \Database \StatementInterface ;
88use Cake \ORM \Table ;
99
10- class StatementQuery
10+ /**
11+ * Wrapper around a prepared SQL statement that executes it
12+ * and hydrates the result set into CakePHP entities using
13+ * a mapping strategy inferred from column aliases.
14+ *
15+ * This class is created via `prepareNativeStatement()` and
16+ * `mapNativeStatement()` in the `NativeSQLMapperTrait`.
17+ */
18+ class NativeQueryResultMapper
1119{
20+ /**
21+ * The root table used to determine entity classes,
22+ * associations, and hydration rules.
23+ *
24+ * @var \Cake\ORM\Table
25+ */
1226 protected Table $ rootTable ;
27+
28+ /**
29+ * The prepared PDO statement to be executed.
30+ *
31+ * @var \Cake\Database\StatementInterface
32+ */
1333 protected StatementInterface $ stmt ;
34+
35+ /**
36+ * Whether the statement has already been executed.
37+ *
38+ * @var bool
39+ */
1440 protected bool $ isExecuted ;
1541
1642 /**
17- * @var mixed[]|null
43+ * Custom mapping strategy used to hydrate entities.
44+ * If null, a MappingStrategy will be automatically built
45+ * based on detected column aliases.
46+ *
47+ * @var array<string,mixed>|null
1848 */
1949 protected $ mapStrategy = null ;
2050
51+ /**
52+ * Constructor.
53+ *
54+ * @param \Cake\ORM\Table $rootTable The root table instance.
55+ * @param \Cake\Database\StatementInterface $stmt The prepared statement.
56+ */
2157 public function __construct (Table $ rootTable , StatementInterface $ stmt )
2258 {
2359 $ this ->rootTable = $ rootTable ;
@@ -26,21 +62,26 @@ public function __construct(Table $rootTable, StatementInterface $stmt)
2662 }
2763
2864 /**
29- * Provide a custom mapping strategy.
65+ * Provide a custom mapping strategy instead of relying
66+ * on automatic alias inference.
67+ *
68+ * The structure must match the output of MappingStrategy::toArray().
3069 *
31- * @param mixed[] $strategy
70+ * @param array<string, mixed> $strategy Mapping configuration.
3271 * @return $this
3372 */
34- public function mapStrategy (array $ strategy ): self
73+ public function setMappingStrategy (array $ strategy ): self
3574 {
3675 $ this ->mapStrategy = $ strategy ;
3776 return $ this ;
3877 }
3978
4079 /**
41- * Execute and hydrate results.
80+ * Execute the SQL statement if not executed yet, fetch all rows,
81+ * build (or use) the mapping strategy, and hydrate the result set
82+ * into entities.
4283 *
43- * @return \Cake\Datasource\EntityInterface[]
84+ * @return \Cake\Datasource\EntityInterface[] Hydrated entity list.
4485 */
4586 public function all (): array
4687 {
@@ -64,10 +105,17 @@ public function all(): array
64105 }
65106
66107 /**
67- * Extracts aliases of the columns from the query's result set.
108+ * Extract column aliases used in the SQL result set.
109+ *
110+ * Each column must follow `{Alias}__{column}` format.
111+ * Throws UnknownAliasException if the alias format is invalid.
112+ *
113+ * @param array<int,array<string,mixed>|mixed> $rows Result set rows.
114+ * @return string[] Sorted list of unique aliases.
68115 *
69- * @param mixed[] $rows Result set rows.
70- * @return string[]
116+ * @throws \InvalidArgumentException If the first row is not an array.
117+ * @throws \Bancer\NativeQueryMapper\ORM\UnknownAliasException
118+ * If a column does not follow expected alias format.
71119 */
72120 protected function extractAliases (array $ rows ): array
73121 {
0 commit comments