ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/runtime/nongui/IBDataOutput.pas
(Generate patch)

Comparing ibx/trunk/runtime/nongui/IBDataOutput.pas (file contents):
Revision 269 by tony, Thu Dec 6 15:55:01 2018 UTC vs.
Revision 270 by tony, Fri Jan 18 11:10:37 2019 UTC

# Line 54 | Line 54 | type
54    TPlanOptions = (poNoPlan,poIncludePlan, poPlanOnly);
55  
56    TAdd2Log = procedure(const Msg: string; IsError: boolean=true) of object;
57 +  TOnFormatTextString = procedure(sender: TObject; var TextString: string) of object;
58  
59    { TIBCustomDataOutput }
60  
61    TIBCustomDataOutput = class(TComponent)
62    private
63 +    FDateFormat: string;
64      FIBSQL: TIBSQL;
65      FIncludeHeader: Boolean;
66 +    FOnFormatTextString: TOnFormatTextString;
67      FPlanOptions: TPlanOptions;
68      FRowCount: integer;
69      FShowPerformanceStats: boolean;
70 +    FTimeFormat: string;
71 +    FTimestampFormat: string;
72      function GetDatabase: TIBDatabase;
73      function GetTransaction: TIBTransaction;
74      procedure SetDatabase(AValue: TIBDatabase);
# Line 72 | Line 77 | type
77      procedure HeaderOut(Add2Log: TAdd2Log); virtual;
78      procedure FormattedDataOut(Add2Log: TAdd2Log); virtual; abstract;
79      procedure TrailerOut(Add2Log: TAdd2Log); virtual;
80 +    function FormatTimestamp(aValue: ISQLData): string;
81 +    function FormatDate(aValue: ISQLData): string;
82 +    function FormatTime(aValue: ISQLData): string;
83 +    function FormatTextString(aValue: string): string;
84      property IncludeHeader: Boolean read FIncludeHeader write FIncludeHeader default true;
85 +    property OnFormatTextString: TOnFormatTextString read FOnFormatTextString write FOnFormatTextString;
86    public
87      constructor Create(aOwner: TComponent); override;
88      procedure Assign(Source: TPersistent); override;
# Line 85 | Line 95 | type
95      property PlanOptions: TPlanOptions read FPlanOptions write FPlanOptions;
96      property RowCount: integer read FRowCount write FRowCount;
97      property ShowPerformanceStats: boolean read FShowPerformanceStats write FShowPerformanceStats;
98 +    property TimestampFormat: string read FTimestampFormat write FTimestampFormat;
99 +    property DateFormat: string read FDateFormat write FDateFormat;
100 +    property TimeFormat: string read FTimeFormat write FTimeFormat;
101   end;
102  
103    TDataOutputFormatter = class of TIBCustomDataOutput;
# Line 93 | Line 106 | type
106  
107    TIBCSVDataOut = class(TIBCustomDataOutput)
108    private
109 +    FFieldSeparator: string;
110 +    FHeaderSeparator: string;
111      FQuoteChar: char;
112 +    FQuoteStrings: boolean;
113    protected
114      procedure HeaderOut(Add2Log: TAdd2Log); override;
115      procedure FormattedDataOut(Add2Log: TAdd2Log); override;
# Line 101 | Line 117 | type
117      constructor Create(aOwner: TComponent); override;
118    published
119      property IncludeHeader;
120 +    property FieldSeparator: string read FFieldSeparator write FFieldSeparator;
121 +    property HeaderSeparator: string read FHeaderSeparator write FHeaderSeparator;
122 +    property QuoteStrings: boolean read FQuoteStrings write FQuoteStrings default true;
123      property QuoteChar: char read FQuoteChar write FQuoteChar default '''';
124 +    property OnFormatTextString;
125    end;
126  
127    { TIBInsertStmtsOut }
# Line 137 | Line 157 | type
157      procedure TrailerOut(Add2Log: TAdd2Log); override;
158    published
159      property IncludeHeader;
160 +    property OnFormatTextString;
161    end;
162  
163   implementation
# Line 208 | Line 229 | begin
229            FColWidths[i] := 21; {leave room for the decimal point}
230  
231        SQL_TIMESTAMP:
232 <        FColWidths[i] := 23;
232 >        if TimestampFormat = '' then  {Default format}
233 >          FColWidths[i] := GetDateTimeStrLength(dfTimestamp)
234 >        else
235 >          FColWidths[i] := Length(TimestampFormat);
236  
237        SQL_TYPE_DATE:
238 <        FColWidths[i] := 10;
238 >        if DateFormat = '' then {Default format}
239 >          FColWidths[i] := GetDateTimeStrLength(dfDateTime)
240 >        else
241 >          FColWidths[i] := Length(DateFormat);
242  
243        SQL_TYPE_TIME:
244 <        FColWidths[i] := 12;
244 >        if TimeFormat = '' then {Default format}
245 >          FColWidths[i] := GetDateTimeStrLength(dfTime)
246 >        else
247 >          FColWidths[i] := Length(TimeFormat);
248  
249        SQL_BLOB:
250          if SQLSubType = 1 then
# Line 259 | Line 289 | var i: integer;
289   begin
290    s := '|';
291    for i := 0 to FIBSQL.Current.Count - 1 do
292 <  with FIBSQL.Current[i] do
292 >  with FIBSQL do
293    begin
294 <    if IsNull then
294 >    if Current[i].IsNull then
295        s += TextAlign('NULL',FColWidths[i],taCentre)
296      else
297 <    case SQLType of
297 >    case Current[i].SQLType of
298      SQL_VARYING, SQL_TEXT:
299 <      s += TextAlign(AsString,FColWidths[i],taLeft);
299 >      s += TextAlign(FormatTextString(Current[i].AsString),FColWidths[i],taLeft);
300  
301      SQL_TIMESTAMP:
302 <      s += TextAlign(FormatDateTime(sTimeStampFormat,AsDateTime),FColWidths[i],taLeft);
302 >      s += TextAlign(FormatTimeStamp(Current[i]),FColWidths[i],taLeft);
303  
304      SQL_TYPE_DATE:
305 <      s += TextAlign(FormatDateTime(sDateFormat,AsDateTime),FColWidths[i],taLeft);
305 >      s += TextAlign(FormatDate(Current[i]),FColWidths[i],taLeft);
306  
307      SQL_TYPE_TIME:
308 <      s += TextAlign(FormatDateTime(sTimeFormat,AsDateTime),FColWidths[i],taLeft);
308 >      s += TextAlign(FormatTime(Current[i]),FColWidths[i],taLeft);
309  
310      SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT,
311      SQL_LONG, SQL_SHORT, SQL_INT64:
312 <      s += TextAlign(AsString,FColWidths[i],taRight);
312 >      s += TextAlign(Current[i].AsString,FColWidths[i],taRight);
313  
314      SQL_BOOLEAN, SQL_ARRAY:
315 <      s += TextAlign(AsString,FColWidths[i],taCentre);
315 >      s += TextAlign(Current[i].AsString,FColWidths[i],taCentre);
316  
317      SQL_BLOB:
318 <      if SQLSubType = 1 then
319 <        s += TextAlign(TruncateTextBlob(AsString),FColWidths[i],taLeft)
318 >      if Current[i].SQLSubType = 1 then
319 >        s += TextAlign(TruncateTextBlob(Current[i].AsString),FColWidths[i],taLeft)
320        else
321          s += TextAlign(sBlob,FColWidths[i],taCentre);
322      end;
# Line 379 | Line 409 | begin
409            s += QuoteChar + SQLSafeString(Current[i].AsString) + QuoteChar;
410  
411        SQL_TIMESTAMP:
412 <        s += QuoteChar + FormatDateTime(sTimeStampFormat,Current[i].AsDateTime) + QuoteChar;
412 >        s += QuoteChar + FormatTimeStamp(Current[i]) + QuoteChar;
413  
414        SQL_TYPE_DATE:
415 <        s += QuoteChar + FormatDateTime(sDateFormat,Current[i].AsDateTime) + QuoteChar;
415 >        s += QuoteChar + FormatDate(Current[i]) + QuoteChar;
416  
417        SQL_TYPE_TIME:
418 <        s += QuoteChar + FormatDateTime(sTimeFormat,Current[i].AsDateTime) + QuoteChar;
418 >        s += QuoteChar + FormatTime(Current[i]) + QuoteChar;
419  
420        else
421          s += Current[i].AsString;
# Line 412 | Line 442 | begin
442    s := '';
443    for i := 0 to FIBSQL.MetaData.Count - 1 do
444    begin
445 <    if i <> 0 then s += ',';
445 >    if i <> 0 then s += HeaderSeparator;
446      s += FIBSQL.MetaData[i].getAliasName;
447    end;
448    Add2Log(s,false);
449   end;
450  
451   procedure TIBCSVDataOut.FormattedDataOut(Add2Log: TAdd2Log);
452 +
453 +  function GetQuoteChar: string;
454 +  begin
455 +    if QuoteStrings then
456 +      Result := QuoteChar
457 +    else
458 +      Result := '';
459 +  end;
460 +
461   var i: integer;
462      s: string;
463   begin
# Line 428 | Line 467 | begin
467      for i := 0 to Current.Count - 1 do
468      with Current[i] do
469      begin
470 <      if i <> 0 then s += ',';
470 >      if i <> 0 then s += FieldSeparator;
471        case SQLType of
472        SQL_BLOB:
473          if SQLSubType <> 1 then
474            s += sBlob
475          else
476 <          s += QuoteChar + Current[i].AsString + QuoteChar;
476 >          s += GetQuoteChar + Current[i].AsString + GetQuoteChar;
477  
478 <      SQL_VARYING,SQL_TEXT,
479 <      SQL_TIMESTAMP,SQL_TYPE_DATE,SQL_TYPE_TIME:
480 <        s += QuoteChar + Current[i].AsString + QuoteChar;
478 >      SQL_VARYING,SQL_TEXT:
479 >        s += GetQuoteChar + FormatTextString(Current[i].AsString) + GetQuoteChar;
480 >
481 >      SQL_TIMESTAMP:
482 >        s += GetQuoteChar + FormatTimeStamp(Current[i]) + GetQuoteChar;
483 >
484 >      SQL_TYPE_DATE:
485 >        s += GetQuoteChar + FormatDate(Current[i]) + GetQuoteChar;
486 >
487 >      SQL_TYPE_TIME:
488 >        s += GetQuoteChar + FormatTime(Current[i]) + GetQuoteChar;
489  
490        else
491          s += Current[i].AsString;
# Line 451 | Line 498 | end;
498   constructor TIBCSVDataOut.Create(aOwner: TComponent);
499   begin
500    inherited Create(aOwner);
501 +  FQuoteStrings := true;
502    FQuoteChar := '''';
503 +  FTimestampFormat := '';
504 +  FDateFormat := '';
505 +  FTimestampFormat := '';
506 +  FFieldSeparator := ',';
507 +  FHeaderSeparator := ',';
508   end;
509  
510   { TIBCustomDataOutput }
# Line 486 | Line 539 | begin
539    //stub
540   end;
541  
542 + function TIBCustomDataOutput.FormatTimestamp(aValue: ISQLData): string;
543 + begin
544 +  if TimeStampFormat <> '' then
545 +    Result := FormatDateTime(TimeStampFormat,aValue.AsDateTime)
546 +  else
547 +    Result := aValue.AsString;
548 + end;
549 +
550 + function TIBCustomDataOutput.FormatDate(aValue: ISQLData): string;
551 + begin
552 +  if DateFormat <> '' then
553 +    Result := FormatDateTime(DateFormat,aValue.AsDateTime)
554 +  else
555 +    Result := aValue.AsString;
556 + end;
557 +
558 + function TIBCustomDataOutput.FormatTime(aValue: ISQLData): string;
559 + begin
560 +  if TimeFormat <> '' then
561 +    Result := FormatDateTime(TimeFormat,aValue.AsDateTime)
562 +  else
563 +    Result := aValue.AsString;
564 + end;
565 +
566 + function TIBCustomDataOutput.FormatTextString(aValue: string): string;
567 + begin
568 +  Result := aValue;
569 +  if assigned(FOnFormatTextString) then
570 +    OnFormatTextString(self,Result);
571 + end;
572 +
573   constructor TIBCustomDataOutput.Create(aOwner: TComponent);
574   begin
575    inherited Create(aOwner);
576    FIBSQL := TIBSQL.Create(self);
577    FIncludeHeader := true;
578 +  FTimestampFormat := sTimestampFormat;
579 +  FDateFormat := sDateFormat;
580 +  FTimeFormat := sTimeFormat;
581   end;
582  
583   procedure TIBCustomDataOutput.Assign(Source: TPersistent);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines