public virtual string GetTableNameForType(Type objectType, Type mappingAttributesType)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(objectType, mappingAttributesType);

            return(mappingContext.GetTableNameForType(objectType));
        }
        public virtual int DeleteAllFromDatabase(Type objectToDeleteType, SpringBaseDao baseDao, Type mappingAttributesType)
        {
            MappingContext mappingContext        = MappingContext.GetMappingContext(objectToDeleteType, mappingAttributesType);
            List <Table>   deleteFirstFromTables = null;

            foreach (Table table in mappingContext.Tables.Values)
            {
                if (table.ForeignKeys.Count > 1)
                {
                    CollectionUtils.Add(table, ref deleteFirstFromTables);
                }
            }
            if (deleteFirstFromTables != null)
            {
                // Need to order tables in dependency deletion order
                bool didSwapAny;
                int  maxIterationCount = 20;
                do
                {
                    didSwapAny = false;
                    for (int i = deleteFirstFromTables.Count - 1; i >= 0; --i)
                    {
                        Table checkPossibleSwapTable = deleteFirstFromTables[i];
                        int   swapBeforeIndex        = -1;
                        for (int j = i - 1; j >= 0; --j)
                        {
                            Table checkPossibleParentTable = deleteFirstFromTables[j];
                            foreach (ForeignKeyColumn foreignKeyColumn in checkPossibleSwapTable.ForeignKeys)
                            {
                                if (foreignKeyColumn.ForeignTable == checkPossibleParentTable)
                                {
                                    swapBeforeIndex = j;
                                }
                            }
                        }
                        if (swapBeforeIndex != -1)
                        {
                            didSwapAny = true;
                            deleteFirstFromTables.RemoveAt(i);
                            deleteFirstFromTables.Insert(swapBeforeIndex, checkPossibleSwapTable);
                        }
                    }
                } while ((--maxIterationCount > 0) && didSwapAny);
                if (maxIterationCount == 0)
                {
                    throw new MappingException("Failed to order tables in dependency deletion order");
                }
            }
            int deleteCount = 0;

            baseDao.TransactionTemplate.Execute(delegate
            {
                if (deleteFirstFromTables != null)
                {
                    foreach (Table table in deleteFirstFromTables)
                    {
                        baseDao.DoSimpleDelete(table.TableName, null, null);
                    }
                }
                deleteCount = baseDao.DoSimpleDelete(mappingContext.GetTableNameForType(objectToDeleteType), null, null);
                return(null);
            });
            return(deleteCount);
        }
Example #3
0
        public string GetTableNameForType(Type objectType)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(objectType);

            return(mappingContext.GetTableNameForType(objectType));
        }