Exemple #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Table{TEntity, TPrimaryKey}"/>
        ///     class.
        /// </summary>
        /// <param name="database"> The database. </param>
        /// <param name="primaryKey"> The primary key. </param>
        /// <param name="identitySpecification"> The identity specification. </param>
        public Table(
            IDatabase database,
            IKeyInfo <TEntity, TPrimaryKey> primaryKey,
            IdentitySpecification <TEntity> identitySpecification)

            : base(database, false)
        {
            this.VerifyType();

            this.id = Interlocked.Increment(ref counter);

            this.indexes       = new List <IIndex <TEntity> >();
            this.constraints   = new ConstraintCollection <TEntity>();
            this.entityService = database
                                 .DatabaseEngine
                                 .ServiceProvider
                                 .GetService <IEntityService>();

            this.primaryKeyIndex =
                this.CreateUniqueIndex(new DictionaryIndexFactory(), primaryKey);

            this.RegisterTimestampConstraints();

            if (identitySpecification != null)
            {
                this.identityField = new IdentityField <TEntity>(identitySpecification);
            }
        }
        /// <summary>
        /// Searches a unique value. the field should be unique
        /// </summary>
        /// <typeparam name="TField"></typeparam>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public RowId Find <TField>(IField <TRecord, TField> field, TField value)
        {
            IUniqueIndex <TField> index = (IUniqueIndex <TField>) this.FindIndex(field, true);

            if (index != null)
            {
                return(index.FindUnique(value, this.StoreSnapshot.Version));
            }
            throw new InvalidOperationException(Properties.Resources.ErrorNoIndex(this.Name, field.Name));
        }
Exemple #3
0
 public void Setup()
 {
     _items = new ObservableList <TestObject>(new []
     {
         new TestObject(1, "one"),
         new TestObject(2, "two")
     });
     _itemsByName = _items.BuildUniqueIndex(v => v.Name);
     _itemsById   = _items.BuildUniqueIndex(v => v.Id);
 }
 public ForeignKey(
     string name, IUniqueIndex <TField> primaryKey, TableSnapshot <TRecord> childTable, IField <TRecord, TField> childColumn, ForeignKeyAction action, bool allowsDefault
     )
 {
     this.Name          = name;
     this.primaryKey    = primaryKey;
     this.childTable    = childTable;
     this.childColumn   = childColumn;
     this.action        = action;
     this.allowsDefault = allowsDefault;
 }
        /// <summary>
        ///     Creates a relation between two tables.
        /// </summary>
        /// <typeparam name="TPrimary">
        ///     The type of the entities of the primary table.
        /// </typeparam>
        /// <typeparam name="TPrimaryKey">
        ///     The type of the primary key of the entities of the primary table.
        /// </typeparam>
        /// <typeparam name="TForeign">
        ///     The type of the entities of the foreign table.
        /// </typeparam>
        /// <typeparam name="TForeignKey">
        ///     Type type of the foreign key of the foreign table.
        /// </typeparam>
        /// <param name="primaryIndex">
        ///     An IIndex that specifies the primary key.
        /// </param>
        /// <param name="foreignIndex">
        ///     An IIndex that specifies the foreign key.
        /// </param>
        /// <param name="convertForeignToPrimary">
        ///     A function to convert a foreign key to the corresponding primary key.
        /// </param>
        /// <param name="convertPrimaryToForeign">
        ///     A function to convert a primary key to the corresponding foreign key.
        /// </param>
        /// <returns>
        ///     The relation.
        /// </returns>
        public Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey>(
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            Func <TForeignKey, TPrimaryKey> convertForeignToPrimary,
            Func <TPrimaryKey, TForeignKey> convertPrimaryToForeign,
            RelationOptions relationOptions
            )
            where TPrimary : class
            where TForeign : class
        {
            if (primaryIndex == null)
            {
                throw new ArgumentNullException("primaryIndex");
            }

            if (foreignIndex == null)
            {
                throw new ArgumentNullException("foreignIndex");
            }

            if (convertForeignToPrimary == null)
            {
                throw new ArgumentNullException("convertForeignToPrimary");
            }

            if (convertPrimaryToForeign == null)
            {
                throw new ArgumentNullException("convertPrimaryToForeign");
            }

            if (relationOptions == null)
            {
                // Use default relation options
                relationOptions = new RelationOptions();
            }

            var result = new Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey>(
                primaryIndex,
                foreignIndex,
                convertForeignToPrimary,
                convertPrimaryToForeign,
                relationOptions);

            IRelationInternal relation = result;

            relation.ValidateAll();

            this.relationMapping[relation.PrimaryTable].Referring.Add(relation);
            this.relationMapping[relation.ForeignTable].Referred.Add(relation);
            this.relations.Add(relation);

            return(result);
        }
