Beispiel #1
0
        internal void Delete(ISerializableObject iso, ISerializerTransaction transaction)
        {
            if (!IsManaged(iso))
            {
                return;
            }



            AttributeWorker w = AttributeWorker.GetInstance(Target);


            ResolverData <ISerializableObject> data = new ResolverData <ISerializableObject>();

            data.FieldsToResolve = AttributeWorker.RetrieveAllFields(iso.GetType());
            data.HandledItem     = iso;

            DeleteHandler h = new DeleteHandler(this);
            RelationResolver <ISerializableObject> res = new RelationResolver <ISerializableObject>();

            res.Handler = h;


            h.MODE = DeleteHandler.DOWNMODE;
            res.StartRelationResolving(data);


            _objectPool.DeleteObject(iso, _connection, transaction);

            h.MODE = DeleteHandler.UPMODE;
            res.StartRelationResolving(data);
        }
        public CsvPKey(Type type, String csvKey, String target)
        {
            _csvKey = csvKey;
            _type   = type;
            IDictionary <String, FieldInfo> ids = AttributeWorker.GetInstance(target).GetPrimaryKeyFields(type);

            String[] split = Split(_csvKey, ";", "/;");

            foreach (String s in split)
            {
                String[]  tmp = Split(s, "=", "/=");
                FieldInfo f   = ids[tmp[0]];

                tmp[1] = tmp[1].Replace("/;", ";");
                tmp[1] = tmp[1].Replace("/=", "=");
                tmp[1] = tmp[1].Replace("//", "/");

                Object val;
                if (f.FieldType.IsGenericType && f.FieldType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Type t = Nullable.GetUnderlyingType(f.FieldType);
                    val = Convert.ChangeType(tmp[1], t, null);
                }
                else
                {
                    val = Convert.ChangeType(tmp[1], f.FieldType, null);
                }

                _fields.Add(f);
                _values.Add(val);
            }
        }
Beispiel #3
0
        internal bool Exists(String column, Object value, String table, ISerializerTransaction transaction)
        {
            AttributeWorker w = AttributeWorker.GetInstance(Target);
            StringBuilder   b = new StringBuilder();

            b.Append("SELECT ").Append(column).Append(" FROM ").Append(table).Append(" WHERE ").Append(column).Append("=");
            b.Append(SqlUtil.SqlConvert(value));

            DbCommand com = _connection.CreateCommand();

            transaction.Guard(com);
            com.CommandText = b.ToString();
            //Console.WriteLine(com.CommandText);


            DbDataReader r = com.ExecuteReader();

            if (r.Read())
            {
                r.Close();
                return(true);
            }
            r.Close();
            return(false);
        }
        internal void copyToSinkObject(ISerializableObject from, ISerializableObject to)
        {
            AttributeWorker w = AttributeWorker.GetInstance(sinkSerializer.Target);//Am besten durch Kopiervorgang ersetzen, der ohne Attributeworker auskommt.

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

            foreach (FieldInfo fi in fis)
            {
                if (w.IsAutoincID(fi))
                {
                    continue;                   //Kann man aus Lookup-Tabelle holen
                }
                if (!w.IsPersistentField(fi))
                {
                    continue;                          //wirklich  nötig?
                }
                //DirectSync wird im Moment nicht unterstützt
                if (AttributeWorker.IsRowGuid(fi))
                {
                    continue;                               //Kann man als Lookup-Tabelle definieren
                }
                Object val = fi.GetValue(from);
                fi.SetValue(to, val);
            }
        }
Beispiel #5
0
        public ISerializableObject CreateISerializableObject(Type type)
        {
            AttributeWorker w = AttributeWorker.GetInstance(Target);

            ISerializableObject iso = null;

            try
            {
                iso = (ISerializableObject)Activator.CreateInstance(type);
            }
            catch (InvalidCastException)
            {
                throw new SerializerException();
            }

            _objectPool.Register(iso);

            ResolverData <ISerializableObject> data = new ResolverData <ISerializableObject>();

            data.FieldsToResolve = AttributeWorker.RetrieveAllFields(iso.GetType());
            data.HandledItem     = iso;
            CreateHandler handler = new CreateHandler(this);
            RelationResolver <ISerializableObject> resolver = new RelationResolver <ISerializableObject>();

            resolver.Handler = handler;
            resolver.StartRelationResolving(data);

            return(iso);
        }
Beispiel #6
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);
            }
        }
