Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Transitioning from standard SQL to JCR-SQL2 is not straight forward and presents some new challenges. One of the problems I faced when I started to use JCR-SQL2 was how do I select children nodes that have a particular type under a parent node whose ID I know.

To give some more background on this, I have namespaces defined like this:
 Namespaces
'wall' => 'http://travelblog.ws/ns/wall'
'pst' => 'http://travelblog.ws/ns/post'


Further I have a data type, tb:Post, defined like this as a CND file (this is a simplified version based on the actual definition):
 Data Type
<tb = 'http://travelblog.ws/ns'>
[tb:Post]
- tb:text (string)
- tb:postDate (date) mandatory


The tb:Post data type is used to create child nodes in the 'pst' namespace. These are attached to a parent node in the 'wall' namespace. The data looks something like this:
 Data
/ (root)
+-- wall:1
| +-- pst:1
| +-- pst:2
| ...
+-- wall:2
| +-- pst:3
| ...




So what I wanted to do was select all of the child nodes for say the wall:1 parent. I also wanted to do some sorting based on a property defined in tb:Post, tb:postDate in my case. Getting all the child nodes is simple if you have the parent node already, simply call getNodes() and you're done, but that doesn't sort your results, it also won't let you limit how much data is returned for paging purposes.

So we get to the JCR-SQL2 statement that will meet all of the above criteria...
 JCR=SQL2
SELECT * FROM [tb:Post] AS posts
WHERE posts.[jcr:path] LIKE '/wall:1/%'
ORDER BY tb:postDate DESC;


The above is a bit unintuitive at first because you're selecting from a data type?! Remember JCR doesn't define tables so in that context it makes sense. The where clause filters the data for the parent node using the ID of the parent and the jcr:path pseudo-property. The the sorting is done by tb:postDate.

It looks straight forward after getting it to work!

That simple example can be further built out to include support for pagination by adding the LIMIT clause.

-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Tiny Arcade revision 6 kit assembly and decal application

Jersey JAX-RS filters and interceptors execution order for a simple GET request

How to stop macOS adding shadows to window screenshots

How to run Atari Lynx games on the SNES Classic Mini

Maven dependency scopes with relation to WAR file packaging and the WEB-INF/lib directory

Hacking the Sonoff B1 WiFi LED bulb to run custom firmware

What does an idle WebLogic server run on the hour to cause a CPU spike

How to open the Sonoff B1 wifi LED bulb to access its internal circuitry

Australian release SNES mini classic unboxing and a quick play through

Troubleshooting high CPU usage for JVM threads

Recent Galleries

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity