Skip to content

Commit 7614c6d

Browse files
committed
Initial commit
0 parents  commit 7614c6d

36 files changed

+1854
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Composer dependencies
2+
/vendor/
3+
4+
# Composer lock file (optional—commit if you want reproducible builds)
5+
/composer.lock
6+
7+
# PHPStorm settings
8+
.idea/
9+
10+
# Log files
11+
*.log

changelog.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Changelog
2+
3+
All notable changes to **comphp/database** will be documented in this file.
4+
5+
## [0.1.0] - 2025-03-16
6+
7+
### Added
8+
- **Initial Release** of the `comphp/database` library.
9+
- **DatabaseManager** providing centralized connection management.
10+
- **MySQL Driver** (`MysqlDatabaseDriver`) with essential operations (fetch, insert, update, etc.).
11+
- **Alias Database Driver** enabling alias-based connection references.
12+
- **Transaction Support** allowing atomic multi-query operations.
13+
- **PSR-3 Logging** integration for query actions and exceptions.
14+
- **PSR-14 Events** (`ConnectedEvent`, `QueryExecutedEvent`) for event-driven usage.
15+
- **Extensibility** through `comphp/extensible` (register new drivers, manage them via attributes).
16+
- **Query Profiling** to log query execution duration and errors.
17+

code_of_conduct.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
- Using welcoming and inclusive language.
11+
- Being respectful of differing viewpoints and experiences.
12+
- Gracefully accepting constructive criticism.
13+
- Focusing on what is best for the community.
14+
15+
Examples of unacceptable behavior by participants include:
16+
- Harassment, intimidation, or discriminatory comments.
17+
- Trolling, insulting, or derogatory comments.
18+
- Unwelcome sexual attention or advances.
19+
- Deliberate intimidation or stalking.
20+
21+
## Reporting and Enforcement
22+
23+
If you experience or witness any behavior that violates this Code of Conduct, please report it by emailing [conduct@commonphp.org](mailto:conduct@commonphp.org). All complaints will be reviewed promptly and confidentially by the project maintainers.
24+
25+
Project maintainers are responsible for enforcing this code throughout the project and community. They have the right to remove, edit, or reject any contributions or comments that do not align with this Code of Conduct.
26+
27+
By participating in this project, you agree to abide by these guidelines and help create a positive environment for everyone.

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "comphp/database",
3+
"license": "MIT",
4+
"autoload": {
5+
"psr-4": {
6+
"Neuron\\Database\\": "src/"
7+
}
8+
},
9+
"autoload-dev": {
10+
"psr-4": {
11+
"NeuronTests\\Database\\": "test/"
12+
}
13+
},
14+
"require": {
15+
"php": "^8.4",
16+
"psr/log": "^3.0",
17+
"comphp/extensible": "^0.1",
18+
"comphp/events": "^0.2",
19+
"ext-pdo": "*"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^12.0"
23+
}
24+
}

contributing.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Contributing
2+
3+
Thank you for considering contributing to this project! Your help is essential in making this library better for everyone.
4+
5+
## How to Report Bugs
6+
- Before opening a new issue, please search the issue tracker for similar reports.
7+
- When reporting a bug, include a clear and descriptive title, a detailed explanation, and steps to reproduce the issue.
8+
9+
## How to Request Features
10+
- Check the existing issues to ensure your feature hasn’t already been requested.
11+
- Provide a clear description of the feature, its benefits, and any potential implementation ideas.
12+
13+
## Pull Request Guidelines
14+
- Fork the repository and create your branch from `master`.
15+
- Follow the coding style guidelines (PSR standards).
16+
- Write clear commit messages that explain the rationale behind your changes.
17+
- Ensure all tests pass before submitting your pull request.
18+
- Include relevant documentation updates if necessary.
19+
20+
## Additional Guidelines
21+
- Respect the existing code style and architecture.
22+
- Be open to feedback and constructive criticism.
23+
- Engage in discussions on issues and pull requests respectfully.
24+
25+
Thank you for your contributions!

examples/alias-driver-usage.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Neuron\Database\DatabaseManager;
4+
use Neuron\Database\Drivers\AliasDatabaseDriver;
5+
use Neuron\Database\Drivers\MysqlDatabaseDriver;
6+
use Neuron\Extensibility\ExtensionStore;
7+
use Neuron\Extensibility\InstantiatorInterface;
8+
use Psr\EventDispatcher\EventDispatcherInterface;
9+
use Psr\Log\LoggerInterface;
10+
11+
/** @var LoggerInterface $logger */
12+
/** @var InstantiatorInterface $instantiator */
13+
/** @var EventDispatcherInterface $eventDispatcher */
14+
15+
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
16+
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
17+
18+
// Register primary database connection
19+
$database->connect('primary', MysqlDatabaseDriver::class, [
20+
'database' => 'primary_db',
21+
'host' => 'localhost',
22+
'username' => 'user',
23+
'password' => 'password',
24+
]);
25+
26+
// Register an alias connection pointing to primary
27+
$database->connect('secondary', AliasDatabaseDriver::class, [
28+
'database' => $database,
29+
'target' => 'primary'
30+
]);
31+
32+
// Using the alias connection transparently
33+
$result = $database->fetchOne('SELECT COUNT(*) AS total FROM users', [], 'secondary');
34+
35+
echo "Total users: " . $result['total'] . "\n";

