示例#1
0
        public UpdateStates UpdateObject(ISerializableObject iso, DbConnection connection, ISerializerTransaction transaction)
        {
            UpdateStates    state = UpdateStates.UPDATED;
            AttributeWorker w     = AttributeWorker.GetInstance(Target);
            DbCommand       com   = connection.CreateCommand();
            Guid            g     = AttributeWorker.RowGuid(iso);
            GenericWeakReference <ISerializableObject> tmp;

            try
            {
                tmp = _persistentObjects[g];
            }
            catch (KeyNotFoundException ex)
            {
                throw new UpdateException("update failed. object is not known by the system and must be loaded first", ex);
            }
            com.CommandText = _owner.SerializeUpdate(iso, ref state, tmp);
            transaction.Guard(com);
            //Console.WriteLine(com.CommandText);
            try
            {
                if (com.ExecuteNonQuery() < 1)
                {
                    state = UpdateStates.NO_ROWS_AFFECTED;
                    return(state);
                }
                return(state);
            }
            catch (Exception ex)
            {
                throw new SerializerException("update failed", ex);
            }
        }
示例#2
0
        protected override void SynchronizeImpl()
        {
            ISerializableObject source = StateOwner.SourceObject;
            ISerializableObject sink   = StateOwner.SinkSerializer.CreateISerializableObject(StateOwner.SynchronizedType);

            CopyUtils.PlainCopy(StateOwner.SinkSerializer, KeyCarrier, sink, new FieldLock(source.GetType()), true);
            CopyUtils.PlainCopy(StateOwner.SinkSerializer, source, sink, new FieldLock(source.GetType()), false);

            StateOwner.SinkSerializer.Connector.InsertPlain(sink);
            StateOwner.SinkObject = sink;

            SyncItem si1 = StateOwner.SyncSerializer.CreateISerializableObject <SyncItem>();
            SyncItem si2 = StateOwner.SyncSerializer.CreateISerializableObject <SyncItem>();

            si1.SyncGuid = AttributeWorker.RowGuid(source);
            si2.SyncGuid = AttributeWorker.RowGuid(sink);
            si1.ClassID  = source.GetType().Name;
            si2.ClassID  = source.GetType().Name;

            si1.Hashcode = SyncContainer.ComputeHashCode(StateOwner.SourceSerializer.Target, source);
            si2.Hashcode = SyncContainer.ComputeHashCode(StateOwner.SinkSerializer.Target, sink);

            si1.CounterPart = si2;

            StateOwner.SyncSerializer.Connector.Save(si1);
            StateOwner.SyncSerializer.ConnectOneToOne(si1, si2);
            StateOwner.SyncSerializer.Connector.Save(si2);
            StateOwner.SyncSerializer.ConnectOneToOne(si2, si1);
            StateOwner.SyncSerializer.Connector.Save(si1);

            StateOwner.UpdateFieldStates(StateOwner.SourceSerializer.Target, si1, source);//Deaktiviert
            StateOwner.UpdateFieldStates(StateOwner.SinkSerializer.Target, si2, sink);
        }
示例#3
0
 internal void AddISerializableObject(ISerializableObject iso)
 {
     if (!_pooledObjects.ContainsKey(AttributeWorker.RowGuid(iso)))
     {
         SyncContainer container = new SyncContainer(iso, _syncSerializer, _serializer);
         _pooledObjects[AttributeWorker.RowGuid(iso)] = container;
     }
 }
示例#4
0
 public bool IsManagedObject(ISerializableObject iso)
 {
     try
     {
         return(_persistentObjects[AttributeWorker.RowGuid(iso)].Target != null);
     }
     catch (KeyNotFoundException)
     {
         return(false);
     }
 }
