src/libraries/adapters/DatabaseSimpleDb.php
/**
    * Query builder for searching photos.
    *
    * @param array $filters Filters to be applied before obtaining the result
    * @param int $limit Total results to return
    * @param offset $offset Starting point of results to return
    * @return array Components required to build the query
    */
Query builder for searching photos.
@param array $filters Filters to be applied before obtaining the result
@param int $limit Total results to return
@param offset $offset Starting point of results to return
@return array Components required to build the query
Query builder for searching photos . @param array $filters Filters to be applied before obtaining the result @param int $limit Total results to return @param offset $offset Starting point of results to return @return array Components required to build the query
private function buildQuery($filters, $limit, $offset)
  {
    $where = '';
    $sortBy = null;
    if(!empty($filters) && is_array($filters))
    {
      foreach($filters as $name => $value)
      {
        switch($name)
        {
          case 'hash':
            $where = $this->buildWhere($where, "hash='{$value}')");
            break;
          case 'groups':
            $where = $this->buildWhere($where, "(groups IN('" . implode("','", $value) . "') OR permission='1')");
            break;
          case 'page':
            if($value > 1)
            {
              $value = min($value, 40); 
              $offset = ($limit * $value) - $limit;
              $page = $value;
            }
            break;
          case 'permission':
            $where = $this->buildWhere($where, "permission='1'");
            break;
          case 'sortBy':
            $sortBy = 'ORDER BY ' . str_replace(',', ' ', $value);
            $field = substr($value, 0, strpos($value, ','));
            $where = $this->buildWhere($where, "{$field} IS NOT NULL");
            break;
          case 'tags':
            if(!is_array($value))
              $value = (array)explode(',', $value);
            $where = $this->buildWhere($where, "tags IN('" . implode("','", $value) . "')");
            break;
          case 'type': 
            $value = $this->_($value);
            $where = $this->buildWhere($where, "type='{$value}'");
            break;
        }
      }
    }
    if(!empty($offset))
    {
      $iterator = max(1, intval($offset - 1));
      $nextToken = null;
      $params = array('ConsistentRead' => 'true');
      if(!isset($page))
        $page = 1;
      $currentPage = 1;
      $thisLimit = min($iterator, $offset);
      do
      {
        $res = $this->db->select("SELECT * FROM `{$this->domainPhoto}` {$where} {$sortBy} LIMIT {$iterator}", $params);
        if(!$res->body->SelectResult->NextToken)
          break;
        $nextToken = $res->body->SelectResult->NextToken;
        $params['NextToken'] = $nextToken;
        $currentPage++;
      }while($currentPage < $page);
    }
    $params = array('ConsistentRead' => 'true');
    if(isset($nextToken) && !empty($nextToken))
      $params['NextToken'] = $nextToken;
    return array('params' => $params, 'where' => $where, 'sortBy' => $sortBy, 'limit' => $limit);
  }
private function buildQuery ( $ filters , $ limit , $ offset ) { $ where = '' ; $ sortBy = null ; if ( ! empty ( $ filters ) && is_array ( $ filters ) ) { foreach ( $ filters as $ name => $ value ) { switch ( $ name ) { case 'hash' : $ where = $ this -> buildWhere ( $ where , " hash=' { $ value } ') " ) ; break ; case 'groups' : $ where = $ this -> buildWhere ( $ where , " (groups IN(' " . implode ( " ',' " , $ value ) . " ') OR permission='1') " ) ; break ; case 'page' : if ( $ value > 1 ) { $ value = min ( $ value , 40 ) ; $ offset = ( $ limit * $ value ) - $ limit ; $ page = $ value ; } break ; case 'permission' : $ where = $ this -> buildWhere ( $ where , " permission='1' " ) ; break ; case 'sortBy' : $ sortBy = 'ORDER BY ' . str_replace ( ',' , ' ' , $ value ) ; $ field = substr ( $ value , 0 , strpos ( $ value , ',' ) ) ; $ where = $ this -> buildWhere ( $ where , " { $ field }  IS NOT NULL " ) ; break ; case 'tags' : if ( ! is_array ( $ value ) ) $ value = ( array ) explode ( ',' , $ value ) ; $ where = $ this -> buildWhere ( $ where , " tags IN(' " . implode ( " ',' " , $ value ) . " ') " ) ; break ; case 'type' : $ value = $ this -> _ ( $ value ) ; $ where = $ this -> buildWhere ( $ where , " type=' { $ value } ' " ) ; break ; } } } if ( ! empty ( $ offset ) ) { $ iterator = max ( 1 , intval ( $ offset - 1 ) ) ; $ nextToken = null ; $ params = array ( 'ConsistentRead' => 'true' ) ; if ( ! isset ( $ page ) ) $ page = 1 ; $ currentPage = 1 ; $ thisLimit = min ( $ iterator , $ offset ) ; do { $ res = $ this -> db -> select ( " SELECT * FROM ` { $ this -> domainPhoto } `  { $ where } { $ sortBy }  LIMIT  { $ iterator } " , $ params ) ; if ( ! $ res -> body -> SelectResult -> NextToken ) break ; $ nextToken = $ res -> body -> SelectResult -> NextToken ; $ params [ 'NextToken' ] = $ nextToken ; $ currentPage ++ ; } while ( $ currentPage < $ page ) ; } $ params = array ( 'ConsistentRead' => 'true' ) ; if ( isset ( $ nextToken ) && ! empty ( $ nextToken ) ) $ params [ 'NextToken' ] = $ nextToken ; return array ( 'params' => $ params , 'where' => $ where , 'sortBy' => $ sortBy , 'limit' => $ limit ) ; }
Query builder for searching photos.
Query builder for searching photos .
// TODO: support logic for multiple conditions // 40 pages at max of 2,500 recursion limit means 100k photos // type for activities