/// <summary> /// Creates a DBMLTable for a document, with the details held in xml element /// </summary> /// <param name="doc">The DBMLDocument this table belongs to</param> /// <param name="e">The XmlElement containing the details of the table</param> public DBMLTable(DBMLDoc doc, XmlElement e) { _elem = e; _tableName = _elem.GetAttribute("Name"); if(string.IsNullOrEmpty(_tableName)) throw new ArgumentException("Cannot get table name"); XmlNodeList nl = _elem.GetElementsByTagName("Type"); if(nl.Count != 1 || nl[0].NodeType != XmlNodeType.Element) throw new ArgumentException("Cannot Identity type for table " + _tableName); _typeElem = nl[0] as XmlElement; _cols = new DBMLColumnList(); try { foreach(XmlNode n in _typeElem.ChildNodes) { XmlElement ce = n as XmlElement; if(ce != null && ce.Name == "Column") _cols.Add(new DBMLColumn(ce)); } } catch(Exception ex) { throw new ArgumentException(string.Format("Problem parsing table {0} {1}", _tableName, ex.Message)); } }
/// <summary> /// Creates an Xml element with the details of the table /// </summary> /// <param name="doc">The parent doc</param> /// <returns>XmlElement for the new document</returns> public XmlElement CreateDBML(DBMLDoc doc) { XmlElement table = doc.AddElement("Table"); doc.AddAttributes(table, _elem.Attributes, string.Empty); XmlElement type = doc.AddElement("Type"); doc.AddAttributes(type, _typeElem.Attributes, string.Empty); _cols.Sort(); foreach (DBMLColumn c in _cols) { if (c.Found) { type.AppendChild(c.CreateDBML(doc)); } } doc.AddNodes(type, _typeElem.ChildNodes, "Column"); table.AppendChild(type); doc.AddNodes(table, _elem.ChildNodes, "Type"); return(table); }
/// <summary> /// Creates a DBMLTable for a document, with the details held in xml element /// </summary> /// <param name="doc">The DBMLDocument this table belongs to</param> /// <param name="e">The XmlElement containing the details of the table</param> public DBMLTable(DBMLDoc doc, XmlElement e) { _elem = e; _tableName = _elem.GetAttribute("Name"); if (string.IsNullOrEmpty(_tableName)) { throw new ArgumentException("Cannot get table name"); } XmlNodeList nl = _elem.GetElementsByTagName("Type"); if (nl.Count != 1 || nl[0].NodeType != XmlNodeType.Element) { throw new ArgumentException("Cannot Identity type for table " + _tableName); } _typeElem = nl[0] as XmlElement; _cols = new DBMLColumnList(); try { foreach (XmlNode n in _typeElem.ChildNodes) { XmlElement ce = n as XmlElement; if (ce != null && ce.Name == "Column") { _cols.Add(new DBMLColumn(ce)); } } } catch (Exception ex) { throw new ArgumentException(string.Format("Problem parsing table {0} {1}", _tableName, ex.Message)); } }
/// <summary> /// Creates an Xml Element with the details of the column for a new DBML file /// </summary> /// <param name="doc">The parent document</param> /// <returns>An Xml Element cotaining the details of the column</returns> public XmlElement CreateDBML(DBMLDoc doc) { if (!_found) { return(null); } XmlElement col = doc.AddElement("Column"); doc.AddAttribute(col, "Name", _name); doc.AddAttribute(col, "Type", clrType); doc.AddAttribute(col, "DbType", dbType); if (_isPrimaryKey) { doc.AddAttribute(col, "IsPrimaryKey", "true"); } if (_isIdentity || _dbGenerated) { doc.AddAttribute(col, "IsDbGenerated", "true"); } doc.AddAttribute(col, "CanBeNull", _canBeNull ? "true" : "false"); if (_elem != null) { doc.AddAttributes(col, _elem.Attributes, COL_KNOWNATTR); } return(col); }
/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary> /// <param term='commandName'>The name of the command to execute.</param> /// <param term='executeOption'>Describes how the command should be run.</param> /// <param term='varIn'>Parameters passed from the caller to the command handler.</param> /// <param term='varOut'>Parameters passed from the command handler to the caller.</param> /// <param term='handled'>Informs the caller if the command was handled or not.</param> /// <seealso class='Exec' /> public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if(commandName == "DBMLUpdate.Connect.DBMLUpdate") { // Some initial setup create an output pain and grab status bar if(_output == null) _output = _applicationObject.ToolWindows.OutputWindow.OutputWindowPanes.Add("DBML Update"); _output.Clear(); EnvDTE.StatusBar status = _applicationObject.StatusBar; _output.Activate(); try { //Check where dealing with a dbml file if(_applicationObject.ActiveDocument != null && _applicationObject.ActiveDocument.FullName.EndsWith(".dbml", StringComparison.InvariantCultureIgnoreCase)) { _output.OutputString("Beginning Processing...\r\n"); // Grab dbml and save it if its unsaved Document actDoc = _applicationObject.ActiveDocument; ProjectItem pi = actDoc.ProjectItem; string projPath = pi.ContainingProject.FullName; string docFileName = actDoc.FullName; if(!actDoc.Saved) { status.Text = "Saving Linq-to-SQL desinger diagram..."; actDoc.Save(docFileName); } UpdateStatus(_output, status, "Parsing DBML file..."); // Real work begins in DBMLDoc DBMLDoc doc = new DBMLDoc(docFileName, pi); _output.OutputString(string.Format("Parsed {0} Tables\r\n", doc.TableCount)); UpdateStatus(_output, status, "Updating from Database..."); doc.DBUpdate(_output); status.Text = "Closing the DBML designer..."; actDoc.Close(actDoc.Saved ? vsSaveChanges.vsSaveChangesNo : vsSaveChanges.vsSaveChangesYes); UpdateStatus(_output, status, "Creating new DBML file..."); doc.CreateDBML(docFileName, _output); status.Text = "Opening designer..."; Window win = OpenDesigner(pi, docFileName); UpdateStatus(_output, status, "Re-generating classes..."); try { ((VSProjectItem)win.ProjectItem.Object).RunCustomTool(); } catch(Exception ex) { try { win.Document.Save(docFileName); } catch { UpdateStatus(_output, status, "Code re-generation failed: " + ex.Message); } } _output.OutputString("Finished Processing\r\n"); } else _output.OutputString("The active document is not a Linq-to-SQL file.\r\n"); } catch(Exception ex) { _output.OutputString("Refreshing entities failed due to exception:" + ex.ToString() + " Message:" + ex.Message + "\r\n"); } finally { status.Text = string.Empty; status.Progress(false, "", 0, 0); if(_output != null) _output.Activate(); } handled = true; } } }
/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary> /// <param term='commandName'>The name of the command to execute.</param> /// <param term='executeOption'>Describes how the command should be run.</param> /// <param term='varIn'>Parameters passed from the caller to the command handler.</param> /// <param term='varOut'>Parameters passed from the command handler to the caller.</param> /// <param term='handled'>Informs the caller if the command was handled or not.</param> /// <seealso class='Exec' /> public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if (commandName == "DBMLUpdate.Connect.DBMLUpdate") { // Some initial setup create an output pain and grab status bar if (_output == null) { _output = _applicationObject.ToolWindows.OutputWindow.OutputWindowPanes.Add("DBML Update"); } _output.Clear(); EnvDTE.StatusBar status = _applicationObject.StatusBar; _output.Activate(); try { //Check where dealing with a dbml file if (_applicationObject.ActiveDocument != null && _applicationObject.ActiveDocument.FullName.EndsWith(".dbml", StringComparison.InvariantCultureIgnoreCase)) { _output.OutputString("Beginning Processing...\r\n"); // Grab dbml and save it if its unsaved Document actDoc = _applicationObject.ActiveDocument; ProjectItem pi = actDoc.ProjectItem; string projPath = pi.ContainingProject.FullName; string docFileName = actDoc.FullName; if (!actDoc.Saved) { status.Text = "Saving Linq-to-SQL desinger diagram..."; actDoc.Save(docFileName); } UpdateStatus(_output, status, "Parsing DBML file..."); // Real work begins in DBMLDoc DBMLDoc doc = new DBMLDoc(docFileName, pi); _output.OutputString(string.Format("Parsed {0} Tables\r\n", doc.TableCount)); UpdateStatus(_output, status, "Updating from Database..."); doc.DBUpdate(_output); status.Text = "Closing the DBML designer..."; actDoc.Close(actDoc.Saved ? vsSaveChanges.vsSaveChangesNo : vsSaveChanges.vsSaveChangesYes); UpdateStatus(_output, status, "Creating new DBML file..."); doc.CreateDBML(docFileName, _output); status.Text = "Opening designer..."; Window win = OpenDesigner(pi, docFileName); UpdateStatus(_output, status, "Re-generating classes..."); try { ((VSProjectItem)win.ProjectItem.Object).RunCustomTool(); } catch (Exception ex) { try { win.Document.Save(docFileName); } catch { UpdateStatus(_output, status, "Code re-generation failed: " + ex.Message); } } _output.OutputString("Finished Processing\r\n"); } else { _output.OutputString("The active document is not a Linq-to-SQL file.\r\n"); } } catch (Exception ex) { _output.OutputString("Refreshing entities failed due to exception:" + ex.ToString() + " Message:" + ex.Message + "\r\n"); } finally { status.Text = string.Empty; status.Progress(false, "", 0, 0); if (_output != null) { _output.Activate(); } } handled = true; } } }
/// <summary> /// Creates an Xml element with the details of the table /// </summary> /// <param name="doc">The parent doc</param> /// <returns>XmlElement for the new document</returns> public XmlElement CreateDBML(DBMLDoc doc) { XmlElement table = doc.AddElement("Table"); doc.AddAttributes(table, _elem.Attributes, string.Empty); XmlElement type = doc.AddElement("Type"); doc.AddAttributes(type, _typeElem.Attributes, string.Empty); _cols.Sort(); foreach(DBMLColumn c in _cols) { if(c.Found) type.AppendChild(c.CreateDBML(doc)); } doc.AddNodes(type, _typeElem.ChildNodes, "Column"); table.AppendChild(type); doc.AddNodes(table, _elem.ChildNodes, "Type"); return table; }