Skip to content

Commit 82a4d42

Browse files
committed
code cleanup
1 parent c042369 commit 82a4d42

8 files changed

+159
-165
lines changed

Source/Persistence/Mapping/Spring.Persistence.Mapping.Attributes.pas

+5-5
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,9 @@ constructor TableAttribute.Create(const tableName: string; const schema: string)
394394

395395
function TableAttribute.GetNamespace: string;
396396
begin
397-
Result := '';
398-
if Schema <> '' then
399-
Result := Schema + '.';
400-
Result := Result + TableName;
397+
Result := TableName;
398+
if fSchema <> '' then
399+
Result := fSchema + '.' + Result;
401400
end;
402401

403402
function TableAttribute.GetTableName: string;
@@ -416,7 +415,8 @@ function TableAttribute.GetTableName: string;
416415

417416
{$REGION 'SequenceAttribute'}
418417

419-
constructor SequenceAttribute.Create(const sequenceName: string; initialValue: NativeInt; increment: Integer);
418+
constructor SequenceAttribute.Create(const sequenceName: string;
419+
initialValue: NativeInt; increment: Integer);
420420
begin
421421
inherited Create;
422422
fSequenceName := sequenceName;

Source/Persistence/SQL/Spring.Persistence.SQL.Generators.ASA.pas

