Skip to content

Commit 22afe08

Browse files
committed
Añade soporte para obtención de consultas con bindings en Query Builders (getQueryWithBindings) y refactoriza métodos en Table, TableEntity, y EntityManager para retornar resultados consistentes y respetar campos reservados.
1 parent 509640d commit 22afe08

File tree

8 files changed

+66
-15
lines changed

8 files changed

+66
-15
lines changed

src/Connection/PDOConnection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhobosFramework\Database\Connection;
44

5+
use Exception;
56
use PDO;
67
use PDOStatement;
78
use PDOException;
@@ -84,14 +85,15 @@ public function getPDO(): PDO {
8485

8586
/**
8687
* {@inheritdoc}
88+
* @throws QueryException
8789
*/
8890
public function execute(string $sql, array $params = []): PDOStatement {
8991
try {
9092
$pdo = $this->getPDO();
9193
$stmt = $pdo->prepare($sql);
92-
$stmt->execute($params);
94+
$_ = $stmt->execute($params);
9395
return $stmt;
94-
} catch (PDOException $e) {
96+
} catch (Exception $e) {
9597
throw new QueryException(
9698
"Query execution failed: {$e->getMessage()}",
9799
(int)$e->getCode(),

src/Entity/EntityManager.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ abstract class EntityManager implements EntityInterface {
4242
*/
4343
protected array $_dirty = [];
4444

45+
/**
46+
* Campos reservados (no se pueden modificar)
47+
*/
48+
protected array $_reserved = [
49+
'schema',
50+
'entity',
51+
'pk'
52+
];
53+
4554
/**
4655
* {@inheritdoc}
4756
*/
@@ -160,7 +169,9 @@ public function toArray(bool $onlyDirty = false): array {
160169
continue;
161170
}
162171

163-
$data[$name] = $this->$name;
172+
if(!in_array($name, $this->_reserved)) {
173+
$data[$name] = $this->$name;
174+
}
164175
}
165176

166177
return $data;

src/Entity/Table.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function find(
2929
?int $limitFrom = null,
3030
?int $limitTo = null,
3131
bool $dryRun = false
32-
): array|string;
32+
): array;
3333

3434
/**
3535
* Busca el primer registro que coincida
@@ -43,7 +43,7 @@ public static function findFirst(
4343
array $where = [],
4444
string|array|null $order = null,
4545
bool $dryRun = false
46-
): static|null|string;
46+
): static|null|array;
4747

4848
/**
4949
* Elimina registros
@@ -57,7 +57,7 @@ public static function delete(
5757
array $where,
5858
?int $limit = null,
5959
bool $dryRun = false
60-
): int|string;
60+
): int|array;
6161

6262
/**
6363
* Guarda el registro (INSERT o UPDATE según corresponda)

src/Entity/TableEntity.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function find(
3333
?int $limitFrom = null,
3434
?int $limitTo = null,
3535
bool $dryRun = false
36-
): array|string {
36+
): array {
3737
$qb = static::query()
3838
->select('*')
3939
->from(static::getIdentification());
@@ -51,7 +51,7 @@ public static function find(
5151
}
5252

5353
if ($dryRun) {
54-
return $qb->getQuery();
54+
return $qb->getQueryWithBindings();
5555
}
5656

5757
$rows = $qb->fetch();
@@ -65,7 +65,7 @@ public static function findFirst(
6565
array $where = [],
6666
string|array|null $order = null,
6767
bool $dryRun = false
68-
): static|null|string {
68+
): static|null|array {
6969
$qb = static::query()
7070
->select('*')
7171
->from(static::getIdentification())
@@ -80,7 +80,7 @@ public static function findFirst(
8080
}
8181

8282
if ($dryRun) {
83-
return $qb->getQuery();
83+
return $qb->getQueryWithBindings();
8484
}
8585

8686
$row = $qb->fetchFirst();
@@ -122,7 +122,7 @@ public static function delete(
122122
array $where,
123123
?int $limit = null,
124124
bool $dryRun = false
125-
): int|string {
125+
): int|array {
126126
$deleteQuery = (new DeleteQuery(static::getConnection()))
127127
->from(static::getIdentification())
128128
->where($where);
@@ -132,7 +132,7 @@ public static function delete(
132132
}
133133

134134
if ($dryRun) {
135-
return $deleteQuery->getQuery();
135+
return $deleteQuery->getQueryWithBindings();
136136
}
137137

138138
return $deleteQuery->execute();
@@ -160,7 +160,6 @@ public function save(): bool {
160160
*/
161161
protected function performInsert(): bool {
162162
$data = $this->toArray();
163-
164163
// Remover PKs auto-increment que estén vacías
165164
foreach (static::getPrimaryKey() as $pk) {
166165
if (empty($data[$pk])) {

src/QueryBuilder/DeleteQuery.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function getQuery(): string {
6363
return $sql;
6464
}
6565

66+
/**
67+
* Genera el SQL con los bindings
68+
*/
69+
public function getQueryWithBindings(): array {
70+
return [
71+
'query' => $this->getQuery(),
72+
'bindings' => $this->getBindings()
73+
];
74+
}
75+
6676
/**
6777
* Obtiene los bindings
6878
*/

src/QueryBuilder/InsertQuery.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public function getQuery(): string {
6868
return $this->getSingleInsertQuery();
6969
}
7070

71+
/**
72+
* Genera el SQL con los bindings
73+
*/
74+
public function getQueryWithBindings(): array {
75+
return [
76+
'query' => $this->getQuery(),
77+
'bindings' => $this->getBindings()
78+
];
79+
}
80+
7181
/**
7282
* Genera SQL para insert simple
7383
*/

src/QueryBuilder/QueryBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ public function fetchColumn(string|int $column = 0): mixed {
199199
return $result[$column] ?? null;
200200
}
201201

202+
public function getQueryWithBindings(): array {
203+
return [
204+
'query' => $this->getQuery(),
205+
'bindings' => $this->getBindings()
206+
];
207+
}
208+
202209
public function getQuery(): string {
203210
$parts = [];
204211

src/QueryBuilder/UpdateQuery.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ public function getQuery(): string {
5454
}
5555
$setStr = implode(', ', $setParts);
5656

57-
/** @noinspection SqlNoDataSourceInspection */
58-
/** @noinspection SqlWithoutWhere */
57+
/**
58+
* @noinspection SqlNoDataSourceInspection
59+
* @noinspection SqlWithoutWhere
60+
*/
5961
$sql = "UPDATE {$this->table} SET {$setStr}";
6062

6163
if ($this->whereClause->hasConditions()) {
@@ -65,6 +67,16 @@ public function getQuery(): string {
6567
return $sql;
6668
}
6769

70+
/**
71+
* Genera el SQL con los bindings
72+
*/
73+
public function getQueryWithBindings(): array {
74+
return [
75+
'query' => $this->getQuery(),
76+
'bindings' => $this->getBindings()
77+
];
78+
}
79+
6880
/**
6981
* Obtiene los bindings
7082
*/

0 commit comments

Comments
 (0)