示例#5
0
        public void InsertObject(ISerializableObject iso, DbConnection connection, ISerializerTransaction transaction)
        {
            AttributeWorker w   = AttributeWorker.GetInstance(Target);
            DbCommand       com = connection.CreateCommand();

            com.CommandText = _owner.SerializeInsert(iso);
            transaction.Guard(com);
            //Console.WriteLine(com.CommandText);
            try
            {
                //Console.WriteLine(com.ExecuteNonQuery());
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new SerializerException("insert failed", ex);
            }



            FieldInfo autoincField;
            Guid      g = AttributeWorker.RowGuid(iso);

            if ((autoincField = w.GetAutoincField(iso.GetType())) != null)
            {
                StringBuilder b = new StringBuilder();

                b.Append("SELECT * FROM ").Append(w.GetTableMapping(iso.GetType(), _owner.Praefix)).Append(" WHERE ");
                b.Append(w.GetColumnMapping(AttributeWorker.RowGuid(iso.GetType()))).Append("='");
                b.Append(g.ToString()).Append("'");

                com.CommandText = b.ToString();
                DbDataReader r = com.ExecuteReader();

                if (r.Read())
                {
                    Object o = r[w.GetColumnMapping(w.GetAutoincField(iso.GetType()))];
                    autoincField.SetValue(iso, o);
                }
                else
                {
                    r.Close();
                    throw new SerializerException("Insert core method failed! error unknown...");
                }

                r.Close();
            }


            GenericWeakReference <ISerializableObject> tmp = new GenericWeakReference <ISerializableObject>(iso);

            _persistentObjects[g] = tmp;
            MemoriseKeys(iso, w, tmp);
        }
示例#6
0
        public void RegisterType(Type t)
        {
            FieldInfo f = AttributeWorker.RowGuid(t);

            if (f == null)
            {
                throw new TypeDefException("Type " + t + " has no row guid column!");
            }

            _registeredTypes.Add(t);
        }
示例#7
0
        private ISerializableObject FetchObject(Serializer s, Guid guid)
        {
            FieldInfo     fi  = AttributeWorker.RowGuid(_synchronizedType);
            String        col = AttributeWorker.GetInstance(s.Target).GetColumnMapping(fi);
            StringBuilder tmp = new StringBuilder();

            tmp.Append(col).Append("='").Append(guid).Append("'");;
            IRestriction        res = RestrictionFactory.SqlRestriction(_synchronizedType, tmp.ToString());
            ISerializableObject iso = s.Connector.Load(_synchronizedType, res);

            return(iso);
        }
示例#8
0
        //Konstruktor
        internal SyncContainer(ISerializableObject iso, Serializer syncSerializer, Serializer sourceSerializer)
        {
            SyncState = new PrematureState(this);

            _synchronizedType = iso.GetType();
            _sourceGuid       = AttributeWorker.RowGuid(iso);

            _syncSerializer   = syncSerializer;
            _sourceSerializer = sourceSerializer;

            _parents  = new List <SyncContainer>();
            _children = new List <SyncContainer>();
        }
示例#9
0
        public void DeleteObject(ISerializableObject iso, DbConnection connection, ISerializerTransaction transaction)
        {
            if (AttributeWorker.RowGuid(iso) == null)
            {
                throw new SerializerException();
            }

            DbCommand com = connection.CreateCommand();

            transaction.Guard(com);
            com.CommandText = _owner.Delete(iso);
            //Console.WriteLine(com.CommandText);
            //Console.WriteLine(com.ExecuteNonQuery());
            com.ExecuteNonQuery();

            _persistentObjects.Remove(AttributeWorker.RowGuid(iso));
        }
