Browse Source

* Patch from Lacak2 for Mantis #20379 FirstLineAsSchema improvements.

git-svn-id: trunk@20611 -
marco 13 years ago
parent
commit
25f29e1048
2 changed files with 36 additions and 12 deletions
  1. 15 12
      packages/fcl-db/src/sdf/sdfdata.pp
  2. 21 0
      packages/fcl-db/src/sdf/testsdf.pp

+ 15 - 12
packages/fcl-db/src/sdf/sdfdata.pp

@@ -722,8 +722,6 @@ begin
 end;
 end;
 
 
 procedure TFixedFormatDataSet.InternalPost;
 procedure TFixedFormatDataSet.InternalPost;
-var
-  i: Longint;
 begin
 begin
   FSaveChanges := TRUE;
   FSaveChanges := TRUE;
   inherited UpdateRecord;
   inherited UpdateRecord;
@@ -862,7 +860,12 @@ var
 begin
 begin
   if not IsCursorOpen then
   if not IsCursorOpen then
     exit;
     exit;
-  if (FData.Count = 0) or (Trim(FData[0]) = '') then
+  if (FData.Count = 0) and (Schema.Count > 0) and FirstLineAsSchema then
+  begin
+    Schema.Delimiter := Delimiter;
+    FData.Append(Schema.DelimitedText);
+  end
+  else if (FData.Count = 0) or (Trim(FData[0]) = '') then
     begin
     begin
     FirstLineAsSchema := FALSE;
     FirstLineAsSchema := FALSE;
     FDataOffset:=0;
     FDataOffset:=0;
@@ -957,7 +960,7 @@ begin
 
 
     while Boolean(Byte(pStrEnd[0])) and (pStrEnd[0] in [#1..' ']) do
     while Boolean(Byte(pStrEnd[0])) and (pStrEnd[0] in [#1..' ']) do
     begin
     begin
-     if FFMultiLine=true then
+     if FFMultiLine then
       begin
       begin
        if ((pStrEnd[0]=CR) or (pStrEnd[0]=LF)) then
        if ((pStrEnd[0]=CR) or (pStrEnd[0]=LF)) then
         begin
         begin
@@ -979,7 +982,7 @@ begin
 
 
     if (pStr[0] = '"') then
     if (pStr[0] = '"') then
      begin
      begin
-      if FFMultiLine=true then
+      if FFMultiLine then
        begin
        begin
         repeat
         repeat
          Inc(pStrEnd);
          Inc(pStrEnd);
@@ -1036,21 +1039,21 @@ begin
   begin
   begin
     Str := Trim(Copy(pansichar(Buffer), p, FieldDefs[i].Size));
     Str := Trim(Copy(pansichar(Buffer), p, FieldDefs[i].Size));
     Inc(p, FieldDefs[i].Size);
     Inc(p, FieldDefs[i].Size);
-    if FFMultiLine=true then
+    if FFMultiLine then
       begin
       begin
        // If multiline enabled, quote whenever we find carriage return or linefeed
        // If multiline enabled, quote whenever we find carriage return or linefeed
-       if ((QuoteMe=False) and (StrScan(PChar(Str), #10) <> nil)) then QuoteMe:=true;
-       if ((QuoteMe=False) and (StrScan(PChar(Str), #13) <> nil)) then QuoteMe:=true;
+       if (not QuoteMe) and (StrScan(PChar(Str), #10) <> nil) then QuoteMe:=true;
+       if (not QuoteMe) and (StrScan(PChar(Str), #13) <> nil) then QuoteMe:=true;
       end
       end
     else
     else
       begin
       begin
        // If we don't allow multiline, remove all CR and LF because they mess with the record ends:
        // If we don't allow multiline, remove all CR and LF because they mess with the record ends:
-       StringReplace(Str, #10, '', [rfReplaceAll]);
-       StringReplace(Str, #13, '', [rfReplaceAll]);
+       Str := StringReplace(Str, #10, '', [rfReplaceAll]);
+       Str := StringReplace(Str, #13, '', [rfReplaceAll]);
       end;
       end;
     // Check for any delimiters occurring in field text
     // Check for any delimiters occurring in field text
-    if ((QuoteMe=False) and (StrScan(PChar(Str), FDelimiter) <> nil)) then QuoteMe:=true;
-    if (QuoteMe=True) then
+    if ((not QuoteMe) and (StrScan(PChar(Str), FDelimiter) <> nil)) then QuoteMe:=true;
+    if (QuoteMe) then
       Str := QuoteDelimiter + Str + QuoteDelimiter;
       Str := QuoteDelimiter + Str + QuoteDelimiter;
     Result := Result + Str + FDelimiter;
     Result := Result + Str + FDelimiter;
   end;
   end;

+ 21 - 0
packages/fcl-db/src/sdf/testsdf.pp

@@ -38,6 +38,27 @@ begin
     end;
     end;
 end;
 end;
 
 
+Procedure DoTest2;
+begin
+  // if file does not exists, then create it with schema at first line
+  With TSdfDataSet.Create(Nil) do
+    try
+      Delimiter := #9;
+      FileName := 'fpc2.ssy';
+      FileMustExist := False;
+      FirstLineAsSchema := True;
+      Schema.Add('First Name');
+      Schema.Add('Last Name');
+      Schema.Add('Email');
+      Open;
+      AppendRecord(['FName', 'LName', '[email protected]']);
+      Close;
+    finally
+      Free;
+    end;
+end;
+
 begin
 begin
   DoTest;
   DoTest;
+  DoTest2;
 end.
 end.