3

Is there a nice way to assign a string to a variable when it spans many lines?

The reason for this is I have some large SQL statements (which I want in the pas) but it's annoying like this

var
  sql : string;
begin
  sql := 'SELECT * ' +
         'FROM foo ' +
         'WHERE `this`=0';

That is annoying to copy and paste into terminal / another program because I have to remove the ' and ' + etc.

Is there a way to so something like...

var
  sql : string;
begin
  sql := ""SELECT *
         FROM foo
         WHERE `this`=0"";

So some way to assign a block of text/string with new lines without having to concat it.

Wizzard
  • 12,582
  • 22
  • 68
  • 101

6 Answers6

4

Your question is:

Can a Delphi string literal span multiple lines?

The answer is no.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
4

As there is no way of expressing strings in this way in SQL, I normally use the RegEx search and replace available in the Delphi IDE to format strings in the required way.

  SELECT *
  FROM foo
  WHERE `this`=0

Regular expression replace dialog

This replaces any line with the line enclosed in quotes, followed by + sLineBreak +

  sql :=
      '  SELECT *' + sLineBreak +
      '  FROM foo' + sLineBreak +
      '  WHERE `this`=0' + sLineBreak +

I then just tidy up the last line:

  sql :=
      '  SELECT *' + sLineBreak +
      '  FROM foo' + sLineBreak +
      '  WHERE `this`=0';

Of course the same can be done with any preceding or trailing text, such as qry.SQL.Add('\0');

Gerry Coll
  • 5,867
  • 1
  • 27
  • 36
1

Not that I know of (at least not out of the box). Anyway, you might want to take a look at this:

How to assign a multiline string value without quoting each line?

Community
  • 1
  • 1
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
1

You could keep the SQL in a component which has a TStrings property like TSQLQuery, but my solution for longer / complex statements is to keep an 'example' copy as a source code comment, which has actual parameters to make tests easier, and keep both version in sync.

mjn
  • 36,362
  • 28
  • 176
  • 378
1

If you like the way C# does it (like I do), then don't forget to vote for this QC report:

http://qc.embarcadero.com/wc/qcmain.aspx?d=2012

It suggests to make your example look like this:

var
  sql : string;
begin
  sql := @'SELECT *
         FROM foo
         WHERE `this`=0';
Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • Hmm, how would that be ambiguous? Am I missing something? – Wouter van Nifterick May 02 '11 at 18:49
  • the whitespace is ambigous this matters not for the sql example, but in other contexts it would sucks and additionally it breaks backward compatability (but that another matter of course) – Johan May 02 '11 at 18:58
  • Note that [QualityCentral has now been shut down](https://community.embarcadero.com/blogs/entry/quality-keeps-moving-forward), so you can't access `qc.embarcadero.com` links anymore. If you need access to old QC data, look at [QCScraper](http://www.uweraabe.de/Blog/2017/06/09/how-to-save-qualitycentral/). – Remy Lebeau Jun 09 '17 at 17:32
1

If you install GExperts in Delphi, the IDE will automatically insert a '+ after pressing >enter< if you're inside a string and haven't closed it yet.

Download link: http://www.gexperts.org/download.html

Johan
  • 74,508
  • 24
  • 191
  • 319