Subject [IBO] Re: Default values not defaulting for some field
Author ming
> I think you misunderstand how the Firebird/IB column default works...
> 1. It works ONLY for an insert; AND
> 2. It is not available until the record is written to disk; AND
> 3. It is not written if the column name is supplied in the column

rule number 2 AND 3, but my field "last_upadate" with default value=current_timestamp has working and have value by the state insert event if commit press or not pressed.
it is the different by current_user.

here the snipset of form:
----------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IB_Components, IB_Access, Grids, IB_Grid, IB_TransactionBar,
IB_DatasetBar, ExtCtrls, IB_UpdateBar, StdCtrls;

type
TForm1 = class(TForm)
IB_Connection1: TIB_Connection;
IB_Query1: TIB_Query;
IB_Transaction1: TIB_Transaction;
IB_UpdateBar1: TIB_UpdateBar;
IB_DataSource1: TIB_DataSource;
IB_DatasetBar1: TIB_DatasetBar;
IB_TransactionBar1: TIB_TransactionBar;
IB_Grid1: TIB_Grid;
Memo1: TMemo;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

end.
-------------
the view of text form
-------------
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 300
ClientWidth = 920
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object IB_UpdateBar1: TIB_UpdateBar
Left = 152
Top = 256
Width = 120
Height = 25
Ctl3D = False
ParentCtl3D = False
TabOrder = 0
DataSource = IB_DataSource1
ReceiveFocus = False
CustomGlyphsSupplied = []
end
object IB_DatasetBar1: TIB_DatasetBar
Left = 278
Top = 256
Width = 120
Height = 25
Ctl3D = False
ParentCtl3D = False
TabOrder = 1
DataSource = IB_DataSource1
ReceiveFocus = False
CustomGlyphsSupplied = []
end
object IB_TransactionBar1: TIB_TransactionBar
Left = 8
Top = 256
Width = 120
Height = 25
Ctl3D = False
ParentCtl3D = False
TabOrder = 2
CustomGlyphsSupplied = []
IB_Transaction = IB_Transaction1
ReceiveFocus = False
end
object IB_Grid1: TIB_Grid
Left = 8
Top = 8
Width = 390
Height = 242
CustomGlyphsSupplied = []
DataSource = IB_DataSource1
TabOrder = 3
end
object Memo1: TMemo
Left = 404
Top = 8
Width = 501
Height = 242
Lines.Strings = (

'/***************************************************************' +
'***************/'

'/*** Generated by IBExpert 2/6/2012 9:51:23 AM ' +
' ***/'

'/***************************************************************' +
'***************/'
''

'/***************************************************************' +
'***************/'

'/*** Following SET SQL DIALECT is just for the Database Com' +
'parer ***/'

'/***************************************************************' +
'***************/'
'SET SQL DIALECT 3;'
''
''
''

'/***************************************************************' +
'***************/'

'/*** Tables ' +
' ***/'

'/***************************************************************' +
'***************/'
''
''
'CREATE GENERATOR GEN_TCONTACT_ID;'
''
'CREATE TABLE TCONTACT ('

' CONTACT_ID INTEGER_1 NOT NULL /* INTEGER_1 = INTEGER */' +
','
' TITLE_NAME VARCHAR10 /* VARCHAR10 = VARCHAR(10) */,'

' FIRST_NAME VARCHAR20 NOT NULL /* VARCHAR20 = VARCHAR(20' +
') */,'
' LAST_NAME VARCHAR20 /* VARCHAR20 = VARCHAR(20) */,'
' FULL_NAME COMPUTED BY (first_name ||'
'case'
' when last_name is null then '#39#39
' else '#39' '#39' || last_name'
'end),'
' SEX CHAR1 /* CHAR1 = CHAR(1) */,'
' JOB_POSITION VARCHAR40 /* VARCHAR40 = VARCHAR(40) */,'
' MOBILE_PHONE1 VARCHAR20 /* VARCHAR20 = VARCHAR(20) */,'
' MOBILE_PHONE2 VARCHAR20 /* VARCHAR20 = VARCHAR(20) */,'
' EMAIL1 VARCHAR30 /* VARCHAR30 = VARCHAR(30) */,'
' EMAIL2 VARCHAR30 /* VARCHAR30 = VARCHAR(30) */,'
' HOME_ADDR VARCHAR80 /* VARCHAR80 = VARCHAR(80) */,'
' HOME_CITY VARCHAR30 /* VARCHAR30 = VARCHAR(30) */,'
' HOME_PROVINCE VARCHAR30 /* VARCHAR30 = VARCHAR(30) */,'
' HOME_POST_CODE VARCHAR10 /* VARCHAR10 = VARCHAR(10) */,'
' HOME_COUNTRY VARCHAR20 /* VARCHAR20 = VARCHAR(20) */,'
' HOME_PHONE VARCHAR20 /* VARCHAR20 = VARCHAR(20) */,'
' NPWP VARCHAR20 /* VARCHAR20 = VARCHAR(20) */,'
' BIRTHDATE DATE_1 /* DATE_1 = DATE */,'

' YEARS_OLD COMPUTED BY (((CAST('#39'TODAY'#39' AS DATE)-tcontac' +
't.BIRTHDATE)/360)),'
' INFO VARCHAR80 /* VARCHAR80 = VARCHAR(80) */,'

' ISSUSPEND BOOLEAN_DEFAULT_0 /* BOOLEAN_DEFAULT_0 = SMA' +
'LLINT DEFAULT 0 */,'

' LAST_UPDATE TIMESTAMP_1 DEFAULT current_timestamp NOT NU' +
'LL /* TIMESTAMP_1 = '
'TIMESTAMP */,'

' LAST_USER VARCHAR30 DEFAULT current_user /* VARCHAR30 ' +
'= VARCHAR(30) */,'
' KODE_CABANG INTEGER_1 /* INTEGER_1 = INTEGER */'
');'
''
''
''
''
'/* Check constraints definition */'
''

'ALTER TABLE TCONTACT ADD CONSTRAINT CHK1_TCONTACT_SEX check (sex' +
' in ('#39'F'#39','#39'M'#39'));'

'ALTER TABLE TCONTACT ADD CONSTRAINT CHK1_TCONTACT_EMAIL1 check (' +
'email1 like '#39'%@%.'
'%'#39');'

'ALTER TABLE TCONTACT ADD CONSTRAINT CHK1_TCONTACT_EMAIL2 check (' +
'email2 like '#39'%@%.'
'%'#39');'
''
''

'/***************************************************************' +
'***************/'

'/*** Unique Constraints ' +
' ***/'

'/***************************************************************' +
'***************/'
''

'ALTER TABLE TCONTACT ADD CONSTRAINT UNQ1_TCONTACT UNIQUE (FIRST_' +
'NAME, LAST_NAME, '
'BIRTHDATE);'
''
''

'/***************************************************************' +
'***************/'

'/*** Primary Keys ' +
' ***/'

'/***************************************************************' +
'***************/'
''

'ALTER TABLE TCONTACT ADD CONSTRAINT PK_TCONTACT PRIMARY KEY (CON' +
'TACT_ID);'
''
''

'/***************************************************************' +
'***************/'

'/*** Triggers ' +
' ***/'

'/***************************************************************' +
'***************/'
''
''
'SET TERM ^ ;'
''
''
''

'/***************************************************************' +
'***************/'

'/*** Triggers for tables ' +
' ***/'

'/***************************************************************' +
'***************/'
''
''
''
'/* Trigger: TCONTACT_AIUD0 */'
'CREATE OR ALTER TRIGGER TCONTACT_AIUD0 FOR TCONTACT'
'ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 0'
'AS'
'begin'
' /* Trigger text */'
' POST_EVENT '#39'Contact Person'#39';'
'end'
'^'
''
''
'/* Trigger: TCONTACT_BI */'
'CREATE OR ALTER TRIGGER TCONTACT_BI FOR TCONTACT'
'ACTIVE BEFORE INSERT POSITION 0'
'AS'
'BEGIN'
' IF (NEW.CONTACT_ID IS NULL) THEN'
' NEW.CONTACT_ID = GEN_ID(GEN_TCONTACT_ID,1);'
'END'
'^'
''
''
'/* Trigger: TCONTACT_BIU0 */'
'CREATE OR ALTER TRIGGER TCONTACT_BIU0 FOR TCONTACT'
'ACTIVE BEFORE UPDATE POSITION 0'
'AS'
'begin'
' /* Trigger text */'
' new.last_user = current_user;'
' new.last_update = current_timestamp;'
'end'
'^'
''
''
'SET TERM ; ^'
''
''
''

'/***************************************************************' +
'***************/'

'/*** Privileges ' +
' ***/'

'/***************************************************************' +
'***************/')
TabOrder = 4
end
object IB_Connection1: TIB_Connection
CacheStatementHandles = False
PasswordStorage = psNotSecure
SQLDialect = 3
Params.Strings = (
'PATH=D:\project\GL\bin\db\MITRACHEM.FDB'
'CHARACTER SET=UTF8'
'USER NAME=SYSDBA'
'SQL DIALECT=3')
Left = 40
Top = 16
SavedPassword = '.JuMbLe.01.432B0639073E0E4B49'
end
object IB_Query1: TIB_Query
DatabaseName = 'D:\project\GL\bin\db\MITRACHEM.FDB'
IB_Connection = IB_Connection1
IB_Transaction = IB_Transaction1
SQL.Strings = (
'SELECT *'
'FROM TCONTACT'
'FOR UPDATE')
GeneratorLinks.Strings = (
'CONTACT_ID=GEN_TCONTACT_ID')
GetServerDefaults = True
KeyLinks.Strings = (
'CONTACT_ID')
KeyRelation = 'TCONTACT'
RequestLive = True
Left = 120
Top = 16
end
object IB_Transaction1: TIB_Transaction
IB_Connection = IB_Connection1
Isolation = tiCommitted
Left = 40
Top = 80
end
object IB_DataSource1: TIB_DataSource
Dataset = IB_Query1
Left = 192
Top = 16
end
end
------------



