Default values on new record

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
omul_rau
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 16.01.2006, 12:10

Default values on new record

Post by omul_rau »

Is it any solution to get default values for a field on new record ?
For a field that must be not null, the server default values is never used.

PS: I use zeos with postgres.
10x
Marius
btrewern
Expert Boarder
Expert Boarder
Posts: 193
Joined: 06.10.2005, 18:51

Post by btrewern »

I think this works (from memory):

Remove the Not Null restrictions from the delphi field objects in question. Then if you post the record with these fields set as null PostgreSql uses it's default values. You will have to refetch the row in question to see the row values.

Regards,

Ben
omul_rau
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 16.01.2006, 12:10

Post by omul_rau »

Yes I can do this.
But is better that the default value to be set to a field and the customer
can modify this value. It is an ellegant solution for sequences too.

I found in ZDbcCachedResultSet.pas a method: CalculateRowDefaults.
How can I use this to set fields values to default when a new record is created.
tygrys
Junior Boarder
Junior Boarder
Posts: 33
Joined: 09.12.2005, 18:31
Location: Poland

Post by tygrys »

You can use "onNewRecord" event to set your defaults.

Tygrys
krluigo
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 11.10.2005, 19:42

Post by krluigo »

change the TZAbstractRODataset.DoOnNewRecord (in ZAbstractRODataset.pas) for the using of TField.DefaultExpresson:

Code: Select all

procedure TZAbstractRODataset.DoOnNewRecord;
var
  I: Integer;
  MasterField, DetailField: TField;
  Temp: Int64;

// start new code

  vStr : String;

  function StringInArray(const pStr : String; const pArray : array of String) : Boolean;
  var
   ii : Integer;
  begin
    Result := False;
    for ii := Low(pArray) to High(pArray) do
    begin
      Result := pStr = pArray[ii];
      if Result then Exit;
    end;
  end;

begin
  for I := 0 to Fields.Count - 1 do
  begin
    with Fields[I] do
    begin
      vStr := UpperCase(Trim(DefaultExpression));
      if vStr <> '' then
      begin
        if Fields[I] is TDateTimeField then
        begin
          if StringInArray(vStr, ['NOW', 'CURRENT_TIME', 'CURRENT_TIMESTAMP']) then
          begin
            AsDateTime := Now;
          end
          else if StringInArray(vStr, ['TODAY', 'CURRENT_DATE']) then
          begin
            AsDateTime := Trunc(Now);
          end
          else if (vStr = 'TOMORROW') then
          begin
            AsDateTime := Trunc(Now) + 1;
          end
          else if (vStr = 'YESTERDAY') then
          begin
            AsDateTime := Trunc(Now) - 1;
          end
          else if vStr <> 'NULL' then
          begin
            AsString := DefaultExpression;
          end;
        end
        else if vStr <> 'NULL' then
        begin
          AsString := DefaultExpression;
        end;
      end;
    end;
  end;

// end new code

  if MasterLink.Active and (MasterLink.Fields.Count > 0) then
  begin
    for I := 0 to MasterLink.Fields.Count - 1 do

niztor
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 04.11.2005, 19:05

Post by niztor »

maybe this can help:

http://seegernet.koolhost.de/zeosforum/ ... .php?t=298

it removes Required property in columns with default values, then Zeos
uses default values.
omul_rau
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 16.01.2006, 12:10

Post by omul_rau »

krluigo wrote:change the TZAbstractRODataset.DoOnNewRecord (in ZAbstractRODataset.pas) for the using of TField.DefaultExpresson:

Code: Select all

...............
begin
  for I := 0 to Fields.Count - 1 do
  begin
    with Fields[I] do
    begin
      vStr := UpperCase(Trim(DefaultExpression));
      if vStr <> '' then
................
In fact the Filed.DefaultExpression is allways emty.
Is it a bug ?
If not, how can I set DefaultExpression when the query is open.

Thx
Marius
Post Reply