/// <summary> /// Drop table from Db /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public async void DropTableHandler(object sender, TableModel e) { var dialog = await this.ShowMessageAsync("Drop table", $"Do you really want to drop {e.Title} ? Changes can't be undone!", MessageDialogStyle.AffirmativeAndNegative); if (dialog == MessageDialogResult.Affirmative) { var res = _updater.DropTable(e); if (res != null) { await this.ShowMessageAsync("Drop table", res); Output.WriteLine(OutputPanelListener.PrepareException(res)); return; } DatabaseModelDesigner designer; if (TryGetSelectedDesigner(out designer)) { var facade = new DiagramFacade(designer); facade.RemoveTable(e); } await DatabaseConnectionSidebar.RefreshTreeData(); } }
/// <summary> /// Open saved diagram /// </summary> /// <param name="sender"></param> /// <param name="diagramModel">Diagram for opening</param> private async void DatabaseConnectionSidebarOnAddDiagram(object sender, DiagramModel diagramModel) { Mouse.OverrideCursor = Cursors.Wait; DiagramFacade.CreateNewDiagram(this, diagramModel.Name); LayoutAnchorable panel; if (TryGetSelectedPanel(out panel)) { DatabaseModelDesigner designer; if (TryGetSelectedDesigner(out designer)) { panel.IsActiveChanged -= AnchorableDesignerActiveChangedHandler; var facade = new DiagramFacade(designer.ViewModel); try { await facade.LoadDiagram(designer.ModelDesignerCanvas, XDocument.Parse(diagramModel.Xml)); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); } panel.IsActiveChanged += AnchorableDesignerActiveChangedHandler; } } await DatabaseConnectionSidebar.RefreshTreeData(); Mouse.OverrideCursor = null; }
/// <summary> /// Rename table in DB /// </summary> /// <param name="oldName">Old table name</param> /// <param name="newName">New table name</param> public void RenameTable(string oldName, string newName) { SqlCommand command = Database.CreateCommand(SqlRenameTable); command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("objname", oldName); command.Parameters.AddWithValue("newname", newName); Output.WriteLine(OutputPanelListener.PrepareSql(command, command.Parameters)); command.ExecuteNonQuery(); }
/// <summary> /// Update primary key constraint /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public async void UpdatePrimaryKeyConstraintHandler(object sender, TableModel e) { var res = _updater.UpdatePrimaryKeyConstraint(e); if (res != null) { await this.ShowMessageAsync("Primary key constraint", res); Output.WriteLine(OutputPanelListener.PrepareException(res)); } }
/// <summary> /// Drop column handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public async void RemoveRowHandler(object sender, EditRowEventArgs e) { _flyoutTableModel = e.TableModel; _flyoutRowEventArgs = e; var res = _updater.RemoveColumn(_flyoutTableModel, ref _flyoutRowEventArgs); if (res != null) { await this.ShowMessageAsync("Drop column", res); Output.WriteLine(OutputPanelListener.PrepareException(res)); } }
/// <summary> /// Drop column from DB table /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void RemoveColumn_OnExecuted(object sender, ExecutedRoutedEventArgs e) { var res = _updater.RemoveColumn(_flyoutTableModel, ref _flyoutRowEventArgs); if (res != null) { await this.ShowMessageAsync("Drop column", res); Output.WriteLine(OutputPanelListener.PrepareException(res)); } ToggleRowFlyout(); }
/// <summary> /// Rename column in table /// </summary> /// <param name="table">Table name</param> /// <param name="oldName">Old name</param> /// <param name="newName">New name</param> /// <returns>Exception message if failed, NULL if not</returns> public string RenameColumn(string table, string oldName, string newName) { try { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); ctx.RenameColumn(table, oldName, newName); return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } }
/// <summary> /// Drop table from DB /// </summary> /// <param name="table">Table model</param> /// <returns>Exception message if failed, NULL if not</returns> public string DropTable(TableModel table) { try { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); ctx.RemoveTable(table); return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException || exception is ApplicationException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } }
/// <summary> /// Execute selected SQL /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RunSelectedQuery_OnExecuted(object sender, ExecutedRoutedEventArgs e) { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); try { var data = ctx.ExecuteRawQuery(QueryEditor.SelectedText); OnQueryResultReady(data); } catch (Exception exception) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); } }
/// <summary> /// Add columnd to table in DB /// </summary> /// <param name="table">Table name</param> /// <param name="column">Column model</param> /// <returns>Exception message if failed, NULL if not</returns> public string AddColumn(string table, TableRowModel column) { try { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); ctx.AddColumn(table, column); return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } }
/// <summary> /// Create foreign key constraint /// </summary> /// <param name="model">Relationship model</param> /// <returns>Exception message if failed, NULL if not</returns> public string AddRelationship(RelationshipModel model) { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); try { ctx.AddRelationship(model); return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } }
/// <summary> /// Save diagram to DB /// </summary> /// <returns>Returns 1 if saved, 0 if not</returns> public int SaveDiagram() { XDocument doc = XDocument.Parse(ViewModel.CreateElement().ToString()); var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); int saveDiagram = 0; try { saveDiagram = ctx.SaveDiagram(ViewModel.DiagramTitle, doc); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); } return(saveDiagram); }
/// <summary> /// Rename table in DB /// </summary> /// <param name="oldName">Old table name</param> /// <param name="model">Table model</param> /// <returns>Exception message if failed, NULL if not</returns> public string RenameTable(string oldName, TableModel model) { try { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); ctx.RenameTable(oldName, model.Title); TableModel fresh = RefreshModel(model); model.Title = fresh.Title; return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { model.Title = oldName; Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } }
/// <summary> /// Alter column in table /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ApplyAttributeEdit_OnExecuted(object sender, ExecutedRoutedEventArgs e) { TableModel refreshed; var res = _updater.AddOrUpdateCollumn(_flyoutTableModel, MainWindowViewModel.FlyoutRowModel, out refreshed, ref _flyoutRowEventArgs); if (res != null) { await this.ShowMessageAsync("Column error", res); Output.WriteLine(OutputPanelListener.PrepareException(res)); } else { _flyoutTableModel.RefreshModel(refreshed); } ToggleRowFlyout(); }
/// <summary> /// Update PK constraint /// </summary> /// <param name="model">Table model</param> /// <returns>Exception message if failed, NULL if not</returns> public string UpdatePrimaryKeyConstraint(TableModel model) { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); try { ctx.UpdatePrimaryKeyConstraint(model); return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } finally { TableModel fresh = ctx.ReadTableDetails(model.Id, model.Title); model.RefreshModel(fresh); } }
/// <summary> /// Drop column in table /// </summary> /// <param name="table">Table model</param> /// <param name="args">Arguments provided by drop event</param> /// <returns>Exception message if failed, NULL if not</returns> public string RemoveColumn(TableModel table, ref EditRowEventArgs args) { var ctx = new DatabaseContext(SessionProvider.Instance.ConnectionType); try { ctx.RemoveColumn(table.Title, args.RowModel.Name); return(null); } catch (Exception exception) when(exception is SqlException || exception is OracleException || exception is ApplicationException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); return(exception.Message); } finally { TableModel model = ctx.ReadTableDetails(table.Id, table.Title); table.RefreshModel(model); args = null; } }
/// <summary> /// On drop table item /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ModelDesignerCanvas_OnPreviewDrop(object sender, DragEventArgs e) { var data = e.Data.GetData("ConnectionPanelTableDragFormat") as TableModel; if (data != null) { var facade = new DiagramFacade(ViewModel); Point position = e.GetPosition(ModelDesignerCanvas); facade.AddTableCallbackAction += async model => { try { await facade.AddRelationShipsForTable(model, ModelDesignerCanvas); } catch (Exception exception) when(exception is SqlException || exception is OracleException) { Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); } }; facade.AddTable(data, (int)position.X, (int)position.Y); } }
/// <summary> /// Rename selected table and refresh treeview /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public async void RenameTableHandler(object sender, TableModel e) { var originalName = e.Title; var dialog = new TableNameDialog() { Model = e, Owner = this }; dialog.ShowDialog(); string res = _updater.RenameTable(originalName, e); if (res != null) { await this.ShowMessageAsync("Rename table", res); Output.WriteLine(OutputPanelListener.PrepareException(res)); return; } await DatabaseConnectionSidebar.RefreshTreeData(); }
private async void ConnectToOracleOwnConnString_OnExecuted(object sender, ExecutedRoutedEventArgs e) { var example = @"USER ID=c##username;PASSWORD=y0urP455woRd;DATA SOURCE=""(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.14.10)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))"""; var res = await this.ShowInputAsync("Connection string", $"Use customized connection string \n (Example: {example})"); if (res != null) { await DiagramFacade.CloseDiagramsOnDisconnect(this); SessionProvider.Instance.Disconnect(); DatabaseConnectionSidebar.HideDatabaseStackPanels(); ProgressDialogController progressDialogController = null; Func <ProgressDialogController, Task> closeProgress = async t => { if (t != null) { if (t.IsOpen) { await t.CloseAsync(); } } }; try { progressDialogController = await this.ShowProgressAsync("Please wait", "Connecting to server...", false, new MetroDialogSettings() { AnimateShow = false, AnimateHide = false }); progressDialogController.SetIndeterminate(); var db = new OracleDatabase(); OracleConnectionStringBuilder builder = new OracleConnectionStringBuilder(res); builder.ConnectionTimeout = 2; await db.TryToConnectToServer(builder.ConnectionString); SessionProvider.Instance.OwnConnectionString = res; SessionProvider.Instance.UseOwnConnectionString = true; SessionProvider.Instance.Username = builder.UserID; SessionProvider.Instance.ConnectionType = ConnectionType.Oracle; //Init work await Task.Factory.StartNew(() => { using (IOracleMapper mapper = new OracleMapper()) { TableModel model = mapper.ListTables().FirstOrDefault(); if (model != null) { mapper.ListForeignKeys(model.Title); } } }); await closeProgress(progressDialogController); await this.ShowMessageAsync("Connected", $"Successfuly connected to server"); var flyout = Flyouts.Items[2] as Flyout; if (flyout != null) { flyout.IsOpen = !flyout.IsOpen; } await DatabaseConnectionSidebar.LoadOracleData(); } catch (OracleException exception) { await closeProgress(progressDialogController); await this.ShowMessageAsync("Connection error", exception.Message); Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); SessionProvider.Instance.ConnectionType = ConnectionType.None; } catch (ArgumentException) { await closeProgress(progressDialogController); await this.ShowMessageAsync("Connection error", "Connection string is not valid"); SessionProvider.Instance.ConnectionType = ConnectionType.None; } } }
/// <summary> /// Connect to Oracle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ConnectToOracle_OnExecuted(object sender, ExecutedRoutedEventArgs e) { await DiagramFacade.CloseDiagramsOnDisconnect(this); SessionProvider.Instance.Disconnect(); DatabaseConnectionSidebar.HideDatabaseStackPanels(); ProgressDialogController progressDialogController = null; Func <ProgressDialogController, Task> closeProgress = async t => { if (t != null) { if (t.IsOpen) { await t.CloseAsync(); } } }; try { progressDialogController = await this.ShowProgressAsync("Please wait", "Connecting to server...", false, new MetroDialogSettings() { AnimateShow = false, AnimateHide = false }); progressDialogController.SetIndeterminate(); var db = new OracleDatabase(); await db.BuildSession(OracleServerNameTextBox.Text, OraclePortTextBox.Text, OracleSidTextBox.Text, OracleUsernameTextBox.Text, OraclePasswordBox.Password); //Init work await Task.Factory.StartNew(() => { using (IOracleMapper mapper = new OracleMapper()) { TableModel model = mapper.ListTables().FirstOrDefault(); if (model != null) { mapper.ListForeignKeys(model.Title); } } }); await closeProgress(progressDialogController); await this.ShowMessageAsync("Connected", $"Successfuly connected to {SessionProvider.Instance.ServerName}"); var flyout = Flyouts.Items[2] as Flyout; if (flyout != null) { flyout.IsOpen = !flyout.IsOpen; } await DatabaseConnectionSidebar.LoadOracleData(); } catch (OracleException exception) { await closeProgress(progressDialogController); await this.ShowMessageAsync("Connection error", exception.Message); Output.WriteLine(OutputPanelListener.PrepareException(exception.Message)); SessionProvider.Instance.ConnectionType = ConnectionType.None; } }