在Golang中处理长查询

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

Long queries in golang

问题

这是一个关于完全不了解golang的问题,目标是找到一种方法使长查询更易读。

我的尝试是将SQL文本放入一个变量中,然后执行该变量。

伪代码(没有真正的代码):

var query = 
SELECT * FROM foo
UNION ALL
SELECT * FROM bar
UNION ALL
SELECT * FROM other
...

db.prepare (var query)
db.query (var query)

这可能是一个愚蠢的问题,但我已经搜索过了,没有找到如何在go中使长查询更加“可读”的线索。大多数示例都是基于“hello world”级别的。在真实世界中,查询可能非常长。

谢谢。

英文:

This is a question based on absolute no knowledge of golang and the aim is to find if there is a way to make long queries readable.

My attempt is to put the sql text in a variable and then execute the variable.

Pseudocode (no real code):

var query = 
SELECT * FROM foo
UNION ALL
SELECT * FROM bar
UNION ALL
SELECT * FROM other
...

db.prepare (var query)
db.query (var query)

This is maybe a dumb question, but I have searched and found no clue how to make long queries more "readable" in go. Most examples are based on "hello world" level. In the real world queries can be quite long.

TIA,

答案1

得分: 29

你可以将查询声明为多行字符串文字。

query := `
  SELECT * FROM foo
  UNION ALL
  SELECT * FROM bar
  UNION ALL
  SELECT * FROM other`

然后可以使用DB.Query来执行查询。

rows, err := db.Query(query)

有许多不同的驱动程序可用于许多不同的SQL数据库。它们都会提供一个DB对象供您使用。因此,您可以适当地使用DB.Prepare和DB.Query。有关更多信息,请查看database/sql包的文档。

英文:

You can declare the query as a multiline string literal.

query := `
  SELECT * FROM foo
  UNION ALL
  SELECT * FROM bar
  UNION ALL
  SELECT * FROM other`

And use it with DB.Query.

rows, err := db.Query(query)

There are many different drivers for many different sql databases you can use. They all would give you a DB object to work with. So you can use DB.Prepare, DB.Query appropriately. Check docs of database/sql package for more info.

答案2

得分: 1

有两种方法可以实现这个。

1. 使用反引号:
使用键盘上的符号**`,也称为反引号**。
这样,你的查询将会像这样:

query := `INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
          VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');`

2. 使用字符串拼接:
在行之间使用字符串拼接字符,这样你的查询将会像这样:

query := "INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)" +
		"VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');"

我更喜欢第一种方法 在Golang中处理长查询

英文:

There are two methods for this

1. Using backquote:
Use (backquote\backtick) which is with symbol ` in keyboard.
So that your query will look like

query := `INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
          VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');`

2. Using string concatenation:
Use string concatenation characters between lines So that your query will look like

query :="INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)"+
		"VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');"

I prefer the 1st one 在Golang中处理长查询

huangapple
  • 本文由 发表于 2016年3月27日 15:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/36244767.html
匿名

发表评论

匿名网友

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

确定