Skip to content

Commit 39a78c4

Browse files
committed
Implementa capa de base de datos para el PhobosFramework.
- Añade archivo `composer.json` para definir dependencias y configuración del paquete. - Introduce una colección de helpers generales para la gestión de conexiones, transacciones y consultas. - Define interfaces y clases relacionadas con los Query Builders (`QueryBuilderInterface`, cláusulas SQL básicas como `WhereClause`, `LimitClause`, etc.). - Implementa excepciones específicas para la capa de base de datos (`DatabaseException`, `ConfigurationException`, `UnsupportedDriverException`). - Añade soporte para la generación dinámica de SQL en componentes como `SelectClause`, `JoinClause`, entre otros.
0 parents  commit 39a78c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4091
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Composer
2+
composer.phar
3+
/vendor/
4+
composer.lock
5+
6+
# IntelliJ project files
7+
.idea
8+
*.iml
9+
out
10+
gen
11+
12+
# others
13+
utils.md

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Marcel Rojas <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "mongoose-studios/phobos-framework-database",
3+
"description": "Phobos PHP Framework Database Layer",
4+
"version": "3.0.1",
5+
"type": "library",
6+
"license": "MIT",
7+
"require": {
8+
"php": ">=8.3",
9+
"ext-pdo": "*",
10+
"mongoose-studios/phobos-framework": "3.0.2"
11+
},
12+
"authors": [
13+
{
14+
"name": "Marcel Rojas",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"PhobosFramework\\Database\\": "src/"
21+
},
22+
"files": [
23+
"src/helpers.php"
24+
]
25+
},
26+
"repositories": [
27+
{
28+
"type": "path",
29+
"url": "../PhobosFramework"
30+
}
31+
]
32+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace PhobosFramework\Database\Connection;
4+
5+
use PDO;
6+
use PDOStatement;
7+
use PhobosFramework\Database\Drivers\DriverInterface;
8+
9+
/**
10+
* Interface para manejar conexiones a base de datos
11+
*/
12+
interface ConnectionInterface {
13+
/**
14+
* Conecta a la base de datos
15+
*
16+
* @return void
17+
*/
18+
public function connect(): void;
19+
20+
/**
21+
* Desconecta de la base de datos
22+
*
23+
* @return void
24+
*/
25+
public function disconnect(): void;
26+
27+
/**
28+
* Verifica si está conectado
29+
*
30+
* @return bool
31+
*/
32+
public function isConnected(): bool;
33+
34+
/**
35+
* Obtiene la instancia PDO
36+
*
37+
* @return PDO
38+
*/
39+
public function getPDO(): PDO;
40+
41+
/**
42+
* Ejecuta una query y retorna el statement
43+
*
44+
* @param string $sql SQL a ejecutar
45+
* @param array $params Parámetros para binding
46+
* @return PDOStatement
47+
*/
48+
public function execute(string $sql, array $params = []): PDOStatement;
49+
50+
/**
51+
* Ejecuta una query y retorna todas las filas
52+
*
53+
* @param string $sql SQL a ejecutar
54+
* @param array $params Parámetros para binding
55+
* @return array
56+
*/
57+
public function query(string $sql, array $params = []): array;
58+
59+
/**
60+
* Ejecuta una query y retorna la primera fila
61+
*
62+
* @param string $sql SQL a ejecutar
63+
* @param array $params Parámetros para binding
64+
* @return array|null
65+
*/
66+
public function queryFirst(string $sql, array $params = []): ?array;
67+
68+
/**
69+
* Inicia una transacción
70+
*
71+
* @return bool
72+
*/
73+
public function beginTransaction(): bool;
74+
75+
/**
76+
* Commitea una transacción
77+
*
78+
* @return bool
79+
*/
80+
public function commit(): bool;
81+
82+
/**
83+
* Hace rollback de una transacción
84+
*
85+
* @return bool
86+
*/
87+
public function rollback(): bool;
88+
89+
/**
90+
* Verifica si está en una transacción
91+
*
92+
* @return bool
93+
*/
94+
public function inTransaction(): bool;
95+
96+
/**
97+
* Obtiene el último ID insertado
98+
*
99+
* @param string|null $sequence Nombre de secuencia (para PostgreSQL)
100+
* @return string|false
101+
*/
102+
public function lastInsertId(?string $sequence = null): string|false;
103+
104+
/**
105+
* Obtiene el nombre de la conexión
106+
*
107+
* @return string
108+
*/
109+
public function getName(): string;
110+
111+
/**
112+
* Obtiene el driver utilizado
113+
*
114+
* @return string
115+
*/
116+
public function getDriverName(): string;
117+
118+
/**
119+
* Obtiene la instancia del driver
120+
*
121+
* @return DriverInterface
122+
*/
123+
public function getDriver(): DriverInterface;
124+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<?php
2+
3+
namespace PhobosFramework\Database\Connection;
4+
5+
use PhobosFramework\Database\Drivers\DriverInterface;
6+
use PhobosFramework\Database\Exceptions\ConnectionException;
7+
use PhobosFramework\Database\Exceptions\UnsupportedDriverException;
8+
9+
/**
10+
* Administra múltiples conexiones a bases de datos
11+
*/
12+
class ConnectionManager {
13+
protected static ?self $instance = null;
14+
protected array $connections = [];
15+
protected array $configs = [];
16+
protected array $drivers = [];
17+
protected ?string $defaultConnection = null;
18+
protected array $transactionManagers = [];
19+
20+
/**
21+
* Obtiene la instancia singleton
22+
*
23+
* @return self
24+
*/
25+
public static function getInstance(): self {
26+
if (self::$instance === null) {
27+
self::$instance = new self();
28+
}
29+
30+
return self::$instance;
31+
}
32+
33+
/**
34+
* Registra un driver
35+
*
36+
* @param string $name Nombre del driver
37+
* @param DriverInterface $driver Instancia del driver
38+
* @return void
39+
*/
40+
public function registerDriver(string $name, DriverInterface $driver): void {
41+
$this->drivers[$name] = $driver;
42+
}
43+
44+
/**
45+
* Agrega una configuración de conexión
46+
*
47+
* @param string $name Nombre de la conexión
48+
* @param array $config Configuración
49+
* @return void
50+
*/
51+
public function addConnection(string $name, array $config): void {
52+
$this->configs[$name] = $config;
53+
54+
if ($this->defaultConnection === null) {
55+
$this->defaultConnection = $name;
56+
}
57+
}
58+
59+
/**
60+
* Establece la conexión por defecto
61+
*
62+
* @param string $name Nombre de la conexión
63+
* @return void
64+
*/
65+
public function setDefaultConnection(string $name): void {
66+
if (!isset($this->configs[$name])) {
67+
throw new ConnectionException("Connection '{$name}' is not configured");
68+
}
69+
70+
$this->defaultConnection = $name;
71+
}
72+
73+
/**
74+
* Obtiene una conexión
75+
*
76+
* @param string|null $name Nombre de la conexión (null para la por defecto)
77+
* @return ConnectionInterface
78+
*/
79+
public function getConnection(?string $name = null): ConnectionInterface {
80+
$name = $name ?? $this->defaultConnection;
81+
82+
if ($name === null) {
83+
throw new ConnectionException('No default connection configured');
84+
}
85+
86+
if (!isset($this->connections[$name])) {
87+
$this->connections[$name] = $this->createConnection($name);
88+
}
89+
90+
return $this->connections[$name];
91+
}
92+
93+
/**
94+
* Obtiene el TransactionManager para una conexión
95+
*
96+
* @param string|null $name Nombre de la conexión
97+
* @return TransactionManager
98+
*/
99+
public function getTransactionManager(?string $name = null): TransactionManager {
100+
$name = $name ?? $this->defaultConnection;
101+
102+
if (!isset($this->transactionManagers[$name])) {
103+
$connection = $this->getConnection($name);
104+
$this->transactionManagers[$name] = new TransactionManager($connection);
105+
}
106+
107+
return $this->transactionManagers[$name];
108+
}
109+
110+
/**
111+
* Crea una nueva conexión
112+
*
113+
* @param string $name Nombre de la conexión
114+
* @return ConnectionInterface
115+
*/
116+
protected function createConnection(string $name): ConnectionInterface {
117+
if (!isset($this->configs[$name])) {
118+
throw new ConnectionException("Connection '{$name}' is not configured");
119+
}
120+
121+
$config = $this->configs[$name];
122+
$driverName = $config['driver'] ?? null;
123+
124+
if ($driverName === null) {
125+
throw new ConnectionException("Driver not specified for connection '{$name}'");
126+
}
127+
128+
if (!isset($this->drivers[$driverName])) {
129+
throw new UnsupportedDriverException(
130+
"Driver '{$driverName}' is not registered. Make sure the driver package is installed."
131+
);
132+
}
133+
134+
$driver = $this->drivers[$driverName];
135+
return new PDOConnection($name, $config, $driver);
136+
}
137+
138+
/**
139+
* Cierra todas las conexiones
140+
*
141+
* @return void
142+
*/
143+
public function closeAll(): void {
144+
foreach ($this->connections as $connection) {
145+
$connection->disconnect();
146+
}
147+
148+
$this->connections = [];
149+
$this->transactionManagers = [];
150+
}
151+
152+
/**
153+
* Cierra una conexión específica
154+
*
155+
* @param string $name Nombre de la conexión
156+
* @return void
157+
*/
158+
public function close(string $name): void {
159+
if (isset($this->connections[$name])) {
160+
$this->connections[$name]->disconnect();
161+
unset($this->connections[$name]);
162+
unset($this->transactionManagers[$name]);
163+
}
164+
}
165+
166+
/**
167+
* Obtiene todas las conexiones configuradas
168+
*
169+
* @return array
170+
*/
171+
public function getConfiguredConnections(): array {
172+
return array_keys($this->configs);
173+
}
174+
175+
/**
176+
* Obtiene la conexión por defecto
177+
*
178+
* @return string|null
179+
*/
180+
public function getDefaultConnectionName(): ?string {
181+
return $this->defaultConnection;
182+
}
183+
184+
/**
185+
* Verifica si una conexión existe
186+
*
187+
* @param string $name Nombre de la conexión
188+
* @return bool
189+
*/
190+
public function hasConnection(string $name): bool {
191+
return isset($this->configs[$name]);
192+
}
193+
194+
/**
195+
* Obtiene todos los drivers registrados
196+
*
197+
* @return array
198+
*/
199+
public function getRegisteredDrivers(): array {
200+
return array_keys($this->drivers);
201+
}
202+
}

0 commit comments

Comments
 (0)