public void Remove(T obj)
        {
            String sqlCommand;

            SqlParameter[] parameters;

            sqlCommand = @"DELETE FROM " + RelationshipTableName
                         + " WHERE " + ChildKeyColumnName + " = @" + ChildKeyColumnName
                         + " AND " + ParentKeyColumnName + " = @" + ParentKeyColumnName;

            parameters = new SqlParameter[]
            {
                new SqlParameter(ParentKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(_parentObject.ActiveRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , _parentObject.Id),

                new SqlParameter(ChildKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(ActiveRecord <T> .GetMap().PrimaryKeyColumnMap.Property.PropertyType)
                                 , obj.Id)
            };

            //Execute Delete Command
            ActiveRecordSession.GetSession().DatabaseServices.ExecuteNonQuery(sqlCommand, parameters);

            ObjectList.Remove(obj);
            _retrivedObjects.Remove(obj.Id);
        }
Beispiel #2
0
        public static T Find(long id)
        {
            if (id == null)
            {
                return(null);
            }

            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName
                           + " WHERE " + activeRecordMap.PrimaryKeyColumnName + " = @" + activeRecordMap.PrimaryKeyColumnName;


            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter(activeRecordMap.PrimaryKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(activeRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , id)
            };

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query, parameters);

            return(ObjectRetrievalServices.RetrieveObject <T>(dataReader, activeRecordMap));
        }
        public void SetToIds(List <long> ids)
        {
            List <T> newObjectList       = new List <T>();
            List <T> objectsToRemoveList = new List <T>();

            foreach (long id in ids)
            {
                newObjectList.Add(ActiveRecord <T> .Find(id));
            }

            foreach (T obj in ObjectList)
            {
                if (!newObjectList.Contains(obj))
                {
                    objectsToRemoveList.Add(obj);
                }
            }

            foreach (T obj in objectsToRemoveList)
            {
                Remove(obj);
            }

            foreach (T obj in newObjectList)
            {
                if (!ObjectList.Contains(obj))
                {
                    ObjectList.Add(obj);
                }
            }
        }
        protected override void Retrieve()
        {
            String sqlQuery;

            SqlParameter[] parameters;

            sqlQuery = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(ActiveRecord <T> .GetMap().ColumnList(true), ActiveRecord <T> .GetMap().TableName, SqlBuilderServices.ColumnListType.List)
                       + " FROM " + ActiveRecord <T> .GetMap().TableName
                       + " INNER JOIN " + RelationshipTableName + " ON " + RelationshipTableName + "." + ChildKeyColumnName + " = " + ActiveRecord <T> .GetMap().TableName + "." + ChildKeyColumnName
                       + " WHERE " + ParentKeyColumnName + " = @" + ParentKeyColumnName;

            parameters = new SqlParameter[]
            {
                new SqlParameter(ParentKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(_parentObject.ActiveRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , _parentObject.ActiveRecordMap.PrimaryKeyColumnMap.Property.GetValue(_parentObject, null))
            };

            ObjectList = ActiveRecord <T> .FindManyWithSql(sqlQuery, parameters);

            foreach (T obj in ObjectList)
            {
                _retrivedObjects.Add(obj.Id);
            }

            _loadStatus = LoadStatuses.Loaded;
        }
Beispiel #5
0
        public static List <T> FindManyWithSql(String sqlQuery)
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(sqlQuery);

            return(ObjectRetrievalServices.RetrieveObjects <T>(dataReader, activeRecordMap));
        }
Beispiel #6
0
        public static List <T> FindAll()
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName;

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query);

            return(ObjectRetrievalServices.RetrieveObjects <T>(dataReader, activeRecordMap));
        }
Beispiel #7
0
        public static T FindFirstWhere(String where, SqlParameter[] parameters)
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName
                           + " WHERE " + where;

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query, parameters);

            return(ObjectRetrievalServices.RetrieveObject <T>(dataReader, activeRecordMap));
        }
