clue / reactphp-sqlite

Async SQLite database, lightweight non-blocking process wrapper around file-based database extension (ext-sqlite3), built on top of ReactPHP.
https://clue.engineering/2019/introducing-reactphp-sqlite
MIT License
51 stars 10 forks source link

Add ability to set worker file and cwd (Phar support) #32

Closed azurre closed 2 years ago

azurre commented 4 years ago

Fix make able use reactphp-sqlite with phar applications + fix filename regexp (windows path support)

azurre commented 4 years ago

The problem:

Achieve wanted:

Exapmle:

<?php
use Clue\React\SQLite\Factory;
(function() {
    // --- additional part for phar application ---
    $options = getopt('', ['sqlite::']);
    if (isset($options['sqlite'])) { // Run worker
        if (isset($_SERVER['argv'][2])) { // Handle Windows
            $_SERVER['argv'][1] = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : $_SERVER['argv'][1];
        } else {
            unset($_SERVER['argv'][1]); // Handle *nix
        }
        return include __DIR__ . '/vendor/clue/reactphp-sqlite/res/sqlite-worker.php';
    }
    $worker = $cwd = null;
    if (strpos(__DIR__, 'phar://') === 0) {
        $app = Phar::running(false);
        $worker = "{$app} --sqlite=1";
        $cwd = dirname($app);
    }
    // -------------------------------------------
    require_once __DIR__ . '/vendor/autoload.php';
    $loop = React\EventLoop\Factory::create();
    $factory = new Factory($loop, $worker, $cwd);
    $db = $factory->openLazy('users.db');
    $db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');
    $name = 'Alice';
    $db->query('INSERT INTO foo (bar) VALUES (?)', [$name])->then(
        function(Clue\React\SQLite\Result $result) use ($name) {
            echo 'New ID for ' . $name . ': ' . $result->insertId . PHP_EOL;
        }
    );
    $db->quit();
    $loop->run();
})();

As a result $ php test.phar

New ID for Alice: 8

and

$ php test.php

New ID for Alice: 9

works as expected on windows & *nix

azurre commented 4 years ago

Hi @clue

I've updated my previous comment

clue commented 2 years ago

I'm closing this for now as it hasn't received any input in a while and I believe this is best addressed with #5 in a future version. If you feel this is still relevant, please come back with more details and we can always reopen this :+1:

Thank you for your effort, keep it up! :+1: