예제 #1
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
        }
예제 #2
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);
        }
예제 #3
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);
            }
예제 #4
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 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();
            }
        }
예제 #6
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);
            }
        }