From 550a139dac24ada2117f12221d612f16dab66e0a Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Sun, 19 May 2013 04:30:14 +0000 Subject: [PATCH] Support default values of BIT columns in table editor and SQL export. Fixes issue #2544. --- source/dbconnection.pas | 11 ++++++----- source/helpers.pas | 12 ++++++++---- source/table_editor.pas | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 4d2768ff..093b87ea 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -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 diff --git a/source/helpers.pas b/source/helpers.pas index 2cbf258f..361038a4 100644 --- a/source/helpers.pas +++ b/source/helpers.pas @@ -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'; diff --git a/source/table_editor.pas b/source/table_editor.pas index df17ecbb..12ca19eb 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -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