PHP code example of k-kinzal / ztd-query-sqlite

1. Go to this page and download the library: Download k-kinzal/ztd-query-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/ */

    

k-kinzal / ztd-query-sqlite example snippets


use ZtdQuery\Config\ZtdConfig;
use ZtdQuery\Platform\Sqlite\SqliteSessionFactory;

// $connection implements ZtdQuery\Connection\ConnectionInterface
$session = SqliteSessionFactory::create($connection, ZtdConfig::default());

use ZtdQuery\Platform\Sqlite\SqliteQueryGuard;
use ZtdQuery\Platform\Sqlite\SqliteParser;
use ZtdQuery\Rewrite\QueryKind;

$parser = new SqliteParser();
$guard = new SqliteQueryGuard($parser);

$guard->classify('SELECT * FROM users');
// => QueryKind::READ

$guard->classify('INSERT INTO users (name) VALUES (\'Alice\')');
// => QueryKind::WRITE_SIMULATED

$guard->classify('CREATE TABLE logs (id INT)');
// => QueryKind::DDL_SIMULATED

$guard->classify('BEGIN');
// => null (unsupported)

use ZtdQuery\Platform\Sqlite\SqliteRewriter;

// Rewrite a single statement
$plan = $rewriter->rewrite('SELECT email FROM users WHERE id = 1');
// $plan->sql() returns the CTE-shadowed query
// $plan->kind() returns the QueryKind

// Rewrite multiple statements (e.g., multi-query)
$plans = $rewriter->rewriteMultiple('SELECT 1; SELECT 2');

use ZtdQuery\Platform\Sqlite\SqliteErrorClassifier;

$classifier = new SqliteErrorClassifier();

$classifier->isUnknownSchemaError(1); // true (SQL error or missing database)