PHP code example of absorbing / cql-core

1. Go to this page and download the library: Download absorbing/cql-core 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/ */

    

absorbing / cql-core example snippets




QL\CQL;

// Create a CQL instance
$cql = new CQL();

// Execute a query
$results = $cql->execute("
    DEFINE 'users.csv' AS users WITH HEADERS
    SELECT name, age
    FROM users
    WHERE age > 25
");

// Iterate over results
foreach ($results as $row) {
    echo "{$row['name']} is {$row['age']} years old\n";
}

// Or use convenience methods
$array = $cql->query($query);        // Returns array
$first = $cql->first($query);        // Returns first row
$count = $cql->count($query);        // Returns count

$cql = new CQL();

$affected = $cql->statement("
    DEFINE 'users.csv' AS users WITH HEADERS
    UPDATE users SET age = age + 1 WHERE name = 'Alice'
");

// execute() also accepts write statements, returning a Collection:
// [['affected_rows' => 1]]

use CQL\CQL;

// Basic usage
$cql = new CQL();
$results = $cql->execute($query);

// Convenience methods
$array = $cql->query($query);   // Returns array
$first = $cql->first($query);   // Returns first row
$count = $cql->count($query);   // Returns row count

// Static factory methods
$cql = CQL::auto();              // Automatic mode (default)
$cql = CQL::streaming();         // Always stream
$cql = CQL::normal();            // Never stream
$cql = CQL::auto(10 * 1024 * 1024); // Custom 10MB threshold

// Configuration
$cql = new CQL([
    'streaming' => true,
    'autoStreamingThreshold' => 20 * 1024 * 1024
]);

// Fluent configuration
$cql = (new CQL())
    ->setStreaming(true)
    ->setAutoStreamingThreshold(30 * 1024 * 1024);

$cql = new CQL();
$results = $cql->query($query);

// Sorting
usort($results, fn($a, $b) => $a['age'] <=> $b['age']);

// Limiting
$limited = array_slice($results, 0, 10);

// Distinct
$unique = array_unique($results, SORT_REGULAR);

// Aggregation
$total = array_sum(array_column($results, 'amount'));
$average = $total / count($results);

// Grouping
$grouped = [];
foreach ($results as $row) {
    $grouped[$row['category']][] = $row;
}



use CQL\CQL;

// users.csv:
// id,name,age,status
// 1,Alice,30,active
// 2,Bob,25,inactive
// 3,Charlie,35,active

$cql = new CQL();

$results = $cql->execute("
    DEFINE 'users.csv' AS users WITH HEADERS
    SELECT name, age
    FROM users
    WHERE age >= 30 AND status = 'active'
");

// Output:
// {"name":"Alice","age":"30"}
// {"name":"Charlie","age":"35"}



use CQL\CQL;

// users.csv:
// id,name
// 1,Alice
// 2,Bob

// orders.csv:
// order_id,user_id,product,amount
// 101,1,Laptop,1200
// 102,1,Mouse,25
// 103,2,Keyboard,75

$cql = new CQL();

$results = $cql->execute("
    DEFINE 'users.csv' AS users WITH HEADERS
    DEFINE 'orders.csv' AS orders WITH HEADERS
    SELECT users.name, orders.product, orders.amount
    FROM users
    JOIN orders ON users.id = orders.user_id
    WHERE orders.amount > 50
");

// Output:
// {"users.name":"Alice","orders.product":"Laptop","orders.amount":"1200"}
// {"users.name":"Bob","orders.product":"Keyboard","orders.amount":"75"}



use CQL\CQL;

$cql = new CQL();

$results = $cql->execute("
    DEFINE 'employees.csv' AS emp WITH HEADERS
    SELECT emp.first_name AS name, emp.salary AS pay
    FROM emp
    WHERE emp.department = 'Engineering'
");

// Output uses aliased column names:
// {"name":"John","pay":"95000"}
// {"name":"Jane","pay":"105000"}



use CQL\CQL;

// products.csv:
// name,price,quantity
// Widget,10.50,100
// Gadget,25.00,50
// Doohickey,5.75,200

$cql = new CQL();

$results = $cql->execute("
    DEFINE 'products.csv' AS products WITH HEADERS
    SELECT name, price, quantity
    FROM products
    WHERE price * quantity > 1000
");

// Output:
// {"name":"Gadget","price":"25.00","quantity":"50"}
// {"name":"Doohickey","price":"5.75","quantity":"200"}



use CQL\CQL;

// For large CSV files (>50MB), use streaming mode to reduce memory usage

// Explicit streaming
$cql = CQL::streaming();
$results = $cql->execute("
    DEFINE 'large_dataset.csv' AS data WITH HEADERS
    SELECT id, name, value
    FROM data
    WHERE value > 1000
");

// Automatic mode (recommended) - decides based on file size
$cql = CQL::auto();
$results = $cql->execute($query);

// Custom threshold
$cql = CQL::auto(10 * 1024 * 1024); // 10MB threshold
$results = $cql->execute($query);

use CQL\CQL;
use CQL\Exceptions\{
    DataSourceException,
    InterpreterException,
    SyntaxException
};

try {
    $cql = new CQL();
    $results = $cql->execute($query);
} catch (DataSourceException $e) {
    // Handle file not found, permission errors, etc.
    echo "Data source error: " . $e->getMessage();
} catch (SyntaxException $e) {
    // Handle invalid query syntax
    echo "Syntax error: " . $e->getMessage();
} catch (InterpreterException $e) {
    // Handle execution errors
    echo "Execution error: " . $e->getMessage();
}

use CQL\Engine\Operators\BaseComparisonOperator;
use CQL\Engine\Operators\Contracts\ComparisonOperatorInterface;

class LikeOperator extends BaseComparisonOperator implements ComparisonOperatorInterface
{
    public static function symbols(): array
    {
        return ['LIKE'];
    }

    public static function evaluate(mixed $left, mixed $right): bool
    {
        $pattern = str_replace('%', '.*', preg_quote($right, '/'));
        return (bool) preg_match("/^{$pattern}$/i", $left);
    }
}

// Register the operator
use CQL\Engine\Operators\Registry\OperatorRegistry;
OperatorRegistry::register(LikeOperator::class);

$cql = new CQL();
$results = $cql->execute($query);

// Get result count
$count = $results->count();

// Convert to array
$array = $results->toArray();

// Get first result
$first = $results->first();

// Chain operations
$filtered = $results
    ->filter(fn($row) => $row['age'] > 30)
    ->map(fn($row) => ['name' => strtoupper($row['name'])]);

// Iterate
foreach ($results as $row) {
    // Process each row
}

// Or use convenience methods directly
$array = $cql->query($query);   // Returns array
$first = $cql->first($query);   // Returns first row
$count = $cql->count($query);   // Returns count

use CQL\CQL;

// Explicit normal mode (always load into memory)
$cql = CQL::normal();

// Explicit streaming mode (always stream)
$cql = CQL::streaming();

// Automatic mode with default 50MB threshold (recommended)
$cql = new CQL();
// or
$cql = CQL::auto();

// Automatic mode with custom 10MB threshold
$cql = CQL::auto(10 * 1024 * 1024);

// Configuration via constructor
$cql = new CQL([
    'streaming' => true,
    'autoStreamingThreshold' => 20 * 1024 * 1024
]);
sql
SELECT function(column) [AS alias]
SELECT function(*)
sql
SELECT function(date_column) [AS alias]
WHERE function(date_column) operator value
GROUP BY function(date_column)