Home Why doesn't factoring out with a DefaultExpression in a TDataSet work
Reply: 1

Why doesn't factoring out with a DefaultExpression in a TDataSet work

cvz
1#
cvz Published in 2018-02-09 20:00:25Z

I am working with the Delphi FireDac technology in connection with a PostgreSQL database. I would like to set a floating value as DefaultExpression for a Field at Runtime. The next lines works fine:

FDMemTable1.FieldByName( 'some_fieldname').FieldKind  := fkData;
FDMemTable1.FieldByName( 'some_fieldname').DefaultExpression := '5.5';

But when I try to factor this out with the next code, I get an error: Exception class EFDException with message '[FireDAC][Stan][Eval]-104. Type mismatch in expression'.

procedure SetDefaultFieldValue (
    var aFDMemTable : TFDMemTable;
    aFieldName  : string;
    aFieldValue : string;
    aFieldKind  : TFieldKind );
  begin
    aFDMemTable.FieldByName( aFieldName ).FieldKind  := aFieldKind;
    aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  QuotedStr( aFieldValue );
  end;

What am I doing wrong?


Thanks all for the suggestions. The problem seems to be - as @Graig Young pointed out- the different kind of Columms (Numeric or String etc). I needed the QuotedStr for the String-Fields. Otherwise I get an error:

[aFieldValue] Column or Function is not found. Add FireDAC.Stan.ExprFuncs to the uses clause (which I have)

So @Victoria, leaving out QuotedStr works indeed for the Numeric-Field, but factoring like the next lines doesn't work for String-Fields.

procedure SetDefaultFieldValue (
    var aFDMemTable : TFDMemTable;
    aFieldName  : string;
    aFieldValue : string;
    aFieldKind  : TFieldKind );
  begin
    aFDMemTable.FieldByName( aFieldName ).FieldKind  := aFieldKind;
    aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  aFieldValue;
  end;

DefaultExpression expects a string-value. Trying to refine my question: Is it possible to factor-out DefaultExpression for "any" FieldType? if so, how?

cvz
2#
cvz Reply to 2018-02-10 16:17:56Z

Sorry for any unclearness, but the next code avoids the mentioned errors.

procedure SetDefaultFieldValue(
    var aFDMemTable : TFDMemTable;
    aFieldName      : string;
    aFieldValue     : string;
    aFieldType      : TFieldType);
  begin
    aFDMemTable.FieldByName( aFieldName ).FieldKind  := fkData;

    case aFieldType of
      ftString  : aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  QuotedStr( aFieldValue ) ;
      ftSingle  : aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  aFieldValue ;
      ftInteger : aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  aFieldValue ;
    end;

  end;

As @Graig Young mentioned the way of handling String-Fields should be different form Numeric-Fields.

Now the next code works.

SetDefaultFieldValue( aAliasTable.FDMemTable1, 'some_fieldname', 'a_string_value' , ftString );
SetDefaultFieldValue( aAliasTable.FDMemTable1, 'another_fieldname', '5.5' , ftSingle );

Thanks to all!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298608 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO