Skip to content

Commit

Permalink
Added database-sqlite component.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcht authored Feb 20, 2024
0 parents commit 83f2c0d
Show file tree
Hide file tree
Showing 17 changed files with 3,070 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tests export-ignore
/.github export-ignore
13 changes: 13 additions & 0 deletions .github/workflows/close-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Close Pull Request

on:
pull_request_target:
types: [ opened ]

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: superbrothers/close-pull-request@v3
with:
comment: "Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/hyperf/hyperf repository.<br><br> This Pull Request will close automatically.<br><br> Thanks! "
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

name: Release

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Hyperf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "hyperf/database-sqlite",
"type": "library",
"description": "The sqlite driver for hyperf/database.",
"license": "MIT",
"keywords": [
"php",
"swoole",
"hyperf",
"database",
"sqlite"
],
"homepage": "https://hyperf.io",
"support": {
"docs": "https://hyperf.wiki",
"issues": "https://github.com/hyperf/hyperf/issues",
"pull-request": "https://github.com/hyperf/hyperf/pulls",
"source": "https://github.com/hyperf/hyperf"
},
"require": {
"php": ">=8.1",
"hyperf/collection": "~3.1.0",
"hyperf/database": "~3.1.0",
"hyperf/support": "~3.1.0",
"hyperf/stringable": "~3.1.0"
},
"autoload": {
"psr-4": {
"Hyperf\\Database\\SQLite\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\Database\\SQLite\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
},
"hyperf": {
"config": "Hyperf\\Database\\SQLite\\ConfigProvider"
}
}
}
30 changes: 30 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\SQLite;

use Hyperf\Database\SQLite\Connectors\SQLiteConnector;
use Hyperf\Database\SQLite\Listener\RegisterConnectionListener;

class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [
'db.connector.sqlite' => SQLiteConnector::class,
],
'listeners' => [
RegisterConnectionListener::class,
],
];
}
}
50 changes: 50 additions & 0 deletions src/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\SQLite\Connectors;

use Hyperf\Database\Connectors\Connector;
use Hyperf\Database\Connectors\ConnectorInterface;
use InvalidArgumentException;
use PDO;

class SQLiteConnector extends Connector implements ConnectorInterface
{
/**
* Establish a database connection.
*
* @return PDO
*
* @throws InvalidArgumentException
*/
public function connect(array $config)
{
$options = $this->getOptions($config);

// SQLite supports "in-memory" databases that only last as long as the owning
// connection does. These are useful for tests or for short lifetime store
// querying. In-memory databases may only have a single open connection.
if ($config['database'] === ':memory:') {
return $this->createConnection('sqlite::memory:', $config, $options);
}

$path = realpath($config['database']);

// Here we'll verify that the SQLite database exists before going any further
// as the developer probably wants to know if the database exists and this
// SQLite driver will not throw any exception if it does not by default.
if ($path === false) {
throw new InvalidArgumentException("Database ({$config['database']}) does not exist.");
}

return $this->createConnection("sqlite:{$path}", $config, $options);
}
}
65 changes: 65 additions & 0 deletions src/Listener/RegisterConnectionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\SQLite\Listener;

use Hyperf\Context\ApplicationContext;
use Hyperf\Database\Connection;
use Hyperf\Database\SQLite\SQLiteConnection;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
use Psr\Container\ContainerInterface;

class RegisterConnectionListener implements ListenerInterface
{
/**
* Create a new connection factory instance.
*/
public function __construct(protected ContainerInterface $container)
{
}

public function listen(): array
{
return [
BootApplication::class,
];
}

/**
* Register sqlite connection.
*/
public function process(object $event): void
{
Connection::resolverFor('sqlite', function ($connection, $database, $prefix, $config) {
if ($config['database'] === ':memory:') {
$connection = $this->createPersistentPdoResolver($connection, $config);
}

return new SQLiteConnection($connection, $database, $prefix, $config);
});
}

protected function createPersistentPdoResolver($connection, $config)
{
return function () use ($config, $connection) {
/** @var \Hyperf\Contract\ContainerInterface $container */
$container = ApplicationContext::getContainer();
$key = "sqlite.presistent.pdo.{$config['name']}";

if (! $container->has($key)) {
$container->set($key, call_user_func($connection));
}

return $container->get($key);
};
}
}
Loading

0 comments on commit 83f2c0d

Please sign in to comment.