如何在Golang中运行Datastore GQL查询?

huangapple go评论80阅读模式
英文:

How to run a Datastore GQL query in Golang?

问题

我在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") 似乎是不正确的。

更新:
我非常了解查询类型。问题是我想直接使用GQL而不是将它们转换为查询类型。

英文:

I have the following query in GQL:

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

How can I directly run this query in Golang?

query := datastore.NewQuery("SELECT * FROM Task WHERE Uuid = \"FOOBAR\" ORDER BY CreateTimeMs DESC LIMIT 1") seems to be incorrect.

UPDATE:<br>
I am very aware of the Query types. The thing is that I would like to use GQL directly instead of translating them into Query types.

答案1

得分: 1

我在搜索解决完全相同的问题时,偶然在GQL文档中看到了这个注释:

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

所以这是不可行的...

然而,通过REST API projects.runQuery来实现应该是可行的,但是你需要自己将结果解组为结构体。

英文:

I was searching to solve exactly the same problem when I came across this note in the GQL documentation:

> Note: Google Cloud Client Library for Java and Google Cloud Client Library for Ruby support GQL, but other Google Cloud client libraries do not.

So that's a no-go ...

However, it should be feasible to implement it over the REST api projects.runQuery but then you have to unmarshal the result into structs by yourself.

答案2

得分: 0

请参考Query类型的文档,通过查看其方法并阅读各自的文档,您应该能够理解如何将您的SQL转换为datastore.Query特定的方法调用。

例如,您的SQL可以写成:

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

这只是构建了您的Query值,要获取实际结果,您需要运行查询以获取一个迭代器,其Next方法可用于读取结果。

如果您想避免运行-迭代-下一个的操作,可以使用GetAll查询方法。

var tt = []*Task{}
q := datastore.NewQuery("Task").Filter("Uuid =", "FOOBAR").Order("-CreateTimeMs").Limit(1)
if _, err := q.GetAll(tt); err != nil {
    panic(err)
}
log.Println(tt[0]) // your task

在这里阅读更多信息:

英文:

Take a look at the Query type documentation, from looking at its methods and reading their respective documentation you should be able to understand how to translate your SQL into datastore.Query specific method calls.

For example your sql can be written as:

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

This just builds your Query value, to get the actual result you need to run the query to get an iterator whose Next method can be used to read the result.

If you want to avoid the run-iterate-next dance you can use the GetAll query method.

var tt = []*Task{}
q := datastore.NewQuery(&quot;Task&quot;).Filter(&quot;Uuid =&quot;, &quot;FOOBAR&quot;).Order(&quot;-CreateTimeMs&quot;).Limit(1)
if _, err := q.GetAll(tt); err != nil {
    panic(err)
}
log.Println(tt[0]) // your task

Read more here:

huangapple
  • 本文由 发表于 2017年4月30日 23:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/43707878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定