function statement(Sep, Quote: String): string;
var
I: integer;
begin
Result := Sep;
for I := 0 to
pred(ItemCount) do
Result
:= format('%s%s%s%s%s',[Result,Sep,Quote,Item[I],Quote])
Delete(Result, 1,
Length(Sep);
end;
The
main advantage of this is speed where the list is large - the conditional is
removed from the iteration.
(Note that many variants of SQL limit the number of
parameters in a list to 255 and the length of the list to 2000 or
4000.)
One
of our standard method for comma separated lists is to use a stringlist (not
efficient but simple and avoids empty lists):
function statement(FieldName, EmptyCondition: String; Quote:
Boolean): string;
var
i integer;
ResList :
TStringList;
begin
ResList := TStringList.Create;
for I := 0 to pred(ItemCount)
do
if Quote
ResList.Add(Format('''%s''',[Item[i]]))
else
ResList.Add(Item[i]);
if ResList.Count > 0
then
Result := Format('%s in
(%s)',[FieldName, ResList.CommaText]
else
Result :=
EmptyCondition;
ResList.Free;
end;
Hi all,
Further to my enpty string question, I'm
writing a lot of
functions that return a set of strings joined
by a separator
ie 'col1, col2,...' or 'col1 = 'a' and colb =
'b'' (as you may guess
these are all parts of SQL
Statements)
given a function 'Itemcount' that
returns the number of items
and item(i) that returns the item string, and
function sep what is the
best form of such a function
to seed the duscussion I'll give you one of my
variants
function statement: string;
var
I: integer;
begin
Result := '';
for I := 0 to pred(ItemCount)
do
if I = 0 then Result :=
Item(I)
else Result :=
format('%s%s%s',[Result,Sep,Item(I)])
end;
Variants and explainations pls
Neven