如何在Golang中运行数据存储区GQL查询?

时间:2017-04-30 15:09:35

标签: go google-cloud-datastore gql

我在GQL中有以下查询:

"SELECT * FROM Task WHERE Uuid = \"FOOBAR\" ORDER BY CreateTimeMs DESC LIMIT 1"

如何在Golang中直接运行此查询?

query := datastore.NewQuery("SELECT * FROM Task WHERE Uuid = \"FOOBAR\" ORDER BY CreateTimeMs DESC LIMIT 1")似乎不正确。

更新
我非常清楚Query types。问题是我想直接使用GQL而不是将它们转换为查询类型。

2 个答案:

答案 0 :(得分:1)

当我在GQL documentation中看到这个说明时,我正在寻找解决完全相同的问题:

  

注意: Google Cloud Client Library for Java和Google Cloud Client Library for Ruby支持GQL,但其他Google Cloud客户端库则不支持。

这样就不行了......

但是,在REST api projects.runQuery上实现它应该是可行的,但是你必须自己将结果解组成结构。

答案 1 :(得分:0)

查看Query类型文档,通过查看其方法并阅读各自的文档,您应该能够理解如何将SQL转换为$sub_match_teams = " SELECT m1.*, m2.team_id AS opponent_id FROM match_teams m1 JOIN match_teams m2 ON m2.match_id = m1.match_id AND m2.team_id <> m1.team_id WHERE m1.match_id IN (10,12,15,61,62,63,66,70,73,77) "; $sub_result = " SELECT team_id, COUNT(match_id) AS total_matches, SUM(goals_for) AS total_goals_for, SUM(points) AS total_points FROM ({$sub_match_teams}) m GROUP BY team_id "; $final_query = " SELECT r1.*, SUM(m.points) AS total_points_tie FROM ({$sub_result}) r1 LEFT JOIN ({$sub_result}) r2 ON r2.total_points = r1.total_points AND r2.team_id <> r1.team_id LEFT JOIN ({$sub_match_teams}) m ON m.team_id = r1.team_id AND m.opponent_id = r2.team_id GROUP BY r1.team_id ORDER BY r1.total_points DESC, total_points_tie DESC, r1.total_goals_for DESC "; 特定方法调用。

例如,你的sql可以写成:

datastore.Query

这只会构建您的查询值,以获得run查询所需的实际结果,以获得iterator Next method可用于读取结果。

如果你想避免run-iterate-next舞蹈,你可以使用GetAll查询方法。

q := datastore.NewQuery("Task").Filter("Uuid =", "FOOBAR").Order("-CreateTimeMs").Limit(1)

在这里阅读更多内容: