public void UtilsTest_LiteDataTable_Query() { var repoUser = RF.Concrete <TestUserRepository>(); using (var tranWhole = RF.TransactionScope(repoUser)) { repoUser.Save(new TestUser() { Age = 1 }); repoUser.Save(new TestUser() { Age = 1 }); repoUser.Save(new TestUser() { Age = 1 }); using (var dba = DbAccesserFactory.Create(repoUser)) { var table = dba.QueryLiteDataTable("Select * from Users where id > {0}", 0); var columns = table.Columns; Assert.IsTrue(columns.Find("UserName") != null); Assert.IsTrue(columns.Find("Age") != null); var rows = table.Rows; Assert.IsTrue(rows.Count == 3); Assert.IsTrue(rows[0].Values.Length == columns.Count); Assert.IsTrue(rows[0].GetInt32("Age") == 1); } } }
protected override void Execute() { using (var dba = DbAccesserFactory.Create(ConnectionStringNames.RafyPlugins)) { dba.RawAccesser.ExecuteText("DELETE FROM AUDITITEM"); } }
public void DMT_RefreshComments() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { context.RefreshComments(); if (context.DbSetting.ProviderName.Contains("MySql")) { //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { Func <string, DataRow[]> queryComments = tableName => { var table = db.QueryDataTable(@"show full columns from `" + tableName + "`"); return(table.Rows.Cast <DataRow>().ToArray()); }; var rows = queryComments("ARTICLE"); Assert.IsTrue(rows.Any(r => r["Field"].ToString() == "Id"), "实体的标识属性。"); Assert.IsTrue(rows.Any(r => r["Field"].ToString() == "AdministratorId"), "文章的管理员"); Assert.IsTrue(rows.Any(r => r["Field"].ToString() == "CreatedTime"), "实体的创建时间。"); rows = queryComments("Roles"); var roleTypeDesc = rows.FirstOrDefault(r => r["Field"].ToString() == "RoleType"); Assert.IsNotNull(roleTypeDesc, "枚举属性必须有注释。"); var comment = roleTypeDesc["Comment"].ToString(); Assert.AreEqual(comment, @"角色的类型 0:(Normal, 一般) 1:(Administrator, 管理员)"); } } else if (!DbSetting.IsOracleProvider(context.DbSetting)) { //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { Func <string, DataRow[]> queryComments = tableName => { var table = db.QueryDataTable(@"select t.name tableName, c.name columnName, p.Value Comment from sys.all_columns c join sys.tables t on c.object_id = t.object_id join sys.extended_properties p on p.major_id = c.object_id and p.minor_id = c.column_id where t.name = '" + tableName + "'"); return(table.Rows.Cast <DataRow>().ToArray()); }; var rows = queryComments("ARTICLE"); Assert.IsTrue(rows.Any(r => r["columnName"].ToString() == "Id"), "主键必须有注释。"); Assert.IsTrue(rows.Any(r => r["columnName"].ToString() == "AdministratorId"), "外键必须有注释。"); Assert.IsTrue(rows.Any(r => r["columnName"].ToString() == "CreatedTime"), "扩展属性必须有注释。"); rows = queryComments("Roles"); var roleTypeDesc = rows.FirstOrDefault(r => r["columnName"].ToString() == "RoleType"); Assert.IsNotNull(roleTypeDesc, "枚举属性必须有注释。"); var comment = roleTypeDesc["Comment"].ToString(); Assert.AreEqual(comment, @"角色的类型 0:(Normal, 一般) 1:(Administrator, 管理员)"); //WF_ 开头的动态属性。 } } } }
public void UtilsTest_DBA_DbAccesserParameter() { var repo = RF.ResolveInstance <BookRepository>(); using (var dba = DbAccesserFactory.Create(repo)) { using (var reader = dba.QueryDataReader(@"SELECT * FROM BOOK WHERE UpdatedTime < {0}", new DbAccesserParameter(DateTime.Now, DbType.DateTime) )) { //do nothing; } } }
public void DMT_ManualMigrate() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { context.HistoryRepository = new DbHistoryRepository(); context.RunDataLossOperation = DataLossOperation.All; try { context.ManualMigrations.Clear(); context.ManualMigrations.Add(new DMT_ManualMigrateEntity()); context.ManualMigrations.Add(new DMT_ManualMigrateTest()); //手工更新 context.MigrateManually(); //历史记录 var histories = context.GetHistories(); Assert.IsTrue(histories.Count == 2); Assert.IsTrue(histories[0] is DMT_ManualMigrateTest); Assert.IsTrue(histories[1] is DMT_ManualMigrateEntity); //数据库结构 var database = context.DatabaseMetaReader.Read(); var table = database.FindTable("TestingTable"); Assert.IsTrue(table.Columns.Count == 2); var pk = table.FindPrimaryColumn(); Assert.IsTrue(pk.Name.EqualsIgnoreCase("Id")); Assert.IsTrue(pk.DbType == DbType.Int32); //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { var rows = db.QueryDataTable("select * from TestingTable"); Assert.IsTrue(rows.Rows.Count == 2); } var repo = RF.Find <TestUser>(); Assert.IsTrue(repo.CountAll() == 10); } finally { //回滚 context.RollbackAll(RollbackAction.DeleteHistory); var database = context.DatabaseMetaReader.Read(); Assert.IsTrue(database.FindTable("TestingTable") == null); } } }
public void UtilsTest_DBA_FormattedSql_DbAccesserParameter() { var repo = RF.ResolveInstance <BookRepository>(); using (var dba = DbAccesserFactory.Create(repo)) { FormattedSql sql = @"SELECT * FROM BOOK WHERE UpdatedTime < {0}"; sql.Parameters.Add(new DbAccesserParameter(DateTime.Now, DbType.DateTime)); using (var reader = dba.QueryDataReader(sql, sql.Parameters.ToArray())) { //do nothing; } } }
public void DMT_RefreshComments() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { context.RefreshComments(); if (!DbSetting.IsOracleProvider(context.DbSetting)) { //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { var rowsCount = Convert.ToInt32(db.QueryValue( @"select COUNT(0) from sys.extended_properties p join sys.tables t on p.major_id = t.object_id where t.name = 'ARTICLE'")); Assert.IsTrue(rowsCount > 0); } } } }
private static List <PropertyInfo> ParseProperties(string sql, string dbName) { var properties = new List <PropertyInfo>(); using (var dba = DbAccesserFactory.Create(dbName)) { var table = dba.QueryDataTable(sql); var columns = table.Columns; foreach (DataColumn column in columns) { if (!column.ColumnName.EqualsIgnoreCase(Entity.IdProperty.Name)) { var property = new PropertyInfo { Name = column.ColumnName, Type = column.DataType }; properties.Add(property); } } } var matches = Regex.Matches(sql, @"(?<name>[\w_]+)\s*,?\s*--(?<comment>.+)$", RegexOptions.Multiline); foreach (Match match in matches) { var name = match.Groups["name"].Value; var comment = match.Groups["comment"].Value; var property = properties.FirstOrDefault(p => p.Name.EqualsIgnoreCase(name)); if (property != null) { property.Comment = comment.Trim(); } } return(properties); }
/// <summary> /// 创建数据库操作对象 /// </summary> /// <returns></returns> public IDbAccesser CreateDbAccesser() { return(DbAccesserFactory.Create(this.DbSetting)); }