public override JoinClause Create(JoinAttribute joinAttribute) { SimpleJoinAttribute simpleJoinAttribute; if ((simpleJoinAttribute = joinAttribute as SimpleJoinAttribute) == null) { throw new ArgumentException("Attribute must be SimpleJoinAttribute"); } var splitter = GetSplitter(simpleJoinAttribute); var selects = new List <string>(); if (simpleJoinAttribute.TableSelectColumns != null && simpleJoinAttribute.TableSelectColumns.Any()) { foreach (var tableSelectColumn in simpleJoinAttribute.TableSelectColumns) { selects.AddRange( tableSelectColumn.Value.Select(column => column.IsExpression ? column.Select : $"{column.Table}.{column.Select}")); } } else { selects.Add(String.IsNullOrEmpty(simpleJoinAttribute.JoinedTableAlias) ? $"{simpleJoinAttribute.JoinedTable}.*" : $"{simpleJoinAttribute.JoinedTableAlias}.*"); } string joinSql; if (String.IsNullOrEmpty(simpleJoinAttribute.JoinedTableAlias)) { joinSql = string.Format("{0} on {0}.{1} = {2}.{3}{4}", simpleJoinAttribute.JoinedTable, simpleJoinAttribute.JoinedTableField, simpleJoinAttribute.CurrentTable, simpleJoinAttribute.CurrentTableField, GetAddOnClauses(simpleJoinAttribute)); } else { joinSql = string.Format("{0} {1} on {1}.{2} = {3}.{4}{5}", simpleJoinAttribute.JoinedTable, simpleJoinAttribute.JoinedTableAlias, simpleJoinAttribute.JoinedTableField, simpleJoinAttribute.CurrentTableAlias, simpleJoinAttribute.CurrentTableField, GetAddOnClauses(simpleJoinAttribute)); } var result = new JoinClause { JoinSqls = new List <string> { joinSql, }, SelectsSql = selects, Splitter = splitter, JoinType = simpleJoinAttribute.JoinType, HasJoin = true, Order = joinAttribute.Order, }; return(result); }
public override JoinClause Create(JoinAttribute joinAttribute) { ManyToManyJoinAttribute manyToManyJoinAttribute; if ((manyToManyJoinAttribute = joinAttribute as ManyToManyJoinAttribute) == null) { throw new ArgumentException("Attribute must be ManyToManyJoinAttribute"); } var splitter = GetSplitter(manyToManyJoinAttribute); var selects = new List <string>(); if (manyToManyJoinAttribute.TableSelectColumns != null && manyToManyJoinAttribute.TableSelectColumns.Any()) { foreach (var tableSelectColumn in manyToManyJoinAttribute.TableSelectColumns) { selects .AddRange( tableSelectColumn.Value.Select( selectClause => selectClause.IsExpression ? selectClause.Select : string.Format("{0}.{1}", selectClause.Table, selectClause.Select))); } } else { selects.Add(string.Format("{0}.*", manyToManyJoinAttribute.JoinedTable)); } var joins = new List <string> { CreateJoinCommunication(manyToManyJoinAttribute), CreateJoinJoined(manyToManyJoinAttribute), }; var result = new JoinClause { JoinSqls = joins, SelectsSql = selects, Splitter = splitter, JoinType = manyToManyJoinAttribute.JoinType, HasJoin = true, Order = joinAttribute.Order, }; return(result); }
public void WillPopulateProperties() { var expectedJoinType = JoinType.None; Type expectedType = null; var expectedLeftKey = String.Empty; var expectedRightKey = String.Empty; var expectedJoinTable = String.Empty; var expectedJoinTableLeftKey = String.Empty; var expectedJoinTableRightKey = String.Empty; var expectedJoinTableJoinType = JoinType.None; var expectedParentProperty = String.Empty; var expectedChildProperty = String.Empty; TestRunner .DoCustomSetup(() => { expectedJoinType = JoinType.Left; expectedType = typeof(Object); expectedLeftKey = DataGenerator.GenerateString(); expectedRightKey = DataGenerator.GenerateString(); expectedJoinTable = DataGenerator.GenerateString(); expectedJoinTableLeftKey = DataGenerator.GenerateString(); expectedJoinTableRightKey = DataGenerator.GenerateString(); expectedJoinTableJoinType = JoinType.Right; expectedParentProperty = DataGenerator.GenerateString(); expectedChildProperty = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var join = new JoinAttribute(expectedJoinType, expectedType, expectedLeftKey, expectedRightKey, expectedJoinTable, expectedJoinTableLeftKey, expectedJoinTableRightKey, expectedJoinTableJoinType, expectedParentProperty, expectedChildProperty); Asserter.AssertEquality(expectedJoinType, join.JoinType); Asserter.AssertEquality(expectedType.ToString(), join.JoinedType.ToString()); Asserter.AssertEquality(expectedLeftKey, expectedLeftKey); Asserter.AssertEquality(expectedRightKey, join.RightKey); Asserter.AssertEquality(expectedJoinTable, join.JoinTable); Asserter.AssertEquality(expectedJoinTableLeftKey, join.JoinTableLeftKey); Asserter.AssertEquality(expectedJoinTableRightKey, join.JoinTableRightKey); Asserter.AssertEquality(expectedJoinTableJoinType, join.JoinTableJoinType); Asserter.AssertEquality(expectedParentProperty, join.ParentProperty); Asserter.AssertEquality(expectedChildProperty, join.ChildProperty); }); }
public void WillReturnRightKeyIfChildPropertyPassedToConstructorIsEmpty() { var expected = String.Empty; TestRunner .DoCustomSetup(() => { expected = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var attribute = new JoinAttribute(JoinType.Left, typeof(object), DataGenerator.GenerateString(), expected, childProperty: String.Empty); Asserter.AssertEquality(expected, attribute.RightKey); Asserter.AssertEquality(expected, attribute.ChildProperty); }); }
public void WillReturnLeftKeyIfParentPropertyPassedToConstructorIsNull() { var expected = String.Empty; TestRunner .DoCustomSetup(() => { expected = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var attribute = new JoinAttribute(JoinType.Left, typeof(object), expected, DataGenerator.GenerateString()); Assert.AreEqual(expected, attribute.LeftKey); Assert.AreEqual(expected, attribute.ParentProperty); }); }
public JoinClause CreateNotJoin(JoinAttribute joinAttribute) { SimpleJoinAttribute simpleJoinAttribute; if ((simpleJoinAttribute = joinAttribute as SimpleJoinAttribute) == null) { throw new ArgumentException("Attribute must be SimpleJoinAttribute"); } var splitter = GetSplitter(simpleJoinAttribute); return(new JoinClause { HasJoin = false, Splitter = splitter, JoinType = simpleJoinAttribute.JoinType, Order = joinAttribute.Order, }); }
public void WillReturnParentPropertyIfParentPropertyPassedToConstructorHasValue() { var expected = String.Empty; TestRunner .DoCustomSetup(() => { expected = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var attribute = new JoinAttribute(JoinType.Left, typeof(object), DataGenerator.GenerateString(), DataGenerator.GenerateString(), parentProperty: expected); Assert.AreNotEqual(expected, attribute.LeftKey); Asserter.AssertEquality(expected, attribute.ParentProperty); }); }
private void BuildFromJoinAttribute(PropertyMapping propertyMapping, JoinAttribute joinAttribute) { if (joinAttribute == null) { return; } var joinMapping = new JoinMapping { JoinType = joinAttribute.JoinType, LeftKey = joinAttribute.LeftKey, RightKey = joinAttribute.RightKey, JoinTable = joinAttribute.JoinTable, JoinTableLeftKey = joinAttribute.JoinTableLeftKey, JoinTableRightKey = joinAttribute.JoinTableRightKey, JoinTableJoinType = joinAttribute.JoinTableJoinType, ParentProperty = joinAttribute.ParentProperty, ChildProperty = joinAttribute.ChildProperty }; propertyMapping.JoinMapping = joinMapping; }
/// <summary> Write a Join XML Element from attributes in a member. </summary> public virtual void WriteJoin(System.Xml.XmlWriter writer, System.Reflection.MemberInfo member, JoinAttribute attribute, BaseAttribute parentAttribute, System.Type mappedClass) { writer.WriteStartElement( "join" ); // Attribute: <table> writer.WriteAttributeString("table", attribute.Table==null ? DefaultHelper.Get_Join_Table_DefaultValue(member) : GetAttributeValue(attribute.Table, mappedClass)); // Attribute: <schema> if(attribute.Schema != null) writer.WriteAttributeString("schema", GetAttributeValue(attribute.Schema, mappedClass)); // Attribute: <catalog> if(attribute.Catalog != null) writer.WriteAttributeString("catalog", GetAttributeValue(attribute.Catalog, mappedClass)); // Attribute: <subselect> if(attribute.Subselect != null) writer.WriteAttributeString("subselect", GetAttributeValue(attribute.Subselect, mappedClass)); // Attribute: <fetch> if(attribute.Fetch != JoinFetch.Unspecified) writer.WriteAttributeString("fetch", GetXmlEnumValue(typeof(JoinFetch), attribute.Fetch)); // Attribute: <inverse> if( attribute.InverseSpecified ) writer.WriteAttributeString("inverse", attribute.Inverse ? "true" : "false"); // Attribute: <optional> if( attribute.OptionalSpecified ) writer.WriteAttributeString("optional", attribute.Optional ? "true" : "false"); WriteUserDefinedContent(writer, member, null, attribute); System.Collections.ArrayList memberAttribs = GetSortedAttributes(member); int attribPos; // Find the position of the JoinAttribute (its <sub-element>s must be after it) for(attribPos=0; attribPos<memberAttribs.Count; attribPos++) if( memberAttribs[attribPos] is JoinAttribute && ((BaseAttribute)memberAttribs[attribPos]).Position == attribute.Position ) break; // found int i = attribPos + 1; // Element: <subselect> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(SubselectAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is SubselectAttribute ) WriteSubselect(writer, member, memberAttrib as SubselectAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(SubselectAttribute), attribute); // Element: <comment> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(CommentAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is CommentAttribute ) WriteComment(writer, member, memberAttrib as CommentAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(CommentAttribute), attribute); // Element: <key> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(KeyAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is KeyAttribute ) WriteKey(writer, member, memberAttrib as KeyAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(KeyAttribute), attribute); // Element: <property> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(PropertyAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is PropertyAttribute ) WriteProperty(writer, member, memberAttrib as PropertyAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(PropertyAttribute), attribute); // Element: <many-to-one> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(ManyToOneAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is ManyToOneAttribute ) WriteManyToOne(writer, member, memberAttrib as ManyToOneAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(ManyToOneAttribute), attribute); // Element: <component> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(ComponentAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' } WriteUserDefinedContent(writer, member, typeof(ComponentAttribute), attribute); // Element: <dynamic-component> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(DynamicComponentAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is DynamicComponentAttribute ) WriteDynamicComponent(writer, member, memberAttrib as DynamicComponentAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(DynamicComponentAttribute), attribute); // Element: <any> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(AnyAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is AnyAttribute ) WriteAny(writer, member, memberAttrib as AnyAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(AnyAttribute), attribute); // Element: <map> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(MapAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is MapAttribute ) WriteMap(writer, member, memberAttrib as MapAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(MapAttribute), attribute); // Element: <set> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(SetAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is SetAttribute ) WriteSet(writer, member, memberAttrib as SetAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(SetAttribute), attribute); // Element: <list> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(ListAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is ListAttribute ) WriteList(writer, member, memberAttrib as ListAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(ListAttribute), attribute); // Element: <bag> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(BagAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is BagAttribute ) WriteBag(writer, member, memberAttrib as BagAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(BagAttribute), attribute); // Element: <idbag> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(IdBagAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is IdBagAttribute ) WriteIdBag(writer, member, memberAttrib as IdBagAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(IdBagAttribute), attribute); // Element: <array> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(ArrayAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is ArrayAttribute ) WriteArray(writer, member, memberAttrib as ArrayAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(ArrayAttribute), attribute); // Element: <primitive-array> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(PrimitiveArrayAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is PrimitiveArrayAttribute ) WritePrimitiveArray(writer, member, memberAttrib as PrimitiveArrayAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(PrimitiveArrayAttribute), attribute); // Element: <sql-insert> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(SqlInsertAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is SqlInsertAttribute ) WriteSqlInsert(writer, member, memberAttrib as SqlInsertAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(SqlInsertAttribute), attribute); // Element: <sql-update> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(SqlUpdateAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is SqlUpdateAttribute ) WriteSqlUpdate(writer, member, memberAttrib as SqlUpdateAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(SqlUpdateAttribute), attribute); // Element: <sql-delete> for(; i<memberAttribs.Count; i++) { BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute; if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType()) || IsNextElement(memberAttrib, attribute, typeof(SqlDeleteAttribute)) ) break; // next attributes are 'elements' of the same level OR for 'sub-elements' else { if( memberAttrib is JoinAttribute ) break; // Following attributes are for this Join if( memberAttrib is SqlDeleteAttribute ) WriteSqlDelete(writer, member, memberAttrib as SqlDeleteAttribute, attribute, mappedClass); } } WriteUserDefinedContent(writer, member, typeof(SqlDeleteAttribute), attribute); writer.WriteEndElement(); }
public abstract JoinClause Create(JoinAttribute joinAttribute);
protected virtual string GetAddOnClauses(JoinAttribute joinAttribute) { return(string.IsNullOrWhiteSpace(joinAttribute.AddOnClause) ? string.Empty : string.Format(" AND {0}", joinAttribute.AddOnClause)); }
protected virtual string GetAddOnClauses(JoinAttribute joinAttribute) { return(string.IsNullOrWhiteSpace(joinAttribute.AddOnClause) ? string.Empty : $" AND {joinAttribute.AddOnClause}"); }