Example #1
0
        public static void Write(this XmlElement applyNode, XmlNodeSchema schema)
        {
            var xml = applyNode.OwnerDocument;
            if (xml == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(schema.Name))
            {
                return;
            }
            var node = xml.CreateElement(schema.Name);
            foreach (string property in schema.Properties)
            {
                var value = schema.Properties[property];
                if (string.IsNullOrEmpty(value))
                {
                    continue;
                }
                var attribute = xml.CreateAttribute(property);
                attribute.Value = value;
                node.Attributes.Append(attribute);
            }
            applyNode.AppendChild(node);
            if (schema.ChildNodes == null) return;
            foreach (var childNode in schema.ChildNodes)
            {
                Write(node, childNode);
            }
        }
Example #2
0
        protected string[] HBMFileGenerate()
        {
            var tmp = new List<string>();
            foreach (var table in DataBase.Tables)
            {
                var xml = new XmlDocument();
                var version = xml.CreateXmlDeclaration("1.0", "utf-8", "");
                xml.AppendChild(version);
                var root = xml.CreateElement("hibernate-mapping", "urn:nhibernate-mapping-2.2");
                xml.AppendChild(root);
                var classNode = xml.CreateElement("class");
                var nameAttribute = xml.CreateAttribute("name");
                nameAttribute.Value = AssemblyTypeFormat(table);
                classNode.Attributes.Append(nameAttribute);
                var tableAttribute = xml.CreateAttribute("table");
                tableAttribute.Value = string.Format(string.IsNullOrEmpty(table.Owner) ? "{1}" : "{0}.{1}", table.Owner,
                                                     table.Name);
                classNode.Attributes.Append(tableAttribute);
                foreach (var col in table.Columns.OrderBy(t => t.Index).ToList())
                {
                    XmlElement property;
                    if (col.PrimaryKey)
                    {
                        property = xml.CreateElement("id");
                        var genor = xml.CreateElement("generator");
                        var genorClassAttribute = xml.CreateAttribute("class");
                        genorClassAttribute.Value = col.DBType.Equals("int") ? "identity" : "guid";
                        genor.Attributes.Append(genorClassAttribute);
                        property.AppendChild(genor);
                    }
                    else
                    {
                        property = xml.CreateElement("property");
                    }
                    var propertyNameAttribute = xml.CreateAttribute("name");
                    propertyNameAttribute.Value = col.Name;
                    property.Attributes.Append(propertyNameAttribute);

                    var propertyColumnAttribute = xml.CreateAttribute("column");
                    propertyColumnAttribute.Value = col.Name;
                    property.Attributes.Append(propertyColumnAttribute);

                    if (col.Nullable)
                    {
                        var propertyNotNullAttribute = xml.CreateAttribute("not-null");
                        propertyNotNullAttribute.Value =
                            (!col.Nullable).ToString(CultureInfo.InvariantCulture).ToLower();
                        property.Attributes.Append(propertyNotNullAttribute);
                    }
                    classNode.AppendChild(property);
                }
                var parents =
                    table.Parents.Where(
                        t => !string.IsNullOrEmpty(t.ColumnRelated) && !string.IsNullOrEmpty(t.TableRelated)).ToList();
                foreach (var parent in parents)
                {
                    var schema = new XmlNodeSchema
                                     {
                                         Name = "many-to-one",
                                         Properties =
                                             new NameValueCollection
                                                 {
                                                     {"name", parent.ClassName ?? parent.TableRelated},
                                                     {"column", parent.Column},
                                                     {"cascade", "none"},
                                                     {"unique", "true"},
                                                     {"class", AssemblyTypeFormat(parent)},
                                                 }
                                     };
                    classNode.Write(schema);
                }
                classNode.Attributes.Remove(
                    classNode.Attributes.OfType<XmlAttribute>().FirstOrDefault(
                        t => t.Name.Equals("xmlns", StringComparison.OrdinalIgnoreCase)));
                var childs =
                    table.Childs.Where(
                        t => !string.IsNullOrEmpty(t.ColumnRelated) && !string.IsNullOrEmpty(t.TableRelated)).ToList();
                foreach (var child in childs)
                {
                    var schema = new XmlNodeSchema
                                     {
                                         Name = "list",
                                         Properties = new NameValueCollection
                                                          {
                                                              {"name", child.TableRelated + "List"},
                                                              {"table", child.TableRelated},
                                                              {"cascade", "all-delete-orphan"},
                                                              {"lazy", "true"},
                                                          }
                                     };
                    schema.ChildNodes = new List<XmlNodeSchema>
                                            {
                                                new XmlNodeSchema
                                                    {
                                                        Name = "key",
                                                        Properties =
                                                            new NameValueCollection {{"column", child.ColumnRelated}}
                                                    },
                                                new XmlNodeSchema
                                                    {
                                                        Name = "one-to-many",
                                                        Properties =
                                                            new NameValueCollection
                                                                {{"class", AssemblyTypeFormat(child)}}
                                                    }
                                            };
                    classNode.Write(schema);
                }

                root.AppendChild(classNode);
                var fileName = Path.Combine(Directory.FullName, string.Format("{0}.hbm.xml", table.Name));
                xml.Save(fileName);
                tmp.Add(fileName);
            }
            return tmp.ToArray();
        }