Page 1 of 1
Default values on new record
Posted: 16.01.2006, 12:46
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
Posted: 16.01.2006, 18:40
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
Posted: 16.01.2006, 20:55
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.
Posted: 17.01.2006, 17:26
by tygrys
You can use "onNewRecord" event to set your defaults.
Tygrys
Posted: 18.01.2006, 21:48
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
Posted: 18.01.2006, 22:12
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.
Posted: 19.01.2006, 17:07
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