mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-30 23:50:26 +02:00
* Add header row
git-svn-id: trunk@43557 -
This commit is contained in:
parent
94e0b499df
commit
0c25ada935
@ -23,10 +23,10 @@ Type
|
|||||||
FEnabled: Boolean;
|
FEnabled: Boolean;
|
||||||
FField: TField;
|
FField: TField;
|
||||||
FFieldName: String;
|
FFieldName: String;
|
||||||
FExportedName: String;
|
FExportedName: UTF8String;
|
||||||
function GetExportedName: String;
|
function GetExportedName: UTF8String;
|
||||||
function GetExporter: TCustomDatasetExporter;
|
function GetExporter: TCustomDatasetExporter;
|
||||||
procedure SetExportedName(const AValue: String);
|
procedure SetExportedName(const AValue: UTF8String);
|
||||||
Protected
|
Protected
|
||||||
Procedure BindField (ADataset : TDataset); virtual;
|
Procedure BindField (ADataset : TDataset); virtual;
|
||||||
procedure SetFieldName(const AValue: String); virtual;
|
procedure SetFieldName(const AValue: String); virtual;
|
||||||
@ -38,7 +38,7 @@ Type
|
|||||||
Published
|
Published
|
||||||
Property Enabled : Boolean Read FEnabled Write FEnabled default True;
|
Property Enabled : Boolean Read FEnabled Write FEnabled default True;
|
||||||
Property FieldName : String Read FFieldName Write SetFieldName;
|
Property FieldName : String Read FFieldName Write SetFieldName;
|
||||||
Property ExportedName : String Read GetExportedName Write SetExportedName;
|
Property ExportedName : UTF8String Read GetExportedName Write SetExportedName;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TExportFields }
|
{ TExportFields }
|
||||||
@ -335,7 +335,7 @@ begin
|
|||||||
FEnabled:=True;
|
FEnabled:=True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TExportFieldItem.GetExportedName: String;
|
function TExportFieldItem.GetExportedName: UTF8String;
|
||||||
begin
|
begin
|
||||||
Result:=FExportedName;
|
Result:=FExportedName;
|
||||||
If (Result='') then
|
If (Result='') then
|
||||||
@ -348,7 +348,7 @@ begin
|
|||||||
Result:=(Collection as TExportFields).Exporter;
|
Result:=(Collection as TExportFields).Exporter;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TExportFieldItem.SetExportedName(const AValue: String);
|
procedure TExportFieldItem.SetExportedName(const AValue: UTF8String);
|
||||||
|
|
||||||
Var
|
Var
|
||||||
I : TExportFieldItem;
|
I : TExportFieldItem;
|
||||||
|
@ -29,9 +29,13 @@ Type
|
|||||||
TFixedExportFormatSettings = Class (TExportFormatSettings)
|
TFixedExportFormatSettings = Class (TExportFormatSettings)
|
||||||
private
|
private
|
||||||
FCharMode: TCharMode;
|
FCharMode: TCharMode;
|
||||||
|
FHeaderRow: Boolean;
|
||||||
Public
|
Public
|
||||||
Procedure Assign(Source: TPersistent); override;
|
Procedure Assign(Source: TPersistent); override;
|
||||||
Published
|
Published
|
||||||
|
// Whether or not the file should have a header row with field names
|
||||||
|
Property HeaderRow : Boolean Read FHeaderRow Write FHeaderRow default true;
|
||||||
|
// How to handle Unicode ?
|
||||||
Property CharMode : TCharMode Read FCharMode Write FCharMode;
|
Property CharMode : TCharMode Read FCharMode Write FCharMode;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -41,12 +45,12 @@ Type
|
|||||||
FCurrentRowUnicode : UnicodeString;
|
FCurrentRowUnicode : UnicodeString;
|
||||||
function GetCharMode: TCharMode;
|
function GetCharMode: TCharMode;
|
||||||
function GeTFixedExportFormatSettings: TFixedExportFormatSettings;
|
function GeTFixedExportFormatSettings: TFixedExportFormatSettings;
|
||||||
procedure SeTFixedExportFormatSettings(AValue: TFixedExportFormatSettings);
|
procedure SetFixedExportFormatSettings(AValue: TFixedExportFormatSettings);
|
||||||
Protected
|
Protected
|
||||||
function ExportFieldAsUniCodeString(EF: TExportFieldItem): UnicodeString; virtual;
|
function ExportFieldAsUniCodeString(EF: TExportFieldItem; isHeader: Boolean=False): UnicodeString; virtual;
|
||||||
procedure ExportFieldAnsi(EF: TExportFieldItem); virtual;
|
procedure ExportFieldAnsi(EF: TExportFieldItem; isHeader: Boolean=False); virtual;
|
||||||
procedure ExportFieldUTF16(EF: TExportFieldItem); virtual;
|
procedure ExportFieldUTF16(EF: TExportFieldItem; isHeader: Boolean=False); virtual;
|
||||||
procedure ExportFieldUTF8(EF: TExportFieldItem); virtual;
|
procedure ExportFieldUTF8(EF: TExportFieldItem; isHeader: Boolean=False); virtual;
|
||||||
Procedure BuildDefaultFieldMap(AMap : TExportFields); override;
|
Procedure BuildDefaultFieldMap(AMap : TExportFields); override;
|
||||||
Function CreateExportFields : TExportFields; override;
|
Function CreateExportFields : TExportFields; override;
|
||||||
Function CreateFormatSettings: TCustomExportFormatSettings; override;
|
Function CreateFormatSettings: TCustomExportFormatSettings; override;
|
||||||
@ -55,13 +59,12 @@ Type
|
|||||||
Procedure DoDataRowStart; override;
|
Procedure DoDataRowStart; override;
|
||||||
Procedure ExportField(EF : TExportFieldItem); override;
|
Procedure ExportField(EF : TExportFieldItem); override;
|
||||||
Procedure DoDataRowEnd; override;
|
Procedure DoDataRowEnd; override;
|
||||||
|
Procedure DoDataHeader; override;
|
||||||
Property CharMode : TCharMode Read GetCharMode;
|
Property CharMode : TCharMode Read GetCharMode;
|
||||||
Property FixedFormatSettings : TFixedExportFormatSettings Read GeTFixedExportFormatSettings Write SeTFixedExportFormatSettings;
|
Property FormatSettings : TFixedExportFormatSettings Read GetFixedExportFormatSettings Write SetFixedExportFormatSettings;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TFixedLengthExporter = Class(TCustomFixedLengthExporter)
|
TFixedLengthExporter = Class(TCustomFixedLengthExporter)
|
||||||
Public
|
|
||||||
Property FixedFormatSettings;
|
|
||||||
Published
|
Published
|
||||||
Property FileName;
|
Property FileName;
|
||||||
Property Dataset;
|
Property Dataset;
|
||||||
@ -93,7 +96,10 @@ uses math;
|
|||||||
procedure TFixedExportFormatSettings.Assign(Source: TPersistent);
|
procedure TFixedExportFormatSettings.Assign(Source: TPersistent);
|
||||||
begin
|
begin
|
||||||
if (Source is TFixedExportFormatSettings) then
|
if (Source is TFixedExportFormatSettings) then
|
||||||
|
begin
|
||||||
CharMode:=TFixedExportFormatSettings(Source).CharMode;
|
CharMode:=TFixedExportFormatSettings(Source).CharMode;
|
||||||
|
HeaderRow:=TFixedExportFormatSettings(Source).HeaderRow;
|
||||||
|
end;
|
||||||
inherited Assign(Source);
|
inherited Assign(Source);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -117,19 +123,19 @@ end;
|
|||||||
{ TCustomFixedLengthExporter }
|
{ TCustomFixedLengthExporter }
|
||||||
|
|
||||||
|
|
||||||
procedure TCustomFixedLengthExporter.SeTFixedExportFormatSettings(AValue: TFixedExportFormatSettings);
|
procedure TCustomFixedLengthExporter.SetFixedExportFormatSettings(AValue: TFixedExportFormatSettings);
|
||||||
begin
|
begin
|
||||||
FormatSettings:=AValue;
|
Inherited FormatSettings:=AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TCustomFixedLengthExporter.GetCharMode: TCharMode;
|
function TCustomFixedLengthExporter.GetCharMode: TCharMode;
|
||||||
begin
|
begin
|
||||||
Result:=FixedFormatSettings.CharMode;
|
Result:=FormatSettings.CharMode;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TCustomFixedLengthExporter.GeTFixedExportFormatSettings: TFixedExportFormatSettings;
|
function TCustomFixedLengthExporter.GeTFixedExportFormatSettings: TFixedExportFormatSettings;
|
||||||
begin
|
begin
|
||||||
Result:=Formatsettings as TFixedExportFormatSettings;
|
Result:=(Inherited Formatsettings) as TFixedExportFormatSettings;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCustomFixedLengthExporter.BuildDefaultFieldMap(AMap: TExportFields);
|
procedure TCustomFixedLengthExporter.BuildDefaultFieldMap(AMap: TExportFields);
|
||||||
@ -190,12 +196,12 @@ Const
|
|||||||
Case charmode of
|
Case charmode of
|
||||||
cmUTF8:
|
cmUTF8:
|
||||||
begin
|
begin
|
||||||
LTrue:=Length(UTF8Decode(FixedFormatSettings.BooleanTrue));
|
LTrue:=Length(UTF8Decode(FormatSettings.BooleanTrue));
|
||||||
LFalse:=Length(UTF8Decode(FixedFormatSettings.BooleanFalse));
|
LFalse:=Length(UTF8Decode(FormatSettings.BooleanFalse));
|
||||||
end;
|
end;
|
||||||
else
|
else
|
||||||
LTrue:=Length(FixedFormatSettings.BooleanTrue);
|
LTrue:=Length(FormatSettings.BooleanTrue);
|
||||||
LFalse:=Length(FixedFormatSettings.BooleanFalse);
|
LFalse:=Length(FormatSettings.BooleanFalse);
|
||||||
end;
|
end;
|
||||||
Result:=Max(LTrue,LFalse);
|
Result:=Max(LTrue,LFalse);
|
||||||
end;
|
end;
|
||||||
@ -271,7 +277,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
Function TCustomFixedLengthExporter.ExportFieldAsUniCodeString(EF: TExportFieldItem) : UnicodeString;
|
Function TCustomFixedLengthExporter.ExportFieldAsUniCodeString(EF: TExportFieldItem; isHeader : Boolean = False) : UnicodeString;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
S,SS : UnicodeString;
|
S,SS : UnicodeString;
|
||||||
@ -279,7 +285,10 @@ Var
|
|||||||
L,W : Integer;
|
L,W : Integer;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
S:=UTF8Decode(FormatField(EF.Field));
|
if isHeader then
|
||||||
|
S:=UTF8Decode(EF.ExportedName)
|
||||||
|
else
|
||||||
|
S:=UTF8Decode(FormatField(EF.Field));
|
||||||
If EF is TFixedLengthExportFieldItem then
|
If EF is TFixedLengthExportFieldItem then
|
||||||
begin
|
begin
|
||||||
FL:=TFixedLengthExportFieldItem(EF);
|
FL:=TFixedLengthExportFieldItem(EF);
|
||||||
@ -306,21 +315,21 @@ begin
|
|||||||
Result:=S;
|
Result:=S;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCustomFixedLengthExporter.ExportFieldUTF16(EF: TExportFieldItem);
|
procedure TCustomFixedLengthExporter.ExportFieldUTF16(EF: TExportFieldItem; isHeader : Boolean = False);
|
||||||
|
|
||||||
begin
|
begin
|
||||||
FCurrentRowUnicode:=FCurrentRowUnicode+ExportFieldAsUnicodeString(EF);
|
FCurrentRowUnicode:=FCurrentRowUnicode+ExportFieldAsUnicodeString(EF,isHeader);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure TCustomFixedLengthExporter.ExportFieldUTF8(EF: TExportFieldItem);
|
procedure TCustomFixedLengthExporter.ExportFieldUTF8(EF: TExportFieldItem; isHeader : Boolean = False);
|
||||||
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
FCurrentRow:=FCurrentRow+UTF8Encode(ExportFieldAsUnicodeString(EF));
|
FCurrentRow:=FCurrentRow+UTF8Encode(ExportFieldAsUnicodeString(EF,isHeader));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCustomFixedLengthExporter.ExportFieldAnsi(EF: TExportFieldItem);
|
procedure TCustomFixedLengthExporter.ExportFieldAnsi(EF: TExportFieldItem; isHeader : Boolean = False);
|
||||||
|
|
||||||
Var
|
Var
|
||||||
S,SS : String;
|
S,SS : String;
|
||||||
@ -328,7 +337,10 @@ Var
|
|||||||
FL : TFixedLengthExportFieldItem;
|
FL : TFixedLengthExportFieldItem;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
S:=FormatField(EF.Field);
|
if isHeader then
|
||||||
|
S:=EF.ExportedName
|
||||||
|
else
|
||||||
|
S:=FormatField(EF.Field);
|
||||||
If EF is TFixedLengthExportFieldItem then
|
If EF is TFixedLengthExportFieldItem then
|
||||||
begin
|
begin
|
||||||
FL:=TFixedLengthExportFieldItem(EF);
|
FL:=TFixedLengthExportFieldItem(EF);
|
||||||
@ -365,6 +377,31 @@ begin
|
|||||||
FCurrentRowUnicode:='';
|
FCurrentRowUnicode:='';
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TCustomFixedLengthExporter.DoDataHeader;
|
||||||
|
|
||||||
|
Var
|
||||||
|
I : Integer;
|
||||||
|
EF: TExportFieldItem;
|
||||||
|
|
||||||
|
begin
|
||||||
|
FCurrentRow:='';
|
||||||
|
if FormatSettings.HeaderRow then
|
||||||
|
begin
|
||||||
|
For I:=0 to ExportFields.Count-1 do
|
||||||
|
begin
|
||||||
|
EF:=ExportFields[I];
|
||||||
|
If EF.Enabled then
|
||||||
|
Case CharMode of
|
||||||
|
cmANSI : ExportFieldAnsi(EF,True);
|
||||||
|
cmUTF8 : ExportFieldUTF8(EF,True);
|
||||||
|
cmUTF16 : ExportFieldUTF16(EF,True);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
DoDataRowEnd;
|
||||||
|
end;
|
||||||
|
inherited DoDataHeader;
|
||||||
|
end;
|
||||||
|
|
||||||
Procedure RegisterFixedExportFormat;
|
Procedure RegisterFixedExportFormat;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -60,6 +60,7 @@ type
|
|||||||
procedure TestFixedTextExportUTF8;
|
procedure TestFixedTextExportUTF8;
|
||||||
procedure TestFixedTextExportUTF16;
|
procedure TestFixedTextExportUTF16;
|
||||||
procedure TestFixedTextExportBoolean;
|
procedure TestFixedTextExportBoolean;
|
||||||
|
procedure TestFixedTextExportHeader;
|
||||||
procedure TestJSONExport;
|
procedure TestJSONExport;
|
||||||
procedure TestRTFExport;
|
procedure TestRTFExport;
|
||||||
procedure TestSQLExport;
|
procedure TestSQLExport;
|
||||||
@ -169,7 +170,7 @@ begin
|
|||||||
DBConnector.StartTest(TestName);
|
DBConnector.StartTest(TestName);
|
||||||
FExportTempDir:=IncludeTrailingPathDelimiter(ExpandFileName(''))+'exporttests'+PathDelim; //Store output in subdirectory
|
FExportTempDir:=IncludeTrailingPathDelimiter(ExpandFileName(''))+'exporttests'+PathDelim; //Store output in subdirectory
|
||||||
ForceDirectories(FExportTempDir);
|
ForceDirectories(FExportTempDir);
|
||||||
// FKeepFilesAfterTest:=true; //keep test files; consistent with other units right now
|
FKeepFilesAfterTest:=true; //keep test files; consistent with other units right now
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TTestDBExport.TearDown;
|
procedure TTestDBExport.TearDown;
|
||||||
@ -621,7 +622,7 @@ begin
|
|||||||
try
|
try
|
||||||
Exporter := TFixedLengthExporter.Create(nil);
|
Exporter := TFixedLengthExporter.Create(nil);
|
||||||
Exporter.Dataset:=DS;
|
Exporter.Dataset:=DS;
|
||||||
Exporter.FixedFormatSettings.CharMode:=cmUTF8;
|
Exporter.FormatSettings.CharMode:=cmUTF8;
|
||||||
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
||||||
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
||||||
TFixedLengthExportFieldItem(Exporter.ExportFields[0]).Width:=3;
|
TFixedLengthExportFieldItem(Exporter.ExportFields[0]).Width:=3;
|
||||||
@ -662,7 +663,7 @@ begin
|
|||||||
try
|
try
|
||||||
Exporter := TFixedLengthExporter.Create(nil);
|
Exporter := TFixedLengthExporter.Create(nil);
|
||||||
Exporter.Dataset:=DS;
|
Exporter.Dataset:=DS;
|
||||||
Exporter.FixedFormatSettings.CharMode:=cmUTF16;
|
Exporter.FormatSettings.CharMode:=cmUTF16;
|
||||||
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
||||||
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
||||||
TFixedLengthExportFieldItem(Exporter.ExportFields[0]).Width:=3;
|
TFixedLengthExportFieldItem(Exporter.ExportFields[0]).Width:=3;
|
||||||
@ -725,8 +726,8 @@ begin
|
|||||||
DS:=GetBooleanDS;
|
DS:=GetBooleanDS;
|
||||||
try
|
try
|
||||||
Exporter := TFixedLengthExporter.Create(nil);
|
Exporter := TFixedLengthExporter.Create(nil);
|
||||||
Exporter.FixedFormatSettings.BooleanFalse:='false';
|
Exporter.FormatSettings.BooleanFalse:='false';
|
||||||
Exporter.FixedFormatSettings.BooleanTrue:='True';
|
Exporter.FormatSettings.BooleanTrue:='True';
|
||||||
Exporter.Dataset:=DS;
|
Exporter.Dataset:=DS;
|
||||||
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
||||||
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
||||||
@ -750,6 +751,49 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TTestDBExport.TestFixedTextExportHeader;
|
||||||
|
|
||||||
|
var
|
||||||
|
DS : TBufDataset;
|
||||||
|
Exporter: TFixedLengthExporter;
|
||||||
|
F : text;
|
||||||
|
S : UTF8String;
|
||||||
|
haveFile : Boolean;
|
||||||
|
|
||||||
|
begin
|
||||||
|
haveFile:=False;
|
||||||
|
Exporter:=Nil;
|
||||||
|
DS:=GetBooleanDS;
|
||||||
|
try
|
||||||
|
Exporter := TFixedLengthExporter.Create(nil);
|
||||||
|
Exporter.FormatSettings.BooleanFalse:='false';
|
||||||
|
Exporter.FormatSettings.BooleanTrue:='True';
|
||||||
|
Exporter.FormatSettings.HeaderRow:=True;
|
||||||
|
Exporter.Dataset:=DS;
|
||||||
|
Exporter.FileName := FExportTempDir + lowercase(TestName) + '.txt';
|
||||||
|
Exporter.BuildDefaultFieldMap(Exporter.ExportFields);
|
||||||
|
AssertEquals('Correct width',5, TFixedLengthExportFieldItem(Exporter.ExportFields[0]).Width);
|
||||||
|
AssertEquals('Output count',2,Exporter.Execute);
|
||||||
|
AssertTrue('Output file must be created', FileExists(Exporter.FileName));
|
||||||
|
AssertFalse('Output file must not be empty', (GetFileSize(Exporter.FileName) = 0));
|
||||||
|
AssignFile(F,Exporter.FileName);
|
||||||
|
Reset(F);
|
||||||
|
haveFile:=True;
|
||||||
|
Readln(F,S);
|
||||||
|
AssertEquals('Correct header line','F ',S); // 1 extra
|
||||||
|
Readln(F,S);
|
||||||
|
AssertEquals('Correct first line','True ',S); // 1 extra
|
||||||
|
Readln(F,S);
|
||||||
|
AssertEquals('Correct second line','false',S);
|
||||||
|
finally
|
||||||
|
if HaveFile then
|
||||||
|
closeFile(F);
|
||||||
|
if (FKeepFilesAfterTest = False) then
|
||||||
|
DeleteFile(Exporter.FileName);
|
||||||
|
Exporter.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TTestDBExport.TestJSONExport;
|
procedure TTestDBExport.TestJSONExport;
|
||||||
var
|
var
|
||||||
Exporter: TSimpleJSONExporter;
|
Exporter: TSimpleJSONExporter;
|
||||||
|
Loading…
Reference in New Issue
Block a user