From 33730aa064b342095b6325d206ae6958efcaf034 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Thu, 21 Feb 2013 08:56:58 +0000 Subject: [PATCH] Create database dialog: Remove charset pulldown, leave collation pulldown alone. Init collation pulldown with the server variable "collation_server". --- out/locale/en/LC_MESSAGES/default.po | 16 +-- source/createdatabase.dfm | 77 +++++------ source/createdatabase.pas | 185 +++++++++------------------ 3 files changed, 102 insertions(+), 176 deletions(-) diff --git a/out/locale/en/LC_MESSAGES/default.po b/out/locale/en/LC_MESSAGES/default.po index d217e0b8..29f0756f 100644 --- a/out/locale/en/LC_MESSAGES/default.po +++ b/out/locale/en/LC_MESSAGES/default.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: HeidiSQL\n" "POT-Creation-Date: 2012-11-05 21:40\n" -"PO-Revision-Date: 2013-02-19 10:19+0100\n" +"PO-Revision-Date: 2013-02-21 09:48+0100\n" "Last-Translator: Ansgar Becker \n" "Language-Team: English (http://www.transifex.com/projects/p/heidisql/" "language/en/)\n" @@ -486,26 +486,20 @@ msgstr "Create database ..." msgid "&Name:" msgstr "&Name:" -#. CreateDatabaseForm..lblCharset..Caption -#: createdatabase.dfm:37 -msgid "&Character set:" -msgstr "&Character set:" - #. CreateDatabaseForm..lblCollation..Caption #: createdatabase.dfm:45 msgid "C&ollation:" msgstr "C&ollation:" -#. CreateDatabaseForm..lblPreview..Caption -#: createdatabase.dfm:54 -msgid "SQL &preview for CREATE DATABASE:" -msgstr "SQL &preview for CREATE DATABASE:" - #. CreateDatabaseForm..editDBName..TextHint #: createdatabase.dfm:63 msgid "Enter database name" msgstr "Enter database name" +#. Create database dialog +msgid "Servers default: %s" +msgstr "Servers default: %s" + #. DataSortingForm..pnlBevel..btnAddCol..Caption #: data_sorting.dfm:63 msgid "Add Col" diff --git a/source/createdatabase.dfm b/source/createdatabase.dfm index 705b3fec..12cccaaf 100644 --- a/source/createdatabase.dfm +++ b/source/createdatabase.dfm @@ -3,7 +3,7 @@ object CreateDatabaseForm: TCreateDatabaseForm Top = 0 BorderStyle = bsDialog Caption = 'Create database ...' - ClientHeight = 218 + ClientHeight = 227 ClientWidth = 317 Color = clBtnFace Font.Charset = DEFAULT_CHARSET @@ -18,7 +18,7 @@ object CreateDatabaseForm: TCreateDatabaseForm OnShow = FormShow DesignSize = ( 317 - 218) + 227) PixelsPerInch = 96 TextHeight = 13 object lblDBName: TLabel @@ -29,95 +29,84 @@ object CreateDatabaseForm: TCreateDatabaseForm Caption = '&Name:' FocusControl = editDBName end - object lblCharset: TLabel - Left = 8 - Top = 46 - Width = 70 - Height = 13 - Caption = '&Character set:' - FocusControl = comboCharset - end object lblCollation: TLabel Left = 8 - Top = 73 + Top = 45 Width = 45 Height = 13 Caption = 'C&ollation:' FocusControl = comboCollation end - object lblPreview: TLabel + object lblCreateCode: TLabel Left = 8 - Top = 138 - Width = 178 + Top = 133 + Width = 65 Height = 13 - Anchors = [akLeft, akRight, akBottom] - Caption = 'SQL &preview for CREATE DATABASE:' + Anchors = [akLeft, akTop, akRight] + Caption = 'CREATE code' + end + object lblServerDefaultCollation: TLabel + Left = 96 + Top = 69 + Width = 78 + Height = 13 + Caption = 'Servers default:' end object editDBName: TEdit - Left = 128 + Left = 96 Top = 16 - Width = 181 + Width = 213 Height = 21 Anchors = [akLeft, akTop, akRight] TabOrder = 0 TextHint = 'Enter database name' OnChange = Modified end - object comboCharset: TComboBox - Left = 128 - Top = 43 - Width = 181 - Height = 21 - Style = csDropDownList - Anchors = [akLeft, akTop, akRight] - Sorted = True - TabOrder = 1 - OnChange = comboCharsetChange - end object btnOK: TButton Left = 153 - Top = 102 + Top = 93 Width = 75 Height = 25 - Anchors = [akRight, akBottom] + Anchors = [akTop, akRight] Caption = 'OK' Default = True - TabOrder = 3 + TabOrder = 2 OnClick = btnOKClick end object btnCancel: TButton Left = 234 - Top = 102 + Top = 93 Width = 75 Height = 25 - Anchors = [akRight, akBottom] + Anchors = [akTop, akRight] Cancel = True Caption = 'Cancel' ModalResult = 2 - TabOrder = 4 + TabOrder = 3 end object comboCollation: TComboBox - Left = 128 - Top = 70 - Width = 181 + Left = 96 + Top = 42 + Width = 213 Height = 21 Style = csDropDownList - TabOrder = 2 + Sorted = True + TabOrder = 1 OnChange = Modified end - object SynMemoPreview: TSynMemo + object SynMemoCreateCode: TSynMemo Left = 8 - Top = 154 + Top = 152 Width = 301 - Height = 56 + Height = 67 SingleLineMode = False - Anchors = [akLeft, akRight, akBottom] + Anchors = [akLeft, akTop, akRight, akBottom] Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'Courier New' Font.Style = [] - TabOrder = 5 + TabOrder = 4 Gutter.Font.Charset = DEFAULT_CHARSET Gutter.Font.Color = clWindowText Gutter.Font.Height = -11 diff --git a/source/createdatabase.pas b/source/createdatabase.pas index 8d328a74..17967e3c 100644 --- a/source/createdatabase.pas +++ b/source/createdatabase.pas @@ -4,22 +4,20 @@ interface uses Windows, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SynEdit, SynMemo, - dbconnection, gnugettext; + dbconnection, gnugettext, SynRegExpr; type TCreateDatabaseForm = class(TForm) editDBName: TEdit; lblDBName: TLabel; - comboCharset: TComboBox; - lblCharset: TLabel; btnOK: TButton; btnCancel: TButton; lblCollation: TLabel; comboCollation: TComboBox; - lblPreview: TLabel; - SynMemoPreview: TSynMemo; + lblCreateCode: TLabel; + SynMemoCreateCode: TSynMemo; + lblServerDefaultCollation: TLabel; procedure btnOKClick(Sender: TObject); - procedure comboCharsetChange(Sender: TObject); procedure Modified(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); @@ -27,9 +25,6 @@ type function GetCreateStatement: String; private { Private declarations } - CollationTable: TDBQuery; - defaultCharset : String; - currentCollation : String; FConnection: TDBConnection; public { Public declarations } @@ -44,15 +39,13 @@ uses main, helpers; {$R *.dfm} -{** - Fetch list with character sets and collations from the server -} procedure TCreateDatabaseForm.FormCreate(Sender: TObject); begin InheritFont(Font); TranslateComponent(Self); + lblCreateCode.Caption := lblCreateCode.Caption + ':'; // Setup SynMemoPreview - SynMemoPreview.Highlighter := Mainform.SynSQLSyn1; + SynMemoCreateCode.Highlighter := Mainform.SynSQLSyn1; end; @@ -61,119 +54,77 @@ end; } procedure TCreateDatabaseForm.FormShow(Sender: TObject); var - selectCharset, - currentCharset, - sql_create : String; - Charset: String; - colpos: Integer; + ServerCollation, Collation, Charset, CreateCode: String; + CollationTable: TDBQuery; + rx: TRegExpr; begin FConnection := MainForm.ActiveConnection; CollationTable := FConnection.CollationTable; - // Detect servers default charset - defaultCharset := FConnection.GetVar( 'SHOW VARIABLES LIKE '+esc('character_set_server'), 1 ); - comboCharset.Enabled := Assigned(CollationTable); - lblCharset.Enabled := comboCharset.Enabled; - comboCollation.Enabled := comboCharset.Enabled; - lblCollation.Enabled := comboCharset.Enabled; - if comboCharset.Enabled then begin - // Create a list with charsets from collations dataset - comboCharset.Items.BeginUpdate; - while not CollationTable.Eof do begin - Charset := CollationTable.Col('Charset'); - if comboCharset.Items.IndexOf(Charset) = -1 then - comboCharset.Items.Add(Charset); - CollationTable.Next; - end; - comboCharset.Items.EndUpdate; - end; - if modifyDB = '' then - begin + // Detect servers default collation + // TODO: Find out how to retrieve the server's default collation on MSSQL + case FConnection.Parameters.NetTypeGroup of + ngMySQL: + ServerCollation := FConnection.GetVar('SHOW VARIABLES LIKE '+FConnection.EscapeString('collation_server'), 1); + ngMSSQL: + ServerCollation := ''; + end; + lblServerDefaultCollation.Caption := f_('Servers default: %s', [ServerCollation]); + + if modifyDB = '' then begin Caption := _('Create database ...'); editDBName.Text := ''; - editDBName.SetFocus; - selectCharset := defaultCharset; + Charset := ''; + Collation := ServerCollation; end else begin Caption := _('Alter database ...'); editDBName.Text := modifyDB; - editDBName.SetFocus; - editDBName.SelectAll; - - // Detect current charset and collation to be able to preselect them in the pulldowns - sql_create := FConnection.GetVar('SHOW CREATE DATABASE '+FConnection.QuoteIdent(modifyDB), 1); - currentCharset := Copy( sql_create, pos('CHARACTER SET', sql_create)+14, Length(sql_create)); - currentCharset := GetFirstWord( currentCharset ); - if currentCharset <> '' then - selectCharset := currentCharset - else - selectCharset := defaultCharset; - currentCollation := ''; - colpos := pos('COLLATE', sql_create); - if colpos > 0 then begin - currentCollation := Copy( sql_create, colpos+8, Length(sql_create)); - currentCollation := GetFirstWord( currentCollation ); + + // Detect current collation + CreateCode := FConnection.GetVar('SHOW CREATE DATABASE '+FConnection.QuoteIdent(modifyDB), 1); + rx := TRegExpr.Create; + rx.Expression := '\sCHARACTER\s+SET\s+(\w+)\b'; + if rx.Exec(CreateCode) then + Charset := rx.Match[1]; + rx.Expression := '\sCOLLATE\s+(\w+)\b'; + if rx.Exec(CreateCode) then + Collation := rx.Match[1]; + rx.Free; + // Find default collation of given charset + if (Collation = '') and (Charset <> '') and Assigned(CollationTable) then begin + while not CollationTable.Eof do begin + if (CollationTable.Col('Charset') = Charset) and (LowerCase(CollationTable.Col('Default')) = 'yes') then + Collation := CollationTable.Col('Collation'); + CollationTable.Next; + end; end; end; - // Preselect charset item in pulldown - if comboCharset.Items.Count > 0 then - begin - if comboCharset.Items.IndexOf(selectCharset) > -1 then - comboCharset.ItemIndex := comboCharset.Items.IndexOf(selectCharset) - else - comboCharset.ItemIndex := 0; - // Invoke selecting default collation - comboCharsetChange( Sender ); + // Select collation in pulldown + comboCollation.Enabled := Assigned(CollationTable); + lblCollation.Enabled := comboCollation.Enabled; + comboCollation.Clear; + if comboCollation.Enabled then begin + CollationTable.First; + while not CollationTable.Eof do begin + comboCollation.Items.Add(CollationTable.Col('Collation')); + CollationTable.Next; + end; + comboCollation.ItemIndex := comboCollation.Items.IndexOf(Collation); + if comboCollation.ItemIndex = -1 then + comboCollation.ItemIndex := 0; end; + editDBName.SetFocus; + editDBName.SelectAll; + // Invoke SQL preview Modified(Sender); MainForm.SetupSynEditors; end; -{** - Charset has been selected: Display fitting collations - and select default one. -} -procedure TCreateDatabaseForm.comboCharsetChange(Sender: TObject); -var - defaultCollation : String; -begin - // Abort if collations were not fetched successfully - if not Assigned(CollationTable) then - Exit; - - // Fill pulldown with fitting collations - comboCollation.Items.BeginUpdate; - comboCollation.Items.Clear; - CollationTable.First; - while not CollationTable.Eof do begin - if CollationTable.Col('Charset') = comboCharset.Text then - begin - comboCollation.Items.Add(CollationTable.Col('Collation')); - if CollationTable.Col('Default') = 'Yes' then - defaultCollation := CollationTable.Col('Collation'); - end; - CollationTable.Next; - end; - - // Preselect default or current collation - if currentCollation <> '' then - defaultCollation := currentCollation; - if comboCollation.Items.IndexOf(defaultCollation) > -1 then - comboCollation.ItemIndex := comboCollation.Items.IndexOf(defaultCollation) - else - comboCollation.ItemIndex := 0; - - comboCollation.Items.EndUpdate; - - // Invoke SQL preview - Modified(Sender); -end; - - {** Create the database } @@ -196,13 +147,10 @@ begin ErrorDialog(f_('Creating database "%s" failed.', [editDBName.Text]), E.Message); // Keep form open end else try - sql := 'ALTER DATABASE ' + FConnection.QuoteIdent( modifyDB ); - if comboCharset.Enabled and (comboCharset.Text <> '') then - begin - sql := sql + ' CHARACTER SET ' + comboCharset.Text; - if comboCollation.Enabled and (comboCollation.Text <> '') then - sql := sql + ' COLLATE ' + esc(comboCollation.Text); - end; + sql := 'ALTER DATABASE ' + FConnection.QuoteIdent(modifyDB); + if comboCollation.Text <> '' then + sql := sql + ' COLLATE ' + FConnection.EscapeString(comboCollation.Text); + if modifyDB = editDBName.Text then begin // Alter database FConnection.Query(sql); @@ -285,8 +233,8 @@ end; } procedure TCreateDatabaseForm.Modified(Sender: TObject); begin - SynMemoPreview.Clear; - SynMemoPreview.Text := GetCreateStatement; + SynMemoCreateCode.Clear; + SynMemoCreateCode.Text := GetCreateStatement; end; @@ -296,13 +244,8 @@ end; function TCreateDatabaseForm.GetCreateStatement: String; begin Result := 'CREATE DATABASE ' + FConnection.QuoteIdent( editDBName.Text ); - if comboCharset.Enabled and (comboCharset.Text <> '') then - begin - Result := Result + ' /*!40100 CHARACTER SET ' + comboCharset.Text; - if comboCollation.Enabled and (comboCollation.Text <> '') then - Result := Result + ' COLLATE ' + esc(comboCollation.Text); - Result := Result + ' */'; - end; + if comboCollation.Enabled and (comboCollation.Text <> '') then + Result := Result + ' /*!40100 COLLATE ' + FConnection.EscapeString(comboCollation.Text) + ' */'; end;