Beispiel #7
0
        public void Activate(string target)
        {
            _target     = target;
            _connection = (DbConnection)CreateConnection();
            if (_connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken)
            {
                try
                {
                    _connection.Close();
                }
                catch (Exception ex)
                {
                }
            }
            _connection.Open();

            //Initialisierung des AttributeWorkers ...
            AttributeWorker.GetInstance(Target);
            VirtualKeyFactory vkf = new VirtualKeyFactory(AttributeWorker.GetInstance(target));

            foreach (Type t in RegisteredTypes)
            {
                vkf.RegisterType(t);
            }

            AttributeWorker.GetInstance(Target).AddVirtualKeyFactory(this, vkf);

            _objectPool = new SortedSerializableObjectPool(this, _registeredTypes);
            ((SortedSerializableObjectPool)_objectPool).Cleaner = new DefaultCleaner();
        }
Beispiel #8
0
        internal virtual String Delete(ISerializableObject iso)
        {
            AttributeWorker w   = AttributeWorker.GetInstance(Target);
            StringBuilder   sql = new StringBuilder();

            sql.Append("DELETE FROM ").Append(w.GetTableMapping(iso.GetType(), _praefix));
            sql.Append(" WHERE ");

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

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

                    if (val == null)
                    {
                        throw new SerializerException("Not deletable ...");
                    }

                    if (!start)
                    {
                        sql.Append(" AND ");
                    }

                    start = false;
                    sql.Append(col).Append("=").Append(SqlUtil.SqlConvert(val));
                }
            }

            return(sql.ToString());
        }
        public CsvPimaryKey(ISerializableObject iso, String target)
        {
            _type = iso.GetType();

            IDictionary <String, FieldInfo> ids = AttributeWorker.GetInstance(target).GetPrimaryKeyFields(iso.GetType());

            List <String> tmpList = new List <String>(ids.Count);
            StringBuilder csvKey  = new StringBuilder();
            String        tmp;

            foreach (String id in ids.Keys)
            {
                StringBuilder tmpBuilder = new StringBuilder();

                _fields.Add(ids[id]);
                _values.Add(ids[id].GetValue(iso));

                try
                {
                    //if (ids[id].GetValue(iso) is DateTime)
                    //{
                    //    DateTime dt = (DateTime)ids[id].GetValue(iso);
                    //    tmp = SqlUtil.SqlConvert(dt);
                    //}
                    //else
                    //    tmp = ids[id].GetValue(iso).ToString();
                    tmp = SqlUtil.SqlConvert(ids[id].GetValue(iso)).ToString();
                }
                catch (NullReferenceException ex)
                {
                    throw new KeyNotFoundException("", ex);
                }

                //Maskierung der Kommata
                tmp = tmp.Replace("/", "//");
                tmp = tmp.Replace(";", "/;");
                tmp = tmp.Replace("=", "/=");

                tmpBuilder.Append(id).Append("=");
                tmpBuilder.Append(tmp);
                tmpList.Add(tmpBuilder.ToString());
            }

            tmpList.Sort();

            bool initial = true;

            foreach (String s in tmpList)
            {
                if (!initial)
                {
                    csvKey.Append(";");
                }
                initial = false;
                csvKey.Append(s);
            }

            _csvKey = csvKey.ToString();
        }
        public override void UpdateOneToMany(Object iterator, ISerializableObject owner, Type storedType, Serializer serializer)
        {
            IVirtualKey vKey = AttributeWorker.GetInstance(serializer.Target).CreateVirtualKey(serializer, owner.GetType(), storedType);

            vKey.InitVirtualKeyBySource(owner);

            ((IDirectAccessIteratorConfiguration)iterator).Restriction = Restrictions.RestrictionFactory.SqlRestriction(storedType, vKey.ToSqlStringForward(serializer.Target));
        }
Beispiel #11
0
        public override String ToSqlString(Serializer.Serializer serializer, Type type, String target)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);

            IList <Type> path = w.ComputeVirtualKeyPath(serializer, _type, type); //ComputePath(type, _type, target);

            return(ComputeSqlJoin(serializer, path, _sqlString, target).ToString());
        }
Beispiel #12
0
        internal void AdjustKeyDownward(ISerializableObject iso)
        {
            AttributeWorker w    = AttributeWorker.GetInstance(_sinkSerializer.Target);
            IVirtualKey     vKey = w.CreateVirtualKey(_sinkSerializer, _synchronizedType, iso.GetType());

            vKey.InitVirtualKeyBySource(FetchObject(_sinkSerializer, _sinkGuid));
            vKey.ApplyVirtualKeyToTarget(iso);
        }
