diff --git a/source/helpers.pas b/source/helpers.pas index c081c778..c00299c5 100644 --- a/source/helpers.pas +++ b/source/helpers.pas @@ -228,6 +228,7 @@ type function GetTableSize(Results: TMySQLQuery): Int64; function GetLightness(AColor: TColor): Byte; procedure ParseTableStructure(CreateTable: WideString; Columns: TObjectList=nil; Keys: TObjectList=nil; ForeignKeys: TObjectList=nil); + procedure ParseViewStructure(ViewName: WideString; Columns: TObjectList); var MainReg : TRegistry; @@ -3126,6 +3127,33 @@ begin end; +procedure ParseViewStructure(ViewName: WideString; Columns: TObjectList); +var + rx: TRegExpr; + Col: TTableColumn; + Results: TMySQLQuery; +begin + // Views reveal their columns only with a SHOW COLUMNS query. + // No keys available in views - SHOW KEYS always returns an empty result + Columns.Clear; + rx := TRegExpr.Create; + rx.Expression := '^(\w+)(\((.+)\))?'; + Results := Mainform.Connection.GetResults('SHOW COLUMNS FROM '+Mainform.mask(ViewName)); + while not Results.Eof do begin + Col := TTableColumn.Create; + Columns.Add(Col); + Col.Name := Results.Col('Field'); + Col.AllowNull := Results.Col('Null') = 'YES'; + if rx.Exec(Results.Col('Type')) then begin + Col.DataType := GetDatatypeByName(rx.Match[1]); + Col.LengthSet := rx.Match[2]; + end; + Results.Next; + end; + rx.Free; +end; + + { *** TTableColumn } diff --git a/source/main.pas b/source/main.pas index d49925a4..4802537a 100644 --- a/source/main.pas +++ b/source/main.pas @@ -6145,9 +6145,16 @@ begin newDbObject := SelectedTable.Text; tabEditor.TabVisible := True; tabData.TabVisible := SelectedTable.NodeType in [lntTable, lntCrashedTable, lntView]; - if SelectedTable.NodeType in [lntTable, lntCrashedTable, lntView] then - SelectedTableCreateStatement := Connection.GetVar('SHOW CREATE TABLE '+Mainform.mask(SelectedTable.Text), 1); - ParseTableStructure(SelectedTableCreateStatement, SelectedTableColumns, SelectedTableKeys, SelectedTableForeignKeys); + SelectedTableColumns.Clear; + SelectedTableKeys.Clear; + SelectedTableForeignKeys.Clear; + case SelectedTable.NodeType of + lntTable, lntCrashedTable: begin + SelectedTableCreateStatement := Connection.GetVar('SHOW CREATE TABLE '+Mainform.mask(SelectedTable.Text), 1); + ParseTableStructure(SelectedTableCreateStatement, SelectedTableColumns, SelectedTableKeys, SelectedTableForeignKeys); + end; + lntView: ParseViewStructure(SelectedTable.Text, SelectedTableColumns); + end; if tabEditor.TabVisible then begin actEditObjectExecute(Sender); // When a table is clicked in the tree, and the current