--- In IBObjects@yahoogroups.com, Helen Borrie <helebor@...> wrote:
>
> At 12:00 PM 1/02/2012, ming wrote:
> >hi all
> >
> >my problem for default value still error in ibo even i use the newest ibo, any sugestion for my problem?
>
> I think you misunderstand how the Firebird/IB column default works...
> 1. It works ONLY for an insert; AND
> 2. It is not available until the record is written to disk; AND
> 3. It is not written if the column name is supplied in the column list.
>
>
> >because the trigger can be alternative but i think not eficient for every table i must add before insert for 1 field only type data varchar.
> >
> >Need solution, any body help me please...
>
> You could provide custom InsertSQL for your datasets, leaving the defaulted column out of the INSERT statement.
> *or*
> You could use the DefaultValue attribute of the field in the dataset. However, this is not ideal as it binds your application layer to the database too tightly.
> Along the same lines, if you are using domains for these columns, you can use IB_Connection.DefaultValues to provide domain_name=value strings for any application level. This still has the tight binding limitation.
>
> In my view, the Before Insert trigger is the only good solution from the point of view of data integrity. If you write it correctly, you will always get the value you want, regardless of the application. The SQL default is almost entirely useless.
>
> Why do you think it is too much work? Are you maintaining proper DDL scripts for your databases?
>
> Helen
>

I Think the almost my table have a field last_user and last_update
so for the best maintenance i create
a domain name 'sysuser' with the default value current_user VARchar30
a domain name 'timestamp_1' with the default value current_timestamp timestamp

the snipset my small project build with delphi 2010 and win7 and fb.25
and ibo 4.9.14 36J

many thank Hellen for your reply