public void AddKey(IKeyInfo key, bool isUnique)
 {
     if (isUnique)
     {
         this.uniqueKeys.Add(key);
     }
     else
     {
         this.otherKeys.Add(key);
     }
 }
예제 #2
0
        private static IKeyInfoHelper GetKeyInfoHelper(IKeyInfo keyInfo)
        {
            var provider = keyInfo as IKeyInfoHelperProvider;

            if (provider == null)
            {
                return(null);
            }

            return(provider.KeyInfoHelper);
        }
예제 #3
0
        internal Index(
            ITable <TEntity> table,
            IKeyInfo <TEntity, TKey> keyInfo,
            IDataStructure <TKey, TEntity> dataStructure)

            : base(table, keyInfo)
        {
            this.dataStructure = dataStructure;

            this.Rebuild();
        }
예제 #4
0
        /// <summary>
        ///     Creates a new unique index.
        /// </summary>
        /// <typeparam name="TUniqueKey">
        ///     The type of the unqiue index key.
        /// </typeparam>
        /// <param name="indexFactory">
        ///     The index factory.
        /// </param>
        /// <param name="keyInfo">
        ///     The definition of the index key
        /// </param>
        /// <returns> The unique index. </returns>
        public IUniqueIndex <TEntity, TUniqueKey> CreateUniqueIndex <TUniqueKey>(
            IIndexFactory indexFactory,
            IKeyInfo <TEntity, TUniqueKey> keyInfo)
        {
            var index = indexFactory.CreateUniqueIndex(this, keyInfo);

            this.indexes.Add(index);
            this.OnIndexChanged();

            return(index);
        }
 public Table <TEntity, TPrimaryKey> CreateTable <TEntity, TPrimaryKey>(
     IKeyInfo <TEntity, TPrimaryKey> primaryKey,
     IdentitySpecification <TEntity> identitySpecification,
     IDatabase database, object tableInfo)
     where TEntity : class
 {
     return(new ExtendedTable <TEntity, TPrimaryKey>(
                database,
                primaryKey,
                identitySpecification, tableInfo));
 }
예제 #6
0
 public void Execute(IGameEngine gameEngine, IKeyInfo commandKey)
 {
     foreach (var command in _commands)
     {
         if (!command.HandlesInput(commandKey))
         {
             continue;
         }
         command.Execute(gameEngine, commandKey);
     }
 }
        public void PrimitiveKeyInfoService_CreateKey()
        {
            PrimitiveKeyInfoService service = new PrimitiveKeyInfoService();

            IKeyInfo <BinaryEntity, Binary> key = null;

            bool res = service.TryCreateKeyInfo <BinaryEntity, Binary>(x => x.Binary, out key);

            Assert.IsTrue(res);
            Assert.IsNotNull(key);
            Assert.AreEqual(1, key.EntityKeyMembers.Length);
            Assert.AreEqual(typeof(Binary), key.KeyType);
        }
