Care to optimize code?
Posted: 07.07.2009, 12:03
I recommend to change from
to this code
Also, here is my version of TZDefaultIdentifierConvertor.Quote
Code: Select all
function TZGenericCachedResolver.FormInsertStatement(Columns: TObjectList;
NewRowAccessor: TZRowAccessor): string;
var
I: Integer;
Current: TZResolverParameter;
TableName: string;
Temp1, Temp2: string;
begin
TableName := DefineTableName;
DefineInsertColumns(Columns);
if Columns.Count = 0 then
begin
Result := '';
Exit;
end;
Temp1 := '';
Temp2 := '';
for I := 0 to Columns.Count - 1 do
begin
Current := TZResolverParameter(Columns[I]);
if Temp1 <> '' then
Temp1 := Temp1 + ',';
Temp1 := Temp1 + IdentifierConvertor.Quote(Current.ColumnName);
if Temp2 <> '' then
Temp2 := Temp2 + ',';
Temp2 := Temp2 + '?';
end;
Result := Format('INSERT INTO %s (%s) VALUES (%s)', [TableName, Temp1, Temp2]);
end;
Code: Select all
function TZGenericCachedResolver.FormInsertStatement(Columns: TObjectList;
NewRowAccessor: TZRowAccessor): string;
var
I: Integer;
Current: TZResolverParameter;
TableName: string;
Temp1, Temp2: string;
l1: Integer;
procedure Append(const app: String);
begin
if Length(Temp1) < l1 + length(app) then
SetLength(Temp1, 2 * (length(app) + l1));
Move(app[1], Temp1[l1+1], length(app));
Inc(l1, length(app));
end;
begin
TableName := DefineTableName;
DefineInsertColumns(Columns);
if Columns.Count = 0 then
begin
Result := '';
Exit;
end;
Temp1 := ''; l1 := 0;
SetLength(Temp2, 2 * Columns.Count - 1);
for I := 0 to Columns.Count - 1 do
begin
Current := TZResolverParameter(Columns[I]);
if Temp1 <> '' then
Append(',');
Append(IdentifierConvertor.Quote(Current.ColumnName));
if I > 0 then
Temp2[I*2] := ',';
Temp2[I*2+1] := '?';
end;
SetLength(Temp1, l1);
Result := Format('INSERT INTO %s (%s) VALUES (%s)', [TableName, Temp1, Temp2]);
end;
Code: Select all
function TZDefaultIdentifierConvertor.Quote(const Value: string): string;
var
QuoteDelim: string;
begin
if IsCaseSensitive(Value) then
begin
QuoteDelim := Metadata.GetIdentifierQuoteString;
if Length(QuoteDelim) > 1 then
Result := QuoteDelim[1] + Result + QuoteDelim[2]
else if Length(QuoteDelim) = 1 then begin
SetLength(Result, length(Value) + 2);
Result[1] := QuoteDelim[1];
Result[length(Result)] := QuoteDelim[1];
Move(value[1], Result[2], length(value));
Exit;
end else
Result := Value;
end else
Result := Value;
end;