Beispiel #8
0
        protected override void Retrieve()
        {
            String where;
            SqlParameter[] parameters;

            where = ChildKeyColumnName + " = @" + ParentKeyColumnName;

            parameters = new SqlParameter[]
            {
                new SqlParameter(ParentKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(ActiveRecord <T> .GetMap().PrimaryKeyColumnMap.Property.PropertyType)
                                 , _parentObject.ActiveRecordMap.ColumnMappings[ParentKeyColumnName].Property.GetValue(_parentObject, null))
            };

            ObjectList = ActiveRecord <T> .FindManyWhere(where, parameters);

            _loadStatus = LoadStatuses.Loaded;
        }
Beispiel #9
0
        public void Refresh()
        {
            ActiveRecordMap activeRecordMap = ActiveRecord <T> .GetMap();

            string query = @"SELECT " + SqlBuilderServices.BuildSqlColumnList(activeRecordMap.ColumnList(true), activeRecordMap.TableName, SqlBuilderServices.ColumnListType.List)
                           + " FROM " + activeRecordMap.TableName
                           + " WHERE " + activeRecordMap.PrimaryKeyColumnName + " = @" + activeRecordMap.PrimaryKeyColumnName;


            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter(activeRecordMap.PrimaryKeyColumnName
                                 , SqlBuilderServices.GetDBTypeFromSystemType(activeRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                 , this.Id)
            };

            IDataReader dataReader = _activeRecordSession.DatabaseServices.ExecuteDataReader(query, parameters);

            ObjectRetrievalServices.RetrieveObject <T>(dataReader, activeRecordMap, true);
        }
Beispiel #10
0
        private void Initialize()
        {
            if (!_activeRecordSession.ActiveRecordCache.ContainsKey(typeof(T)))
            {
                _activeRecordSession.ActiveRecordCache.Add(typeof(T), new Dictionary <object, object>());
            }

            if (_activeRecordMap == null)
            {
                _activeRecordMap = ActiveRecord <T> .GetMap();
            }

            InitializeRelationships();

            _objectStatus = ObjectStatus.New;

            if (_activeRecordMap.PrimaryKeyColumnMap != null)
            {
                _activeRecordMap.PrimaryKeyColumnMap.Property.SetValue(this, -1, null);
            }
        }
Beispiel #11
0
        public override void Save()
        {
            String where;
            SqlParameter[] parameters;

            //TODO: Change to foreach ala Has object
            if (ObjectList.Count > 0)
            {
                if (ObjectList[0] != null && ObjectList[0].Version <= _parentObject.Version)
                {
                    ObjectList[0].Save();
                }

                //Set the parent's foreign key value to the child's primary key value
                _parentObject.ActiveRecordMap.ColumnMappings[ParentKeyColumnName].Property.SetValue(_parentObject, ActiveRecord <T> .GetMap().PrimaryKeyColumnMap.Property.GetValue(ObjectList[0], null), null);
            }
        }
        public override void Save()
        {
            String where;
            SqlParameter[] parameters;

            foreach (T obj in ObjectList)
            {
                //Set each child's foreign key to the parent's primary key
                if (obj.Version < _parentObject.Version)
                {
                    obj.Save();
                }

                //Insert relationship row for newly added/created child objects
                if (!_retrivedObjects.Contains(obj.Id))
                {
                    string command = @"INSERT INTO " + RelationshipTableName
                                     + "(" + ParentKeyColumnName + "," + ChildKeyColumnName + ")"
                                     + " VALUES(@" + ParentKeyColumnName + ", @" + ChildKeyColumnName + ")";

                    parameters = new SqlParameter[]
                    {
                        new SqlParameter(ParentKeyColumnName
                                         , SqlBuilderServices.GetDBTypeFromSystemType(_parentObject.ActiveRecordMap.PrimaryKeyColumnMap.Property.PropertyType)
                                         , _parentObject.Id),

                        new SqlParameter(ChildKeyColumnName
                                         , SqlBuilderServices.GetDBTypeFromSystemType(ActiveRecord <T> .GetMap().PrimaryKeyColumnMap.Property.PropertyType)
                                         , obj.Id)
                    };

                    //Execute Insert Command
                    ActiveRecordSession.GetSession().DatabaseServices.ExecuteNonQuery(command, parameters);

                    _retrivedObjects.Add(obj.Id);
                }
            }
        }