From b3750421b2ea356d1daf9acfd0f588e1eb014b90 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Wed, 20 May 2009 21:10:02 +0000 Subject: [PATCH] Implement rfe #602: Explicitely apply collation to all fields if table collation has changed. Generates a "CONVERT TO CHARSET" clause. --- source/table_editor.dfm | 44 ++++++++++++++++++------------ source/table_editor.pas | 59 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/source/table_editor.dfm b/source/table_editor.dfm index 126c212d..e5104159 100644 --- a/source/table_editor.dfm +++ b/source/table_editor.dfm @@ -267,14 +267,14 @@ object frmTableEditor: TfrmTableEditor Caption = 'Average row length:' end object lblInsertMethod: TLabel - Left = 315 + Left = 258 Top = 98 Width = 79 Height = 13 Caption = 'INSERT method:' end object lblUnion: TLabel - Left = 315 + Left = 258 Top = 52 Width = 63 Height = 13 @@ -295,14 +295,14 @@ object frmTableEditor: TfrmTableEditor Caption = 'Row format:' end object lblCollation: TLabel - Left = 315 + Left = 258 Top = 6 Width = 81 Height = 13 Caption = 'Default collation:' end object lblEngine: TLabel - Left = 315 + Left = 258 Top = 29 Width = 36 Height = 13 @@ -311,7 +311,7 @@ object frmTableEditor: TfrmTableEditor object editAvgRowLen: TEdit Left = 136 Top = 26 - Width = 153 + Width = 110 Height = 21 TabOrder = 1 OnChange = editNumEditChange @@ -319,7 +319,7 @@ object frmTableEditor: TfrmTableEditor object editMaxRows: TEdit Left = 136 Top = 49 - Width = 153 + Width = 110 Height = 21 TabOrder = 2 OnChange = editNumEditChange @@ -337,7 +337,7 @@ object frmTableEditor: TfrmTableEditor object comboRowFormat: TComboBox Left = 136 Top = 95 - Width = 153 + Width = 110 Height = 21 Style = csDropDownList ItemHeight = 0 @@ -345,9 +345,9 @@ object frmTableEditor: TfrmTableEditor OnChange = Modification end object memoUnionTables: TTntMemo - Left = 411 + Left = 354 Top = 49 - Width = 160 + Width = 217 Height = 44 Anchors = [akLeft, akTop, akRight] Lines.Strings = ( @@ -356,9 +356,9 @@ object frmTableEditor: TfrmTableEditor OnChange = Modification end object comboInsertMethod: TComboBox - Left = 411 + Left = 354 Top = 95 - Width = 160 + Width = 217 Height = 21 Style = csDropDownList Anchors = [akLeft, akTop, akRight] @@ -369,27 +369,27 @@ object frmTableEditor: TfrmTableEditor object editAutoInc: TEdit Left = 136 Top = 3 - Width = 152 + Width = 110 Height = 21 TabOrder = 0 OnChange = editNumEditChange end object comboCollation: TComboBox - Left = 411 + Left = 354 Top = 3 - Width = 160 + Width = 104 Height = 21 Style = csDropDownList Anchors = [akLeft, akTop, akRight] ItemHeight = 0 Sorted = True TabOrder = 5 - OnChange = Modification + OnChange = chkCharsetConvertClick end object comboEngine: TComboBox - Left = 411 + Left = 354 Top = 26 - Width = 160 + Width = 217 Height = 21 Style = csDropDownList Anchors = [akLeft, akTop, akRight] @@ -398,6 +398,16 @@ object frmTableEditor: TfrmTableEditor OnChange = Modification OnSelect = comboEngineSelect end + object chkCharsetConvert: TCheckBox + Left = 464 + Top = 5 + Width = 107 + Height = 17 + Anchors = [akTop, akRight] + Caption = 'Convert data' + TabOrder = 9 + OnClick = chkCharsetConvertClick + end end object tabIndexes: TTabSheet Caption = 'Indexes' diff --git a/source/table_editor.pas b/source/table_editor.pas index 5ab26092..6749b6d9 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -71,6 +71,7 @@ type menuClearColumns: TMenuItem; menuMoveUpColumn: TMenuItem; menuMoveDownColumn: TMenuItem; + chkCharsetConvert: TCheckBox; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure editNameChange(Sender: TObject); @@ -127,6 +128,7 @@ type procedure btnRemoveIndexClick(Sender: TObject); procedure menuAddIndexColumnClick(Sender: TObject); procedure PageControlMainChange(Sender: TObject); + procedure chkCharsetConvertClick(Sender: TObject); private { Private declarations } FModified: Boolean; @@ -331,6 +333,8 @@ end; procedure TfrmTableEditor.btnApplyClick(Sender: TObject); var sql: WideString; + i: Integer; + Props: TWideStringlist; begin // Create or alter table if AlterTableName = '' then @@ -339,9 +343,20 @@ begin sql := ComposeAlterStatement; try Mainform.ExecUpdateQuery(sql, False, True); - ResetModificationFlags; // Set table name for altering if Apply was clicked AlterTableName := editName.Text; + if chkCharsetConvert.Checked then begin + // Autoadjust column collations + for i:=0 to Columns.Count-1 do begin + Props := TWideStringlist(Columns.Objects[i]); + if Props[6] <> '' then begin + Props.OnChange := nil; + Props[6] := comboCollation.Text; + Props.OnChange := ColumnsChange; + end; + end; + end; + ResetModificationFlags; except ModalResult := mrNone; end; @@ -367,6 +382,10 @@ begin Parts.Assign(TWideStringlist(Indexes.Objects[i])); OldIndexes.AddObject(Indexes[i], Parts); end; + // Enable converting data for an existing table + chkCharsetConvert.Enabled := AlterTablename <> ''; + // Assist the user in auto unchecking this checkbox so data doesn't get converted more than once accidently + chkCharsetConvert.Checked := False; Modified := False; end; @@ -389,6 +408,7 @@ var AddIt, DropIt: Boolean; dt: TMySQLDataTypeRecord; DefaultType: TColumnDefaultType; + ds: TDataset; begin // Compose ALTER query, called by buttons and for SQL code tab SetStatus('Composing ALTER statement ...'); @@ -397,7 +417,7 @@ begin Specs.Add('RENAME TO ' + Mainform.mask(editName.Text)); if memoComment.Tag = ModifiedFlag then Specs.Add('COMMENT = ' + esc(memoComment.Text)); - if comboCollation.Tag = ModifiedFlag then + if (comboCollation.Tag = ModifiedFlag) or (chkCharsetConvert.Checked) then Specs.Add('COLLATE = ' + comboCollation.Text); if comboEngine.Tag = ModifiedFlag then Specs.Add('ENGINE = ' + comboEngine.Text); @@ -415,6 +435,17 @@ begin Specs.Add('UNION = ('+memoUnionTables.Text+')'); if comboInsertMethod.Enabled and (comboInsertMethod.Tag = ModifiedFlag) and (comboInsertMethod.Text <> '') then Specs.Add('INSERT_METHOD = '+comboInsertMethod.Text); + if chkCharsetConvert.Checked then begin + ds := Mainform.GetCollations; + while not ds.Eof do begin + if ds.FieldByName('Collation').AsWideString = comboCollation.Text then begin + Specs.Add('CONVERT TO CHARSET '+ds.FieldByName('Charset').AsString); + break; + end; + ds.Next; + end; + end; + // Update columns for i:=0 to Columns.Count - 1 do begin AddIt := True; @@ -441,8 +472,13 @@ begin end; if Props[5] <> '' then ColSpec := ColSpec + ' COMMENT '+esc(Props[5]); - if Props[6] <> '' then - ColSpec := ColSpec + ' COLLATE '+Props[6]; + if Props[6] <> '' then begin + ColSpec := ColSpec + ' COLLATE '; + if chkCharsetConvert.Checked then + ColSpec := ColSpec + comboCollation.Text + else + ColSpec := ColSpec + Props[6]; + end; if i = 0 then ColSpec := ColSpec + ' FIRST' else @@ -792,6 +828,8 @@ procedure TfrmTableEditor.listColumnsEditing(Sender: TBaseVirtualTree; begin // No editor for very first column and checkbox columns Allowed := not (Column in [0, 4, 5]); + // No editing of collation allowed if "Convert data" was checked + Allowed := Allowed and ((Column = 8) and (not chkCharsetConvert.Checked)); end; @@ -809,6 +847,8 @@ begin else begin Properties := TWideStringList(Columns.Objects[Node.Index]); CellText := Properties[Column-2]; + if (Column = 8) and (CellText <> '') and (chkCharsetConvert.Checked) then + CellText := comboCollation.Text; end; end; if Column = 6 then @@ -1436,6 +1476,17 @@ begin end; +procedure TfrmTableEditor.chkCharsetConvertClick(Sender: TObject); +begin + if chkCharsetConvert.Checked then + listColumns.Header.Columns[8].Color := clBtnFace + else + listColumns.Header.Columns[8].Color := clWindow; + listColumns.Repaint; + Modification(Sender); +end; + + procedure TfrmTableEditor.btnHelpClick(Sender: TObject); begin // Help button