PHP code example of mongoose-studio / phobos-framework-database-sqlite

1. Go to this page and download the library: Download mongoose-studio/phobos-framework-database-sqlite 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/ */

    

mongoose-studio / phobos-framework-database-sqlite example snippets




return [
    'default' => 'sqlite',

    'drivers' => [
        'sqlite' => PhobosFramework\Database\Drivers\SQLite\SQLiteDriver::class,
    ],

    'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => storage_path('database/app.sqlite'),
            'foreign_keys' => true,
            'journal_mode' => 'WAL',
        ],
    ],
];

'sqlite' => [
    'driver' => 'sqlite',
    'database' => '/var/data/app.sqlite',  // Ruta al archivo
    // 'database' => ':memory:',            // Base de datos en memoria
]

'sqlite' => [
    // ...
    'foreign_keys' => true,   // Default. Ejecuta PRAGMA foreign_keys = ON
    // 'foreign_keys' => false, // Desactiva las restricciones FK
]

'sqlite' => [
    // ...
    'journal_mode' => 'WAL',
]

'sqlite' => [
    // ...
    'busy_timeout' => 5000,  // Espera hasta 5 segundos
]

'sqlite' => [
    // ...
    'synchronous' => 'NORMAL',
]

'sqlite' => [
    // ...
    'options' => [
        PDO::ATTR_TIMEOUT => 5,
    ],
]

// quoteIdentifier() genera:
"users"
"weird ""column"""   // escapa comillas dobles internas

$tm = db()->getTransactionManager();

$tm->begin();                    // BEGIN
try {
    // ... operaciones ...

    $sp = $tm->begin();          // SAVEPOINT anidado
    try {
        // ... más operaciones ...
        $tm->commit($sp);        // RELEASE SAVEPOINT
    } catch (Exception $e) {
        $tm->rollback($sp);      // ROLLBACK TO SAVEPOINT (solo lo anidado)
    }

    $tm->commit();               // COMMIT de la transacción externa
} catch (Exception $e) {
    $tm->rollback();             // ROLLBACK completo
}

$driver = new SQLiteDriver();

$driver->getSetIsolationLevelSQL('SERIALIZABLE');      // PRAGMA read_uncommitted = 0
$driver->getSetIsolationLevelSQL('READ UNCOMMITTED');  // PRAGMA read_uncommitted = 1

$driver->getSetIsolationLevelSQL('READ COMMITTED');    // ❌ InvalidArgumentException
$driver->getSetIsolationLevelSQL('REPEATABLE READ');   // ❌ InvalidArgumentException

'sqlite_test' => [
    'driver' => 'sqlite',
    'database' => ':memory:',
    'foreign_keys' => true,
]

use PhobosFramework\Database\Connection\ConnectionManager;
use PhobosFramework\Database\Drivers\SQLite\SQLiteDriver;

$manager = ConnectionManager::getInstance();
$manager->registerDriver('sqlite', new SQLiteDriver());
$manager->addConnection('default', [
    'driver' => 'sqlite',
    'database' => ':memory:',
]);
$manager->setDefaultConnection('default');

// Crear el esquema una vez y ejercitar tus entidades contra él
$manager->getConnection()->getPDO()->exec('
    CREATE TABLE users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        email TEXT NOT NULL
    )
');

'sqlite' => [
    'driver' => 'sqlite',
    'database' => storage_path('database/app.sqlite'),
    'foreign_keys' => true,
    'journal_mode' => 'WAL',       // Mejor concurrencia
    'synchronous' => 'NORMAL',     // Equilibrio con WAL
    'busy_timeout' => 5000,        // Tolera bloqueos breves
]

'journal_mode' => 'WAL',   // Lectores no bloquean al escritor
'busy_timeout' => 5000,    // Espera en vez de fallar de inmediato

SQLiteDriver
    ├── getDSN()                   - Construye el DSN (archivo o :memory:)
    ├── getPDOOptions()            - Opciones PDO seguras (heredadas de AbstractDriver)
    ├── configure()               - Aplica PRAGMAs post-conexión
    ├── getName()                 - Retorna 'sqlite'
    ├── supportsSavepoints()      - Retorna true
    ├── quoteIdentifier()         - Envuelve en comillas dobles
    ├── getSetIsolationLevelSQL() - SERIALIZABLE / READ UNCOMMITTED (vía PRAGMA)
    └── getLastInsertId()         - ROWID de la última inserción (ignora la secuencia)