Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Support for Parent/Child Records #378

Closed
jonwagner opened this issue Aug 13, 2018 · 1 comment
Closed

Better Support for Parent/Child Records #378

jonwagner opened this issue Aug 13, 2018 · 1 comment

Comments

@jonwagner
Copy link
Owner

jonwagner commented Aug 13, 2018

See if we can automatically handle deserializing the common query pattern of:

SELECT parent.*, child.*
FROM parent JOIN child

Example from Issue #375:

class Beer
{
	public int ID;
	public IList<Glass> Glasses;
}

CREATE PROC GetBeerAndPossibleGlasses AS
	SELECT * FROM Beer
	SELECT b.BeerID, g.* FROM Beer JOIN Glasses ON (...)
while most SQL are written like

   SELECT * FROM Beer LEFT JOIN Glasses ON (...)

I'm thinking that we create a new record reader:

  • Keep a dictionary of parentid/parent.
  • Read a record.
  • Look up the parentid and see if we need to create a new instance or reuse an existing.
  • Add child to the parent using the parent/child relationship.

This will probably stream acceptably.

This project will require:

  • New record reader
  • Record reader Test cases
  • Possible adjustment to the Recordset attributes for interface building
  • Update the interface builder to use the new record reader
  • Interface test cases
  • Update documentation / samples
@jonwagner
Copy link
Owner Author

This is now available in v6.2.4.

The magic to make parent/child records get parsed from the same recordset is:

            var results = Connection().QuerySql(query, parameters,
				Query.Returns(Together<Parent, Child>.Records));

Using Together instead of Some alters the parsing so that the recordset is parsed, child records are grouped, and the list of unique parent records are returned.

This is compatible with all of the structure declarations, like returning two recordsets:

            var results = Connection().QuerySql(query + ";" + query, null,
				Query.Returns(Together<Parent, Child>.Records)
					.Then(Together<Parent, Child>.Records));

Finally, you can use parent/child processing in interface definitions. Setting IsParentAndChild=true on the Recordset attribute tells Insight to use Together Rather than Some when parsing the records.

		public interface IReadParentAndChildren
		{
			[Sql(query)]
			[Recordset(0, typeof(Parent), typeof(Child), IsParentAndChild = true)]
			IList<Parent> CanReturnParentAndChild();
		}

Bonus points to whoever updates the wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant