Пример #1
0
        /// <summary>
        /// Produces a single hbm.xml for the given <see cref="tbl"/>
        /// </summary>
        /// <param name="outputNamespace"></param>
        /// <param name="tbl"></param>
        /// <returns>The path the generated hbm.xml file</returns>
        public static string GetSingleHbmXml(string outputNamespace, string tbl)
        {
            if (HbmKeys == null || HbmOneToMany == null || HbmBags == null)
            {
                throw new RahRowRagee("Assign the static variables HbmKeys, HbmOneToMany and HbmBags" +
                                      " then try again (These values are calculated from Sorting).");
            }
            if (Settings.DoNotReference.Contains(tbl))
            {
                return(null);
            }

            var hbmPk   = HbmKeys.Data;
            var hbmFk   = HbmOneToMany.Data;
            var hbmBags = HbmBags.Data;

            //possiable duplicate names handled within this
            var className = Compose.ClassName(tbl, outputNamespace);

            //not having the naming pattern is exceptional
            Compose.ValidSplit(tbl, 2);

            var tableName  = NfString.ExtractLastWholeWord(tbl, null);
            var schemaName = tbl.Replace(string.Format(".{0}", tableName), string.Empty);

            var xe = XeFactory.HibernateMappingNode();

            var classXe = XeFactory.ClassNode(className, tableName, schemaName);

            var hasNoPkAtAll = GetPk(outputNamespace, tbl, hbmPk, classXe);

            //having no pk, add the contrived comp-key to the class
            if (hasNoPkAtAll)
            {
                GetAllColumnsAsCompositeKey(tbl, classXe, outputNamespace);
            }
            else//simple properties
            {
                foreach (var columnName in Sorting.DbContainers.FlatData.Data.Where(x => string.Equals(x.table_name, tbl, Sorting.C)))
                {
                    var fullColumnName = columnName.column_name;
                    Compose.ValidSplit(fullColumnName, 3);
                    var simplePropXe = GetSimplePropertyHbmXml(columnName, Globals.HbmXmlNames.PROPERTY);
                    classXe.Add(simplePropXe);
                }
            }

            GetFksWhichAreNotPartOfPks(outputNamespace, tbl, hbmFk, classXe);

            GetBags(outputNamespace, tbl, hbmBags, hasNoPkAtAll, className, classXe);

            xe.Add(classXe);

            var hbmXmlOutputPath = Path.Combine(Settings.HbmDirectory,
                                                Compose.HbmFileName(tbl));
            var xmlContent = xe.ToString()
                             .Replace("<hibernate-mapping>", "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\">");

            File.WriteAllText(hbmXmlOutputPath, xmlContent);

            //perform rename of any properties which match classname or are duplicated therein
            CorrectHbmXmlDuplicateNames(hbmXmlOutputPath);
            return(hbmXmlOutputPath);
        }
Пример #2
0
        /// <summary>
        /// Produces a single hbm.xml with the sql-query node in tow.
        /// </summary>
        /// <param name="outputNamespace"></param>
        /// <param name="storedProc"></param>
        /// <returns>The path to the generated hbm.xml file.</returns>
        public static string GetHbmNamedQueryXml(string outputNamespace, string storedProc)
        {
            if (Sorting.AllStoredProcNames == null || Sorting.AllStoredProcNames.Count <= 0)
            {
                throw new RahRowRagee("There doesn't appear to be any stored procs here.");
            }

            if (Settings.DoNotReference.Contains(storedProc) || !Sorting.AllStoredProx.ContainsKey(storedProc))
            {
                return(null);
            }

            var sp = Sorting.AllStoredProx[storedProc];

            if (sp == null)
            {
                return(null);
            }

            var returnedData = sp.ReturnedData;

            if (returnedData == null || returnedData.Count <= 0)
            {
                return(null);
            }

            if (returnedData.Keys.Count <= 0)
            {
                Settings.WriteToStoredProcLog(string.Format("Stored proc named '{0}' has an empty dataset", sp.ProcName));
                if (!Sorting.EmptyDatasetProx.Contains(sp.ProcName))
                {
                    Sorting.EmptyDatasetProx.Add(sp.ProcName);
                }
                return(null);
            }

            //both the return types and the callable sql-query are wrapped in this root node
            var xe = XeFactory.HibernateMappingNode();

            if (returnedData.Count > 1)
            {
                Settings.WriteToStoredProcLog(string.Format("Stored Proc named '{0}' returns a multi-table dataset", storedProc));
                if (!Sorting.MultiTableDsProx.Contains(storedProc))
                {
                    Sorting.MultiTableDsProx.Add(storedProc);
                }
            }

            //possiable duplicate names handled within this
            var className = Compose.ClassName(string.Format("{0}.{1}{2}", Globals.STORED_PROX_FOLDER_NAME, storedProc, "Table"), outputNamespace);

            var classXe = XeFactory.ClassNode(className, null, null);

            classXe.Add(XeFactory.IdNode(null));

            var sqlQryName = NfString.SafeDotNetIdentifier(storedProc);
            var returnsXe  = XeFactory.ReturnNode(sqlQryName, className);

            foreach (var cMeta in returnedData[returnedData.Keys.First()])
            {
                var simplePropName     = Compose.PropertyName(cMeta.column_name, true);
                var simplePropColumn   = NfString.ExtractLastWholeWord(cMeta.column_name, null);
                var simplePropDataType = Util.Lexicon.DotNet2HbmTypes[cMeta.data_type];

                classXe.Add(XeFactory.PropertyNode(Globals.HbmXmlNames.PROPERTY, simplePropName, null, simplePropDataType,
                                                   (cMeta.data_type == "System.String"
                        ? Globals.MSSQL_MAX_VARCHAR.ToString(CultureInfo.InvariantCulture)
                        : null), cMeta.is_nullable.HasValue && cMeta.is_nullable == true, string.Empty));

                returnsXe.Add(XeFactory.ReturnPropertyNode(simplePropColumn, simplePropName));
            }

            var sqlQryXe = XeFactory.SqlQueryNode(sqlQryName);

            sqlQryXe.Add(returnsXe);

            sqlQryXe.Add(new XCData(sp.ToHbmSql()));

            xe.Add(classXe);
            xe.Add(sqlQryXe);

            var hbmXmlOutputPath = Path.Combine(Settings.HbmStoredProcsDirectory,
                                                Compose.HbmFileName(storedProc.Replace(" ", Globals.REPLACE_SPACE_WITH_SEQUENCE)));
            var xmlContent = xe.ToString()
                             .Replace("<hibernate-mapping>", "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\">");

            File.WriteAllText(hbmXmlOutputPath, xmlContent);
            CorrectHbmXmlDuplicateNames(hbmXmlOutputPath, true);
            return(hbmXmlOutputPath);
        }