Beispiel #13
0
        internal virtual String SerializeInsert(ISerializableObject iso, string table)
        {
            AttributeWorker w      = AttributeWorker.GetInstance(Target);
            StringBuilder   sql    = new StringBuilder();
            StringBuilder   values = new StringBuilder();

            sql.Append("INSERT INTO ").Append(table);
            sql.Append(" (");

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

            bool start = true;

            foreach (FieldInfo fi in fis)
            {
                if (w.IsAutoincID(fi))
                {
                    continue;
                }

                try
                {
                    String col = w.GetColumnMapping(fi);
                    Object val = fi.GetValue(iso);



                    if (val == null && (w.IsID(fi) && !w.IsAutoincID(fi)))
                    {
                        throw new SerializerException("PRIMARY KEY FIELD NOT SET!!!!!!!!");
                    }

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

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

            sql.Append(") VALUES (");

            sql.Append(values);

            sql.Append(")");

            return(sql.ToString());
        }
Beispiel #14
0
        protected StringBuilder ComputeSqlJoin(Serializer.Serializer serializer, IList <Type> path, String restriction, String target)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);

            QueryGenerator ret = new QueryGenerator();

            ret.AddQueryComponent(new PlaceHolderComponent());

            //StringBuilder b = new StringBuilder();
            //b.Append(" @@@");

            for (int i = 0; i < path.Count - 1; i++)
            {
                Type from = path[i];
                Type to   = path[i + 1];
                //if (i > 0)
                //{
                //    b.Replace(" @@@", " WHERE  @@@");
                //}

                IVirtualKey    vKey = w.CreateVirtualKey(serializer, from, to);
                QueryGenerator gen1 = vKey.ToSqlRestriction(target, from, to, serializer);
                ret.ReplacePlaceHolders(gen1);
                //b.Replace(" @@@", vKey.ToSqlRestriction(target, from, to));

                if (i == path.Count - 2)
                {
                    QueryGenerator gen2 = new QueryGenerator();
                    gen2.AddQueryComponent(new SqlStringComponent(" WHERE "));
                    gen2.AddQueryComponent(new SqlStringComponent(restriction));
                    ret.ReplacePlaceHolders(gen2);

                    //b.Replace(" @@@", " WHERE  @@@");
                    //b.Replace(" @@@", restriction);
                }
            }

            if (path.Count == 1)
            {
                QueryGenerator gen3 = new QueryGenerator();
                gen3.AddQueryComponent(new SqlStringComponent(" WHERE "));
                gen3.AddQueryComponent(new SqlStringComponent(restriction));
                ret.ReplacePlaceHolders(gen3);

                //b.Replace(" @@@", " WHERE  @@@");
                //b.Replace(" @@@", restriction);
            }


            //Entferne das führende " WHERE "

            return(ret.ToSqlString());

            //return b.Remove(0,7);
        }
Beispiel #15
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);
        }
Beispiel #16
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);
        }
        public override Object LoadOneToMany(ISerializableObject owner, Type storedType, Serializer serializer)
        {
            Type genType = typeof(DirectAccesIteratorImpl <>).MakeGenericType(new Type[] { storedType });
            IDirectAccessIteratorConfiguration conf = (IDirectAccessIteratorConfiguration)Activator.CreateInstance(genType);

            IVirtualKey vKey = AttributeWorker.GetInstance(serializer.Target).CreateVirtualKey(serializer, owner.GetType(), storedType);

            vKey.InitVirtualKeyBySource(owner);

            conf.Restriction = Restrictions.RestrictionFactory.SqlRestriction(storedType, vKey.ToSqlStringForward(serializer.Target));
            conf.Serializer  = serializer;

            return(conf);
        }
Beispiel #18
0
        public void ConnectOneToOne(ISerializableObject source, ISerializableObject target)
        {
            IVirtualKey vKey = AttributeWorker.GetInstance(Target).CreateVirtualKey(this, source.GetType(), target.GetType());

            vKey.InitVirtualKeyBySource(source);
            vKey.ApplyVirtualKeyToTarget(target);

            try
            {
                vKey.TargetField.SetValue(target, source);
            }
            catch (Exception)
            {
            }
        }