示例#10
0
        public ISerializableObject RetrieveObject(Type objectType, DbDataReader reader, ref bool isLoaded)
        {
            if (_cleaner == null)
            {
                throw new NullReferenceException("No cleaner attached SerializableObjectPool needs a Cleaner to work...");
            }

            ISerializableObject ret = null;



            FieldInfo fi = AttributeWorker.RowGuid(objectType);
            Object    o  = reader[AttributeWorker.GetInstance(Target).GetColumnMapping(fi)];
            Guid      g  = (Guid)o;



            try
            {
                ret      = _persistentObjects[g].Target;
                isLoaded = false;

                if (ret == null)
                {
                    throw new KeyNotFoundException();
                }
            }
            catch (KeyNotFoundException)
            {
                ret = Deserialize(objectType, reader);
                GenericWeakReference <ISerializableObject> tmp = new GenericWeakReference <ISerializableObject>(ret);
                _persistentObjects[AttributeWorker.RowGuid(ret)] = tmp;
                MemoriseKeys(ret, AttributeWorker.GetInstance(Target), tmp);
                isLoaded = true;
            }

            _cleaner.Cleanup();

            return(ret);
        }
示例#11
0
            public void HandleManyToOne(ISerializableObject handledItem, ManyToOneAttribute attr, FieldInfo field)
            {
                ISerializableObject iso = (ISerializableObject)field.GetValue(handledItem);

                if (iso != null)
                {
                    try
                    {
                        SyncContainer s = _pooledObjects[AttributeWorker.RowGuid(iso)];
                        if (!_owner._parents.Contains(s))
                        {
                            _owner._parents.Add(s);
                        }
                        if (!s._children.Contains(_owner))
                        {
                            s._children.Add(_owner);
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                }
            }
示例#12
0
            public void HandleOneToMany(ISerializableObject handledItem, OneToManyAttribute attr, FieldInfo field)
            {
                IEnumerable set = (IEnumerable)field.GetValue(handledItem);

                foreach (ISerializableObject iso in set)
                {
                    try
                    {
                        SyncContainer s = _pooledObjects[AttributeWorker.RowGuid(iso)];
                        if (!_owner._children.Contains(s))
                        {
                            _owner._children.Add(s);
                        }
                        if (!s._parents.Contains(_owner))
                        {
                            s._parents.Add(_owner);
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                }
            }
示例#13
0
        internal void Analyze()
        {
            SyncItem            sourceSyncItem = FetchSyncItem(_sourceGuid);
            SyncItem            sinkSyncItem;
            ISerializableObject source = FetchObject(_sourceSerializer, _sourceGuid);


            if (sourceSyncItem == null)//In der Synchronisationstabelle gibt es keinen korrepondierenden Eintrag
            {
                #if DEBUG
                logger.Debug(source.GetType().Name + ", guid=" + AttributeWorker.RowGuid(source) + " is not synchronized and will be inserted");
                #endif

                SyncState = new InsertState(this);
            }
            else //in der Synchronisationstabelle ist ein Eintrag vorhanden
            {
                sinkSyncItem = sourceSyncItem.CounterPart;
                _sinkGuid    = sinkSyncItem.SyncGuid;

                ISerializableObject sink = FetchObject(_sinkSerializer, _sinkGuid); //Das korrepondierende Objekt wird aus der Zieldatenbankgeholt

                if (sink == null)                                                   //Wenn es nicht vorhanden ist wurde es gelöscht
                {
                    #if DEBUG
                    logger.Debug(sourceSyncItem.ClassID + ", guid=" + AttributeWorker.RowGuid(source) + " does not exist, the conflict has to be resolved");
                    #endif

                    SyncState = new DeletedState(this, sourceSyncItem);
                }
                else
                {
                    String h1           = sinkSyncItem.Hashcode;
                    String sinkHashCode = ComputeHashCode(_sinkSerializer.Target, sink);


                    if (h1 != sinkHashCode)//Änderung der Daten in der Zieldatenbank--warum ist das schon ein Konflikt?
                    {
                        #if DEBUG
                        logger.Debug(sourceSyncItem.ClassID + ", guid=" + AttributeWorker.RowGuid(source) + " is involved in a conflict, which has to be resolved");
                        #endif
                        SyncState = new ConflictState(this, sourceSyncItem);
                    }
                    else //zieldatenbank unverändert
                    {
                        String sourceHashCode = ComputeHashCode(SourceSerializer.Target, source);

                        if (sourceHashCode == sourceSyncItem.Hashcode)//Quelldatenbank unverändert
                        {
                            #if DEBUG
                            logger.Debug(sourceSyncItem.ClassID + ", guid=" + AttributeWorker.RowGuid(source) + " has not changed and will not be synchronized");
                            #endif
                            SyncState = new IgnoreState(this);
                        }
                        else//Quelldatenbank verändert-Zieldatenbank unverändert ->update
                        {
                            #if DEBUG
                            logger.Debug(sourceSyncItem.ClassID + ", guid=" + AttributeWorker.RowGuid(source) + " has changed on the sourceside and will be updated");
                            #endif
                            SyncState = new UpdateState(this, sourceSyncItem);
                        }
                    }
                }
            }

            AttributeWorker   w   = AttributeWorker.GetInstance(_sinkSerializer.Target);
            IList <FieldInfo> fis = w.GetFieldByAttribute <CheckForeingKeyConstraintAttribute>(_synchronizedType);


            foreach (FieldInfo fi in fis)
            {
                Object val = fi.GetValue(source);
                if (val == null)
                {
                    continue;
                }

                CheckForeingKeyConstraintAttribute attr = w.GetAttribute <CheckForeingKeyConstraintAttribute>(fi);
                String table  = attr.UnmappedTable;
                String column = attr.ExternColumn;
                if (column == null)
                {
                    column = w.GetColumnMapping(fi);
                }

                if (!_sinkSerializer.Connector.Exists(column, val, table))
                {
                    //Hier kann man einen neuen Status einführen, der dieses Problem abfängt
                    throw new InvalidOperationException("Insertion of " + _synchronizedType.Name + " will fail. Unmapped foreign key " + column + "=" + val + " does not exist in target database!");
                }
            }
        }
示例#14
0
 public void Register(ISerializableObject iso)
 {
     AttributeWorker.RowGuid(iso, Guid.NewGuid());
 }
示例#15
0
        internal virtual String SerializeUpdate(ISerializableObject iso, ref UpdateStates state, GenericWeakReference <ISerializableObject> tmp)
        {
            AttributeWorker w   = AttributeWorker.GetInstance(Target);
            StringBuilder   sql = new StringBuilder();

            StringBuilder where = new StringBuilder();

            Guid      g = AttributeWorker.RowGuid(iso);
            FieldInfo f = AttributeWorker.RowGuid(iso.GetType());

            sql.Append("UPDATE ").Append(w.GetTableMapping(iso.GetType(), _praefix));
            sql.Append(" SET ");
            where.Append(" WHERE ").Append(AttributeWorker.GetInstance(Target).GetColumnMapping(f));
            where.Append("='").Append(g).Append("'");;

            FieldInfo[] fis = AttributeWorker.RetrieveAllFields(iso.GetType());



            bool start = true;

            foreach (FieldInfo fi in fis)
            {
                try
                {
                    String col = w.GetColumnMapping(fi);
                    Object val = fi.GetValue(iso);
                    Object oldVal;


                    if (w.IsAutoincID(fi))
                    {
                        continue;
                    }
                    if (AttributeWorker.IsRowGuid(fi))
                    {
                        continue;
                    }

                    if (w.IsID(fi))
                    {
                        oldVal = tmp.Properties[fi.Name];
                        if (!Object.Equals(val, oldVal))
                        {
                            state = UpdateStates.PRIMARYKEY_MODIFIED;
                        }
                        tmp.Properties[fi.Name] = val;
                    }

                    if (!start)
                    {
                        sql.Append(", ");
                    }
                    else
                    {
                        start = false;
                    }

                    sql.Append(col).Append("=").Append(SqlUtil.SqlConvert(val));
                }
                catch (SerializerException) { }
            }

            sql.Append(where);
            return(sql.ToString());
        }