public virtual Dictionary <string, int> SaveToDatabase(object objectToSave, SpringBaseDao baseDao, Type mappingAttributesType, bool inheritMappingAttributes)
        {
            Type           objectToSaveType = objectToSave.GetType();
            MappingContext mappingContext   = MappingContext.GetMappingContext(objectToSaveType, mappingAttributesType, inheritMappingAttributes);

            BuildObjectSql(mappingContext, baseDao);

            IBeforeSaveToDatabase beforeSaveToDatabase = objectToSave as IBeforeSaveToDatabase;

            if (beforeSaveToDatabase != null)
            {
                beforeSaveToDatabase.BeforeSaveToDatabase();
            }

            Dictionary <string, int> insertRowCounts = new Dictionary <string, int>();

            baseDao.TransactionTemplate.Execute(delegate
            {
                baseDao.AdoTemplate.ClassicAdoTemplate.Execute(delegate(IDbCommand command)
                {
                    ColumnCachedValues cachedValues = new ColumnCachedValues();
                    SaveToDatabase(null, objectToSave, null, cachedValues, mappingContext,
                                   insertRowCounts, command);
                    return(null);
                });
                return(null);
            });
            return(insertRowCounts);
        }
Esempio n. 2
0
        public virtual IList LoadFromDatabase(Type typeToLoad, SpringBaseDao baseDao,
                                              IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauses)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(typeToLoad);

            BuildObjectSql(mappingContext.ObjectPaths, baseDao);

            string objectPath = typeToLoad.FullName;

            IList rtnList = null;

            baseDao.AdoTemplate.ClassicAdoTemplate.Execute(delegate(IDbCommand command)
            {
                IList list =
                    LoadFromDatabase(typeToLoad, objectPath, appendSelectWhereClauses,
                                     mappingContext, command);
                if (list != null)
                {
                    rtnList = list;
                    foreach (object obj in rtnList)
                    {
                        IAfterLoadFromDatabase afterLoadFromDatabase = obj as IAfterLoadFromDatabase;
                        if (afterLoadFromDatabase != null)
                        {
                            afterLoadFromDatabase.AfterLoadFromDatabase();
                        }
                    }
                }
                return(null);
            });
            return(rtnList);
        }
        public virtual void BuildDatabase(Type objectToSaveType, SpringBaseDao baseDao, Type mappingAttributesType, bool inheritMappingAttributes)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(objectToSaveType, mappingAttributesType, inheritMappingAttributes);

            bool createdDatabase;
            IDictionary <string, DataTable> tableSchemas =
                BuildDatabase(mappingContext.Tables, baseDao, out createdDatabase);

            BuildTables(tableSchemas, mappingContext, baseDao, createdDatabase);
        }
        public virtual Dictionary <string, int> SaveToDatabase <T>(IEnumerable <T> objectsToSave, SpringBaseDao baseDao,
                                                                   bool deleteAllBeforeSave, Type mappingAttributesType,
                                                                   bool inheritMappingAttributes)
        {
            Type           objectToSaveType = typeof(T);
            MappingContext mappingContext   = MappingContext.GetMappingContext(objectToSaveType, mappingAttributesType, inheritMappingAttributes);

            BuildObjectSql(mappingContext, baseDao);

            Dictionary <string, int> insertRowCounts = new Dictionary <string, int>();

            baseDao.TransactionTemplate.Execute(delegate
            {
                baseDao.AdoTemplate.ClassicAdoTemplate.Execute(delegate(IDbCommand command)
                {
                    if (deleteAllBeforeSave)
                    {
                        DeleteAllFromDatabase(objectToSaveType, baseDao, mappingAttributesType);
                    }
                    int count = 0;
                    CollectionUtils.ForEach(objectsToSave, delegate(T objectToSave)
                    {
                        ++count;
                        bool doSave = true;
                        ICanSaveToDatabase checkToSaveToDatabase = objectToSave as ICanSaveToDatabase;
                        if (checkToSaveToDatabase != null)
                        {
                            if (!checkToSaveToDatabase.CanSaveToDatabase(this, baseDao))
                            {
                                doSave = false;
                            }
                        }
                        if (doSave)
                        {
                            IBeforeSaveToDatabase beforeSaveToDatabase = objectToSave as IBeforeSaveToDatabase;
                            if (beforeSaveToDatabase != null)
                            {
                                beforeSaveToDatabase.BeforeSaveToDatabase();
                            }

                            ColumnCachedValues cachedValues = new ColumnCachedValues();
                            SaveToDatabase(null, objectToSave, null, cachedValues, mappingContext, insertRowCounts, command);
                        }
                    });
                    return(null);
                });
                return(null);
            });
            return(insertRowCounts);
        }
Esempio n. 5
0
        public virtual IList LoadFromDatabase(Type typeToLoad, SpringBaseDao baseDao,
                                              IDictionary <string, DbAppendSelectWhereClause> appendSelectWhereClauses,
                                              Type mappingAttributesType)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(typeToLoad, mappingAttributesType);

            BuildObjectSql(mappingContext, baseDao);

            Dictionary <object, IList> list = null;

            baseDao.AdoTemplate.ClassicAdoTemplate.Execute(delegate(IDbCommand command)
            {
                ColumnCachedValues cachedValues = new ColumnCachedValues();
                list =
                    LoadObjectInstancesToList(typeToLoad, null, null, appendSelectWhereClauses, cachedValues, mappingContext, command);
                return(null);
            });
            if (list != null)
            {
                IList rtnList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(typeToLoad));
                foreach (KeyValuePair <object, IList> pair in list)
                {
                    foreach (object listElement in pair.Value)
                    {
                        rtnList.Add(listElement);
                    }
                }
                foreach (object obj in rtnList)
                {
                    IAfterLoadFromDatabase afterLoadFromDatabase = obj as IAfterLoadFromDatabase;
                    if (afterLoadFromDatabase != null)
                    {
                        afterLoadFromDatabase.AfterLoadFromDatabase();
                    }
                }

                return(rtnList);
            }
            else
            {
                return(null);
            }
        }
        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);
        }
        public virtual object GetPrimaryKeyValueForObject(object obj, Type mappingAttributesType)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(obj.GetType(), mappingAttributesType);

            return(mappingContext.GetPrimaryKeyValueForObject(obj));
        }
        public virtual string GetPrimaryKeyNameForType(Type objectType, Type mappingAttributesType)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(objectType, mappingAttributesType);

            return(mappingContext.GetPrimaryKeyNameForType(objectType));
        }
Esempio n. 9
0
        public string GetPrimaryKeyNameForType(Type objectType)
        {
            MappingContext mappingContext = MappingContext.GetMappingContext(objectType);

            return(mappingContext.GetPrimaryKeyNameForType(objectType));
        }