예제 #8
0
        public bool TryCreateKeyInfo <TEntity, TKey>(
            Expression <Func <TEntity, TKey> > keySelector,
            out IKeyInfo <TEntity, TKey> result) where TEntity : class
        {
            if (!ValidateType(typeof(TKey)))
            {
                result = null;
                return(false);
            }

            IKeyInfoHelper helper = PrimitiveKeyInfo <TEntity, TKey> .KeyInfoHelper;


            if (!helper.TryParseKeySelectorExpression(keySelector.Body, true, out MemberInfo[] members))
예제 #9
0
        public Table <TEntity, TPrimaryKey> CreateTable <TEntity, TPrimaryKey>(
            IKeyInfo <TEntity, TPrimaryKey> primaryKey,
            IdentitySpecification <TEntity> identitySpecification,
            IDatabase database)
            where TEntity : class
        {
            Table <TEntity, TPrimaryKey> table =
                new DefaultTable <TEntity, TPrimaryKey>(
                    database,
                    primaryKey,
                    identitySpecification);

            return(table);
        }
        public EncryptionService(IKeyInfo keyInfo, IOutputPrinter consolePrinter)
        {
            _consolePrinter = consolePrinter;
            _keyinfo        = keyInfo;

            //Create a key if necessary
            if (!File.Exists(_keyinfo.KeyPath))
            {
                PublishKey();
            }

            //Load the key
            LoadKey();
        }
 public static IIndex CreateIndex(ITable table, IKeyInfo key, bool unique)
 {
     return(typeof(DatabaseReflectionHelper.WrapperMethods)
            .GetMethod("CreateIndex")
            .MakeGenericMethod(
                table.EntityType,
                table.PrimaryKeyIndex.KeyInfo.KeyType,
                key.KeyType)
            .Invoke(null, new object[] {
         table,
         key,
         unique
     }) as IIndex);
 }
예제 #12
0
        public static IKeyInfo EnsureKey(
            MemberInfo[] members,
            bool unique,
            DbTableInfoBuilder tableBuilder)
        {
            IKeyInfo keyInfo = tableBuilder.FindKey(members, false, unique);

            if (keyInfo == null)
            {
                keyInfo = KeyInfoHelper.CreateKeyInfo(tableBuilder.EntityType, members);
                tableBuilder.AddKey(keyInfo, unique);
            }

            return(keyInfo);
        }
예제 #13
0
        private static IKeyInfoHelper GetKeyInfoHelper(IKeyInfo keyInfo)
        {
            if (!(keyInfo is IKeyInfoHelperProvider helperProvider))
            {
                throw new ArgumentException("", "keyInfo");
            }

            var helper = helperProvider.KeyInfoHelper;

            if (helper == null)
            {
                throw new ArgumentException("", "keyInfo");
            }

            return(helper);
        }
예제 #14
0
        public static IKeyInfo CreateKeyInfo(LambdaExpression selector)
        {
            Type entityType = selector.Parameters[0].Type;
            Type resultType = selector.Body.Type;

            MethodInfo factory = ReflectionHelper
                                 .GetMethodInfo <IKeyInfoFactory>(f => f.Create <object, object>(null))
                                 .GetGenericMethodDefinition()
                                 .MakeGenericMethod(entityType, resultType);

            IKeyInfo result = factory.Invoke(
                ExtendedKeyInfoFactory.Instance,
                new object[] { selector }) as IKeyInfo;

            return(result);
        }
예제 #15
0
 public DbRelationInfo(
     string primaryTable,
     string foreignTable,
     IKeyInfo primaryKeyInfo,
     IKeyInfo foreignKeyInfo,
     Delegate primaryToForeignConverter,
     Delegate foreignToPrimaryConverter,
     bool cascadedDelete)
 {
     this.PrimaryTable              = primaryTable;
     this.ForeignTable              = foreignTable;
     this.PrimaryKeyInfo            = primaryKeyInfo;
     this.ForeignKeyInfo            = foreignKeyInfo;
     this.PrimaryToForeignConverter = primaryToForeignConverter;
     this.ForeignToPrimaryConverter = foreignToPrimaryConverter;
     this.CascadedDelete            = cascadedDelete;
 }
예제 #16
0
 public DbRelationInfo(
     string primaryTable, 
     string foreignTable,
     IKeyInfo primaryKeyInfo,
     IKeyInfo foreignKeyInfo, 
     Delegate primaryToForeignConverter,
     Delegate foreignToPrimaryConverter,
     bool cascadedDelete)
 {
     this.PrimaryTable = primaryTable;
     this.ForeignTable = foreignTable;
     this.PrimaryKeyInfo = primaryKeyInfo;
     this.ForeignKeyInfo = foreignKeyInfo;
     this.PrimaryToForeignConverter = primaryToForeignConverter;
     this.ForeignToPrimaryConverter = foreignToPrimaryConverter;
     this.CascadedDelete = cascadedDelete;
 }
            public static IIndex <TEntity, TKey> CreateIndex <TEntity, TPrimaryKey, TKey>(
                Table <TEntity, TPrimaryKey> table,
                IKeyInfo <TEntity, TKey> key,
                bool unique)

                where TEntity : class
            {
                IIndexFactory factory = new RedBlackTreeIndexFactory();

                if (unique)
                {
                    return(table.CreateUniqueIndex(factory, key));
                }
                else
                {
                    return(table.CreateIndex(factory, key));
                }
            }
        public bool TryCreateKeyInfo <TEntity, TKey>(
            Expression <Func <TEntity, TKey> > keySelector,
            out IKeyInfo <TEntity, TKey> result) where TEntity : class
        {
            result = null;
            bool success = false;

            foreach (IKeyInfoService factory in this.factories)
            {
                if (factory.TryCreateKeyInfo(keySelector, out result))
                {
                    success = true;
                    break;
                }
            }

            return(success);
        }
예제 #19
0
        public override void Execute(IKeyInfo directionKey)
        {
            this.Engine.Player.Move(directionKey);

            ICharacter currentEnemy = this.FindEnemy();

            if (currentEnemy != null)
            {
                this.EnterBattle(currentEnemy);
                return;
            }

            IGameItem currentItem = this.FindItem();

            if (currentItem != null)
            {
                this.CollectItem(currentItem);
            }
        }
예제 #20
0
        private void MovePlayer(IGameEngine gameEngine, IKeyInfo keyInfo)
        {
            gameEngine.Player.Move(gameEngine, keyInfo);

            var currentEnemy = FindEnemy(gameEngine);

            if (currentEnemy != null)
            {
                EnterBattle(gameEngine, currentEnemy);
                return;
            }

            var currentItem = FindItem(gameEngine);

            if (currentItem != null)
            {
                CollectItem(gameEngine, currentItem);
            }
        }
        private static IIndex FindIndex(ITable table, IKeyInfo key, bool unique)
        {
            IEnumerable <IIndex> indexes = table.Indexes;

            if (unique)
            {
                indexes = indexes.OfType <IUniqueIndex>();
            }

            foreach (IIndex index in indexes)
            {
                if (index.KeyInfo == key)
                {
                    return(index);
                }
            }

            throw new InvalidOperationException("Index was not found");
        }
예제 #22
0
        private static Func <TFrom, TTo> CreateConversion <TFrom, TTo>(
            IKeyInfo <TFrom> fromKeyInfo,
            IKeyInfo <TTo> toKeyInfo,
            int[] mapping)
        {
            IKeyInfoHelper from = GetKeyInfoHelper(fromKeyInfo);
            IKeyInfoHelper to   = GetKeyInfoHelper(toKeyInfo);

            ParameterExpression keyParam = Expression.Parameter(fromKeyInfo.KeyType);

            Expression body =
                KeyExpressionHelper.CreateKeyConversionExpression(
                    keyParam,
                    toKeyInfo.EntityKeyMembers,
                    mapping,
                    from,
                    to);

            return(Expression.Lambda <Func <TFrom, TTo> >(body, keyParam).Compile());
        }
예제 #23
0
            public static Table <TEntity, TPrimaryKey> CreateTable <TEntity, TPrimaryKey>(
                Database database,
                IKeyInfo <TEntity, TPrimaryKey> primaryKeyInfo,
                Expression <Func <TEntity, long> > identity,
                object[] constraintFactories)

                where TEntity : class
            {
                Table <TEntity, TPrimaryKey> table = database.Tables.Create <TEntity, TPrimaryKey>(
                    primaryKeyInfo,
                    identity != null ? new IdentitySpecification <TEntity>(identity) : null);

                foreach (var constraintFactory in
                         constraintFactories.Cast <IConstraintFactory <TEntity> >())
                {
                    table.Contraints.Add(constraintFactory);
                }

                return(table);
            }
예제 #24
0
        private static Delegate CreateConverter(
            MethodInfo converterMethod,
            IKeyInfo primaryKeyInfo,
            IKeyInfo foreignKeyInfo,
            IRelationContraint[] relationConstraints)
        {
            MethodInfo factory = converterMethod
                                 .GetGenericMethodDefinition()
                                 .MakeGenericMethod(primaryKeyInfo.KeyType, foreignKeyInfo.KeyType);

            Delegate result = factory.Invoke(
                null,
                new object[]
            {
                primaryKeyInfo,
                foreignKeyInfo,
                relationConstraints.ToArray()
            }) as Delegate;

            return(result);
        }
예제 #25
0
        public DbTableInfo(
            string tableName,
            Type entityType,
            MemberInfo identityField,
            PropertyInfo[] properties,
            IKeyInfo primaryKeyInfo,
            IKeyInfo[] uniqueKeys,
            IKeyInfo[] foreignKeys,
            object[] constraintFactories)
        {
            this.TableName           = tableName;
            this.EntityType          = entityType;
            this.IdentityField       = identityField;
            this.Properties          = properties;
            this.ConstraintFactories = constraintFactories;
            this.PrimaryKeyInfo      = primaryKeyInfo;
            this.UniqueKeys          = uniqueKeys;
            this.ForeignKeys         = foreignKeys;

            this.initializer = new FastLazy <Func <object[], object> >(CreateEntityInitializer);
        }
예제 #26
0
        public DbTableInfo(
            string tableName, 
            Type entityType, 
            MemberInfo identityField,
            PropertyInfo[] properties,
            IKeyInfo primaryKeyInfo,
            IKeyInfo[] uniqueKeys,
            IKeyInfo[] foreignKeys,
            object[] constraintFactories)
        {
            this.TableName = tableName;
            this.EntityType = entityType;
            this.IdentityField = identityField;
            this.Properties = properties;
            this.ConstraintFactories = constraintFactories;
            this.PrimaryKeyInfo = primaryKeyInfo;
            this.UniqueKeys = uniqueKeys;
            this.ForeignKeys = foreignKeys;

            this.initializer = new FastLazy<Func<object[], object>>(CreateEntityInitializer);
        }
예제 #27
0
        /// <summary>
        ///     Initializes a database table.
        /// </summary>
        /// <typeparam name="TEntity">
        ///     Specifies the type of the entities of the table.
        ///  </typeparam>
        /// <typeparam name="TPrimaryKey">
        ///     Specifies the type of the primary key of the entities.
        ///  </typeparam>
        /// <param name="primaryKey">
        ///     An IKeyInfo object that represents the primary key of the entities.
        /// </param>
        /// <param name="identitySpecification">
        ///     An IdentitySpecification to set an identity field.
        /// </param>
        /// <returns>
        ///     The table.
        /// </returns>
        public Table <TEntity, TPrimaryKey> Create <TEntity, TPrimaryKey>(
            IKeyInfo <TEntity, TPrimaryKey> primaryKey,
            IdentitySpecification <TEntity> identitySpecification,
            object tableInfo = null)

            where TEntity : class
        {
            if (primaryKey == null)
            {
                throw new ArgumentNullException("primaryKey");
            }

            ITableService service = this.database
                                    .DatabaseEngine
                                    .ServiceProvider
                                    .GetService <ITableService>();

            if (service == null)
            {
                throw new NMemoryException(ExceptionMessages.ServiceNotFound, "TableService");
            }

            Table <TEntity, TPrimaryKey> table =
                service.CreateTable <TEntity, TPrimaryKey>(
                    primaryKey,
                    identitySpecification,
                    this.database,
                    tableInfo);

            if (table == null)
            {
                throw new NMemoryException(ExceptionMessages.TableNotCreated);
            }

            this.RegisterTable(table);

            return(table);
        }
예제 #28
0
        public bool TryCreateKeyInfo <TEntity, TKey>(
            Expression <Func <TEntity, TKey> > keySelector,
            out IKeyInfo <TEntity, TKey> result) where TEntity : class
        {
            if (!ReflectionHelper.IsAnonymousType(typeof(TKey)))
            {
                result = null;
                return(false);
            }

            IKeyInfoHelper helper = AnonymousTypeKeyInfo <TEntity, TKey> .KeyInfoHelper;

            MemberInfo[] members;

            if (!helper.TryParseKeySelectorExpression(keySelector.Body, true, out members))
            {
                result = null;
                return(false);
            }

            result = new AnonymousTypeKeyInfo <TEntity, TKey>(members);
            return(true);
        }
예제 #29
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.primaryKeyIndex =
                this.CreateUniqueIndex(new DictionaryIndexFactory(), primaryKey);

            this.RegisterTimestampConstraints();

            if (identitySpecification != null)
            {
                this.identityField = new IdentityField <TEntity>(identitySpecification);
            }
        }
