Igor's Blog

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

, ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
StrFunc() Online
300 George St Blogumentary
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Where to find the last item (Robbie's lost lyrics) in GravityFalls Lake level

Detecting print requests to print contents of a DIV with JavaScript

Using Google DFP with AdSense on responsive pages

How to change the email subject line in Gmail

Force PHP GD library to load JPEG images that have invalid data

How to uninstall Samsung Smart Switch from your Mac

Facebook adds weather forecasts but forgets to adjust them to your time zone

No sound or microphone when answering calls on an iPhone

Replacing the standard Odroid XU4 fan and heatsink

PHP PDO doesn't work binding multiple IN statement values

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again