From 9ef549c3e7a93240b90f520110378b19f5d27f45 Mon Sep 17 00:00:00 2001
From: Adam Gent <adam.gent@evocatus.com>
Date: Tue, 7 May 2013 09:16:56 -0400
Subject: [PATCH] Partial parser validate comparison incorrect. Fixed #26

---
 .../co/jirm/core/sql/SqlPartialParser.java    |  2 +-
 .../jirm/core/sql/SqlPartialParserTest.java   |  7 ++-
 .../sql/issue26-partial-test-validate.sql     | 60 +++++++++++++++++++
 3 files changed, 67 insertions(+), 2 deletions(-)
 create mode 100644 jirm-core/src/test/resources/co/jirm/core/sql/issue26-partial-test-validate.sql

diff --git a/jirm-core/src/main/java/co/jirm/core/sql/SqlPartialParser.java b/jirm-core/src/main/java/co/jirm/core/sql/SqlPartialParser.java
index 68fdc10..6bf6d73 100644
--- a/jirm-core/src/main/java/co/jirm/core/sql/SqlPartialParser.java
+++ b/jirm-core/src/main/java/co/jirm/core/sql/SqlPartialParser.java
@@ -574,7 +574,7 @@ protected ExpandedSql _expand(DeclarationSql f, Set<String> seenPaths) {
 				r.getStartIndex();
 				
 				DeclarationSql ds = e.getDeclaration();
-				boolean validate = ! r.isSame() || ds.getDeclaredSql().equals(r.getDeclaredSql());
+				boolean validate = ! r.isSame() || ds.inner().equals(r.inner());
 				check.state(validate, 
 						"Reference '> {}' in {} at line: {}" +
 						" does" +
diff --git a/jirm-core/src/test/java/co/jirm/core/sql/SqlPartialParserTest.java b/jirm-core/src/test/java/co/jirm/core/sql/SqlPartialParserTest.java
index 60656c2..8c4ccba 100644
--- a/jirm-core/src/test/java/co/jirm/core/sql/SqlPartialParserTest.java
+++ b/jirm-core/src/test/java/co/jirm/core/sql/SqlPartialParserTest.java
@@ -87,11 +87,16 @@ public void testExpand() throws Exception {
 	}
 	
 	@Test(expected=JirmIllegalStateException.class)
-	public void testValidate() throws Exception {
+	public void testValidateInvalid() throws Exception {
 		Parser p = SqlPartialParser.Parser.create();
 		p.expand("/co/jirm/core/sql/partial-test-validate.sql#other");
 	}
 	
+	@Test
+	public void testIssue26ValidateValid() throws Exception {
+		SqlPartialParser.parseFromPath("/co/jirm/core/sql/issue26-partial-test-validate.sql#other");
+	}
+	
 	@Test
 	public void testIssue25ValidateErrorMessage() throws Exception {
 		try {
diff --git a/jirm-core/src/test/resources/co/jirm/core/sql/issue26-partial-test-validate.sql b/jirm-core/src/test/resources/co/jirm/core/sql/issue26-partial-test-validate.sql
new file mode 100644
index 0000000..e6b8525
--- /dev/null
+++ b/jirm-core/src/test/resources/co/jirm/core/sql/issue26-partial-test-validate.sql
@@ -0,0 +1,60 @@
+SELECT
+-- {#stuff}
+c.id, c.name, c.tags, c.category, c.description, 
+c.division, c.experience_level as "experienceLevel", 
+c.locations, c.type, c.parent_id as "parentId", 
+g.latitude as "latitude", g.longitude as "longitude"
+-- {/stuff}
+FROM campaign c
+LEFT OUTER JOIN 
+	(SELECT DISTINCT cg.campaign, geo.latitude, geo.longitude from campaign_geo cg
+	INNER JOIN geo geo on geo.id = cg.geo 
+	WHERE geo.latitude IS NOT NULL AND geo.longitude IS NOT NULL AND cg.createts < now() -- {}
+	) g on g.campaign = c.id
+WHERE 
+-- {> #blah}
+c.type = 'JOBPAGE' AND c.createts < now() -- {}
+-- {<}
+ORDER BY c.createts ASC, c.id, g.latitude, g.longitude
+LIMIT 100 -- {}
+OFFSET 1 -- {}
+
+SELECT
+-- {> #stuff}
+c.id, c.name, c.tags, c.category, c.description, 
+c.division, c.experience_level as "experienceLevel", 
+c.locations, c.type, c.parent_id as "parentId", 
+g.latitude as "latitude", g.longitude as "longitude"
+-- {<}
+FROM campaign c
+LEFT OUTER JOIN 
+	(SELECT DISTINCT cg.campaign, geo.latitude, geo.longitude from campaign_geo cg
+	INNER JOIN geo geo on geo.id = cg.geo 
+	WHERE geo.latitude IS NOT NULL AND geo.longitude IS NOT NULL AND cg.createts < now() -- {}
+	) g on g.campaign = c.id
+WHERE 
+-- {#blah}
+c.type = 'JOBPAGE' AND c.createts < now() -- {}
+-- {/blah}
+ORDER BY c.createts ASC, c.id, g.latitude, g.longitude
+LIMIT 100 -- {}
+OFFSET 1 -- {}
+
+-- {#other}
+SELECT
+-- {> #stuff same }
+c.id, c.name, c.tags, c.category, c.description, 
+c.division, c.experience_level as "experienceLevel", 
+c.locations, c.type, c.parent_id as "parentId", 
+g.latitude as "latitude", g.longitude as "longitude"
+-- {<}
+FROM campaign c
+LEFT OUTER JOIN 
+	(SELECT DISTINCT cg.campaign, geo.latitude, geo.longitude from campaign_geo cg
+	INNER JOIN geo geo on geo.id = cg.geo 
+	WHERE geo.latitude IS NOT NULL AND geo.longitude IS NOT NULL AND cg.createts < now() -- {}
+	) g on g.campaign = c.id
+ORDER BY c.createts ASC, c.id, g.latitude, g.longitude
+LIMIT 100 -- {}
+OFFSET 1 -- {}
+-- {/other}