multiline string (sql query) with ` signs in it

8,172 views
Skip to first unread message

Geert-Johan Riemer

unread,
Jun 29, 2011, 9:49:44 AM6/29/11
to golan...@googlegroups.com
Hey all,

I want to store a large SQL query (string) in my code. Because it is so large, I want to use multiple lines for it.
I can use the ` as begin/end delimiters for a multi-line string, however, the ` sign is also used in my SQL query around every table name and field name. I tried some combinations (like ```, """, ''', "'", etc.) that might also give me multi line string support as other languages do sometimes, but I couldn't find anything that works...
Is there something like that in go? and if not: can it be added? I think it would be a nice feature to have.

Michael Jones

unread,
Jun 29, 2011, 9:53:24 AM6/29/11
to golan...@googlegroups.com
"part 1" +
"part 2" +
:
"part n"
--

Michael T. Jones

   Chief Technology Advocate, Google Inc.

   1600 Amphitheatre Parkway, Mountain View, California 94043

   Email: m...@google.com  Mobile: 650-335-5765  Fax: 650-649-1938

   Organizing the world's information to make it universally accessible and useful


chris dollin

unread,
Jun 29, 2011, 10:07:57 AM6/29/11
to golan...@googlegroups.com

[I find the extremely bare nature of Go's raw strings to be
a definite plus, although I didn't at first.]

Well, there's the startlingly clumsy

"bit the first" +
"and the second" +
"and the third"

[1]
If you weren't using a lot of backticks inside the string, then
you could just write

`+"WHATEVER"+`

when you wanted to use fancy escape syntax (or a quoted
backtick) WHATEVER in your string -- do you really need to
quote *every* table name and field? I suppose it is the safe
thing to do -- or you could post-process the string and so your
own escape replaces, which isn't hard unless they might
turn up as literals in the SQL.

Chris

[1] I say "startling clumsy" because I've had to do that in Java
and it rapidly becomes tedious, especially since you then have
to remember to put the \n's in by hand.

--
Chris "allusive" Dollin

Jan Mercl

unread,
Jun 29, 2011, 10:52:13 AM6/29/11
to golan...@googlegroups.com
Yet another way

chris dollin

unread,
Jun 29, 2011, 11:00:09 AM6/29/11
to golan...@googlegroups.com
On 29 June 2011 15:52, Jan Mercl <jan....@nic.cz> wrote:

> Yet another way.

Nitpick "another" because

or you could post-process the string and so your own
escape replaces,

(complete with missing word).

--
Chris "allusive" Dollin

Jan Mercl

unread,
Jun 29, 2011, 11:14:28 AM6/29/11
to golan...@googlegroups.com
On Wednesday, June 29, 2011 5:00:09 PM UTC+2, chris dollin wrote:
On 29 June 2011 15:52, Jan Mercl <jan....@nic.cz> wrote:

> Yet another way.

Nitpick "another" because

  or you could post-process the string and so your own
  escape replaces,

(complete with missing word).


You're right. I haven't read your post thoroughly enough thus I noticed only your '+' proposal ;-)

Geert-Johan Riemer

unread,
Jun 29, 2011, 1:14:42 PM6/29/11
to golan...@googlegroups.com
hum, the strings.Replace solution IS a solution..
However, I guess the Go compiler is not smart enough to do this at compile-time and therefore this will use CPU power (which shouldn't be necessary).
Wouldn't it be nicer if there was something like:

php's heredoc
$myString = <<ENDOFSTRING
lots of multiline
text and stuff
ENDOFSTRING
// do stuff with $myString

or maybe even better, python's triple quote:
mystr = """
And this is a multiline string literal
enclosed in triple single quotes.
"""

I don't think this is too hard or problematic to add to the compiler (but then, I never wrote a compiler so I can't really tell if it is or not..)

Maybe some kind of support for indentation handling would be nice, like:
    myString := """
        |SELECT *
        |FROM `mytable`
        |WHERE `mytable`.`mycolumn` = 'foo'
        |;
    """

This way the tabs before the | wouldn't be added to the final string, which makes it cleaner. On the other hand; always having to put |'s in front of each line is not so nice.. But it's an idea..

chris dollin

unread,
Jun 29, 2011, 1:58:17 PM6/29/11
to golan...@googlegroups.com
On 29 June 2011 18:14, Geert-Johan Riemer <gjr1...@gmail.com> wrote:
> hum, the strings.Replace solution IS a solution..
> However, I guess the Go compiler is not smart enough to do this at
> compile-time and therefore this will use CPU power (which shouldn't be
> necessary).

You can arrange for it to happen during initialisation, so
its not a big run-time deal.

> Wouldn't it be nicer if there was something like:

Probably not, no.

No, really. Go provides a very simple tool -- the raw
string -- and you can get lots of useful effects by working
on the string. It's not clear to me that it would be worth
adding the complications of php/python's big strings
to Go -- it's not a matter of how hard it is to add to the
compiler, it's a matter of how much complication it adds
to the language and its users.

Chris

(Now, interpolation in non-raw strings would be nice.
But we do have fmt.Sprintf, so that's not a big deal either.)

--
Chris "allusive" Dollin

Steven Blenkinsop

unread,
Jun 29, 2011, 2:07:34 PM6/29/11
to golan...@googlegroups.com

You could write a small program that takes text and outputs the Go
formatted string (%q) which you can paste in your code, along with a
comment containing the original text. Another way is to read it from a
file during from gram initialization (the string would be read from
your binary anyways).

I do agree that multiline quotes look ugly.

fengg...@nibirutech.com

unread,
Sep 23, 2014, 10:59:15 PM9/23/14
to golan...@googlegroups.com
I also encountered this problem when I want to develop a data mapper, in which I have to handle very long SQLs, Golang's raw string won't help, It's really a headache for me.

dlin

unread,
Sep 23, 2014, 11:20:58 PM9/23/14
to golan...@googlegroups.com
http://golang.org/ref/spec#String_literals
According to spec, I think it is cleaner and the authors may not want to change it.

So, use + and " to handle it.


On Wednesday, June 29, 2011 9:49:44 PM UTC+8, Geert-Johan Riemer wrote:

Frank Schröder

unread,
Sep 24, 2014, 3:32:27 AM9/24/14
to golan...@googlegroups.com
I've stumbled over this as well and in the end concluded that in almost every case I didn't need the backticks.


On Wednesday, June 29, 2011 3:49:44 PM UTC+2, Geert-Johan Riemer wrote:

Geert-Johan Riemer

unread,
Sep 24, 2014, 4:24:30 AM9/24/14
to golan...@googlegroups.com
It's been three years since I posted this topic...
Today I rarely use backticks in SQL queries. I now only use it when it's required (for instance when a keyword is also a column name).
In those cases I use a interpreted string (quotes) or when the query spans multiple lines: a raw string (backticks) with `+"`"+` before and after the column name.
This works fine for me.

Liam

unread,
Sep 24, 2014, 4:30:54 AM9/24/14
to golan...@googlegroups.com


On Wednesday, June 29, 2011 6:49:44 AM UTC-7, Geert-Johan Riemer wrote:
Hey all,

I want to store a large SQL query (string) in my code. Because it is so large, I want to use multiple lines for it.

Maybe the new "go generate" proposal helps with this? Perhaps by enabling a SQL pre-processor?
aQ := <<SQL
...
SQL

Andy Balholm

unread,
Sep 24, 2014, 11:45:48 AM9/24/14
to Liam, golan...@googlegroups.com

On Sep 24, 2014, at 1:30 AM, Liam <networ...@gmail.com> wrote:
> Maybe the new "go generate" proposal helps with this? Perhaps by enabling a SQL pre-processor?

I’ve written a preprocessor that allows embedding SQL and HTML in Go files, like this:

<table>
SELECT name FROM customer {
<tr>
<td>$name</td>
</tr>
}
</table>

I haven’t tried to use it with “go generate” yet, though. It’s limited to PostgreSQL at the moment.

If you promise not to laugh at it too much, I might share the code.

Reply all
Reply to author
Forward
0 new messages