Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

D2010 source formatting 1

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
0
0
US
I have a long SQL sequence that is truncated by a lot of '...'+'..'

i have the lines split not to make it too long but every time i press CTRL+D to format the source, delphi put them back on one line and I get a compilation error because the line is too long.
then i have to go back, and press the enter key to split the concatanation. Is there anyway to exclude a black of source code to be reformated?
Thanks.
P
 
I don't have a version of Delphi which provides source code formatting so I can't try this out. But I would suggest you comment out the lines of SQL using '(*' and '*)' along these lines prior to formatting e.g.
Code:
(*
  SQLcommand := 'SELECT a.field1, b.field2, c.field3 etc'
              + ' FROM master AS a'
              + ' LEFT JOIN slave b'
              + ' LEFT JOIN customer c'
              etc
*)
But you will need to be careful if you have something like
Code:
  SELECT COUNT(*) FROM ...
as the *) will be treated as closing the comment.

Of course you must remember to uncomment the SQL statement after formatting your source code.




Andrew
 
I will try that as a workaround and let you know...
Thanks.
 
Don't build one big long string. The SQL text is stored in a TStrings object, which you're directly modifying by its Text property. When all you use is the Text property of this SQL TStrings object, then it's all put into one single line.

Therefore, you need to make use of the TStrings... I made it a complex example to show you how this method makes things more flexible and easier to work with

Code:
Q.SQL.Text:= 'select'; 
Q.SQL.Append('a.ID'); //At least one static field
Q.SQL.Append(', a.Field2'); 
Q.SQL.Append(', b.Field3'); 
if UseField4 then
  Q.SQL.Append(', c.Field4'); 
Q.SQL.Append('from TableA a'); 
Q.SQL.Append('left join TableB b on b.ID = a.BID'); 
Q.SQL.Append('left join TableC c on c.ID = a.CID'); 
Q.SQL.Append('left join TableD d on d.ID = a.DID'); 
Q.SQL.Append('where a.ID > 0'); //At least one static condition
  //so the rest of the dynamic conditions can start with 'and' instead of 'where'
if SomeFilter then
  Q.SQL.Append('and b.SomeField = 123'); 
if SomeOtherFilter then
  Q.SQL.Append('and c.SomeOtherField in (1, 3, 5)'); 
if DoOrder then begin
  Q.SQL.Append('order by'); 
  case OrderField of
    1: Q.SQL.Append('a.ID'); 
    2: Q.SQL.Append('b.SomeField');
    3: Q.SQL.Append('c.SomeOtherField');
  end;
  if DoOrderDesc then
    Q.SQL.Append('desc');
end;

Sometimes, I may declare a sub-procedure within one of such procedures, which just makes adding fields even easier...

Code:
procedure RefreshSomeData;
  //Sub-procedure 'A' - simply adds new string to SQL query
  procedure A(const S: String);
  begin
    Q.SQL.Append(S);
  end;
begin
  A('select');
  A('Field1, Field2, Field3');
  A('from MyTable');
end;


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top