+11-12
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,29 @@ function TASASQLGenerator.GenerateGetLastInsertId(
6565
function TASASQLGenerator.GeneratePagedQuery(const sql: string;
6666
limit, offset: Integer): string;
6767
var
68-
LBuilder: TStringBuilder;
69-
LSQL: string;
68+
sqlBuilder: TStringBuilder;
69+
s: string;
7070
begin
71-
LBuilder := TStringBuilder.Create;
72-
LSQL := sql;
71+
sqlBuilder := TStringBuilder.Create;
72+
s := sql;
7373
try
74-
if EndsStr(';', LSQL) then
75-
SetLength(LSQL, Length(LSQL)-1);
74+
if EndsStr(';', s) then
75+
SetLength(s, Length(s) - 1);
7676

77-
LBuilder.Append('SELECT * FROM (')
77+
sqlBuilder.Append('SELECT * FROM (')
7878
.AppendLine
7979
.Append(' SELECT *, ROW_NUMBER() OVER (ORDER BY (NULL)) AS ORM_ROW_NUM FROM (')
8080
.AppendLine.Append(' ')
81-
.Append(LSQL)
81+
.Append(s)
8282
.Append(') AS ORM_TOTAL_1')
8383
.AppendLine
8484
.Append(' ) AS ORM_TOTAL_2')
8585
.AppendLine
86-
.AppendFormat(' WHERE (ORM_ROW_NUM>%0:D) AND (ORM_ROW_NUM <= %0:D+%1:D);', [offset, limit]);
86+
.AppendFormat(' WHERE (ORM_ROW_NUM>%0:d) AND (ORM_ROW_NUM <= %0:d+%1:d);', [offset, limit]);
8787

88-
Result := LBuilder.ToString;
88+
Result := sqlBuilder.ToString;
8989
finally
90-
LBuilder.Free;
90+
sqlBuilder.Free;
9191
end;
9292
end;
9393

@@ -96,7 +96,6 @@ function TASASQLGenerator.GetQueryLanguage: TQueryLanguage;
9696
Result := qlASA;
9797
end;
9898

99-
10099
function TASASQLGenerator.GetSQLDataTypeName(const field: TSQLCreateField): string;
101100
begin
102101
Result := inherited GetSQLDataTypeName(field);

Source/Persistence/SQL/Spring.Persistence.SQL.Generators.Ansi.pas

+32-28
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ TAnsiSQLGenerator = class(TAbstractSQLGenerator)
7070
function GetTempTableName: string; virtual;
7171
function GetPrimaryKeyDefinition(const field: TSQLCreateField): string; virtual;
7272
function GetSplitStatementSymbol: string; virtual;
73-
procedure ParseFullTablename(const fullTableName: string;
73+
procedure ParseFullTableName(const fullTableName: string;
7474
out tableName, schemaName: string); virtual;
7575
function GetEscapeChar: Char; override;
7676
function GetUpdateVersionFieldQuery(const command: TUpdateCommand;
@@ -126,20 +126,20 @@ function TAnsiSQLGenerator.DoGenerateBackupTable(
126126
begin
127127
//select old data to temporary table
128128
SetLength(Result, 2);
129-
Result[0] := Format('SELECT * INTO %0:S FROM %1:S',
129+
Result[0] := Format('SELECT * INTO %0:s FROM %1:s',
130130
[GetTempTableName, tableName]);
131131
//drop table
132-
Result[1] := Format('DROP TABLE %0:S ', [tableName]);
132+
Result[1] := Format('DROP TABLE %0:s ', [tableName]);
133133
end;
134134

135135
function TAnsiSQLGenerator.DoGenerateBackupTableUsingCreate(
136136
const tableName: string): TArray<string>;
137137
begin
138138
SetLength(Result, 2);
139-
Result[0] := Format('CREATE TABLE %0:S AS SELECT * FROM %1:S',
139+
Result[0] := Format('CREATE TABLE %0:s AS SELECT * FROM %1:s',
140140
[GetTempTableName, tableName]);
141141
//drop table
142-
Result[1] := Format('DROP TABLE %0:S ', [tableName]);
142+
Result[1] := Format('DROP TABLE %0:s ', [tableName]);
143143
end;
144144

145145
function TAnsiSQLGenerator.DoGenerateCreateTable(const tableName: string;
@@ -151,7 +151,7 @@ function TAnsiSQLGenerator.DoGenerateCreateTable(const tableName: string;
151151
begin
152152
sqlBuilder := TStringBuilder.Create;
153153
try
154-
sqlBuilder.AppendFormat('CREATE TABLE %0:S ', [tableName])
154+
sqlBuilder.AppendFormat('CREATE TABLE %0:s ', [tableName])
155155
.Append('(')
156156
.AppendLine;
157157
for i := 0 to columns.Count - 1 do
@@ -161,7 +161,7 @@ function TAnsiSQLGenerator.DoGenerateCreateTable(const tableName: string;
161161
sqlBuilder.Append(', ').AppendLine;
162162

163163
//0 - Column name, 1 - Column data type name, 2 - NOT NULL condition
164-
sqlBuilder.AppendFormat('%0:S %1:S %2:S %3:S', [
164+
sqlBuilder.AppendFormat('%0:s %1:s %2:s %3:s', [
165165
GetEscapedFieldName(field),
166166
GetSQLDataTypeName(field),
167167
IfThen(cpNotNull in field.Properties, 'NOT NULL', 'NULL'),
@@ -182,12 +182,12 @@ function TAnsiSQLGenerator.DoGenerateRestoreTable(const tableName: string;
182182
begin
183183
SetLength(Result, 2);
184184

185-
Result[0] := Format('INSERT INTO %0:S (%2:S) SELECT %3:S FROM %1:S' + sLineBreak,
185+
Result[0] := Format('INSERT INTO %0:s (%2:s) SELECT %3:s FROM %1:s' + sLineBreak,
186186
[tableName, GetTempTableName, GetCreateFieldsAsString(createColumns),
187187
GetCopyFieldsAsString(createColumns, dbColumns)]);
188188

189189
//drop temporary table
190-
Result[1] := Format('DROP TABLE %0:S', [GetTempTableName]);
190+
Result[1] := Format('DROP TABLE %0:s', [GetTempTableName]);
191191
end;
192192

193193
function TAnsiSQLGenerator.GenerateCreateForeignKey(
@@ -202,13 +202,13 @@ function TAnsiSQLGenerator.GenerateCreateForeignKey(
202202
for field in command.ForeignKeys do
203203
begin
204204
sqlBuilder.Clear;
205-
sqlBuilder.AppendFormat('ALTER TABLE %0:S ', [command.Table.Name])
205+
sqlBuilder.AppendFormat('ALTER TABLE %0:s ', [command.Table.Name])
206206
.AppendLine
207-
.AppendFormat('ADD CONSTRAINT %0:S', [field.ForeignKeyName])
207+
.AppendFormat('ADD CONSTRAINT %0:s', [field.ForeignKeyName])
208208
.AppendLine
209-
.AppendFormat('FOREIGN KEY(%0:S)', [GetEscapedFieldName(field)])
209+
.AppendFormat('FOREIGN KEY(%0:s)', [GetEscapedFieldName(field)])
210210
.AppendLine
211-
.AppendFormat(' REFERENCES %0:S (%1:S)', [field.ReferencedTableName,
211+
.AppendFormat(' REFERENCES %0:s (%1:s)', [field.ReferencedTableName,
212212
AnsiQuotedStr(field.ReferencedColumnName, GetEscapeChar)])
213213
.AppendLine
214214
.Append(field.ConstraintsAsString);
@@ -252,7 +252,7 @@ function TAnsiSQLGenerator.GenerateDelete(const command: TDeleteCommand): string
252252

253253
sqlBuilder := TStringBuilder.Create;
254254
try
255-
sqlBuilder.AppendFormat('DELETE FROM %0:S', [command.Table.Name]);
255+
sqlBuilder.AppendFormat('DELETE FROM %0:s', [command.Table.Name]);
256256

257257
for i := 0 to command.WhereFields.Count - 1 do
258258
begin
@@ -264,7 +264,7 @@ function TAnsiSQLGenerator.GenerateDelete(const command: TDeleteCommand): string
264264

265265
{TODO -oLinas -cGeneral : implement where operators}
266266

267-
sqlBuilder.AppendFormat('%0:S = %1:S',
267+
sqlBuilder.AppendFormat('%0:s = %1:s',
268268
[GetEscapedFieldName(field), field.ParamName]);
269269
end;
270270

@@ -349,9 +349,9 @@ function TAnsiSQLGenerator.GeneratePagedQuery(const sql: string;
349349
begin
350350
sqlStatement := sql;
351351
if EndsStr(';', sqlStatement) then
352-
SetLength(sqlStatement, Length(sqlStatement)-1);
352+
SetLength(sqlStatement, Length(sqlStatement) - 1);
353353

354-
Result := sqlStatement + Format(' LIMIT %1:D,%0:D %2:S',
354+
Result := sqlStatement + Format(' LIMIT %1:d,%0:d %2:s',
355355
[limit, offset, GetSplitStatementSymbol]);
356356
end;
357357

@@ -403,7 +403,7 @@ function TAnsiSQLGenerator.GenerateUpdate(const command: TUpdateCommand): string
403403
if i > 0 then
404404
sqlBuilder.Append(', ');
405405

406-
sqlBuilder.AppendFormat('%0:S = %1:S',
406+
sqlBuilder.AppendFormat('%0:s = %1:s',
407407
[GetEscapedFieldName(updateField), updateField.ParamName]);
408408
end;
409409

@@ -415,7 +415,7 @@ function TAnsiSQLGenerator.GenerateUpdate(const command: TUpdateCommand): string
415415
else
416416
sqlBuilder.Append(' AND ');
417417

418-
sqlBuilder.AppendFormat('%0:S = %1:S',
418+
sqlBuilder.AppendFormat('%0:s = %1:s',
419419
[GetEscapedFieldName(whereField), whereField.ParamName]);
420420
end;
421421

@@ -617,17 +617,21 @@ function TAnsiSQLGenerator.GetQueryLanguage: TQueryLanguage;
617617
Result := qlAnsiSQL;
618618
end;
619619

620-
procedure TAnsiSQLGenerator.ParseFullTablename(const fullTableName: string; out tableName, schemaName: string);
620+
procedure TAnsiSQLGenerator.ParseFullTableName(const fullTableName: string;
621+
out tableName, schemaName: string);
621622
var
622623
i: Integer;
623624
begin
624625
i := Pos('.', fullTableName);
625-
tableName := fullTableName;
626-
schemaName := '';
627626
if i > 1 then
628627
begin
629628
schemaName := Copy(fullTableName, 1, i - 1);
630-
tableName := Copy(fullTableName, i + 1, Length(fullTableName) - 1);
629+
tableName := Copy(fullTableName, i + 1);
630+
end
631+
else
632+
begin
633+
tableName := fullTableName;
634+
schemaName := '';
631635
end;
632636
end;
633637

@@ -666,7 +670,7 @@ function TAnsiSQLGenerator.GetSQLDataTypeName(const field: TSQLCreateField): str
666670
tkUnknown: ;
667671
tkInteger, tkInt64, tkEnumeration, tkSet:
668672
if field.Precision > 0 then
669-
Result := Format('NUMERIC(%0:D, %1:D)', [field.Precision, field.Scale])
673+
Result := Format('NUMERIC(%0:d, %1:d)', [field.Precision, field.Scale])
670674
else
671675
if typeInfo = System.TypeInfo(Boolean) then
672676
Result := 'BIT'
@@ -682,7 +686,7 @@ function TAnsiSQLGenerator.GetSQLDataTypeName(const field: TSQLCreateField): str
682686
Result := 'TIME'
683687
else
684688
if field.Precision > 0 then
685-
Result := Format('NUMERIC(%0:D, %1:D)', [field.Precision, field.Scale])
689+
Result := Format('NUMERIC(%0:d, %1:d)', [field.Precision, field.Scale])
686690
else
687691
Result := 'FLOAT';
688692
tkString, tkLString: Result := Format('VARCHAR(%D)', [field.Length]);
@@ -715,7 +719,7 @@ function TAnsiSQLGenerator.GetSQLSequenceCount(const sequenceName: string): stri
715719

716720
function TAnsiSQLGenerator.GetSQLTableCount(const tableName: string): string;
717721
begin
718-
Result := Format('SELECT COUNT(*) FROM %0:S %1:S', [tableName, GetSplitStatementSymbol]);
722+
Result := Format('SELECT COUNT(*) FROM %0:s %1:s', [tableName, GetSplitStatementSymbol]);
719723
end;
720724

721725
function TAnsiSQLGenerator.GetSQLTableExists(const tableName: string): string;
@@ -725,7 +729,7 @@ function TAnsiSQLGenerator.GetSQLTableExists(const tableName: string): string;
725729

726730
function TAnsiSQLGenerator.GetTableColumns(const tableName: string): string;
727731
begin
728-
Result := Format('SELECT * FROM %0:S WHERE 1<>2 %1:S', [tableName, GetSplitStatementSymbol]);
732+
Result := Format('SELECT * FROM %0:s WHERE 1<>2 %1:s', [tableName, GetSplitStatementSymbol]);
729733
end;
730734

731735
function TAnsiSQLGenerator.GetTableNameWithAlias(const table: TSQLTable): string;
@@ -742,7 +746,7 @@ function TAnsiSQLGenerator.GetUpdateVersionFieldQuery(
742746
const command: TUpdateCommand; const versionColumn: VersionAttribute;
743747
const version, primaryKey: Variant): Variant;
744748
begin
745-
Result := Format('UPDATE %0:S SET %1:S = coalesce(%1:S,0) + 1 WHERE (%2:S = %3:S) AND (coalesce(%1:S,0) = %4:S)',
749+
Result := Format('UPDATE %0:s SET %1:s = coalesce(%1:s,0) + 1 WHERE (%2:s = %3:s) AND (coalesce(%1:s,0) = %4:s)',
746750
[command.Table.Name, versionColumn.ColumnName,
747751
command.PrimaryKeyColumn.ColumnName, VarToStr(primaryKey), VarToStr(version)]);
748752
end;

Source/Persistence/SQL/Spring.Persistence.SQL.Generators.Firebird.pas

+15-19
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,12 @@ function TFirebirdSQLGenerator.DoGenerateRestoreTable(const tableName: string;
8383
function TFirebirdSQLGenerator.GenerateCreateSequence(
8484
const command: TCreateSequenceCommand): string;
8585
var
86-
LSequence: SequenceAttribute;
86+
sequence: SequenceAttribute;
8787
begin
88-
LSequence := command.Sequence;
89-
Result := '';
88+
sequence := command.Sequence;
89+
Result := Format('CREATE SEQUENCE %0:s;', [sequence.SequenceName]);
9090
if command.SequenceExists then
91-
Result := Format('DROP SEQUENCE %0:S; ', [LSequence.SequenceName]);
92-
93-
Result := Result + Format('CREATE SEQUENCE %0:S;', [LSequence.SequenceName]);
91+
Result := Format('DROP SEQUENCE %0:s;', [sequence.SequenceName]) + sLineBreak + Result;
9492
end;
9593

9694
function TFirebirdSQLGenerator.GenerateGetLastInsertId(
@@ -102,20 +100,19 @@ function TFirebirdSQLGenerator.GenerateGetLastInsertId(
102100
function TFirebirdSQLGenerator.GenerateGetNextSequenceValue(
103101
const sequence: SequenceAttribute): string;
104102
begin
105-
Assert(Assigned(sequence));
106-
Result := Format('SELECT NEXT VALUE FOR %0:S FROM RDB$DATABASE;', [sequence.SequenceName]);
103+
Result := Format('SELECT NEXT VALUE FOR %0:s FROM RDB$DATABASE;', [sequence.SequenceName]);
107104
end;
108105

109106
function TFirebirdSQLGenerator.GeneratePagedQuery(const sql: string;
110107
limit, offset: Integer): string;
111108
var
112-
LSQL: string;
109+
s: string;
113110
begin
114-
LSQL := sql;
115-
if EndsStr(';', LSQL) then
116-
SetLength(LSQL, Length(LSQL)-1);
111+
s := sql;
112+
if EndsStr(';', s) then
113+
SetLength(s, Length(s) - 1);
117114

118-
Result := LSQL + Format(' ROWS %1:D TO %0:D;', [offset + limit, offset]);
115+
Result := s + Format(' ROWS %1:d TO %0:d;', [offset + limit, offset]);
119116
end;
120117

121118
function TFirebirdSQLGenerator.GetQueryLanguage: TQueryLanguage;
@@ -135,16 +132,15 @@ function TFirebirdSQLGenerator.GetSQLDataTypeName(
135132

136133
function TFirebirdSQLGenerator.GetSQLSequenceCount(const sequenceName: string): string;
137134
begin
138-
Result := Format('SELECT COUNT(*) '+
139-
'FROM RDB$GENERATORS '+
140-
'WHERE (RDB$SYSTEM_FLAG=0) AND (RDB$GENERATOR_NAME = %0:S); ', [QuotedStr(sequenceName)]);
135+
Result := Format('SELECT COUNT(*) FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME = %0:s ' +
136+
' AND RDB$SYSTEM_FLAG = 0;', [QuotedStr(sequenceName)]);
141137
end;
142138

143139
function TFirebirdSQLGenerator.GetSQLTableExists(const tableName: string): string;
144140
begin
145-
Result := Format('select count(*) from rdb$relations where rdb$relation_name = %0:S '+
146-
' and rdb$view_blr is null and (rdb$system_flag is null or rdb$system_flag = 0)'
147-
, [QuotedStr(tableName)]);
141+
Result := Format('SELECT COUNT(*) FROM RDB$RELATIONS WHERE RDB$RELATION_NAME = %0:s ' +
142+
' AND RDB$VIEW_BLR IS NULL AND (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0);', [
143+
QuotedStr(tableName)]);
148144
end;
149145

150146
{$ENDREGION}

0 commit comments

Comments
 (0)