예제 #30
0
 public void Execute(IGameEngine gameEngine, IKeyInfo keyInfo)
 {
     MovePlayer(gameEngine, keyInfo);
 }
예제 #31
0
 public void AddKey(IKeyInfo key, bool isUnique)
 {
     if (isUnique)
     {
         this.uniqueKeys.Add(key);
     }
     else
     {
         this.otherKeys.Add(key);
     }
 }
예제 #32
0
        private static Delegate CreateConverter(
            MethodInfo converterMethod,
            IKeyInfo primaryKeyInfo,
            IKeyInfo foreignKeyInfo,
            IRelationContraint[] relationConstraints)
        {
            MethodInfo factory = converterMethod
                .GetGenericMethodDefinition()
                .MakeGenericMethod(primaryKeyInfo.KeyType, foreignKeyInfo.KeyType);

            Delegate result = factory.Invoke(
                null,
                new object[] 
                { 
                    primaryKeyInfo, 
                    foreignKeyInfo, 
                    relationConstraints.ToArray() 
                }) as Delegate;

            return result;
        }
        private static IKeyInfoHelper GetKeyInfoHelper(IKeyInfo keyInfo)
        {
            var provider = keyInfo as IKeyInfoHelperProvider;

            if (provider == null)
            {
                return null;
            }

            return provider.KeyInfoHelper;
        }
예제 #34
0
 public bool HandlesInput(IKeyInfo keyInfo)
 {
     return(keyInfo.Key == ConsoleKey.D);
 }
 public MultipleUniqueKeyFoundException(IKeyInfo keyInfo, Exception inner) 
     : base(GetMessage(keyInfo), inner)
 {
 }
예제 #36
0
 internal IndexBase(ITable <TEntity> table, IKeyInfo <TEntity, TKey> keyInfo)
 {
     this.table   = table;
     this.keyInfo = keyInfo;
 }
        private static string GetMessage(IKeyInfo info)
        {
            var fields = string.Join(", ", info.EntityKeyMembers.Select(x => x.Name));

            return string.Format("Unique key violation ({0})", fields);
        }