1

In a lots of places from a big project the SQL statements were added as

table.SQL.Add(' '
      + ' select tableA.Field1, '
      + ' tableA.Field2 Field2, '      
      + ' tableA.Field3, '
      + ' tableA.Field1 '
      + ' from tableA_!Version tableA left outer join tableB_00 tableB '
      + ' on tableB.Field1ID = :ID '
      + ' and          tableB.Nr = :Nr '
      + ' and tableA.Field12 = tableB.InhaltNummer '
      + ' where tableA.Field233 = 1 '
      + ' order by tableA.Field1 '
);

And I want to select this block of code and change it to be like

with     table.SQL do
 begin    
          Add('select tableA.Field1,');
          Add('tableA.Field2 Field2,');      
          Add('tableA.Field3,');
          Add('tableA.Field1');
          Add('from tableA_!Version tableA left outer join tableB_00 tableB');
          Add('on tableB.Field1ID = :ID');
          Add('and          tableB.Nr = :Nr');
          Add('and tableA.Field12 = tableB.InhaltNummer');
          Add('where tableA.Field233 = 1');
          Add('order by tableA.Field1');
end;          

because later the SQL statement is changed by using direct line number e.g

table.SQL.Lines[5] := 'something'

I was looking at Delphi's refactoring menu, GExpert or CNPack features but did not find something useful.Is there any feature which can help me?

RBA
  • 12,337
  • 16
  • 79
  • 126
  • Regular expression text replace. Or record and replay a macro, but that's a lot of clicks :) – TLama Apr 16 '14 at 10:14
  • I was thinking of regular expressions, but I thought that it can be made in another way – RBA Apr 16 '14 at 10:16
  • 2
    You can do this fairly easily with a plain replace and a regex replace in the Code Editor. The first (plain) you replace `+` with `Add(`. The second use a regex; search for `'$`, and replace with `);`. You'll need to either skip the first match (the one at the end of the line with the original `Add(' ')`) or allow it; either way you'll have to fix that one line afterward. (If you select the block of text first before you do the search/replace, you can use the 'selected text' option and do a replace all. – Ken White Apr 16 '14 at 12:58
  • I thought that something like http://docwiki.embarcadero.com/RADStudio/XE5/en/Live_Templates exists. – RBA Apr 16 '14 at 13:01
  • @KenWhite that way, you can't just run the replace on the whole code... you have to run it on each separate block. – GabrielF Apr 16 '14 at 13:01
  • @GabrielF: That's why I didn't post it as an answer; it's just a possible idea. If you can be sure that the only string concats you have like that (a + at the left side, a single ' at EOL at the right, you can just do a whole file and then fix up the few compiler errors for those that weren't right. (Check a copy into your VCS first, of course, in case you break something badly. :-) I use RegexBuddy instead for this sort of thing, because it has real regex support.) – Ken White Apr 16 '14 at 13:03
  • 1
    I've been trying to build a single regex to do the whole job, but it seems like it's more difficult than I thought. So far, I have [this](http://regexr.com/38neb), which matches everything needed, but fails to output the correct result, because regex engines don't keep all the matches for a repeated group (the exception being the .NET engine) (ref.: http://www.regular-expressions.info/captureall.html)... i don't know how to go any further with this, so I believe that Ken's solution (or macro) might be the way to go. – GabrielF Apr 16 '14 at 13:49
  • What about this answer from @KenWhite from a few years ago. http://stackoverflow.com/questions/1872503/how-to-assign-a-multiline-string-value-without-quoting-each-line/1873980#1873980 –  Apr 16 '14 at 21:02

1 Answers1

0

Castalia for Delphi (full disclosure: I'm the creator) has two tools that will do this. Read about them here: http://delphiblog.twodesk.com/working-with-code-in-your-code-multipaste-and-smart-trim

jthurman
  • 465
  • 7
  • 13