examples/basic-usage.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Neuron\Database\DatabaseManager;
4+
use Neuron\Database\Drivers\MysqlDatabaseDriver;
5+
use Neuron\Database\FetchMode;
6+
use Neuron\Extensibility\ExtensionStore;
7+
use Neuron\Extensibility\InstantiatorInterface;
8+
use Psr\EventDispatcher\EventDispatcherInterface;
9+
use Psr\Log\LoggerInterface;
10+
11+
// Assume these instances are created or injected via your application's DI container
12+
/** @var LoggerInterface $logger */
13+
/** @var InstantiatorInterface $instantiator */
14+
/** @var EventDispatcherInterface $eventDispatcher */
15+
16+
// Setup extensibility
17+
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
18+
19+
// Create a new database manager
20+
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
21+
22+
// Connect to the database using MysqlDatabaseDriver
23+
$database->connect('_default_', MysqlDatabaseDriver::class, [
24+
'database' => 'my_database',
25+
'host' => 'localhost',
26+
'username' => 'user',
27+
'password' => 'password',
28+
]);
29+
30+
// Execute a basic query
31+
$result = $database->fetchAll('SELECT * FROM users LIMIT 5');
32+
33+
foreach ($result as $user) {
34+
echo "User ID: {$user['id']}, Name: {$user['name']}\n";
35+
}

examples/error-handling.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Neuron\Database\DatabaseException;
4+
use Neuron\Database\DatabaseManager;
5+
use Neuron\Database\Drivers\MysqlDatabaseDriver;
6+
use Neuron\Extensibility\ExtensionStore;
7+
use Neuron\Extensibility\InstantiatorInterface;
8+
use Psr\EventDispatcher\EventDispatcherInterface;
9+
use Psr\Log\LoggerInterface;
10+
11+
/** @var LoggerInterface $logger */
12+
/** @var InstantiatorInterface $instantiator */
13+
/** @var EventDispatcherInterface $eventDispatcher */
14+
15+
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
16+
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
17+
18+
$database->connect('_default_', MysqlDatabaseDriver::class, [
19+
'database' => 'nonexistent_db', // Intentional wrong DB name
20+
'host' => 'localhost',
21+
'username' => 'user',
22+
'password' => 'wrongpassword',
23+
]);
24+
25+
try {
26+
$database->execute('SELECT * FROM users');
27+
} catch (DatabaseException $e) {
28+
echo "Database error: " . $e->getMessage();
29+
}

examples/query-profiling.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Neuron\Database\DatabaseManager;
4+
use Neuron\Database\Drivers\MysqlDatabaseDriver;
5+
use Neuron\Extensibility\ExtensionStore;
6+
use Neuron\Extensibility\InstantiatorInterface;
7+
use Neuron\Database\Events\QueryExecutedEvent;
8+
use Psr\EventDispatcher\EventDispatcherInterface;
9+
use Psr\Log\LoggerInterface;
10+
11+
/** @var LoggerInterface $logger */
12+
/** @var InstantiatorInterface $instantiator */
13+
/** @var EventDispatcherInterface $eventDispatcher */
14+
15+
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
16+
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
17+
18+
// Connect to your database
19+
$database->connect('_default_', MysqlDatabaseDriver::class, [
20+
'database' => 'my_database',
21+
'host' => 'localhost',
22+
'username' => 'user',
23+
'password' => 'password',
24+
]);
25+
26+
// Enable profiling
27+
$database->enableProfiling();
28+
29+
// Listen to query executed events for profiling output
30+
$eventDispatcher->listen(QueryExecutedEvent::class, function (QueryExecutedEvent $event) {
31+
echo sprintf(
32+
"[%s] Query executed: %s | Duration: %.4f seconds\n",
33+
strtoupper($event->action),
34+
$event->query,
35+
$event->duration
36+
);
37+
});
38+
39+
// Execute queries
40+
$database->fetchAll('SELECT * FROM users LIMIT 10');
41+
$database->execute('UPDATE users SET last_login = NOW() WHERE id = ?', [1]);

examples/transaction-usage.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Neuron\Database\DatabaseManager;
4+
use Neuron\Database\Drivers\MysqlDatabaseDriver;
5+
use Neuron\Extensibility\ExtensionStore;
6+
use Neuron\Extensibility\InstantiatorInterface;
7+
use Psr\EventDispatcher\EventDispatcherInterface;
8+
use Psr\Log\LoggerInterface;
9+
10+
/** @var LoggerInterface $logger */
11+
/** @var InstantiatorInterface $instantiator */
12+
/** @var EventDispatcherInterface $eventDispatcher */
13+
14+
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
15+
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
16+
17+
$database->connect('_default_', MysqlDatabaseDriver::class, [
18+
'database' => 'my_database',
19+
'host' => 'localhost',
20+
'username' => 'user',
21+
'password' => 'password',
22+
]);
23+
24+
try {
25+
$database->transaction(function ($db) {
26+
$db->execute('INSERT INTO accounts (name, balance) VALUES (?, ?)', ['Checking', 500]);
27+
$db->execute('INSERT INTO transactions (account_id, amount) VALUES (?, ?)', [1, 500]);
28+
});
29+
30+
echo "Transaction completed successfully.\n";
31+
} catch (Throwable $e) {
32+
echo "Transaction failed: " . $e->getMessage();
33+
}

0 commit comments

Comments
 (0)