public override IMCollection <Group> FindAll() { const string query = "SELECT id, description FROM _group"; IMCollection <DbRow> dbRows = Database.ExecuteNativeQuery(query, (command, newParameter) => { }); ResultTransformer <Group> transformer = new ResultTransformer <Group>(dbRows); return(transformer.Transform()); }
public override IMCollection <User> FindAll() { const string query = "SELECT id, first_name, last_name FROM _user"; IMCollection <DbRow> dbRows = Database.ExecuteNativeQuery(query, (command, newParameter) => { }); ResultTransformer <User> transformer = new ResultTransformer <User>(dbRows); return(transformer.Transform()); }
public override Group FindByIdLazyMode(int anId) { const string queryGroup = "SELECT id, description FROM _group WHERE id=@ID"; IMCollection <DbRow> dbRows = Database.ExecuteNativeQuery(queryGroup, (command, newParameter) => { command.Parameters.Add(newParameter("@ID", DbType.Int32)); command.Parameters["@ID"].Value = anId; }); ResultTransformer <Group> transformerGroup = new ResultTransformer <Group>(dbRows); Group aGroup = transformerGroup.Transform().GetFirstOrDefault(new Group()); return(aGroup); }
public override Permission FindByIdLazyMode(int anId) { const string query = "SELECT id, description FROM permission WHERE id=@ID"; IMCollection <DbRow> dbRows = Database.ExecuteNativeQuery(query, (command, newParameter) => { command.Parameters.Add(newParameter("@ID", DbType.Int32)); command.Parameters["@ID"].Value = anId; }); ResultTransformer <Permission> transformer = new ResultTransformer <Permission>(dbRows); return(transformer.Transform().GetFirstOrDefault(new Permission())); }
public override User FindByIdLazyMode(int anId) { const string query = "SELECT id, first_name, last_name, email, idiom_id FROM _user WHERE id=@ID"; IMCollection <DbRow> dbRows = Database.ExecuteNativeQuery(query, (command, newParameter) => { command.Parameters.Add(newParameter("@ID", DbType.Int32)); command.Parameters["@ID"].Value = anId; }); ResultTransformer <User> transformer = new ResultTransformer <User>(dbRows); return(transformer.Transform().GetFirstOrDefault(new User())); }
private IMCollection <Permission> FindPermissionsByGroupIdLazyMode(int aGroupId) { const string queryGroupPermission = "SELECT p.id, p.description " + "FROM group_permission gp " + "join PERMISSION p on gp.permission_id=p.id " + "WHERE gp.group_id=@GROUP_ID "; IMCollection <DbRow> dbPermissionRows = Database.ExecuteNativeQuery(queryGroupPermission, (command, newParameter) => { command.Parameters.Add(newParameter("@GROUP_ID", DbType.Int32)); command.Parameters["@GROUP_ID"].Value = aGroupId; }); ResultTransformer <Permission> transformerPermission = new ResultTransformer <Permission>(dbPermissionRows); return(transformerPermission.Transform()); }
private IMCollection <Group> FindGroupsByUserIdLazyMode(int aUserId) { const string queryUserPermission = "SELECT g.id, g.description " + "FROM user_group ug " + "join _GROUP g on ug.group_id=g.id " + "WHERE ug.user_id=@USER_ID "; IMCollection <DbRow> dbGroupRows = Database.ExecuteNativeQuery(queryUserPermission, (command, newParameter) => { command.Parameters.Add(newParameter("@USER_ID", DbType.Int32)); command.Parameters["@USER_ID"].Value = aUserId; }); ResultTransformer <Group> transformerPermission = new ResultTransformer <Group>(dbGroupRows); return(transformerPermission.Transform()); }
private static ICollection <BaseTable> listBaseTableQuery(UIProgress prgDialog, Session session, long?projectId, string qs, Type bindClass, ICollection <long> ids, bool cloneResult) { IList <BaseTable> resList = new List <BaseTable>(ids.Count); ICollection <ICollection <long> > chunks = splitIntoChunks(ids, 1900); if (prgDialog != null) { prgDialog.Indeterminate = false; prgDialog.TotalTimes = chunks.Count; prgDialog.setProgress("Loading items...", 0); } ResultTransformer rt = null; if (bindClass != null) { rt = Transformers.aliasToBean(bindClass); } foreach (ICollection <long> chunk in chunks) { Query q = session.createQuery(qs).setCacheable(false).setParameterList("ids", chunk); if (projectId != null) { q.setLong("prjId", projectId); } if (rt != null) { q = q.setResultTransformer(rt); } IList <BaseTable> objs = q.list(); foreach (BaseTable obj in objs) { if (cloneResult) { obj = (BaseTable)obj.clone(); } resList.Add(obj); } if (prgDialog != null) { prgDialog.incrementProgress(1); } } return(resList); }
public void LinqResultTransformer_ShouldHaveEqualityBasedOnCtorParameter() { Func <object[], object> d1 = x => new object(); Func <IEnumerable <object>, IEnumerable <object> > d2 = x => x; var transformer1 = new ResultTransformer(d1, d2); var transformer2 = new ResultTransformer(d1, d2); transformer1.Should().Be.EqualTo(transformer2); transformer1.GetHashCode().Should().Be.EqualTo(transformer2.GetHashCode()); Func <IEnumerable <object>, IEnumerable <int> > d3 = x => new [] { 1, 2, 3 }; var transformer3 = new ResultTransformer(d1, d3); transformer1.Should().Not.Be.EqualTo(transformer3); transformer1.GetHashCode().Should().Not.Be.EqualTo(transformer3.GetHashCode()); }
public void LinqResultTransformer_ShouldHaveEqualityBasedOnCtorParameter() { DoNothingDelegate d1 = () => { }; DoNothingDelegate d2 = () => { }; var transformer1 = new ResultTransformer(d1, d2); var transformer2 = new ResultTransformer(d1, d2); transformer1.Should().Be.EqualTo(transformer2); transformer1.GetHashCode().Should().Be.EqualTo(transformer2.GetHashCode()); DoNothingDelegate1 d3 = () => { }; var transformer3 = new ResultTransformer(d1, d3); transformer1.Should().Not.Be.EqualTo(transformer3); transformer1.GetHashCode().Should().Not.Be.EqualTo(transformer3.GetHashCode()); }
private void CanTransformWithNullableValuesTest() { DbRow dbRow = new DbRow(); dbRow.Columns.Add(new DbColumn(typeof(string), "aColumn", null)); IMCollection <DbRow> dbRows = new MCollection <DbRow> { dbRow }; IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(dbRows); IMCollection <DummyClassToBeTransformed> transformed = resultTransformer.Transform(); Assertion.AreEqual(1, transformed.Count); Assertion.AreEqual(null, transformed[0].AName); }
public void Transform_NonAnonymousTypeAllPropertiesPresent_SetsAllPropertyValuesAndReturnsInstance() { //prepare _properties = new Dictionary <string, string> { { "Property1", "Property1" }, { "Property2", "Property2" }, { "Property3", "Property3" }, { "Property4", "Property4" } }.ToReadOnlyDictionary(); _transformer = new ResultTransformer(_properties, _mapping.Object); var instance = new object(); _mapping.Setup(m => m.Create(It.IsAny <object[]>(), null)) .Returns(instance); _mapping.Setup(m => m.IsForAnonymousType) .Returns(false); var searchResultAttributesCollection = typeof(SearchResultAttributeCollection).Create <SearchResultAttributeCollection>(); searchResultAttributesCollection .Call("Add", new object[] { "Property1", new DirectoryAttribute("Property1", "prop1") }); searchResultAttributesCollection .Call("Add", new object[] { "Property2", new DirectoryAttribute("Property2", "2") }); searchResultAttributesCollection .Call("Add", new object[] { "Property3", new DirectoryAttribute("Property3", _bytes) }); searchResultAttributesCollection .Call("Add", new object[] { "Property4", new DirectoryAttribute("Property4", _strings) }); var searchResultsEntry = typeof(SearchResultEntry).Create <SearchResultEntry>( new object[] { "dn", searchResultAttributesCollection }); //act var transformed = _transformer.Transform(searchResultsEntry); //assert transformed.Should().Be.SameInstanceAs(instance); _property1.Verify(p => p.SetValue(instance, "prop1")); _property2.Verify(p => p.SetValue(instance, "2")); _property3.Verify(p => p.SetValue(instance, _bytes)); _property4.Verify(p => p.SetValue(instance, _strings)); }
private void CanTransformAnIntTypeTest() { const string columnName = "aInt"; DbRow dbRow = new DbRow(); dbRow.Columns.Add(new DbColumn(typeof(int), columnName, DummyIntValue)); IMCollection <DbRow> dbRows = new MCollection <DbRow> { dbRow }; IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(dbRows); IMCollection <DummyClassToBeTransformed> transformed = resultTransformer.Transform(); Assertion.AreEqual(DummyIntValue, transformed[0].AInt); }
private void CanTransformWithNotRecognizedColumnsTest() { const string dummyColumnName = "dummyName"; DbRow dbRow = new DbRow(); dbRow.Columns.Add(new DbColumn(typeof(string), dummyColumnName, DummyStringValue)); IMCollection <DbRow> dbRows = new MCollection <DbRow> { dbRow }; IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(dbRows); IMCollection <DummyClassToBeTransformed> transformed = resultTransformer.Transform(); Assertion.AreEqual(null, transformed[0].AName); }
private void CanTransformAResultSetWithMoreThanOneItemTest() { DbRow dbRow1 = new DbRow(); dbRow1.Columns.Add(new DbColumn(typeof(string), "aColumn", null)); DbRow dbRow2 = new DbRow(); dbRow2.Columns.Add(new DbColumn(typeof(string), "aColumn", null)); IMCollection <DbRow> dbRows = new MCollection <DbRow> { dbRow1, dbRow2 }; IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(dbRows); Assertion.AreEqual(2, resultTransformer.Transform().Count); }
private void CanTransformABigIntegerTypeTest() { const string columnName = "aBigInteger"; DbRow dbRow = new DbRow(); dbRow.Columns.Add(new DbColumn(typeof(BigInteger), columnName, _dummyBigIntegerValue)); IMCollection <DbRow> dbRows = new MCollection <DbRow> { dbRow }; IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(dbRows); IMCollection <DummyClassToBeTransformed> transformed = resultTransformer.Transform(); Assertion.AreEqual(_dummyBigIntegerValue, transformed[0].ABigInteger); }
private void CanTransformIgnoringUnderscoreInColumnNameTest() { const string columnName = "A_NAME"; DbRow dbRow = new DbRow(); dbRow.Columns.Add(new DbColumn(typeof(string), columnName, DummyStringValue)); IMCollection <DbRow> dbRows = new MCollection <DbRow> { dbRow }; IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(dbRows); IMCollection <DummyClassToBeTransformed> transformed = resultTransformer.Transform(); Assertion.AreEqual(DummyStringValue, transformed[0].AName); }
public void Transform_AnonymousTypeSomePropertiesPresent_SetsPresentPropertyValuesAndReturnsInstance() { //prepare _properties = new Dictionary <string, string> { { "Property1", "Property1" }, { "Property2", "Property2" }, { "Property3", "Property3" }, { "Property4", "Property4" } }.ToReadOnlyDictionary(); _transformer = new ResultTransformer(_properties, _mapping.Object); var instance = new object(); _mapping.Setup(m => m.Properties) .Returns(_properties); _mapping.Setup(m => m.Create(new object[] { "prop1", default(int), default(byte[]), default(string[]) }, null)) .Returns(instance); _mapping.Setup(m => m.IsForAnonymousType) .Returns(true); var searchResultAttributesCollection = typeof(SearchResultAttributeCollection).Create <SearchResultAttributeCollection>(); searchResultAttributesCollection .Call("Add", new object[] { "Property1", new DirectoryAttribute("Property1", "prop1") }); var searchResultsEntry = typeof(SearchResultEntry).Create <SearchResultEntry>( new object[] { "dn", searchResultAttributesCollection }); //act var transformed = _transformer.Transform(searchResultsEntry); //assert transformed.Should().Be.SameInstanceAs(instance); }
public void Transform_non_anonymous_type_with_catch_all_mapping_sets_present_properties_and_sets_catch_all_and_returns_instance() { //prepare _properties = new Dictionary <string, string> { { "Property1", "Property1" }, { "Property2", "Property2" } }.ToReadOnlyDictionary(); _mapping.Setup(m => m.GetPropertyMappingByAttribute("property1", typeof(object))) .Returns(_property1.Object); _mapping.Setup(m => m.GetPropertyMappingByAttribute("property2", typeof(object))) .Returns(_property2.Object); _mapping.Setup(m => m.GetPropertyMappingByAttribute("property3", typeof(object))) .Returns(_property3.Object); _mapping.Setup(m => m.GetPropertyMappingByAttribute("property4", typeof(object))) .Returns(_property4.Object); _transformer = new ResultTransformer(_properties, _mapping.Object); var instance = new object(); _mapping.Setup(m => m.Create(It.IsAny <object[]>(), null)) .Returns(instance); _mapping.Setup(m => m.IsForAnonymousType) .Returns(false); _mapping.Setup(x => x.HasCatchAllMapping) .Returns(true); var catchAll = new Mock <IPropertyMapping>(); _mapping.Setup(x => x.GetCatchAllMapping()) .Returns(catchAll.Object); var searchResultAttributesCollection = typeof(SearchResultAttributeCollection).Create <SearchResultAttributeCollection>(); searchResultAttributesCollection .Call("Add", new object[] { "Property1", new DirectoryAttribute("Property1", "prop1") }); searchResultAttributesCollection .Call("Add", new object[] { "Property2", new DirectoryAttribute("Property2", "2") }); searchResultAttributesCollection .Call("Add", new object[] { "OtherProperty", new DirectoryAttribute("OtherProperty", "2") }); var searchResultsEntry = typeof(SearchResultEntry).Create <SearchResultEntry>( new object[] { "dn", searchResultAttributesCollection }); //act var transformed = _transformer.Transform(searchResultsEntry); //assert transformed.Should().Be.SameInstanceAs(instance); _property1.Verify(p => p.SetValue(instance, "prop1")); _property2.Verify(p => p.SetValue(instance, "2")); _property3.Verify(p => p.SetValue(instance, _bytes), Times.Never()); _property4.Verify(p => p.SetValue(instance, _strings), Times.Never()); catchAll.Verify( p => p.SetValue(instance, It.Is <IDirectoryAttributes>(da => da.Entry == searchResultsEntry)), Times.Once()); }
private void CanTransformAnEmptyResultSetTest() { IResultTransformer <DummyClassToBeTransformed> resultTransformer = new ResultTransformer <DummyClassToBeTransformed>(new MCollection <DbRow>()); Assertion.AreEqual(0, resultTransformer.Transform().Count); }