GDS\Mapper\ProtoBufGQLParser::parse PHP Метод

parse() публичный Метод

We use preg_replace_callback to "prune" down the GQL string so we are left with nothing
public parse ( $str_gql, google\appengine\datastore\v4\GqlQueryArg[] $arr_named_params = [] )
$str_gql
$arr_named_params google\appengine\datastore\v4\GqlQueryArg[]
    public function parse($str_gql, $arr_named_params = [])
    {
        // Record our input params
        foreach ($arr_named_params as $obj_param) {
            if ($obj_param->hasValue()) {
                $this->arr_named_params[$obj_param->getName()] = $obj_param->getValue();
            } else {
                if ($obj_param->hasCursor()) {
                    $this->arr_named_params[$obj_param->getName()] = $obj_param->getCursor();
                }
            }
        }
        // Cleanup before we begin...
        $str_gql = trim($str_gql);
        // Ensure it's a 'SELECT *' query
        if (!preg_match('/^SELECT\\s+\\*\\s+FROM\\s+(.*)/i', $str_gql)) {
            throw new GQL("Sorry, only 'SELECT *' (full Entity) queries are currently supported by php-gds");
        }
        // Tokenize quoted items ** MUST BE FIRST **
        $str_gql = preg_replace_callback("/([`\"'])(?<quoted>.*?)(\\1)/", [$this, 'tokenizeQuoted'], $str_gql);
        // Kind
        $str_gql = preg_replace_callback('/^SELECT\\s+\\*\\s+FROM\\s+(?<kind>[^\\s]*)/i', [$this, 'recordKind'], $str_gql, 1);
        // Offset
        $str_gql = preg_replace_callback('/OFFSET\\s+(?<offset>[^\\s]*)/i', [$this, 'recordOffset'], $str_gql, 1);
        // Limit
        $str_gql = preg_replace_callback('/LIMIT\\s+(?<limit>[^\\s]*)/i', [$this, 'recordLimit'], $str_gql, 1);
        // Order
        $str_gql = preg_replace_callback('/ORDER\\s+BY\\s+(?<order>.*)/i', [$this, 'recordOrder'], $str_gql, 1);
        // Where
        $str_gql = preg_replace_callback('/WHERE\\s+(?<where>.*)/i', [$this, 'recordWhere'], $str_gql, 1);
        // Check we're done
        $str_gql = trim($str_gql);
        if (strlen($str_gql) > 0) {
            throw new GQL("Failed to parse entire query, remainder: [{$str_gql}]");
        }
    }

Usage Example

Пример #1
0
 /**
  * Take a GQL RunQuery request and convert to a standard RunQuery request
  *
  * Always expected to be called in the stack ->gql()->execute()->runGqlAsBasicQuery()
  *
  * @param ProtocolMessage $obj_gql_request
  * @return null
  * @throws \GDS\Exception\GQL
  */
 private function executeGqlAsBasicQuery(ProtocolMessage $obj_gql_request)
 {
     // Set up the new request
     $obj_query_request = $this->setupRunQuery();
     $obj_query = $obj_query_request->mutableQuery();
     // Transfer any transaction data to the new request
     /** @var RunQueryRequest $obj_gql_request */
     if ($obj_gql_request->mutableReadOptions()->hasTransaction()) {
         $obj_query_request->mutableReadOptions()->setTransaction($obj_gql_request->mutableReadOptions()->getTransaction());
     }
     // Parse the GQL string
     $obj_gql_query = $obj_gql_request->getGqlQuery();
     $obj_parser = new ProtoBufGQLParser();
     $obj_parser->parse($obj_gql_query->getQueryString(), $obj_gql_query->getNameArgList());
     // Start applying to the new RunQuery request
     $obj_query->addKind()->setName($obj_parser->getKind());
     foreach ($obj_parser->getOrderBy() as $arr_order_by) {
         $obj_query->addOrder()->setDirection($arr_order_by['direction'])->mutableProperty()->setName($arr_order_by['property']);
     }
     // Limits, Offsets, Cursors
     $obj_parser->getLimit() && $obj_query->setLimit($obj_parser->getLimit());
     $obj_parser->getOffset() && $obj_query->setOffset($obj_parser->getOffset());
     $obj_parser->getStartCursor() && $obj_query->setStartCursor($obj_parser->getStartCursor());
     // @todo @ $obj_query->setEndCursor();
     // Filters
     $int_filters = count($obj_parser->getFilters());
     if (1 === $int_filters) {
         $this->configureFilterFromGql($obj_query->mutableFilter()->mutablePropertyFilter(), $obj_parser->getFilters()[0]);
     } else {
         if (1 < $int_filters) {
             $obj_composite_filter = $obj_query->mutableFilter()->mutableCompositeFilter()->setOperator(\google\appengine\datastore\v4\CompositeFilter\Operator::AND_);
             foreach ($obj_parser->getFilters() as $arr_filter) {
                 $this->configureFilterFromGql($obj_composite_filter->addFilter()->mutablePropertyFilter(), $arr_filter);
             }
         }
     }
     return $this->execute('RunQuery', $obj_query_request, new RunQueryResponse());
 }