mirror of
https://github.com/HeidiSQL/HeidiSQL.git
synced 2025-08-26 11:17:57 +08:00
Support default values of BIT columns in table editor and SQL export. Fixes issue #2544.
This commit is contained in:
@ -3181,7 +3181,7 @@ procedure TDBConnection.ParseTableStructure(CreateTable: String; Columns: TTable
|
|||||||
var
|
var
|
||||||
ColSpec: String;
|
ColSpec: String;
|
||||||
rx, rxCol: TRegExpr;
|
rx, rxCol: TRegExpr;
|
||||||
i: Integer;
|
i, LiteralStart: Integer;
|
||||||
InLiteral: Boolean;
|
InLiteral: Boolean;
|
||||||
Col: TTableColumn;
|
Col: TTableColumn;
|
||||||
Key: TTableKey;
|
Key: TTableKey;
|
||||||
@ -3317,16 +3317,17 @@ begin
|
|||||||
Col.DefaultType := cdtCurTS;
|
Col.DefaultType := cdtCurTS;
|
||||||
Col.DefaultText := 'CURRENT_TIMESTAMP';
|
Col.DefaultText := 'CURRENT_TIMESTAMP';
|
||||||
Delete(ColSpec, 1, 18);
|
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;
|
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
|
if ColSpec[i] = '''' then
|
||||||
InLiteral := not InLiteral
|
InLiteral := not InLiteral
|
||||||
else if not InLiteral then
|
else if not InLiteral then
|
||||||
break;
|
break;
|
||||||
end;
|
end;
|
||||||
Col.DefaultType := cdtText;
|
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
|
// 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]);
|
Col.DefaultText := StringReplace(Col.DefaultText, '''''', '''', [rfReplaceAll]);
|
||||||
Delete(ColSpec, 1, i);
|
Delete(ColSpec, 1, i);
|
||||||
@ -5103,7 +5104,7 @@ begin
|
|||||||
Result := Result + ' NULL';
|
Result := Result + ' NULL';
|
||||||
end;
|
end;
|
||||||
if DefaultType <> cdtNothing then begin
|
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
|
Result := TrimRight(Result); // Remove whitespace for columns without default value
|
||||||
end;
|
end;
|
||||||
if IsVirtual then
|
if IsVirtual then
|
||||||
|
@ -314,7 +314,7 @@ type
|
|||||||
function StringListCompareAnythingAsc(List: TStringList; Index1, Index2: Integer): Integer;
|
function StringListCompareAnythingAsc(List: TStringList; Index1, Index2: Integer): Integer;
|
||||||
function StringListCompareAnythingDesc(List: TStringList; Index1, Index2: Integer): Integer;
|
function StringListCompareAnythingDesc(List: TStringList; Index1, Index2: Integer): Integer;
|
||||||
function GetColumnDefaultType(var Text: String): TColumnDefaultType;
|
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 GetImageLinkTimeStamp(const FileName: string): TDateTime;
|
||||||
function IsEmpty(Str: String): Boolean;
|
function IsEmpty(Str: String): Boolean;
|
||||||
function IsNotEmpty(Str: String): Boolean;
|
function IsNotEmpty(Str: String): Boolean;
|
||||||
@ -2305,12 +2305,16 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; Text: String): String;
|
function GetColumnDefaultClause(DefaultType: TColumnDefaultType; DataTypeIndex: TDBDatatypeIndex; Text: String): String;
|
||||||
begin
|
begin
|
||||||
|
Text := esc(Text);
|
||||||
|
// Support BIT syntax in MySQL
|
||||||
|
if DataTypeIndex = dtBit then
|
||||||
|
Text := 'b'+Text;
|
||||||
case DefaultType of
|
case DefaultType of
|
||||||
cdtNothing: Result := '';
|
cdtNothing: Result := '';
|
||||||
cdtText: Result := 'DEFAULT '+esc(Text);
|
cdtText: Result := 'DEFAULT '+Text;
|
||||||
cdtTextUpdateTS: Result := 'DEFAULT '+esc(Text)+' ON UPDATE CURRENT_TIMESTAMP';
|
cdtTextUpdateTS: Result := 'DEFAULT '+Text+' ON UPDATE CURRENT_TIMESTAMP';
|
||||||
cdtNull: Result := 'DEFAULT NULL';
|
cdtNull: Result := 'DEFAULT NULL';
|
||||||
cdtNullUpdateTS: Result := 'DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP';
|
cdtNullUpdateTS: Result := 'DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP';
|
||||||
cdtCurTS: Result := 'DEFAULT CURRENT_TIMESTAMP';
|
cdtCurTS: Result := 'DEFAULT CURRENT_TIMESTAMP';
|
||||||
|
@ -561,7 +561,7 @@ begin
|
|||||||
ColSpec := ColSpec + ' NULL';
|
ColSpec := ColSpec + ' NULL';
|
||||||
end;
|
end;
|
||||||
if Col.DefaultType <> cdtNothing then begin
|
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
|
ColSpec := TrimRight(ColSpec); // Remove whitespace for columns without default value
|
||||||
end;
|
end;
|
||||||
if IsVirtual then
|
if IsVirtual then
|
||||||
|
Reference in New Issue
Block a user