Example #1
0
        /// <summary>
        /// Parse only method. Parses and adds all entities found in the given node and adds them to the given
        /// list.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="constraintElements"></param>
        public static void ParseFromXml(XmlNode node, IList constraintElements)
        {
            if (node != null && constraintElements != null)
            {
                foreach (XmlNode constraintNode in node.ChildNodes)
                {
                    if (constraintNode.NodeType.Equals(XmlNodeType.Element))
                    {
                        ConstraintElement constraintElement = new ConstraintElement();

                        constraintElement.Name               = GetAttributeValue(constraintNode, NAME, constraintElement.Name);
                        constraintElement.Type               = GetAttributeValue(constraintNode, TYPE, constraintElement.Type);
                        constraintElement.Clustered          = Boolean.Parse(GetAttributeValue(constraintNode, CLUSTERED, constraintElement.Clustered.ToString()));
                        constraintElement.ForeignEntity.Name = GetAttributeValue(constraintNode, FOREIGN_ENTITY, constraintElement.ForeignEntity.Name);
                        constraintElement.CheckClause        = GetAttributeValue(constraintNode, CHECK_CLAUSE, constraintElement.CheckClause);
                        constraintElement.Prefix             = GetAttributeValue(constraintNode, PREFIX, constraintElement.Prefix);
                        constraintElement.CheckEnum.Name     = GetAttributeValue(constraintNode, CHECK_ENUM, constraintElement.CheckEnum.Name);

                        ColumnElement.ParseFromXml(constraintNode, constraintElement.Columns);

                        constraintElements.Add(constraintElement);
                    }
                }
            }
        }
        public SqlEntityElement(XmlNode sqlEntityNode, SqlEntityData defaults) : base(sqlEntityNode, defaults)
        {
            if (sqlEntityNode != null && SQLENTITY.Equals(sqlEntityNode.Name))
            {
                view = GetAttributeValue(sqlEntityNode, VIEW, view);

                foreach (XmlNode node in GetChildNodes(sqlEntityNode, COLUMNS, ColumnElement.COLUMN))
                {
                    columns.Add(new ColumnElement(node, this));
                }
                foreach (XmlNode node in GetChildNodes(sqlEntityNode, CONSTRAINTS, ConstraintElement.CONSTRAINT))
                {
                    constraints.Add(ConstraintElement.NewInstance(node, this));
                }
                foreach (XmlNode node in GetChildNodes(sqlEntityNode, INDEXES, IndexElement.INDEX))
                {
                    indexes.Add(new IndexElement(node, this));
                }
                foreach (XmlNode node in GetChildNodes(sqlEntityNode, VIEWS, ViewElement.VIEW))
                {
                    views.Add(new ViewElement(node));
                }
            }
            else
            {
                throw new ArgumentException("The XmlNode argument is not a sql entity node.");
            }
        }
        public static ArrayList ParseFromXml(DatabaseElement database, XmlNode databaseNode, Hashtable sqltypes, Hashtable types, ParserValidationDelegate vd)
        {
            ArrayList sqlentities = new ArrayList();

            //databaseNode.ChildNodes corresponds to each <sqlentity> entry in the databases xml file(s) (such as dtg-databases.xml)
            foreach (XmlNode node in databaseNode.ChildNodes)
            {
                if (node.Name.Equals("sqlentity"))
                {
                    SqlEntityElement sqlentity = new SqlEntityElement(database);
                    ParseNodeAttributes(node, sqlentity);
                    sqlentity.View = "vw" + sqlentity.Name;
                    if (node.Attributes["view"] != null)
                    {
                        sqlentity.View = node.Attributes["view"].Value;
                    }
                    if (node.Attributes["audit"] != null)
                    {
                        sqlentity.Audit = Boolean.Parse(node.Attributes["audit"].Value);
                    }

                    //Adds all attributes including all not defined by element class
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        if (!sqlentity.Attributes.ContainsKey(attribute.Name))
                        {
                            sqlentity.Attributes.Add(attribute.Name, attribute.Value);
                        }
                    }

                    sqlentity.Columns     = ColumnElement.ParseFromXml(node, sqlentity, sqltypes, types, vd);
                    sqlentity.Constraints = ConstraintElement.ParseFromXml(node, sqlentity, sqltypes, types, vd);
                    sqlentity.Indexes     = IndexElement.ParseFromXml(node, sqlentity, sqltypes, types, vd);

                    // TODO: this is a hack as many things need to be restructured.  the Elements all need to be parsed first, then
                    // relationships and links need to be created.  Otherwise, the config file becomes order dependent.
                    DatabaseElement d = new DatabaseElement();
                    d.SqlEntities   = sqlentities;
                    sqlentity.Views = ViewElement.ParseFromXml(node, d, sqlentity, sqltypes, types, vd);


                    sqlentities.Add(sqlentity);
                }
            }

            StringCollection names = new StringCollection();

            foreach (SqlEntityElement sqlentity in sqlentities)
            {
                if (names.Contains(sqlentity.Name))
                {
                    vd(new ParserValidationArgs(ParserValidationSeverity.ERROR, "duplicate sqlentity definition for " + sqlentity.Name));
                }
                else
                {
                    names.Add(sqlentity.Name);
                }
            }
            return(sqlentities);
        }
        /// <summary>
        /// Parse only method. Parses and adds all entities found in the given node and adds them to the given
        /// list.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="sqlEntityElements"></param>
        public static void ParseFromXml(XmlNode node, IList sqlEntityElements)
        {
            if (node != null && sqlEntityElements != null)
            {
                foreach (XmlNode sqlEntityNode in node.ChildNodes)
                {
                    if (sqlEntityNode.NodeType.Equals(XmlNodeType.Element))
                    {
                        SqlEntityElement sqlEntityElement = new SqlEntityElement();

                        sqlEntityElement.Name                           = GetAttributeValue(sqlEntityNode, NAME, sqlEntityElement.Name);
                        sqlEntityElement.View                           = GetAttributeValue(sqlEntityNode, VIEW, sqlEntityElement.View);
                        sqlEntityElement.SingleFile                     = Boolean.Parse(GetAttributeValue(sqlEntityNode, SCRIPT_SINGLE_FILE, sqlEntityElement.SingleFile.ToString()));
                        sqlEntityElement.Server                         = GetAttributeValue(sqlEntityNode, SERVER, sqlEntityElement.Server);
                        sqlEntityElement.Database                       = GetAttributeValue(sqlEntityNode, DATABASE, sqlEntityElement.Database);
                        sqlEntityElement.User                           = GetAttributeValue(sqlEntityNode, USER, sqlEntityElement.User);
                        sqlEntityElement.Password                       = GetAttributeValue(sqlEntityNode, PASSWORD, sqlEntityElement.Password);
                        sqlEntityElement.SqlScriptDirectory             = GetAttributeValue(sqlEntityNode, SCRIPT_DIRECTORY, sqlEntityElement.SqlScriptDirectory);
                        sqlEntityElement.StoredProcNameFormat           = GetAttributeValue(sqlEntityNode, STORED_PROC_NAME_FORMAT, sqlEntityElement.StoredProcNameFormat);
                        sqlEntityElement.GenerateSqlViewScripts         = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_VIEW_SCRIPT, sqlEntityElement.GenerateSqlViewScripts.ToString()));
                        sqlEntityElement.GenerateSqlTableScripts        = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_TABLE_SCRIPT, sqlEntityElement.GenerateSqlTableScripts.ToString()));
                        sqlEntityElement.GenerateInsertStoredProcScript = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_INSERT_STORED_PROC_SCRIPT, sqlEntityElement.GenerateInsertStoredProcScript.ToString()));
                        sqlEntityElement.GenerateUpdateStoredProcScript = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_UPDATE_STORED_PROC_SCRIPT, sqlEntityElement.GenerateUpdateStoredProcScript.ToString()));
                        sqlEntityElement.GenerateDeleteStoredProcScript = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_DELETE_STORED_PROC_SCRIPT, sqlEntityElement.GenerateDeleteStoredProcScript.ToString()));
                        sqlEntityElement.GenerateSelectStoredProcScript = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_SELECT_STORED_PROC_SCRIPT, sqlEntityElement.GenerateSelectStoredProcScript.ToString()));
                        sqlEntityElement.AllowInsert                    = Boolean.Parse(GetAttributeValue(sqlEntityNode, ALLOW_INSERT, sqlEntityElement.AllowInsert.ToString())) ||
                                                                          sqlEntityElement.GenerateInsertStoredProcScript;
                        sqlEntityElement.AllowUpdate = Boolean.Parse(GetAttributeValue(sqlEntityNode, ALLOW_UPDATE, sqlEntityElement.AllowUpdate.ToString())) ||
                                                       sqlEntityElement.GenerateUpdateStoredProcScript;
                        sqlEntityElement.AllowDelete = Boolean.Parse(GetAttributeValue(sqlEntityNode, ALLOW_DELETE, sqlEntityElement.AllowDelete.ToString())) ||
                                                       sqlEntityElement.GenerateDeleteStoredProcScript;
                        sqlEntityElement.DefaultDirtyRead  = Boolean.Parse(GetAttributeValue(sqlEntityNode, DEFAULT_DIRTY_READ, sqlEntityElement.DefaultDirtyRead.ToString()));
                        sqlEntityElement.UpdateChangedOnly = Boolean.Parse(GetAttributeValue(sqlEntityNode, UPDATE_CHANGED_ONLY, sqlEntityElement.UpdateChangedOnly.ToString())) &&
                                                             sqlEntityElement.AllowUpdate;
                        sqlEntityElement.ScriptDropStatement                 = Boolean.Parse(GetAttributeValue(sqlEntityNode, SCRIPT_DROP_STATEMENT, sqlEntityElement.ScriptDropStatement.ToString()));
                        sqlEntityElement.UseView                             = Boolean.Parse(GetAttributeValue(sqlEntityNode, USE_VIEW, sqlEntityElement.UseView.ToString()));
                        sqlEntityElement.GenerateProcsForForeignKey          = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_PROCS_FOR_FOREIGN_KEYS, sqlEntityElement.GenerateProcsForForeignKey.ToString()));
                        sqlEntityElement.GenerateOnlyPrimaryDeleteStoredProc = Boolean.Parse(GetAttributeValue(sqlEntityNode, GENERATE_ONLY_PRIMARY_DELETE_STORED_PROC, sqlEntityElement.GenerateOnlyPrimaryDeleteStoredProc.ToString()));
                        sqlEntityElement.AllowUpdateOfPrimaryKey             = Boolean.Parse(GetAttributeValue(sqlEntityNode, ALLOW_UPDATE_OF_PRIMARY_KEY, sqlEntityElement.AllowUpdateOfPrimaryKey.ToString()));
                        sqlEntityElement.CommandTimeout                      = Int32.Parse(GetAttributeValue(sqlEntityNode, COMMAND_TIMEOUT, sqlEntityElement.CommandTimeout.ToString()));
                        sqlEntityElement.ScriptForIndexedViews               = Boolean.Parse(GetAttributeValue(sqlEntityNode, SCRIPT_FOR_INDEXED_VIEWS, sqlEntityElement.ScriptForIndexedViews.ToString()));

                        ColumnElement.ParseFromXml(GetChildNodeByName(sqlEntityNode, COLUMNS), sqlEntityElement.Columns);
                        ConstraintElement.ParseFromXml(GetChildNodeByName(sqlEntityNode, CONSTRAINTS), sqlEntityElement.Constraints);
                        IndexElement.ParseFromXml(GetChildNodeByName(sqlEntityNode, INDEXES), sqlEntityElement.Indexes);
                        ViewElement.ParseFromXml(GetChildNodeByName(sqlEntityNode, VIEWS), sqlEntityElement.Views);

                        sqlEntityElements.Add(sqlEntityElement);
                    }
                }
            }
        }
        /// <summary>
        /// Parse only method. Parses and adds all entities found in the given node and adds them to the given
        /// list.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="viewElements"></param>
        public static void ParseFromXml(XmlNode node, IList viewElements)
        {
            if (node != null && viewElements != null)
            {
                foreach (XmlNode viewNode in node.ChildNodes)
                {
                    if (viewNode.NodeType.Equals(XmlNodeType.Element))
                    {
                        ViewElement viewElement = new ViewElement();

                        viewElement.Name = GetAttributeValue(viewNode, NAME, viewElement.Name);
                        ConstraintElement.ParseFromXml(viewNode, viewElement.Constraints);

                        viewElements.Add(viewElement);
                    }
                }
            }
        }
