Allow columns to have no default value at all. Important for TEXTs and BLOBs which cannot have one. Fixes issue #1261.

This commit is contained in:
Ansgar Becker
2009-08-30 18:31:31 +00:00
parent 506a187239
commit 9e731a78e1
2 changed files with 27 additions and 11 deletions

View File

@ -129,11 +129,11 @@ type
property MaxLength: Integer read FMaxLength write FMaxLength; property MaxLength: Integer read FMaxLength write FMaxLength;
end; end;
TColumnDefaultType = (cdtText, cdtTextUpdateTS, cdtNull, cdtNullUpdateTS, cdtCurTS, cdtCurTSUpdateTS, cdtAutoInc); TColumnDefaultType = (cdtNothing, cdtText, cdtTextUpdateTS, cdtNull, cdtNullUpdateTS, cdtCurTS, cdtCurTSUpdateTS, cdtAutoInc);
TColumnDefaultEditorLink = class(TBaseGridEditorLink) TColumnDefaultEditorLink = class(TBaseGridEditorLink)
private private
FPanel: TPanel; FPanel: TPanel;
FRadioText, FRadioNULL, FRadioCurTS, FRadioAutoInc: TRadioButton; FRadioNothing, FRadioText, FRadioNULL, FRadioCurTS, FRadioAutoInc: TRadioButton;
FCheckCurTS: TCheckbox; FCheckCurTS: TCheckbox;
FMemoText: TTNTMemo; FMemoText: TTNTMemo;
FBtnOK, FBtnCancel: TButton; FBtnOK, FBtnCancel: TButton;
@ -960,9 +960,17 @@ begin
FPanel.BevelOuter := bvNone; FPanel.BevelOuter := bvNone;
FMainControl := FPanel; FMainControl := FPanel;
FRadioNothing := TRadioButton.Create(FPanel);
FRadioNothing.Parent := FPanel;
FRadioNothing.Top := m;
FRadioNothing.Left := m;
FRadioNothing.Width := FRadioNothing.Parent.Width - 2 * FRadioNothing.Left;
FRadioNothing.OnClick := RadioClick;
FRadioNothing.Caption := 'No default value';
FRadioText := TRadioButton.Create(FPanel); FRadioText := TRadioButton.Create(FPanel);
FRadioText.Parent := FPanel; FRadioText.Parent := FPanel;
FRadioText.Top := m; FRadioText.Top := FRadioNothing.Top + FRadioNothing.Height + m;;
FRadioText.Left := m; FRadioText.Left := m;
FRadioText.Width := FRadioText.Parent.Width - 2 * FRadioText.Left; FRadioText.Width := FRadioText.Parent.Width - 2 * FRadioText.Left;
FRadioText.OnClick := RadioClick; FRadioText.OnClick := RadioClick;
@ -1028,6 +1036,7 @@ begin
FBtnCancel.Caption := 'Cancel'; FBtnCancel.Caption := 'Cancel';
FPanel.Height := FBtnOk.Top + FBtnOk.Height + m; FPanel.Height := FBtnOk.Top + FBtnOk.Height + m;
FRadioNothing.Anchors := [akLeft, akTop, akRight];
FRadioText.Anchors := [akLeft, akTop, akRight]; FRadioText.Anchors := [akLeft, akTop, akRight];
FMemoText.Anchors := [akLeft, akTop, akRight, akBottom]; FMemoText.Anchors := [akLeft, akTop, akRight, akBottom];
FRadioNull.Anchors := [akLeft, akBottom, akRight]; FRadioNull.Anchors := [akLeft, akBottom, akRight];
@ -1052,6 +1061,7 @@ begin
inherited PrepareEdit(Tree, Node, Column); inherited PrepareEdit(Tree, Node, Column);
case DefaultType of case DefaultType of
cdtNothing: FRadioNothing.Checked := True;
cdtText, cdtTextUpdateTS: begin cdtText, cdtTextUpdateTS: begin
FRadioText.Checked := True; FRadioText.Checked := True;
FMemoText.Text := DefaultText; FMemoText.Text := DefaultText;
@ -1080,7 +1090,8 @@ begin
Result := not FStopping; Result := not FStopping;
if Result then begin if Result then begin
FPanel.Show; FPanel.Show;
if FRadioText.Checked then FRadioText.SetFocus if FRadioNothing.Checked then FRadioNothing.SetFocus
else if FRadioText.Checked then FRadioText.SetFocus
else if FRadioNull.Checked then FRadioNull.SetFocus else if FRadioNull.Checked then FRadioNull.SetFocus
else if FRadioCurTS.Checked then FRadioCurTS.SetFocus else if FRadioCurTS.Checked then FRadioCurTS.SetFocus
else if FRadioAutoInc.Checked then FRadioAutoInc.SetFocus; else if FRadioAutoInc.Checked then FRadioAutoInc.SetFocus;
@ -1096,7 +1107,9 @@ begin
Result := not FStopping; Result := not FStopping;
if Result then begin if Result then begin
FStopping := True; FStopping := True;
if FRadioText.Checked and FCheckCurTS.Checked then if FRadioNothing.Checked then
newDefaultType := cdtNothing
else if FRadioText.Checked and FCheckCurTS.Checked then
newDefaultType := cdtTextUpdateTS newDefaultType := cdtTextUpdateTS
else if FRadioText.Checked then else if FRadioText.Checked then
newDefaultType := cdtText newDefaultType := cdtText
@ -1114,6 +1127,7 @@ begin
newDefaultType := cdtText; newDefaultType := cdtText;
case newDefaultType of case newDefaultType of
cdtNothing: newText := '';
cdtText, cdtTextUpdateTS: newText := FMemoText.Text; cdtText, cdtTextUpdateTS: newText := FMemoText.Text;
cdtNull, cdtNullUpdateTS: newText := 'NULL'; cdtNull, cdtNullUpdateTS: newText := 'NULL';
cdtCurTS, cdtCurTSUpdateTS: newText := 'CURRENT_TIMESTAMP'; cdtCurTS, cdtCurTSUpdateTS: newText := 'CURRENT_TIMESTAMP';
@ -1134,6 +1148,7 @@ begin
FMemoText.Color := clBtnFace FMemoText.Color := clBtnFace
else else
FMemoText.Color := clWindow; FMemoText.Color := clWindow;
FCheckCurTS.Enabled := not FRadioNothing.Checked;
end; end;
@ -1153,6 +1168,7 @@ end;
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; Text: WideString): WideString; function GetColumnDefaultClause(DefaultType: TColumnDefaultType; Text: WideString): WideString;
begin begin
case DefaultType of case DefaultType of
cdtNothing: Result := '';
cdtText: Result := 'DEFAULT '+esc(Text); cdtText: Result := 'DEFAULT '+esc(Text);
cdtTextUpdateTS: Result := 'DEFAULT '+esc(Text)+' ON UPDATE CURRENT_TIMESTAMP'; cdtTextUpdateTS: Result := 'DEFAULT '+esc(Text)+' ON UPDATE CURRENT_TIMESTAMP';
cdtNull: Result := 'DEFAULT NULL'; cdtNull: Result := 'DEFAULT NULL';

View File

@ -397,18 +397,16 @@ begin
if UpperCase(Copy(ColSpec, 1, 8)) = 'NOT NULL' then begin if UpperCase(Copy(ColSpec, 1, 8)) = 'NOT NULL' then begin
Props[3] := BoolToStr(False); Props[3] := BoolToStr(False);
Delete(ColSpec, 1, 9); Delete(ColSpec, 1, 9);
ColDefaultType := cdtText;
ColDefaultText := '';
end else begin end else begin
Props[3] := BoolToStr(True); Props[3] := BoolToStr(True);
ColDefaultType := cdtNull;
ColDefaultText := 'NULL';
// Sporadically there is a "NULL" found at this position. // Sporadically there is a "NULL" found at this position.
if UpperCase(Copy(ColSpec, 1, 4)) = 'NULL' then if UpperCase(Copy(ColSpec, 1, 4)) = 'NULL' then
Delete(ColSpec, 1, 5); Delete(ColSpec, 1, 5);
end; end;
// Default value // Default value
ColDefaultType := cdtNothing;
ColDefaultText := '';
if UpperCase(Copy(ColSpec, 1, 14)) = 'AUTO_INCREMENT' then begin if UpperCase(Copy(ColSpec, 1, 14)) = 'AUTO_INCREMENT' then begin
ColDefaultType := cdtAutoInc; ColDefaultType := cdtAutoInc;
ColDefaultText := 'AUTO_INCREMENT'; ColDefaultText := 'AUTO_INCREMENT';
@ -687,6 +685,7 @@ begin
DefaultText := Props[4]; DefaultText := Props[4];
DefaultType := GetColumnDefaultType(DefaultText); DefaultType := GetColumnDefaultType(DefaultText);
ColSpec := ColSpec + ' ' + GetColumnDefaultClause(DefaultType, DefaultText); ColSpec := ColSpec + ' ' + GetColumnDefaultClause(DefaultType, DefaultText);
ColSpec := Trim(ColSpec); // Remove whitespace for columns without default value
end; end;
if Props[5] <> '' then if Props[5] <> '' then
ColSpec := ColSpec + ' COMMENT '+esc(Props[5]); ColSpec := ColSpec + ' COMMENT '+esc(Props[5]);
@ -794,6 +793,7 @@ begin
DefaultText := ColProps[4]; DefaultText := ColProps[4];
DefaultType := GetColumnDefaultType(DefaultText); DefaultType := GetColumnDefaultType(DefaultText);
Result := Result + ' ' + GetColumnDefaultClause(DefaultType, DefaultText); Result := Result + ' ' + GetColumnDefaultClause(DefaultType, DefaultText);
Result := Trim(Result); // Remove whitespace for columns without default value
end; end;
if ColProps[5] <> '' then if ColProps[5] <> '' then
Result := Result + ' COMMENT '+esc(ColProps[5]); Result := Result + ' COMMENT '+esc(ColProps[5]);
@ -947,7 +947,7 @@ begin
Properties.Assign(DefProperties); Properties.Assign(DefProperties);
end else begin end else begin
idx := Columns.Count; idx := Columns.Count;
Properties.CommaText := 'INT,10,'+BoolToStr(False)+','+BoolToStr(True)+','+IntToStr(Integer(cdtNull))+'NULL,,'; Properties.CommaText := 'INT,10,'+BoolToStr(False)+','+BoolToStr(True)+','+IntToStr(Integer(cdtNothing))+',,';
end; end;
Columns.InsertObject(idx, 'Column '+IntToStr(idx+1), Properties); Columns.InsertObject(idx, 'Column '+IntToStr(idx+1), Properties);
SelectNode(listColumns, idx); SelectNode(listColumns, idx);