【问题标题】:Cloud Datastore API - php - GqlQuery - Binding argsCloud Datastore API - php - GqlQuery - 绑定参数
【发布时间】:2014-11-12 03:05:09
【问题描述】:

我正在尝试将一些参数绑定到我的 GQL 查询字符串。 当我在不绑定任何东西的情况下运行查询时一切都很好:

$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = 4 限制 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]);

或者:

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]);

这是我得到的: '调用 POST https://www.googleapis.com/datastore/v1beta2/datasets/age-of-deployment/runQuery 时出错:(400) 第 1 行第 52 列出现词法错误。遇到:":" (58),之后:""' in ...

【问题讨论】:

    标签: google-app-engine google-cloud-datastore gql


    【解决方案1】:

    Cloud Datastore GQLPython GQL (App Engine) 处理参数绑定略有不同。

    在这种情况下,您需要使用@ 而不是:,例如:

    SELECT * FROM kind WHERE fieldName = @fieldName LIMIT 1
    

    SELECT * FROM kind WHERE fieldName = @1 LIMIT 1
    

    这是 Cloud Datastore GQL 中用于参数绑定的 reference documentation

    【讨论】:

    • 我只能得到@1 的工作。 @fieldName 不起作用。我不知道如何让它工作。我已经得到了答案,但如果你能解释我如何通过名称 (@fieldName) 传递参数,那就太好了。
    • 您在命名参数中看到的错误消息是什么?您能否确认上例中 $key 的值是“fieldName”?
    • '调用 POST googleapis.com/datastore/v1beta2/datasets/age-of-deployment/… 时出错:(400) 编号的 GQL 查询参数有名称。'在 C:\Users\Behnam\Documents\AgeOfDeployment\Backend\Google\Http\REST.php:79 堆栈跟踪:#0
    • 是的,我使用的 $key 名称与 @ 之后使用的名称完全相同,这意味着 $DB->query_entities("SELECT * FROM kind WHERE fieldName = @fieldName LIMIT 1", ["字段名" => $exampleValue]);
    • 错误消息表明在查询参数上设置了名称,然后将其用作数字 arg(例如,使用 setName(),然后使用 setNumerArgs())。但是,我没有在您上面发布的代码中看到这种情况。您可以尝试打印出 Google_Service_Datastore_GqlQuery 对象来确认吗?
    猜你喜欢
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多