Exemple #6
0
 /// <summary>
 ///     Creates a relation between two tables.
 /// </summary>
 /// <typeparam name="TPrimary">
 ///     The type of the entities of the primary table.
 /// </typeparam>
 /// <typeparam name="TPrimaryKey">
 ///     The type of the primary key of the entities of the primary table.
 /// </typeparam>
 /// <typeparam name="TForeign">
 ///     The type of the entities of the foreign table.
 /// </typeparam>
 /// <typeparam name="TForeignKey">
 ///     Type type of the foreign key of the foreign table.
 /// </typeparam>
 /// <param name="primaryIndex">
 ///     An IIndex that specifies the primary key.
 /// </param>
 /// <param name="foreignIndex">
 ///     An IIndex that specifies the foreign key.
 /// </param>
 /// <param name="convertForeignToPrimary">
 ///     A function to convert a foreign key to the corresponding primary key.
 /// </param>
 /// <param name="convertPrimaryToForeign">
 ///     A function to convert a primary key to the corresponding foreign key.
 /// </param>
 /// <returns>
 ///     The relation.
 /// </returns>
 public Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign,
                                                                                TForeignKey>(
     IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
     IIndex <TForeign, TForeignKey> foreignIndex,
     Func <TForeignKey, TPrimaryKey> convertForeignToPrimary,
     Func <TPrimaryKey, TForeignKey> convertPrimaryToForeign
     )
     where TPrimary : class
     where TForeign : class
 {
     return(this.CreateRelation <TPrimary, TPrimaryKey, TForeign,
                                 TForeignKey>(primaryIndex, foreignIndex, convertForeignToPrimary, convertPrimaryToForeign, null));
 }
Exemple #7
0
        public static Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey>(
            this TableCollection tableCollection,
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            params IRelationContraint[] constraints)

            where TPrimary : class
            where TForeign : class
        {
            return(tableCollection.CreateRelation(
                       primaryIndex,
                       foreignIndex,
                       new RelationOptions(),
                       constraints));
        }
Exemple #8
0
        public static Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey, TConstraint1>(
            this TableCollection tableCollection,
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            Expression <Func <TPrimary, TConstraint1> > constraint1P, Expression <Func <TForeign, TConstraint1> > constraint1F,
            RelationOptions options = null)

            where TPrimary : class
            where TForeign : class
        {
            return(CreateRelation(
                       tableCollection,
                       primaryIndex,
                       foreignIndex,
                       options,
                       new RelationConstraint <TPrimary, TForeign, TConstraint1>(constraint1P, constraint1F)));
        }
Exemple #9
0
        internal Relation(
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            Func <TForeignKey, TPrimaryKey> foreignToPrimary,
            Func <TPrimaryKey, TForeignKey> primaryToForeign,
            RelationOptions options)
        {
            this.isEnabled = true;

            this.primaryIndex = primaryIndex;
            this.foreignIndex = foreignIndex;

            this.convertForeignToPrimary = foreignToPrimary;
            this.convertPrimaryToForeign = primaryToForeign;

            this.options = options;
        }
Exemple #10
0
        public static Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey>(
            this TableCollection tableCollection,
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            RelationOptions options,
            params IRelationContraint[] constraints)

            where TPrimary : class
            where TForeign : class
        {
            Func <TForeignKey, TPrimaryKey> convertForeignToPrimary =
                RelationKeyConverterFactory.CreateForeignToPrimaryConverter(
                    primaryIndex.KeyInfo,
                    foreignIndex.KeyInfo,
                    constraints);

            Func <TPrimaryKey, TForeignKey> convertPrimaryToForeign =
                RelationKeyConverterFactory.CreatePrimaryToForeignConverter(
                    primaryIndex.KeyInfo,
                    foreignIndex.KeyInfo,
                    constraints);

            return(tableCollection.CreateRelation(primaryIndex, foreignIndex, convertForeignToPrimary, convertPrimaryToForeign, options));
        }
 public IReadOnlyDictionary <TKey, T> GetIndexDictionary <TKey>(IUniqueIndex <TKey> index)
 {
     throw new NotImplementedException();
 }