From 59460772e3f5cab6933a4c6a14220eeff3bb2c40 Mon Sep 17 00:00:00 2001
From: scottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Wed, 2 Dec 2009 04:37:32 +0000
Subject: [PATCH] Add two new tables to the query schema: case_branch and
 from_relation.

Also: create a foreign key in stored_query pointing to
from_relation.

M    Open-ILS/src/sql/Pg/002.schema.config.sql
A    Open-ILS/src/sql/Pg/upgrade/0100.schema.query-case-from.sql
M    Open-ILS/examples/fm_IDL.xml


git-svn-id: svn://svn.open-ils.org/ILS/trunk@15052 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 Open-ILS/examples/fm_IDL.xml                       | 43 +++++++++++++++-
 Open-ILS/src/sql/Pg/002.schema.config.sql          |  2 +-
 .../sql/Pg/upgrade/0100.schema.query-case-from.sql | 58 ++++++++++++++++++++++
 3 files changed, 101 insertions(+), 2 deletions(-)
 create mode 100644 Open-ILS/src/sql/Pg/upgrade/0100.schema.query-case-from.sql

diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml
index 5266f754ca..cb8c04270a 100644
--- a/Open-ILS/examples/fm_IDL.xml
+++ b/Open-ILS/examples/fm_IDL.xml
@@ -5422,7 +5422,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 	</class>
 
 	<class id="qxp" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="query::expression" oils_persist:tablename="query.expression" reporter:label="Expression">
-		<fields oils_persist:primary="id" oils_persist:sequence="">
+		<fields oils_persist:primary="id" oils_persist:sequence="expression_id_seq">
 			<field reporter:label="Expression ID" name="id" reporter:datatype="id"/>
 			<field reporter:label="Expression Type" name="type" reporter:datatype="text"/>
 			<field reporter:label="Is Parenthesized" name="parenthesize" reporter:datatype="bool"/>
@@ -5450,6 +5450,47 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 		</permacrud>
 	</class>
 
+	<class id="qcb" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="query::case_branch" oils_persist:tablename="query.case_branch" reporter:label="Case Branch">
+		<fields oils_persist:primary="id" oils_persist:sequence="case_branch_id_seq">
+			<field reporter:label="Case Branch ID" name="id" reporter:datatype="id"/>
+			<field reporter:label="Parent Expression" name="parent_expr" reporter:datatype="link"/>
+			<field reporter:label="Sequence Number" name="seq_no" reporter:datatype="int"/>
+			<field reporter:label="Condition" name="condition" reporter:datatype="link"/>
+			<field reporter:label="Result" name="result" reporter:datatype="link"/>
+		</fields>
+		<links>
+			<link field="parent_expr" reltype="has_a" key="id" map="" class="qxp"/>
+			<link field="condition" reltype="might_have" key="id" map="" class="qxp"/>
+			<link field="result" reltype="has_a" key="id" map="" class="qxp"/>
+		</links>
+		<permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
+		</permacrud>
+	</class>
+
+	<class id="qfr" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="query::from_relation" oils_persist:tablename="query.from_relation" reporter:label="From Relation">
+		<fields oils_persist:primary="id" oils_persist:sequence="from_relation_id_seq">
+			<field reporter:label="From Relation ID" name="id" reporter:datatype="id"/>
+			<field reporter:label="From Relation Type" name="type" reporter:datatype="text"/>
+			<field reporter:label="Table Name" name="table_name" reporter:datatype="text"/>
+			<field reporter:label="Class Name" name="class_name" reporter:datatype="text"/>
+			<field reporter:label="Subquery ID" name="subquery" reporter:datatype="link"/>
+			<field reporter:label="Function Call ID" name="function_call" reporter:datatype="link"/>
+			<field reporter:label="Table Alias" name="table_alias" reporter:datatype="text"/>
+			<field reporter:label="Parent Relation ID" name="parent_relation" reporter:datatype="link"/>
+			<field reporter:label="Sequence Number" name="seq_no" reporter:datatype="int"/>
+			<field reporter:label="Join Type" name="join_type" reporter:datatype="text"/>
+			<field reporter:label="On Clause ID" name="on_clause" reporter:datatype="link"/>
+		</fields>
+		<links>
+			<link field="subquery" reltype="might_have" key="id" map="" class="qsq"/>
+			<link field="function_call" reltype="might_have" key="id" map="" class="qxp"/>
+			<link field="parent_relation" reltype="might_have" key="id" map="" class="qfr"/>
+			<link field="on_clause" reltype="might_have" key="id" map="" class="qxp"/>
+		</links>
+		<permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
+		</permacrud>
+	</class>
+
 	<!-- ********************************************************************************************************************* -->
 	<!-- What follows is a set of example extensions that are useful for PINES.  Comment out or remove if you don't want them. -->
 	<!-- ********************************************************************************************************************* -->
diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql
index 11f32a2e01..3cf3cbc205 100644
--- a/Open-ILS/src/sql/Pg/002.schema.config.sql
+++ b/Open-ILS/src/sql/Pg/002.schema.config.sql
@@ -51,7 +51,7 @@ CREATE TABLE config.upgrade_log (
     install_date    TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
 );
 
-INSERT INTO config.upgrade_log (version) VALUES ('0099'); -- Scott McKellar
+INSERT INTO config.upgrade_log (version) VALUES ('0100'); -- Scott McKellar
 
 CREATE TABLE config.bib_source (
 	id		SERIAL	PRIMARY KEY,
diff --git a/Open-ILS/src/sql/Pg/upgrade/0100.schema.query-case-from.sql b/Open-ILS/src/sql/Pg/upgrade/0100.schema.query-case-from.sql
new file mode 100644
index 0000000000..86ed962380
--- /dev/null
+++ b/Open-ILS/src/sql/Pg/upgrade/0100.schema.query-case-from.sql
@@ -0,0 +1,58 @@
+BEGIN;
+
+INSERT INTO config.upgrade_log (version) VALUES ('0100'); -- Scott McKellar
+
+CREATE TABLE query.case_branch (
+	id            SERIAL        PRIMARY KEY,
+	parent_expr   INT           NOT NULL REFERENCES query.expression
+	                            ON DELETE CASCADE
+	                            DEFERRABLE INITIALLY DEFERRED,
+	seq_no        INT           NOT NULL,
+	condition     INT           REFERENCES query.expression
+	                            DEFERRABLE INITIALLY DEFERRED,
+	result        INT           NOT NULL REFERENCES query.expression
+	                            DEFERRABLE INITIALLY DEFERRED,
+	CONSTRAINT case_branch_parent_seq UNIQUE (parent_expr, seq_no)
+);
+
+CREATE TABLE query.from_relation (
+	id               SERIAL        PRIMARY KEY,
+	type             TEXT          NOT NULL CONSTRAINT relation_type CHECK (
+	                                   type IN ( 'RELATION', 'SUBQUERY', 'FUNCTION' ) ),
+	table_name       TEXT,
+	class_name       TEXT,
+	subquery         INT           REFERENCES query.stored_query,
+	function_call    INT           REFERENCES query.expression,
+	table_alias      TEXT          NOT NULL,
+	parent_relation  INT           REFERENCES query.from_relation
+	                               ON DELETE CASCADE
+	                               DEFERRABLE INITIALLY DEFERRED,
+	seq_no           INT           NOT NULL DEFAULT 1,
+	join_type        TEXT          CONSTRAINT good_join_type CHECK (
+	                                   join_type IS NULL OR join_type IN
+	                                   ( 'INNER', 'LEFT', 'RIGHT', 'FULL' )
+	                               ),
+	on_clause        INT           REFERENCES query.expression
+	                               DEFERRABLE INITIALLY DEFERRED,
+	CONSTRAINT join_or_core CHECK (
+	    ( parent_relation IS NULL AND join_type IS NULL 
+	      AND on_clause IS NULL and table_alias IS NULL )
+	    OR
+	    ( parent_relation IS NOT NULL AND join_type IS NOT NULL
+	      AND on_clause IS NOT NULL )
+	)
+);
+
+CREATE UNIQUE INDEX from_parent_seq
+	ON query.from_relation( parent_relation, seq_no )
+	WHERE parent_relation IS NOT NULL;
+
+-- The following foreign key had to be deferred until
+-- query.from_relation existed
+
+ALTER TABLE query.stored_query
+	ADD FOREIGN KEY (from_clause)
+	REFERENCES query.from_relation
+	DEFERRABLE INITIALLY DEFERRED;
+
+COMMIT;
-- 
2.11.0