/**
  * sets model name based on aql, sets this as a tmp model (_aql_set_in_constructor)
  * @param string $aql      aql statemnt or empty
  * @return Model
  */
 public function getModelAql($aql = null)
 {
     if ($this->getStoredAql()) {
         return $this;
     }
     if (!$aql) {
         $this->_getAql($this->_model_name);
     } elseif (aql::is_aql($aql)) {
         $this->_aql = $aql;
         $this->_aql_set_in_constructor = true;
     } else {
         $this->_model_name = $aql;
         $this->_getAql($this->_model_name);
     }
     return $this;
 }
/**
 *	shorthand for the aql2array class
 *	@param string $aql
 *	@return array
 */
function aql2array($param1, $param2 = null)
{
    if (aql::is_aql($param1)) {
        $r = new aql2array($param1);
        return $r->aql_array;
    } else {
        return aql2array::get($param1, $param2);
    }
}
 /**
  * Returns a getList object with filters defined based on the given AQL
  * @param   string      $aql
  * @param   Boolean     $search_operators
  * @return  \getList
  * @throws  \Exception  if invalid AQL
  */
 public static function autoGenerate($aql, $search_operators = false)
 {
     if (!aql::is_aql($aql)) {
         throw new \Exception('autoGenerate requires AQL.');
     }
     $aql_array = aql2array($aql);
     $min_aql = aql::minAQLFromArr($aql_array);
     $fields = array();
     foreach ($aql_array as $k => $f) {
         $fields = array_merge($fields, $f['fields']);
     }
     $lst = new self();
     $lst->setAQL($min_aql)->defineFilters(array_map(function ($field) use($lst, $search_operators, $fields) {
         $op = array_search($field, $fields);
         return array('operator' => $search_operators ? $op : null, 'callback' => function ($val) use($lst, $fields, $field) {
             $where = \getList::prepVal(\getList::csvToArray($val));
             $lst->where[] = "{$field} in {$where}";
         });
     }, $fields));
     return $lst;
 }
 /**
  * @param   string  $param1
  * @param   string  $param2
  * @param   mixed   $options
  * @return  mixed
  */
 public static function value($param1, $param2, $options = array())
 {
     if (!$param2) {
         return null;
     }
     // third param can be a db connection resource
     if (is_object($options) && get_class($options) == 'ADODB_postgres7') {
         $db_conn = $options;
         $options = array();
     }
     // get connection
     $db_conn = $db_conn ?: $options['db'];
     $db_conn = $db_conn ?: self::getDB();
     $is_aql = aql::is_aql($param1);
     // normalize primary table and aql
     if ($is_aql) {
         $aql = $param1;
         $primary_table = aql::get_primary_table($aql);
     } else {
         list($primary_table, $field) = explode('.', $param1);
         $aql = "{$primary_table} { {$field} }";
     }
     // get where
     $multiple = false;
     $where = call_user_func(function () use($primary_table, $param2, &$multiple) {
         $spr = '%s.%s = \'%s\'';
         $decrypt = function ($r) use($primary_table) {
             return is_numeric($r) ? $r : decrypt($r, $primary_table);
         };
         if (is_numeric($param2)) {
             return sprintf($spr, $primary_table, 'id', $param2);
         }
         if (!is_array($param2)) {
             // check for ide
             $id = $decrypt($param2);
             if (is_numeric($id)) {
                 return sprintf($spr, $primary_table, 'id', $id);
             }
             // otherwise check for slug field on table
             if (!aql2array::table_field_exists($primary_table, 'slug')) {
                 return;
             }
             return sprintf($spr, $primary_table, 'slug', $param2);
         }
         // this is an array
         $multiple = true;
         $param2 = array_filter(array_map($decrypt, $param2));
         $param2[] = -1;
         $ids = implode(',', $param2);
         return "{$primary_table}.id in ({$ids})";
     });
     // return if we dont find a where clause
     if (!$where) {
         return false;
     }
     $clause = array($primary_table => array('where' => array($where), 'order by' => 'id asc'));
     $rs = aql::select($aql, $clause, null, null, null, $db_conn);
     if ($multiple) {
         return $rs;
     }
     if ($is_aql) {
         return $rs[0];
     }
     return $rs[0][$field];
 }