Beispiel #19
0
        public override string ToSqlString(Serializer.Serializer serializer, Type type, string target)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);
            FieldInfo       f = w.RetrieveField(_type, _field, false);

            if (f == null)
            {
                throw new RestrictionException("Field '" + _field + "' not found in '" + _type.ToString() + "'");
            }

            if (!(_value is String) || f.FieldType != typeof(String))
            {
                throw new RestrictionException("LIKE operator can only applied to Strings");
            }
            return(base.ToSqlString(serializer, type, target));
        }
Beispiel #20
0
        public override String ToSqlString(Serializer.Serializer serializer, Type type, String target)
        {
            AttributeWorker w    = AttributeWorker.GetInstance(target);
            IList <Type>    path = w.ComputeVirtualKeyPath(serializer, _type, type); //ComputePath(_type, type, target);

            StringBuilder b = new StringBuilder();
            FieldInfo     f = w.RetrieveField(_type, _field, false);

            if (f == null)
            {
                throw new RestrictionException("Field '" + _field + "' not found in '" + _type.ToString() + "'");
            }
            b.Append(w.GetColumnMapping(f));
            b.Append(_comparer).Append(SqlUtil.SqlConvert(_value));

            String tmp = ComputeSqlJoin(serializer, path, b.ToString(), target).ToString();

            return(tmp);
        }
        public CsvPKey(Type type, DbDataReader reader, String target)
        {
            _type = type;

            IDictionary <String, FieldInfo> ids = AttributeWorker.GetInstance(target).GetPrimaryKeyFields(type);
            List <String> tmpList = new List <String>(ids.Count);
            StringBuilder csvKey  = new StringBuilder();

            foreach (String id in ids.Keys)
            {
                StringBuilder tmpBuilder = new StringBuilder();

                _fields.Add(ids[id]);
                _values.Add(reader[id]);

                String tmp = reader[id].ToString();

                //Maskierung der Kommata
                tmp = tmp.Replace("/", "//");
                tmp = tmp.Replace(";", "/;");
                tmp = tmp.Replace("=", "/=");

                tmpBuilder.Append(id).Append("=");
                tmpBuilder.Append(tmp);
                tmpList.Add(tmpBuilder.ToString());
            }

            tmpList.Sort();

            bool initial = true;

            foreach (String s in tmpList)
            {
                if (!initial)
                {
                    csvKey.Append(";");
                }
                initial = false;
                csvKey.Append(s);
            }

            _csvKey = csvKey.ToString();
        }
Beispiel #22
0
        internal static void PlainCopy(Serializer context, ISerializableObject from, ISerializableObject to, FieldLock fieldLock, bool forceForeignKeyCopy)
        {
            AttributeWorker w = AttributeWorker.GetInstance(context.Target);

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

            foreach (FieldInfo fi in fis)
            {
                if (fieldLock.IsLocked(fi))
                {
                    continue;
                }
                if (AttributeWorker.IsRelationField(fi))
                {
                    continue;
                }
                if (w.IsAutoincID(fi))
                {
                    continue;
                }
                if (!w.IsPersistentField(fi))
                {
                    continue;
                }
                if (AttributeWorker.IsRowGuid(fi))
                {
                    continue;
                }
                if (w.IsForeignKey(context, fi) && !forceForeignKeyCopy)
                {
                    DirectSyncAttribute a = (DirectSyncAttribute)Attribute.GetCustomAttribute(fi, typeof(DirectSyncAttribute));

                    if (a == null)
                    {
                        continue;
                    }
                }


                Object val = fi.GetValue(from);
                fi.SetValue(to, val);
            }
        }
Beispiel #23
0
        public void HandleOneToOneReflexive(ISerializableObject handledItem, OneToOneDefAttribute attr, FieldInfo field)
        {
            Type        type = field.FieldType;
            IVirtualKey vKey = AttributeWorker.GetInstance(_serializer.Target).CreateVirtualKey(_serializer, type, handledItem.GetType());

            try
            {
                vKey.InitVirtualKeyBySource(handledItem);
            }
            catch (InvalidOperationException)
            {
                field.SetValue(handledItem, null);
                return;
            }
            IRestriction        r   = Restrictions.RestrictionFactory.SqlRestriction(type, vKey.ToSqlStringForward(_serializer.Target));
            ISerializableObject tmp = _serializer.Connector.Load(type, r);

            field.SetValue(handledItem, tmp);
        }