Example #6
0
        public static ArrayList ParseFromXml(XmlNode root, SqlEntityElement sqlentity, Hashtable sqltypes, Hashtable types, ParserValidationDelegate vd)
        {
            ArrayList   constraints = new ArrayList();
            XmlNodeList elements    = null;

            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.Name.Equals("constraints"))
                {
                    elements = n.ChildNodes;
                    break;
                }
            }
            if (elements != null)
            {
                foreach (XmlNode node in elements)
                {
                    if (node.NodeType == XmlNodeType.Comment)
                    {
                        continue;
                    }
                    ConstraintElement constraint = new ConstraintElement();
                    constraint.Name = node.Attributes["name"].Value;
                    constraint.Type = node.Attributes["type"].Value;

                    if (node.Attributes["clustered"] != null)
                    {
                        constraint.Clustered = Boolean.Parse(node.Attributes["clustered"].Value);
                    }
                    if (node.Attributes["foreignentity"] != null)
                    {
                        constraint.ForeignEntity.Name = node.Attributes["foreignentity"].Value;
                    }
                    if (node.Attributes["checkclause"] != null)
                    {
                        constraint.CheckClause = node.Attributes["checkclause"].Value;
                    }
                    if (node.Attributes["checkenum"] != null)
                    {
                        constraint.CheckEnum.Name = node.Attributes["checkenum"].Value;
                    }
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        if (n.NodeType == XmlNodeType.Comment)
                        {
                            continue;
                        }
                        ColumnElement column = sqlentity.FindColumnByName(n.Attributes["name"].Value);
                        if (column == null)
                        {
                            vd(ParserValidationArgs.NewError("column specified (" + n.Attributes["name"].Value + ") in constraint (" + constraint.Name + ") not found as column."));
                            column      = new ColumnElement();
                            column.Name = n.Attributes["name"].Value;
                        }
                        if (n.Attributes["foreigncolumn"] != null)
                        {
                            column.ForeignColumn = n.Attributes["foreigncolumn"].Value;
                        }
                        constraint.Columns.Add(column);
                    }
                    constraints.Add(constraint);
                }
            }
            return(constraints);
        }
        public static ArrayList ParseFromXml(XmlNode root, DatabaseElement database, SqlEntityElement sqlentity, Hashtable sqltypes, Hashtable types, ParserValidationDelegate vd)
        {
            ArrayList list = new ArrayList();

            XmlNodeList elements = root.SelectNodes("views/view");

            if (elements != null)
            {
                foreach (XmlNode node in elements)
                {
                    if (node.NodeType == XmlNodeType.Comment)
                    {
                        continue;
                    }
                    ViewElement view = new ViewElement();
                    view.Name = ParseStringAttribute(node, "name", String.Empty);

                    foreach (XmlNode n in node.ChildNodes)
                    {
                        if (node.NodeType == XmlNodeType.Comment)
                        {
                            continue;
                        }
                        ConstraintElement constraint = sqlentity.FindConstraintByName(n.Attributes["name"].Value);
                        if (constraint == null)
                        {
                            vd(ParserValidationArgs.NewError("constraint specified (" + n.Attributes["name"].Value + ") in view (" + view.Name + ") not found."));
                            constraint      = new ConstraintElement();
                            constraint.Name = n.Attributes["name"].Value;
                        }
                        else
                        {
                            if (!constraint.Type.Equals(ConstraintElement.FOREIGN_KEY))
                            {
                                vd(ParserValidationArgs.NewError("View [" + view.Name + "] references a constraint that is not a foreign key constraint: " + constraint.Name));
                            }
                        }
                        constraint.Prefix = ParseStringAttribute(n, "prefix", constraint.ForeignEntity.Name + "_");
                        SqlEntityElement foreignEntity = database.FindSqlEntityByName(constraint.ForeignEntity.Name);

                        // check to see if the foreignEntity is itself
                        if (foreignEntity == null && sqlentity.Name == constraint.ForeignEntity.Name)
                        {
                            foreignEntity = sqlentity;
                        }

                        if (foreignEntity == null)
                        {
                            vd(ParserValidationArgs.NewError("View [" + view.Name + "] references a constraint that references an sql entity that was not defined (or was not defined before this sql entity): " + constraint.ForeignEntity));
                        }
                        else
                        {
                            ArrayList columnsToAdd = new ArrayList();
                            foreach (ColumnElement column in foreignEntity.Columns)
                            {
                                ColumnElement viewColumn = (ColumnElement)column.Clone();
                                if (!constraint.Prefix.Equals(String.Empty))
                                {
                                    viewColumn.Name = constraint.Prefix + viewColumn.Name;
                                }
                                viewColumn.Prefix           = constraint.Prefix;
                                viewColumn.ForeignSqlEntity = constraint.ForeignEntity.Name;
                                viewColumn.ViewColumn       = true;
                                columnsToAdd.Add(viewColumn);
                            }
                            sqlentity.Columns.AddRange(columnsToAdd);
                        }
                        view.Constraints.Add(constraint);
                    }

                    // validation
                    if (view.Name.Equals(String.Empty))
                    {
                        vd(ParserValidationArgs.NewError("View does not have a name: " + Environment.NewLine + node.OuterXml));
                    }

                    list.Add(view);
                }
            }
            return(list);
        }