2014-11-12 2 views
0

일부 GQL 쿼리 문자열에 일부 인수를 바인딩하려고합니다. 모두의 미세 내가 바인딩 아무것도하지 않고 쿼리를 실행하면Cloud Datastore API - php - GqlQuery - 바인딩 args

$은 = $ DB-> query_entities 결과 ("종류 SELECT * FROM을 WHERE은 fieldName = 4 LIMIT 1");

그러나 일부 인수를 바인딩하는 방법을 모르겠습니다.

function query_entities($query_string, $args=[]){  
    $gql_query = new Google_Service_Datastore_GqlQuery(); 
    $gql_query->setQueryString($query_string); 
    $gql_query->setAllowLiteral(true); 

    if(!empty($args)){ 
     $binding_args = []; 

     foreach ($args as $key => $value) { 
      $binding_value = new Google_Service_Datastore_Value(); 
      $binding_value->setIntegerValue($value); 

      $arg = new Google_Service_Datastore_GqlQueryArg(); 
      $arg->setName($key); 
      $arg->setValue($binding_value); 

      $binding_args[] = $arg; 
     } 

     $gql_query->setNameArgs($binding_args); 
    } 

     $req = new Google_Service_Datastore_RunQueryRequest(); 
     $req->setGqlQuery($gql_query); 

     return $this->dataset->runQuery($this->dataset_id, $req, []); 
} 

$exampleValue = 4; 

$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = :fieldName LIMIT 1", ["fieldName" => $exampleValue]); 

또는를 : : 이것은 내가 그렇게 할 노력하고있어 어떻게 줄 1 열에서 (400) 어휘 오류 : POST https://www.googleapis.com/datastore/v1beta2/datasets/age-of-deployment/runQuery를 호출 '오류 :

function query_entities($query_string, $args=[]){  
    $gql_query = new Google_Service_Datastore_GqlQuery(); 
    $gql_query->setQueryString($query_string); 
    $gql_query->setAllowLiteral(true); 

    if(!empty($args)){ 
     $binding_args = []; 

     foreach ($args as $value) { 
      $binding_value = new Google_Service_Datastore_Value(); 
      $binding_value->setIntegerValue($value); 

      $arg = new Google_Service_Datastore_GqlQueryArg(); 
      $arg->setValue($binding_value); 

      $binding_args[] = $arg; 
     } 

     $gql_query->setNumberArgs($binding_args); 
    } 

     $req = new Google_Service_Datastore_RunQueryRequest(); 
     $req->setGqlQuery($gql_query); 

     return $this->dataset->runQuery($this->dataset_id, $req, []); 
} 

$exampleValue = 4; 

$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = :1 LIMIT 1", [$exampleValue]); 

이것은 내가 무엇을 얻을 "..."의 뒤에 : ":"(58) : 발생했습니다.

답변

1

Cloud Datastore GQLPython GQL (App Engine) 처리 인수 바인딩이 약간 다르게 나타납니다. 이 경우

, 당신은 예를 들어, 대신 :@를 사용해야합니다 :

SELECT * FROM kind WHERE fieldName = @fieldName LIMIT 1 

또는

SELECT * FROM kind WHERE fieldName = @1 LIMIT 1 

가 여기에 클라우드 데이터 저장소 GQL 바인딩 인수의 reference documentation입니다.

+0

나는 @ 1 작품 만 가질 수있었습니다. @fieldName이 작동하지 않습니다. 나는 그것이 작동하도록하는 방법을 모르겠다. 나는 이미 내 대답을 얻었지만, 인수를 인자로 전달할 수있는 방법을 설명 할 수 있다면 정말 좋을 것이다 (@fieldName). –

+0

명명 된 인수와 함께 표시되는 오류 메시지는 무엇입니까? 위의 예제에서 $ key의 값이 "fieldName"인지 확인할 수 있습니까? –

+0

'POST 호출 중 오류 발생 https://www.googleapis.com/datastore/v1beta2/datasets/age-of-deployment/runQuery : (400) 번호가 매겨진 GQL 쿼리 인수에 이름이 있습니다.' 스택 트레이스 : # 0 –

관련 문제