Beispiel #1
0
        /// <summary>
        /// Parses a hbm.xml file into this runtime equiv.
        /// being intended for code generation.
        /// </summary>
        /// <param name="hbmXmlFilePath"></param>
        public HbmFileContent(string hbmXmlFilePath)
        {
            _simpleProperties       = new Dictionary <string, string>();
            _fkProperties           = new Dictionary <string, string>();
            _listProperties         = new Dictionary <string, string>();
            _compositeKeyProperties = new Dictionary <string, string>();

            _keyColumns    = new Dictionary <string, ColumnMetadata>();
            _simpleColumns = new Dictionary <string, ColumnMetadata>();
            _fkColumns     = new Dictionary <string, List <ColumnMetadata> >();
            _listColumns   = new Dictionary <string, List <ColumnMetadata> >();

            _spConstNames = new List <HbmStoredProxNames>();
            _keyManyToOnePropertyNames = new List <string>();
            _allPkColumns = new Dictionary <string, string>();

            if (!File.Exists(hbmXmlFilePath))
            {
                throw new ItsDeadJim(string.Format("There isn't any xml file at '{0}'", hbmXmlFilePath));
            }

            _fileNamePath = hbmXmlFilePath;
            _hbmXml       = new XmlDocument();
            _hbmXml.LoadXml(File.ReadAllText(_fileNamePath));
            _nsMgr = new XmlNamespaceManager(_hbmXml.NameTable);
            _nsMgr.AddNamespace(NS, Globals.HBM_XML_NS);

            _classNode =
                _hbmXml.SelectSingleNode(CreateXpath(HbmXmlNames.HIBERNATE_MAPPING, HbmXmlNames.CLASS), _nsMgr) as
                XmlElement;
            if (_classNode == null)
            {
                throw new ItsDeadJim(string.Format("The top-level 'class' node is missing from the xml file at '{0}'", hbmXmlFilePath));
            }

            IsCompositeKey =
                _hbmXml.SelectSingleNode(CreateXpath(HbmXmlNames.HIBERNATE_MAPPING, HbmXmlNames.CLASS, HbmXmlNames.COMPOSITE_ID), _nsMgr) != null;

            var tableNameAttr = _classNode.Attributes[HbmXmlNames.TABLE];

            if (tableNameAttr != null)
            {
                _tableName = tableNameAttr.Value;
            }

            var attrTypeName = GetAttrVal(CreateXpath(HbmXmlNames.HIBERNATE_MAPPING, HbmXmlNames.CLASS), HbmXmlNames.NAME);

            if (NfReflect.IsFullAssemblyQualTypeName(attrTypeName))
            {
                _asmQualTypeName = attrTypeName;
                var nfName = new NfTypeName(attrTypeName);
                _className = nfName.ClassName;
                _namespace = nfName.Namespace;
            }
            else
            {
                _className       = NfReflect.GetTypeNameWithoutNamespace(attrTypeName);
                _namespace       = NfReflect.GetNamespaceWithoutTypeName(attrTypeName);
                _asmQualTypeName = Compose.ClassName(_className, _namespace);
            }

            _dbSchema = GetAttrVal(CreateXpath(HbmXmlNames.HIBERNATE_MAPPING, HbmXmlNames.CLASS), HbmXmlNames.SCHEMA);

            //the stored prox will not have a schema qualifier so try to derive it from the file name
            if (string.IsNullOrWhiteSpace(_dbSchema))
            {
                var ff = Path.GetFileName(_fileNamePath);
                if (!string.IsNullOrWhiteSpace(ff) && ff.Split('.').Length > 3)
                {
                    _dbSchema = ff.Split('.')[0];
                }
                else
                {
                    _dbSchema = "Dbo";
                }
            }

            //get id/composite-id node
            GetIdNode();

            //get id's name and type
            GetIdName();

            //get flat-data's names and types
            GetFlatProperties();

            //get FKs names and types
            GetFkProperties();

            //get IList names and types
            GetListProperties();

            //get stored proc names
            GetStoredProcNames();

            //get composite key's properties
            GetCompositeKeyProperties();

            //condense a list of what is just on this table
            GetAllPkColumns();
        }