Exemplo n.º 1
0
        public void GeneralTest()
        {
            DbStructureGateway g = DbStructureGateway.Instance;

            g.CreateTable(typeof(NewsItem));
            Assert.IsTrue(g.IsValid(typeof(NewsItem)), g.LastError);
            Assert.IsFalse(g.IsValid(typeof(NewsItem2)));


            DbTableCheckResult result = new DbTableCheckResult(DbAccessor.Instance);

            result.Build(typeof(NewsItem2));

            Assert.AreEqual(2, result.FieldsToCreate.Count);
            Assert.AreEqual(1, result.FieldsToUpdate.Count);


            g.DropTable(typeof(NewsItem));
            g.CreateTable(typeof(NewsItem2));
            Assert.IsTrue(g.IsValid(typeof(NewsItem2)));
            Assert.IsFalse(g.IsValid(typeof(NewsItem)));


            result = new DbTableCheckResult(DbAccessor.Instance);
            result.Build(typeof(NewsItem2));

            Assert.AreEqual(0, result.FieldsToCreate.Count);
            Assert.AreEqual(0, result.FieldsToUpdate.Count);
        }
Exemplo n.º 2
0
        ///<summary>
        /// Updates database table to match passed Type
        ///
        /// Notice:
        ///  - affrects only added and changed fields
        ///  - doesn't remove unused columns from database
        ///  - ignores primary key and indexes
        ///</summary>
        ///<param name="type">Type to check</param>
        ///<returns>true on success</returns>
        public bool AlterTable(Type type)
        {
            DbTableCheckResult result = new DbTableCheckResult(accessor);

            result.Build(type);
            accessor.AlterTable(result);
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks if assosiated table exists, and all fields marked as DbFieldAttribute present in database
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool IsValid(Type type)
        {
            if (!IsTableExists(type))
            {
                return(false);
            }

            DbTableCheckResult result = new DbTableCheckResult(accessor);
            bool isAllFieldValid      = result.IsAllFieldValid(type);

            if (!isAllFieldValid)
            {
                LastError = result.LastError;
            }

            return(isAllFieldValid);
        }
Exemplo n.º 4
0
        internal override void AlterTable(DbTableCheckResult checkResult)
        {
            // TODO: (Ndb) update indexes - remove indexes before alter, and recreate after

            StringBuilder sb = new StringBuilder("ALTER TABLE " + checkResult.TableName + " ");

            foreach (var item in checkResult.FieldsToCreate)
            {
                sb.AppendFormat("ADD COLUMN [{0}] {1} NULL,", item.Key, item.Value);
            }

            foreach (var item in checkResult.FieldsToUpdate)
            {
                sb.AppendFormat("CHANGE [{0}] [{0}] {1} NULL,", item.Key, item.Value);
            }
            sb.Remove(sb.Length - 1, 1);

            ExecuteNonQuery(sb.ToString());
        }
Exemplo n.º 5
0
 internal override void AlterTable(DbTableCheckResult checkResult)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 6
0
 internal abstract void AlterTable(DbTableCheckResult checkResult);
Exemplo n.º 7
0
 internal override void AlterTable(DbTableCheckResult checkResult)
 {
     throw new NotImplementedException("besides poor ALTER TABLE support by SqLite, Ndb can't update it's tables now");
 }