views:

1279

answers:

6

Is there I way I can make this look a little better?

conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7 ' +
          'from table1, table2, table3, etc, etc, etc, etc, etc, ' +
          'where etc etc etc etc etc etc etc etc etc etc etc etc etc'

Like, is there a way I imply concatenation?

+7  A: 

Yes, if you don't mind the extra whitespace being inserted:

 conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7
            from table1, table2, table3, etc, etc, etc, etc, etc,
            where etc etc etc etc etc etc etc etc etc etc etc etc etc'

Alternatively you can use a heredoc:

<<-eos
   select attr1, attr2, attr3, attr4, attr5, attr6, attr7
   from table1, table2, table3, etc, etc, etc, etc, etc,
   where etc etc etc etc etc etc etc etc etc etc etc etc etc'
eos
Mark Byers
You could also use `%Q(...)`
BaroqueBobcat
So that doesn't insert a newline? works for me.
Zombies
@Zombies: Newlines are typically allowed in SQL statements and are just treated as ordinary whitespace.
Mark Byers
+3  A: 
conn.exec = <<eos
  select attr1, attr2, attr3, attr4, attr5, attr6, attr7
  from table1, table2, table3, etc, etc, etc, etc, etc,
  where etc etc etc etc etc etc etc etc etc etc etc etc etc
eos
Peter
+1  A: 
conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7 ' <<
        'from table1, table2, table3, etc, etc, etc, etc, etc, ' <<
        'where etc etc etc etc etc etc etc etc etc etc etc etc etc'

<< is the concatenation operator for strings

Dom Brezinski
The other answers use heredocs, which is a fine alternative
Dom Brezinski
@Dom: Ah yep, that's what it's called!
Mark Byers
+1  A: 

I'm not sure if i well understood your question ...

You can try that :

result = "SELECT #{attr1} FROM {#tables} WHERE #{etc}"

Ruby will resolve the content of #{} as ruby code and replace it by the result of the command. So you can concatenate what ever you want.

Note : Most of the time, if you have a lot of strings to concatenate this method is more performant than the one using the + operator.

Niklaos
+1  A: 
conn.exec [
  "select attr1, attr2, attr3, ...",
  "from table1, table2, table3, ...",
  "where ..."
].join(' ')

This suggestion has the advantage over here-documents and long strings that auto-indenters can indent each part of the string appropriately. But it comes at an efficiency cost.

Aidan Cully
@Aidan, You can replace the commas with backslashes (a la C) and no join (or array) will be needed: The interpreter will concatenate the strings at (I think) parse time, making it pretty quick compared to most of the alternatives. One advantage, though, of joining an array of strings is that some auto-indenters do nicer work than they do with, for example, here-doc strings or with \.
Wayne Conrad
+2  A: 

There are multiple syntaxes for multi-line strings as you've already read. My favorite is Perl-style:

conn.exec %q{select attr1, attr2, attr3, attr4, attr5, attr6, attr7
      from table1, table2, table3, etc, etc, etc, etc, etc,
      where etc etc etc etc etc etc etc etc etc etc etc etc etc}

The multi-line string starts with %q, followed by a {, [ or (, and then terminated by the corresponding reversed character. %q does not allow interpolation; %Q does so you can write things like this:

conn.exec %Q{select attr1, attr2, attr3, attr4, attr5, attr6, attr7
      from #{table_names},
      where etc etc etc etc etc etc etc etc etc etc etc etc etc}

I actually have no idea how these kinds of multi-line strings are called so let's just call them Perl multilines.

Note however that whether you use Perl multilines or heredocs as Mark and Peter have suggested, you'll end up with potentially unnecessary whitespaces. Both in my examples and their examples, the "from" and "where" lines contain leading whitespaces because of their indentation in the code. If this whitespace is not desired then you must use concatenated strings as you are doing now.

Hongli