Beispiel #24
0
        internal void Save(ISerializableObject iso, ISerializerTransaction transaction)
        {
            AttributeWorker w = AttributeWorker.GetInstance(Target);

            if (!IsManaged(iso))
            {
//                Console.WriteLine("SERIALIZE");
                _objectPool.InsertObject(iso, _connection, transaction);

                ResolverData <ISerializableObject> rData = new ResolverData <ISerializableObject>();
                rData.HandledItem     = iso;
                rData.FieldsToResolve = AttributeWorker.RetrieveAllFields(iso.GetType());

                LoadHandler handler = new LoadHandler(this);
                RelationResolver <ISerializableObject> res = new RelationResolver <ISerializableObject>();
                res.Handler = handler;
                res.StartRelationResolving(rData);
            }
            else
            {
//                Console.WriteLine("UPDATE");
                UpdateStates state = _objectPool.UpdateObject(iso, _connection, transaction);

                if (state == UpdateStates.NO_ROWS_AFFECTED)
                {
                    throw new SerializerException("entry could not be updated ...");
                }
                else if (state == UpdateStates.PRIMARYKEY_MODIFIED)
                {
                    UpdateEventArgs args = new UpdateEventArgs(state);
                    OnUpdateEvent(args);
                }

                ResolverData <ISerializableObject> data = new ResolverData <ISerializableObject>();
                data.HandledItem     = iso;
                data.FieldsToResolve = AttributeWorker.RetrieveAllFields(iso.GetType());

                RelationResolver <ISerializableObject> res = new RelationResolver <ISerializableObject>();
                res.Handler = new UpdateHandler(this);
                res.StartRelationResolving(data);
            }
        }
Beispiel #25
0
        // ////////////////////////////////////////////



        private ISerializableObject Deserialize(Type type, DbDataReader reader)
        {
            ISerializableObject data = (ISerializableObject)Activator.CreateInstance(type);

            FieldInfo[] fis = AttributeWorker.RetrieveAllFields(type);
            foreach (FieldInfo fi in fis)
            {
                if (AttributeWorker.GetInstance(Target).IsPersistentField(fi))
                {
                    Object o = null;
                    Type   t = null;
                    try
                    {
                        o = reader[AttributeWorker.GetInstance(Target).GetColumnMapping(fi)];
                        t = o.GetType();
                        if (o is DBNull)
                        {
                            o = null;
                        }
                        fi.SetValue(data, o);
                    }
                    catch (ArgumentException ex)
                    {
                        String s          = o.GetType().FullName;
                        Type   targetType = FieldMapping.Mapping[o.GetType().FullName];
                        if (targetType.Equals(typeof(String)))
                        {
                            fi.SetValue(data, o.ToString());
                        }
                    }
                    catch (IndexOutOfRangeException)
                    {
                        System.Text.StringBuilder msg = new System.Text.StringBuilder();
                        msg.Append("column: ").Append(AttributeWorker.GetInstance(Target).GetColumnMapping(fi)).Append(" not found\n");
                        msg.Append("in table: " + AttributeWorker.GetInstance(Target).GetTableMapping(data.GetType(), _owner.Praefix)).Append("!");
                        throw new SerializerException(msg.ToString());
                    }
                }
            }
            return(data);
        }
Beispiel #26
0
        internal static void ForeignKeyCopy(Serializer context, ISerializableObject from, ISerializableObject to, FieldLock fieldLock)
        {
            AttributeWorker w = AttributeWorker.GetInstance(context.Target);

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

            foreach (FieldInfo fi in fis)
            {
                if (fieldLock.IsLocked(fi))
                {
                    continue;
                }
                if (!w.IsForeignKey(context, fi))
                {
                    continue;
                }

                Object val = fi.GetValue(from);
                fi.SetValue(to, val);
            }
        }
Beispiel #27
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);
        }
Beispiel #28
0
        internal static String ComputeHashCode(String target, ISerializableObject iso)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);

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

            StringBuilder b = new StringBuilder();

            foreach (FieldInfo fi in fis)
            {
                if (w.IsPersistentField(fi) && !w.IsID(fi))
                {
                    Object val = fi.GetValue(iso);
                    b.Append(val);
                }
            }

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Byte[] tmp = encoding.GetBytes(b.ToString());


            return(BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(tmp)).Replace("-", "").ToLower());
        }
Beispiel #29
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());
        }
Beispiel #30
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!");
                }
            }
        }