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); }
///<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); }
/// <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); }
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()); }
internal override void AlterTable(DbTableCheckResult checkResult) { throw new NotImplementedException(); }
internal abstract void AlterTable(DbTableCheckResult checkResult);
internal override void AlterTable(DbTableCheckResult checkResult) { throw new NotImplementedException("besides poor ALTER TABLE support by SqLite, Ndb can't update it's tables now"); }