Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 67044 invoked from network); 30 Nov 2005 19:51:44 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 30 Nov 2005 19:51:44 -0000 Received: (qmail 80352 invoked by uid 500); 30 Nov 2005 19:51:43 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 80325 invoked by uid 500); 30 Nov 2005 19:51:42 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 80314 invoked by uid 99); 30 Nov 2005 19:51:42 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Nov 2005 11:51:42 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [65.195.181.50] (HELO webRack01.Segel.com) (65.195.181.50) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Nov 2005 11:53:11 -0800 Received: from dbrack01.segel.com (ns2.segel.com [65.195.181.55]) by webRack01.Segel.com (Postfix) with ESMTP id 4002D17E3F for ; Wed, 30 Nov 2005 14:56:51 -0600 (CST) From: Michael Segel Reply-To: msegel@segel.com Organization: MSCC To: "Derby Discussion" Subject: Re: Recursive Select for Discussion Forum Date: Wed, 30 Nov 2005 13:51:29 -0600 User-Agent: KMail/1.8.2 References: <438344B6.2000003@Sun.COM> <438D367E.6030305@frontiernet.net> <438DF281.40706@sun.com> In-Reply-To: <438DF281.40706@sun.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200511301351.29961.msegel@segel.com> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N On Wednesday 30 November 2005 12:42, Rick Hillegas wrote: > Hi Michael, > > You could streamline your recursive walk by using a temporary table and > a database procedure. The temporary table would hold the ids you > recursively harvest. It would be populated by your database procedure, > which would walk up the levels of your hierarchy. When the procedure > returned, you could then join the temporary table to your > discussion_item table to get the details you needed. It's not an elegant > solution, but by running the procedure inside the database, you would > eliminate a lot of network handshaking. > > Derby does not support hierarchical queries. You're welcome to log an > enhancement request for this feature. > Well if you're going to use Temp tables, why not go outside of just SQL statements and then use cursors? By using prepared statements you could do this and maybe use a little bit of recursion. Since this is SELECT, you'd want to make sure that you do this out side of a Transaction. (Transactions and recursive SQL statements can get messy.) Pretty straight forward from there. You only need to prepare two statements, and each recursion requires a unique resultSet to be declared. Your table would look like this: Table foo: msg_id int -- (you can autogenerate or use long or something else depending on # of messages and retension rules.) parent_id int (set default to 0) msg_author ... msg_header ... msg_txt ... Then create an index on msg_id and also on parent_id; This can be done a couple of ways or permutations. Here's the simplest. Make the following CLASS variables; A database connection con; Create a vector to store the result sets. int rec_lvl = 0 (this is the level of recursion) Create and prepare the following select statement: SELECT msg_id, msg_author, msg_header, msg_txt FROM foo WHERE parent_id = ? ORDER BY msg_id; You then have two methods to write: 1) A Method to pull the data out of the result set, and pop it on to a vector, including the rec_lvl variable. (This will be used to build your tree) 2) The recursive method that finds all the children to the input value of parent_id. So the first time you call the method you pass in the value of 0 for the parent id. Then for each record you get back, you pass the result set to the popOnVector and the current rec_lvl value. You then call the recursive routine, passing in the current msg_id and (rec_lvl + 1); Thats all it takes. The actual building of this is left as an exercise to the student. HTH, But hey, what do I know? You initially wanted to write recursive SQL which is never a good idea. > Regards, > -Rick > > Michael McCutcheon wrote: > > I need to implement an online discussion forum, similar to JavaLobby. > > > > Similar kinds of discussion forums tend to use recursive selects, so > > that in a single select, all of the items for a particular discussion > > thread (and all sub threads, and their sub threads, etc) may be > > retrieved. > > > > For a table, I'm thinking of something like this: > > > > create table discussion_item > > ( > > id integer not null, > > parent_id integer not null, > > creator_user_id varchar, > > title varchar, > > message varchar > > ) > > > > There needs to be some kind of recursive functionality to select a > > item, and it's parents, and it's parents, etc. > > > > Does Derby support any kind of recursive selecting? I know that this > > is supported in Oracle and other databases. > > > > If it does not, is it planned to have it in the future? > > > > If not, are there any ways of handling scenarios like this without > > having to do a select for each item? > > > > Thanks for any input. > > > > Mike