Exemplo n.º 1
0
        public static IStoredField GetDeclaredStoredFieldInHeirarchy(IStoredClass aClass, string attribute)
        {
            try
            {
                IStoredField sField = null;
                if (aClass == null)
                {
                    return(null);
                }

                while (aClass != null)
                {
                    sField = aClass.StoredField(attribute, null);
                    if (sField != null)
                    {
                        break;
                    }
                    aClass = aClass.GetParentStoredClass();
                }
                return(sField);
            }
            catch (Exception e)
            {
                LoggingHelper.HandleException(e);
            }
            return(null);
        }
Exemplo n.º 2
0
        private static void ChangeType()
        {
            StoreInDB(new Person(), new Person("John"));


            // #example: copying the data from the old field type to the new one
            using (IObjectContainer container = Db4oEmbedded.OpenFile("database.db4o"))
            {
                // first get all objects which should be updated
                IList <Person> persons = container.Query <Person>();
                foreach (Person person in persons)
                {
                    // get the database-metadata about this object-type
                    IStoredClass dbClass = container.Ext().StoredClass(person);
                    // get the old field which was an int-type
                    IStoredField oldField = dbClass.StoredField("id", typeof(int));
                    if (null != oldField)
                    {
                        // Access the old data and copy it to the new field!
                        object oldValue = oldField.Get(person);
                        if (null != oldValue)
                        {
                            person.id = new Identity((int)oldValue);
                            container.Store(person);
                        }
                    }
                }
            }

            // #end example
        }
Exemplo n.º 3
0
        private void AssertIndexed(Type clazz, bool expected)
        {
            IStoredClass storedClass = FileSession().StoredClass(clazz);
            IStoredField storedField = storedClass.StoredField("_id", typeof(int));

            Assert.AreEqual(expected, storedField.HasIndex());
        }
        private void AssertStoredType(IStoredClass clazz, string fieldName, Type type)
        {
            IStoredField field = clazz.StoredField(fieldName, null);

            Assert.AreEqual(type.FullName, CrossPlatformServices.SimpleName(field.GetStoredType
                                                                                ().GetName()));
        }
        public virtual void Test()
        {
            for (int i = 0; i < ObjectCount; i++)
            {
                Store(new CommitAfterDroppedFieldIndexTestCase.Item(1));
            }
            IStoredField storedField = FileSession().StoredClass(typeof(CommitAfterDroppedFieldIndexTestCase.Item
                                                                        )).StoredField("_id", null);

            storedField.CreateIndex();
            FileSession().Commit();
            IExtObjectContainer session  = OpenNewSession();
            IObjectSet          allItems = session.Query(typeof(CommitAfterDroppedFieldIndexTestCase.Item
                                                                ));

            for (IEnumerator itemIter = allItems.GetEnumerator(); itemIter.MoveNext();)
            {
                CommitAfterDroppedFieldIndexTestCase.Item item = ((CommitAfterDroppedFieldIndexTestCase.Item
                                                                   )itemIter.Current);
                item._id++;
                session.Store(item);
            }
            // Making sure all storing has been processed.
            session.SetSemaphore("anySemaphore", 0);
            storedField.DropIndex();
            session.Commit();
            storedField.CreateIndex();
        }
Exemplo n.º 6
0
        public virtual void TestTraverseValues()
        {
            IStoredField     field            = StoredField();
            ExpectingVisitor expectingVisitor = new ExpectingVisitor(IntArrays4.ToObjectArray
                                                                         (Foos));

            field.TraverseValues(expectingVisitor);
            expectingVisitor.AssertExpectations();
        }
Exemplo n.º 7
0
        private static void TraverseIndex(IObjectContainer container)
        {
            // #example: Traverse index
            IStoredField storedField = container.Ext()
                                       .StoredClass(typeof(Item)).StoredField("data", typeof(int));

            storedField.TraverseValues(new IndexVisitor());
            // #end example
        }
Exemplo n.º 8
0
        public virtual void TestDropIndex()
        {
            IStoredField field = StoredField();

            field.CreateIndex();
            AssertQuery();
            field.DropIndex();
            Assert.IsFalse(field.HasIndex());
            AssertQuery();
        }
Exemplo n.º 9
0
        public virtual void TestCreateIndexAtRuntime()
        {
            IStoredField field = StoredField();

            Assert.IsFalse(field.HasIndex());
            field.CreateIndex();
            Assert.IsTrue(field.HasIndex());
            AssertQuery();
            field.CreateIndex();
        }
Exemplo n.º 10
0
 public virtual IStoredField[] GetStoredFields()
 {
     IStoredField[] fieldMetadata = _classMetadata.GetStoredFields();
     IStoredField[] storedFields  = new IStoredField[fieldMetadata.Length];
     for (int i = 0; i < fieldMetadata.Length; i++)
     {
         storedFields[i] = new StoredFieldImpl(_transaction, (FieldMetadata)fieldMetadata[
                                                   i]);
     }
     return(storedFields);
 }
Exemplo n.º 11
0
		public virtual IStoredField[] GetStoredFields()
		{
			IStoredField[] fieldMetadata = _classMetadata.GetStoredFields();
			IStoredField[] storedFields = new IStoredField[fieldMetadata.Length];
			for (int i = 0; i < fieldMetadata.Length; i++)
			{
				storedFields[i] = new StoredFieldImpl(_transaction, (FieldMetadata)fieldMetadata[
					i]);
			}
			return storedFields;
		}
Exemplo n.º 12
0
        public Db4oField(IStoredField storedField)
        {
            this.storedField = storedField;
            internalName = storedField.GetName();
            name = AutomaticPropertyUtils.TryParseName(internalName);
            IsIndexed = storedField.HasIndex();

            IReflectClass type = storedField.GetStoredType();
            if(type!=null)
                DataType = type.ToString();
        }
Exemplo n.º 13
0
            public void RunWith(IObjectContainer db)
            {
                IStoredClass storedClass = db.Ext().StoredClass(targetClazz);
                IStoredField storedField = storedClass.StoredField(fieldName, fieldType);
                IObjectSet   result      = db.Query(targetClazz);

                Assert.AreEqual(1, result.Count);
                object obj   = result.Next();
                object value = (object)storedField.Get(obj);

                Assert.AreEqual(fieldValue, value);
            }
Exemplo n.º 14
0
        public virtual void TestTraverseValues()
        {
            IStoredField field = StoredField();

            StringFieldIndexTestCase.ExpectedVisitor visitor = new StringFieldIndexTestCase.ExpectedVisitor
                                                                   (2);
            field.TraverseValues(visitor);
            for (int i = 0; i < _fooValues.Length; i++)
            {
                Assert.AreEqual(_fooValues[i], visitor.GetValues()[i]);
            }
        }
Exemplo n.º 15
0
        public static void Main(string[] args)
        {
            using (IObjectContainer container = Db4oEmbedded.OpenFile("database.db4o"))
            {
                container.Store(new Person("Johnson", "Roman", 42));
                container.Store(new Person("Miller", "John", 21));

                // #example: All stored classes
                // Get the information about all stored classes.
                IStoredClass[] classesInDB = container.Ext().StoredClasses();
                foreach (IStoredClass storedClass in classesInDB)
                {
                    Console.WriteLine(storedClass.GetName());
                }

                // Information for a certain class
                IStoredClass metaInfo = container.Ext().StoredClass(typeof(Person));
                // #end example

                // #example: Accessing stored fields
                IStoredClass metaInfoForPerson = container.Ext().StoredClass(typeof(Person));
                // Access all existing fields
                foreach (IStoredField field in metaInfoForPerson.GetStoredFields())
                {
                    Console.WriteLine("Field: " + field.GetName());
                }
                // Accessing the field 'name' of any type.
                IStoredField nameField = metaInfoForPerson.StoredField("name", null);
                // Accessing the string field 'name'. Important if this field had another time in previous
                // versions of the class model
                IStoredField ageField = metaInfoForPerson.StoredField("age", typeof(int));

                // Check if the field is indexed
                bool isAgeFieldIndexed = ageField.HasIndex();

                // Get the type of the field
                string fieldType = ageField.GetStoredType().GetName();
                // #end example

                // #example: Access via meta data
                IStoredClass metaForPerson = container.Ext().StoredClass(typeof(Person));
                IStoredField metaNameField = metaForPerson.StoredField("name", null);

                IList <Person> persons = container.Query <Person>();
                foreach (Person person in persons)
                {
                    string name = (string)metaNameField.Get(person);
                    Console.WriteLine("Name is " + name);
                }
                // #end example
            }
        }
			private static void ReindexDateTimeField(IStoredField field)
			{
				IReflectClass claxx = field.GetStoredType();
				if (claxx == null)
				{
					return;
				}

				Type t = NetReflector.ToNative(claxx);
				if (t == typeof(DateTime) || t == typeof(DateTime?))
				{
					field.DropIndex();
					field.CreateIndex();
				}
			}
Exemplo n.º 17
0
        public virtual void TestGetStoredFields()
        {
            AssertStoredField(typeof(StoredClassTestCase.Item), FieldName, ItemName, typeof(string
                                                                                            ), true, false);
            AssertStoredField(typeof(StoredClassTestCase.ItemParent), "_array", null, typeof(
                                  string), false, true);
            IStoredClass itemStoredClass  = ItemStoredClass();
            IStoredField storedField      = itemStoredClass.StoredField(FieldName, null);
            IStoredField sameStoredField  = itemStoredClass.GetStoredFields()[0];
            IStoredField otherStoredField = StoredClass(typeof(StoredClassTestCase.ItemParent
                                                               )).GetStoredFields()[0];

            Assert.EqualsAndHashcode(storedField, sameStoredField, otherStoredField);
            Assert.IsNull(itemStoredClass.StoredField(string.Empty, null));
        }
Exemplo n.º 18
0
        private static void ReindexDateTimeField(IStoredField field)
        {
            var claxx = field.GetStoredType();

            if (claxx == null)
            {
                return;
            }

            var t = NetReflector.ToNative(claxx);

            if (t == typeof(T) || t == typeof(T?))
            {
                field.DropIndex();
                field.CreateIndex();
            }
        }
Exemplo n.º 19
0
        public virtual void TestStoredFieldIsolation()
        {
            EmbeddedClientObjectContainerTestCase.Item storedItem = StoreItemToClient1AndCommit
                                                                        ();
            storedItem._name = ChangedName;
            _client1.Store(storedItem);
            EmbeddedClientObjectContainerTestCase.Item retrievedItem = RetrieveItemFromClient2
                                                                           ();
            IStoredClass storedClass = _client2.StoredClass(typeof(EmbeddedClientObjectContainerTestCase.Item
                                                                   ));
            IStoredField storedField   = storedClass.StoredField(FieldName, null);
            object       retrievedName = storedField.Get(retrievedItem);

            Assert.AreEqual(OriginalName, retrievedName);
            _client1.Commit();
            retrievedName = storedField.Get(retrievedItem);
            Assert.AreEqual(ChangedName, retrievedName);
        }
        private void AssertItemStoredField(object expectedValue)
        {
            IObjectContainer db = OpenContainer();

            try
            {
                IObjectSet objectSet = db.Query(typeof(FieldsToTypeHandlerMigrationTestCase.Item)
                                                );
                Assert.AreEqual(1, objectSet.Count);
                FieldsToTypeHandlerMigrationTestCase.Item item = (FieldsToTypeHandlerMigrationTestCase.Item
                                                                  )objectSet.Next();
                IStoredField storedField = db.Ext().StoredClass(typeof(FieldsToTypeHandlerMigrationTestCase.Item
                                                                       )).StoredField("_id", null);
                object actualValue = storedField.Get(item);
                Assert.AreEqual(expectedValue, actualValue);
            }
            finally
            {
                db.Close();
            }
        }
Exemplo n.º 21
0
 private void AssertHasIndexInfo(IObjectContainer db)
 {
     IStoredClass[] scArray = db.Ext().StoredClasses();
     for (int scIndex = 0; scIndex < scArray.Length; ++scIndex)
     {
         IStoredClass sc = scArray[scIndex];
         if (!sc.GetName().Equals(typeof(KnownClassesIndexTestCase.WithIndex).FullName))
         {
             continue;
         }
         IStoredField[] sfArray = sc.GetStoredFields();
         for (int sfIndex = 0; sfIndex < sfArray.Length; ++sfIndex)
         {
             IStoredField sf = sfArray[sfIndex];
             if (sf.HasIndex())
             {
                 return;
             }
         }
         Assert.Fail("no index found");
     }
 }
Exemplo n.º 22
0
        private void AssertStoredField(Type objectClass, string fieldName, object expectedFieldValue
                                       , Type expectedFieldType, bool hasIndex, bool isArray)
        {
            IStoredClass storedClass = StoredClass(objectClass);

            IStoredField[] storedFields = storedClass.GetStoredFields();
            Assert.AreEqual(1, storedFields.Length);
            IStoredField storedField = storedFields[0];

            Assert.AreEqual(fieldName, storedField.GetName());
            IStoredField storedFieldByName = storedClass.StoredField(fieldName, expectedFieldType
                                                                     );

            Assert.AreEqual(storedField, storedFieldByName);
            object item = RetrieveOnlyInstance(objectClass);

            Assert.AreEqual(expectedFieldValue, storedField.Get(item));
            IReflectClass fieldType = storedField.GetStoredType();

            Assert.AreEqual(Reflector().ForClass(expectedFieldType), fieldType);
            Assert.AreEqual(isArray, storedField.IsArray());
            if (IsMultiSession())
            {
                return;
            }
            Assert.AreEqual(hasIndex, storedField.HasIndex());
            // FIXME: test rename
            if (!hasIndex)
            {
                Assert.Expect(typeof(Exception), new _ICodeBlock_113(storedField));
            }
            else
            {
                IntByRef count = new IntByRef();
                storedField.TraverseValues(new _IVisitor4_123(count, expectedFieldValue));
                Assert.AreEqual(1, count.value);
            }
        }
Exemplo n.º 23
0
        public bool IsIndexed()
        {
            try
            {
                IStoredClass storedClass = objectContainer.Ext().StoredClass(m_classname);
                if (null == storedClass)
                {
                    return(false);
                }

                IStoredField field = DataLayerCommon.GetDeclaredStoredFieldInHeirarchy(storedClass, m_fieldname);
                if (field == null)
                {
                    return(false);
                }

                return(field.HasIndex());
            }
            catch (Exception oEx)
            {
                LoggingHelper.HandleException(oEx);
                return(false);
            }
        }
Exemplo n.º 24
0
 private static IndexingState HasIndex(IStoredField sf)
 {
     return sf.HasIndex() ? IndexingState.Indexed : IndexingState.NotIndexed;
 }
Exemplo n.º 25
0
 private static IndexingState HasIndex(IStoredField sf)
 {
     return(sf.HasIndex() ? IndexingState.Indexed : IndexingState.NotIndexed);
 }
Exemplo n.º 26
0
 public _ICodeBlock_113(IStoredField storedField)
 {
     this.storedField = storedField;
 }
Exemplo n.º 27
0
 public virtual IStoredField[] GetStoredFields()
 {
     lock (Lock())
     {
         if (_aspects == null)
         {
             return new IStoredField[0];
         }
         var storedFields = new Collection4();
         TraverseDeclaredFields(new _IProcedure4_1039(storedFields));
         var fields = new IStoredField[storedFields.Size()];
         storedFields.ToArray(fields);
         return fields;
     }
 }
Exemplo n.º 28
0
			public _ICodeBlock_113(IStoredField storedField)
			{
				this.storedField = storedField;
			}