PHP code example of tobento / service-database-storage

1. Go to this page and download the library: Download tobento/service-database-storage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / service-database-storage example snippets


use Tobento\Service\Database\DatabaseInterface;
use Tobento\Service\Database\Storage\StorageDatabaseInterface;
use Tobento\Service\Database\Storage\StorageDatabase;
use Tobento\Service\Storage\StorageInterface;
use Tobento\Service\Storage\InMemoryStorage;

$database = new StorageDatabase(
    storage: new InMemoryStorage([]),
    name: 'inmemory',
);

var_dump($database instanceof DatabaseInterface);
// bool(true)

var_dump($database instanceof StorageDatabaseInterface);
// bool(true)

var_dump($database->storage() instanceof StorageInterface);
// bool(true)

use Tobento\Service\Database\DatabaseFactoryInterface;
use Tobento\Service\Database\DatabaseInterface;
use Tobento\Service\Database\Databases;
use Tobento\Service\Database\Storage\StorageDatabaseFactory;
use Tobento\Service\Database\Storage\StorageDatabaseInterface;
use Tobento\Service\Storage\StorageInterface;
use Tobento\Service\Storage\JsonFileStorage;

$factory = new StorageDatabaseFactory(
    databases: new Databases(),
);

var_dump($factory instanceof DatabaseFactoryInterface);
// bool(true)

// create json file storage:
$database = $factory->createDatabase(
    name: 'file',
    config: [
        'storage' => JsonFileStorage::class,
        'dir' => __DIR__.'/json-file/',
    ],
);

var_dump($database instanceof DatabaseInterface);
// bool(true)

var_dump($database instanceof StorageDatabaseInterface);
// bool(true)

var_dump($database->storage() instanceof StorageInterface);
// bool(true)

use Tobento\Service\Database\DatabaseInterface;
use Tobento\Service\Database\Databases;
use Tobento\Service\Database\PdoDatabase;
use Tobento\Service\Database\PdoDatabaseInterface;
use Tobento\Service\Database\Storage\StorageDatabaseFactory;
use Tobento\Service\Storage\PdoMySqlStorage;
use Tobento\Service\Storage\PdoMariaDbStorage;

$factory = new StorageDatabaseFactory(
    databases: new Databases(
        new PdoDatabase(
            pdo: new \PDO('sqlite::memory:'),
            name: 'mysql'
        ),
    ),
);

$database = $factory->createDatabase(
    name: 'storage',
    config: [
        'storage' => PdoMySqlStorage::class,
        //'storage' => PdoMariaDbStorage::class,
        
        // must be a PdoDatabaseInterface database with mysql driver
        'database' => 'mysql',
    ],
);

// Will throw an exception as
// PdoMySqlStorage::class only supports mysql driver

use Tobento\Service\Database\DatabaseInterface;
use Tobento\Service\Database\Databases;
use Tobento\Service\Database\Storage\StorageDatabaseFactory;
use Tobento\Service\Storage\InMemoryStorage;

$factory = new StorageDatabaseFactory(
    databases: new Databases(),
);

$database = $factory->createDatabase(
    name: 'inmemory',
    config: [
        'storage' => InMemoryStorage::class,
    ],
);

var_dump($database instanceof DatabaseInterface);
// bool(true)

use Tobento\Service\Database\Processor\ProcessorInterface;
use Tobento\Service\Database\Storage\StorageDatabase;
use Tobento\Service\Database\Storage\StorageDatabaseProcessor;
use Tobento\Service\Database\Schema\Table;
use Tobento\Service\Storage\InMemoryStorage;
use Tobento\Service\Storage\Tables\Tables;

// Create table to be processed:
$table = new Table(name: 'users');
$table->primary('id');
$table->string('name');
$table->items(iterable: [
    ['name' => 'John'],
    ['name' => 'Mia'],
]);

// Create the database to be processed on:
$database = new StorageDatabase(
    storage: new InMemoryStorage(
        items: [],
        tables: (new Tables())->add('users', ['id', 'name'], 'id'),
    )
);

// Create the database processor supporting storages:
$processor = new StorageDatabaseProcessor();

var_dump($processor instanceof ProcessorInterface);
// bool(true)

// Process:
$processor->process(table: $table, database: $database);

// The items were successfully inserted
// into the storage by the processor:
$names = $database->storage()->table('users')->column('name')->all();
var_dump($names);
// { [0]=> string(4) "John" [1]=> string(3) "Mia" }