PHP code example of tarsys / aql-gen

1. Go to this page and download the library: Download tarsys/aql-gen 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/ */

    

tarsys / aql-gen example snippets


//configure statement
$connection = new Connection($connectionOptions);
$statement = new Statement($connection, array(
                          "query"     => '',
                          "count"     => true,
                          "sanitize"  => true,
                      ));
                      
use tarsys\AqlGen\AqlGen;

  //mount the query
  $query1 = AqlGen::query('u', 'users'); 
    
//execute 
$statement->setQuery($mainQuery->get());
//$statement->bind($mainQuery->getParams()); //if some params has passed

   //SIMPLE QUERIES

   $query1 = AqlGen::query('u', 'users'); 

     echo $query1->get();
  // Generate:  FOR u IN users RETURN u

  //WITH filter
   $query1 = AqlGen::query('u', 'users')->filter('u.yearsOld == 20');
  
    echo $query1->get();
/* Generate: 
    FOR u IN users 
    FILTER u.yearsOld == 20
    RETURN u
*/


//Example 1: subquery

  $mainQuery = AqlGen::query('u', 'users'); 

  $locations = AqlGen::query('l', 'locations')->filter('u.id == l.id');

  $mainQuery->subquery($locations)
              ->serReturn('{"user": u, "location": l}');

  echo $mainQuery->get();
 /* Generate this string: 
    FOR u IN users 
       FOR l IN locations 
          FILTER u.id == l.id
    RETURN {`user`:u, `location`:l}
  */
  


$mainQuery = AqlGen::query('u', 'users')->filter('u.id == @id', ['id'=> 19]); 

$mainQuery->filter('u.name == @name && u.age == @age')->bindParams(['name'=> 'jhon', 'age' => 20]);
$mainQuery->orFilter('u.group == @group')->bindParam('group', 11);
  
echo $mainQuery->get();
/* Generate: 
    FOR u IN users 
       FILTER u.id == @id  && u.name == @name && u.age == @age ||  u.group == @group
    RETURN u
*/

// USE $mainQuery->getParams(); to retrieve bind params



$mainQuery = AqlGen::query('u', 'users')
            ->let('myvar', 'hello')
            ->let('myfriends', AqlGen::query('f','friends') );
 
 echo $mainQuery->get();
 
 /* Generate this string: 
    FOR u IN users 
       LET  myvar = `hello`
       LET  myfriends = ( 
          FOR f IN friends 
          RETURN f
        )
    RETURN u
  */ 
  


$mainQuery = AqlGen::query('u', 'users')
            ->collect('myvar', 'u.city', 'g');

echo $mainQuery->get();
 
 /* Generate this string: 
    FOR u IN users 
       COLLECT `myvar` = u.city INTO g
    RETURN u
  */



$mainQuery = AqlGen::query('u', 'users')
            ->sort('u.activity', AqlGen::SORT_DESC)
            ->sort(array('u.name','u.created_date')); // asc by default

echo $mainQuery->get();
 
 /* Generate this string: 
    FOR u IN users 
       SORT u.activity DESC, u.name, u.created_date ASC
    RETURN u
  */