Skip to content

AbstractQueryLayer/aql-ext

Repository files navigation

AQL - Abstract Query Language Extension

AQL is a PHP extension that automatically transforms SQL strings into structured data at compile-time using AST hooks.

Features

  • Zero Runtime Overhead: SQL parsing happens at compile-time (once per file)
  • Automatic Detection: Detects SQL queries by keywords (SELECT, INSERT, UPDATE, DELETE, etc.)
  • Heredoc Support: Works with both heredoc and nowdoc syntax
  • String Prefix Detection: Automatically detects SQL in regular strings

How It Works

1. String Prefix Detection

<?php
// Regular string with SQL - automatically detected
$query = "SELECT * FROM users WHERE id = ?";

// After AQL processing, $query becomes:
// [
//   '__aql_query' => true,
//   'sql' => "SELECT * FROM users WHERE id = ?"
// ]

2. Heredoc Support

<?php
$query = <<<SQL
SELECT u.name, u.email
FROM users u
WHERE u.active = true
SQL;

// Also transformed into an array

Installation

From PHP Source

cd /path/to/php-src/ext/aql
phpize
./configure
make
make install

Enable Extension

Add to php.ini:

extension=aql.so
aql.enabled=1

Configuration

  • aql.enabled - Enable/disable AQL processing (default: 1)

Development Roadmap

Phase 1: Basic Infrastructure ✅

  • AST hook integration
  • String detection by SQL keywords
  • Basic array generation

Phase 2: SQL Parser

  • Implement full SQL parser
  • Generate structured AST for SQL
  • Support for:
    • SELECT statements
    • INSERT statements
    • UPDATE statements
    • DELETE statements
    • JOIN operations
    • WHERE conditions
    • ORDER BY, GROUP BY, LIMIT

Phase 3: Advanced Features

  • Parameter binding detection
  • Query optimization hints
  • Custom DSL extensions
  • IDE integration support

Example Output

<?php
$query = "SELECT id, name FROM users WHERE active = 1";

// After AQL transformation:
var_dump($query);
/*
array(2) {
  ["__aql_query"]=>
  bool(true)
  ["sql"]=>
  string(42) "SELECT id, name FROM users WHERE active = 1"
}
*/

Technical Details

AST Hook

AQL uses zend_ast_process hook to intercept the Abstract Syntax Tree after parsing but before compilation. This allows:

  1. Compile-time processing - no runtime overhead
  2. Full AST manipulation - can replace any node
  3. Opcache compatible - transformed code is cached

Detection Algorithm

bool aql_is_sql_string(zend_string *str) {
    // Skip leading whitespace
    // Check first 4-6 characters for SQL keywords:
    // SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP
}

License

PHP License 3.01

Author

Built for TrueAsync PHP project

About

PHP AQL Extention

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors