Support default values of BIT columns in table editor and SQL export. Fixes issue #2544.

This commit is contained in:
Ansgar Becker
2013-05-19 04:30:14 +00:00
parent 1e3fde7234
commit 550a139dac
3 changed files with 15 additions and 10 deletions

View File

@ -3181,7 +3181,7 @@ procedure TDBConnection.ParseTableStructure(CreateTable: String; Columns: TTable
var
ColSpec: String;
rx, rxCol: TRegExpr;
i: Integer;
i, LiteralStart: Integer;
InLiteral: Boolean;
Col: TTableColumn;
Key: TTableKey;
@ -3317,16 +3317,17 @@ begin
Col.DefaultType := cdtCurTS;
Col.DefaultText := 'CURRENT_TIMESTAMP';
Delete(ColSpec, 1, 18);
end else if ColSpec[1] = '''' then begin
end else if (ColSpec[1] = '''') or (Copy(ColSpec, 1, 2) = 'b''') then begin
InLiteral := True;
for i:=2 to Length(ColSpec) do begin
LiteralStart := Pos('''', ColSpec)+1;
for i:=LiteralStart to Length(ColSpec) do begin
if ColSpec[i] = '''' then
InLiteral := not InLiteral
else if not InLiteral then
break;
end;
Col.DefaultType := cdtText;
Col.DefaultText := Copy(ColSpec, 2, i-3);
Col.DefaultText := Copy(ColSpec, LiteralStart, i-LiteralStart-1);
// A single quote gets escaped by single quote - remove the escape char - escaping is done in Save action afterwards
Col.DefaultText := StringReplace(Col.DefaultText, '''''', '''', [rfReplaceAll]);
Delete(ColSpec, 1, i);
@ -5103,7 +5104,7 @@ begin
Result := Result + ' NULL';
end;
if DefaultType <> cdtNothing then begin
Result := Result + ' ' + GetColumnDefaultClause(DefaultType, DefaultText);
Result := Result + ' ' + GetColumnDefaultClause(DefaultType, DataType.Index, DefaultText);
Result := TrimRight(Result); // Remove whitespace for columns without default value
end;
if IsVirtual then

View File

@ -314,7 +314,7 @@ type
function StringListCompareAnythingAsc(List: TStringList; Index1, Index2: Integer): Integer;
function StringListCompareAnythingDesc(List: TStringList; Index1, Index2: Integer): Integer;
function GetColumnDefaultType(var Text: String): TColumnDefaultType;
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; Text: String): String;
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; DataTypeIndex: TDBDatatypeIndex; Text: String): String;
function GetImageLinkTimeStamp(const FileName: string): TDateTime;
function IsEmpty(Str: String): Boolean;
function IsNotEmpty(Str: String): Boolean;
@ -2305,12 +2305,16 @@ begin
end;
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; Text: String): String;
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; DataTypeIndex: TDBDatatypeIndex; Text: String): String;
begin
Text := esc(Text);
// Support BIT syntax in MySQL
if DataTypeIndex = dtBit then
Text := 'b'+Text;
case DefaultType of
cdtNothing: Result := '';
cdtText: Result := 'DEFAULT '+esc(Text);
cdtTextUpdateTS: Result := 'DEFAULT '+esc(Text)+' ON UPDATE CURRENT_TIMESTAMP';
cdtText: Result := 'DEFAULT '+Text;
cdtTextUpdateTS: Result := 'DEFAULT '+Text+' ON UPDATE CURRENT_TIMESTAMP';
cdtNull: Result := 'DEFAULT NULL';
cdtNullUpdateTS: Result := 'DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP';
cdtCurTS: Result := 'DEFAULT CURRENT_TIMESTAMP';

View File

@ -561,7 +561,7 @@ begin
ColSpec := ColSpec + ' NULL';
end;
if Col.DefaultType <> cdtNothing then begin
ColSpec := ColSpec + ' ' + GetColumnDefaultClause(Col.DefaultType, Col.DefaultText);
ColSpec := ColSpec + ' ' + GetColumnDefaultClause(Col.DefaultType, Col.DataType.Index, Col.DefaultText);
ColSpec := TrimRight(ColSpec); // Remove whitespace for columns without default value
end;
if IsVirtual then