Commit a7e975c4 authored by Stéphane Albert's avatar Stéphane Albert

ENH: Removed .sql files.

parent 8ce6fb69
###############################################################################
# SQL Queries.
#------------------------------------------------------------------------------
# SQL queries source files.
set( Monteverdi_SQL_FILES
sql_db_create.sql
sql_db_setup.sql
sql_queries_insert.sql
sql_queries_select.sql
)
# SQL C++ target file
set( Monteverdi_SQL_OUTPUT
${CMAKE_SOURCE_DIR}/Code/Common/Core/mvdSql.cxx
)
#
# Configuration option to enable/disable ?
option(
GENERATE_SQL
"Generate C++ source files from SQL queries source files. WARNING: 'make clean' will delete the source ${Monteverdi_SQL_OUTPUT} file!"
OFF
)
if( GENERATE_SQL )
message( AUTHOR_WARNING "WARNING: GENERATE_SQL option is ON. 'make clean' will delete the source ${Monteverdi_SQL_OUTPUT} file!" )
# Create source list containing absolute filenames because custom commands
# will be called from build dir.
foreach( SOURCE_FILE ${Monteverdi_SQL_FILES} )
get_filename_component( FILE ${SOURCE_FILE} ABSOLUTE )
set( Monteverdi_SQL_SOURCES ${Monteverdi_SQL_SOURCES} ${FILE} )
endforeach( SOURCE_FILE )
# message( STATUS "SQL source files: ${Monteverdi_SQL_SOURCES}")
# message( STATUS "SQL output file: ${Monteverdi_SQL_OUTPUT}")
add_custom_target( Monteverdi_Sql
ALL
#${CMAKE_SOURCE_DIR}/Scripts/genc.sh -s .sql ${Monteverdi_SQL_SOURCES} > ${Monteverdi_SQL_OUTPUT}
#COMMENT "Generating ${Monteverdi_SQL_OUTPUT}"
DEPENDS ${Monteverdi_SQL_OUTPUT}
)
add_custom_command(
OUTPUT ${Monteverdi_SQL_OUTPUT}
DEPENDS ${Monteverdi_SQL_SOURCES}
COMMENT "Generating ${Monteverdi_SQL_OUTPUT}"
COMMAND ${CMAKE_SOURCE_DIR}/Scripts/genc.sh -s .sql ${Monteverdi_SQL_SOURCES} > ${Monteverdi_SQL_OUTPUT}
)
# add_custom_command(
# TARGET Monteverdi_Sql
# PRE_BUILD
# COMMENT "Generating ${Monteverdi_SQL_OUTPUT}"
# COMMAND "${CMAKE_SOURCE_DIR}/Scripts/genc.sh -s .sql ${Monteverdi_SQL_SOURCES} > ${Monteverdi_SQL_OUTPUT}"
# )
endif( GENERATE_SQL )
-----------------------------------------------------------------------------
CREATE TABLE database_attribute
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
value TEXT
);
CREATE UNIQUE INDEX idx_database_attribute_name ON database_attribute( name );
-----------------------------------------------------------------------------
CREATE TABLE dataset
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT NOT NULL,
alias TEXT
);
CREATE UNIQUE INDEX idx_dataset_hash ON dataset( hash );
-----------------------------------------------------------------------------
CREATE TABLE tag
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT NOT NULL
);
CREATE UNIQUE INDEX idx_tag_label ON tag( label );
-----------------------------------------------------------------------------
CREATE TABLE node
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
parent_id INTEGER,
tag_id INTEGER,
label TEXT,
level INTEGER,
path TEXT,
FOREIGN KEY( parent_id ) REFERENCES node( id ),
FOREIGN KEY( tag_id ) REFERENCES tag( id )
);
CREATE INDEX idx_node_tid ON node( tag_id );
CREATE INDEX idx_node_label ON node( label );
CREATE INDEX idx_node_pid ON node( parent_id );
-----------------------------------------------------------------------------
CREATE TABLE dataset_tag_membership(
dataset_id INTEGER,
tag_id INTEGER,
PRIMARY KEY( dataset_id, tag_id ),
FOREIGN KEY( dataset_id ) REFERENCES dataset( id ) ON DELETE CASCADE,
FOREIGN KEY( tag_id ) REFERENCES tag( id ) ON DELETE CASCADE
);
CREATE INDEX idx_ds_tag_membership_dataset_id
ON dataset_tag_membership( dataset_id );
CREATE INDEX idx_ds_tag_membership_tag_id
ON dataset_tag_membership( tag_id );
-----------------------------------------------------------------------------
CREATE TABLE dataset_node_membership(
dataset_id INTEGER,
node_id INTEGER,
PRIMARY KEY( dataset_id, node_id ),
FOREIGN KEY( dataset_id ) REFERENCES dataset( id ) ON DELETE CASCADE,
FOREIGN KEY( node_id ) REFERENCES node( id ) ON DELETE CASCADE
);
CREATE INDEX idx_ds_node_membership_dataset_id
ON dataset_node_membership( dataset_id );
CREATE INDEX idx_ds_node_membership_node_id
ON dataset_node_membership( node_id );
-----------------------------------------------------------------------------
CREATE TABLE dataset_attribute
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
dataset_id INTEGER NOT NULL REFERENCES dataset( id ),
rank INTEGER NOT NULL,
name TEXT NOT NULL,
value TEXT
);
CREATE INDEX idx_dataset_attribute_dsid ON dataset_attribute( dataset_id );
CREATE INDEX idx_dataset_attribute_name ON dataset_attribute( name );
-----------------------------------------------------------------------------
INSERT INTO tag( id, label ) VALUES( 1, 'Root' );
INSERT INTO tag( id, label ) VALUES( 2, 'Datasets' );
INSERT INTO tag( id, label ) VALUES( 3, 'Temporary' );
-----------------------------------------------------------------------------
-- Root
INSERT INTO node( id, parent_id, tag_id, label, level, path )
VALUES( 1, NULL, 1, 'Root', 0, '/');
-- Root/Datasets
INSERT INTO node( id, parent_id, tag_id, label, level, path )
VALUES( 2, 1, 2, 'Datasets', 1, '/1' );
-- Root/Datasets/Cached
INSERT INTO node( id, parent_id, tag_id, label, level, path )
VALUES( 3, 2, 3, 'Temporary', 2, '/1/2' );
-----------------------------------------------------------------------------
-- SQLQ_INSERT_NODE
-- Insert tag-node for tag :child_label under parent-node of tag-node
-- :parent_label.
INSERT INTO node( parent_id, tag_id, label, level, path )
SELECT -- ID (automatic)
node.id AS parent_id,
(SELECT tag.id FROM tag WHERE tag.label=:child_label) AS tag_id,
:child_label AS label,
node.level+1 AS level,
rtrim( node.path, '/' ) || '/' || node.id AS path
FROM node
-- JOIN tag ON node.tag_id=tag.id
-- WHERE tag.id=(SELECT tag.id FROM tag WHERE tag.label=:parent_label)
WHERE node.label=:parent_label;
-----------------------------------------------------------------------------
-- SQLQ_INSERT_NODE_CHILD
-- Insert node labelled as :child_label under parent node :parent_id
INSERT INTO node( parent_id, tag_id, label, level, path )
SELECT -- ID (automatic)
node.id AS parent_id,
(SELECT tag.id FROM tag WHERE tag.label=:child_label) AS tag_id,
:child_label AS label,
node.level+1 AS level,
rtrim( node.path, '/' ) || '/' || node.id AS path
FROM node
WHERE node.id=:parent_id;
-----------------------------------------------------------------------------
-- SQLQ_INSERT_DATASET_TAG_MEMBERSHIP
-- Add dataset-membership of dataset %1 to each tag related to
-- %2 tag-node path list of the form (<id_0>, ...).
INSERT INTO dataset_tag_membership( dataset_id, tag_id )
SELECT %1 AS 'dataset_id',
node.tag_id
FROM node
JOIN tag ON node.tag_id=tag.id
WHERE node.id IN (%2);
-----------------------------------------------------------------------------
-- SQLQ_INSERT_DATASET_NODE_MEMBERSHIP
-- Add dataset-membership of dataset identified by :dataset_id to node
-- identified bby :node_id.
INSERT INTO dataset_node_membership( dataset_id, node_id )
VALUES( :dataset_id, :node_id );
-----------------------------------------------------------------------------
-- List datasets tagged by given label.
SELECT dataset.id, tag.label, dataset.alias, dataset.hash
FROM dataset
JOIN dataset_membership ON dataset.id=dataset_membership.dataset_id
JOIN tag ON dataset_membership.tag_id=tag.id
WHERE tag.label='Datasets';
-----------------------------------------------------------------------------
-- List tags marking a given dataset ordered by tree level.
SELECT node.path, node.level, tag.id AS id, tag.label
FROM node
JOIN tag ON node.tag_id=tag.id
JOIN dataset_membership ON tag.id=dataset_membership.tag_id
WHERE dataset_membership.dataset_id=2
ORDER BY node.level;
-----------------------------------------------------------------------------
-- List tag-node given tag-label.
SELECT tag.label,
node.id,
node.parent_id,
node.tag_id,
node.level,
node.path
FROM node
JOIN tag ON node.tag_id=tag.id
WHERE node.tag_id=2
ORDER BY node.level;
-----------------------------------------------------------------------------
-- List hierarchy tree as column indents.
SELECT tn_0.id AS level_0,
tn_1.id AS level_1,
tn_2.id AS level_2,
tn_3.id AS level_3
FROM node AS tn_0
LEFT JOIN node AS tn_1 ON tn_0.id=tn_1.parent_id
LEFT JOIN node AS tn_2 ON tn_1.id=tn_2.parent_id
LEFT JOIN node AS tn_3 ON tn_2.id=tn_3.parent_id
LEFT JOIN node AS tn_4 ON tn_3.id=tn_4.parent_id
WHERE tn_0.tag_id=(SELECT tag.id FROM tag WHERE tag.label='Root');
-----------------------------------------------------------------------------
-- List all leaf nodes.
SELECT node.id, node.level, node.path, tag.label
FROM node
LEFT JOIN node AS tn_2 ON node.id = tn_2.parent_id
JOIN tag ON node.tag_id=tag.id
WHERE tn_2.id IS NULL;
-----------------------------------------------------------------------------
-- List all non-leaf nodes.
SELECT node.id, node.level, node.path, tag.label, tn_2.id AS child_id
FROM node
JOIN node AS tn_2 ON node.id = tn_2.parent_id
JOIN tag ON node.tag_id=tag.id
ORDER BY node.id;
-----------------------------------------------------------------------------
-- List all children of all/given (non-leaf) node(s).
SELECT node.id, node.level, node.path, tag.label, tn_2.id AS child_id, tn_2.tag_id AS child_tag_id, t2.label
FROM node
JOIN node AS tn_2 ON node.id = tn_2.parent_id
JOIN tag ON node.tag_id=tag.id
JOIN tag AS t2 ON tn_2.tag_id=t2.id
-- WHERE node.tag_id=(SELECT tag.id FROM tag WHERE tag.label='Root')
ORDER BY node.id;
-----------------------------------------------------------------------------
-- Select root tag-node.
SELECT node.id, node.parent_id, node.tag_id, node.level, node.path, tag.label
FROM node
JOIN tag ON node.tag_id=tag.id
WHERE (node.parent_id IS NULL) AND (node.level=0);
-----------------------------------------------------------------------------
-- Select all children of a node which are leaves (have no child).
SELECT
-- node_i.*, tag_i.label,
node_ip1.id,
node_ip1.parent_id,
node_ip1.tag_id,
node_ip1.level,
node_ip1.path,
tag_ip1.label
FROM node AS node_i
JOIN node AS node_ip1 ON node_i.id=node_ip1.parent_id
LEFT JOIN node AS node_ip2 ON node_ip1.id=node_ip2.parent_id
JOIN tag AS tag_i ON node_i.tag_id=tag_i.id
JOIN tag AS tag_ip1 ON node_ip1.tag_id=tag_ip1.id
WHERE node_i.tag_id=2 AND node_ip2.id IS NULL;
-----------------------------------------------------------------------------
-- SQLQ_SELECT_NODE_BY_TAG_LABEL
-- Find node given tag label.
SELECT node.id,
node.parent_id,
node.tag_id,
node.level,
node.path,
tag.label
FROM node
JOIN tag ON node.tag_id=tag.id
WHERE node.tag_id=(SELECT tag.id FROM tag WHERE tag.label=:label);
-----------------------------------------------------------------------------
-- SQLQ_SELECT_NODE_ROOT
-- Find root node.
SELECT node.id,
node.parent_id,
node.tag_id,
node.label,
node.level,
node.path
FROM node
WHERE (node.parent_id IS NULL) AND (node.level=0);
-----------------------------------------------------------------------------
-- SQLQ_SELECT_NODE
-- Find root node.
SELECT node.id,
node.parent_id,
node.tag_id,
node.label,
node.level,
node.path
FROM node
WHERE node.id=:id;
-----------------------------------------------------------------------------
-- SQLQ_SELECT_NODE_CHILDREN
-- List direct children of node identified by :node_id
SELECT node_ip1.id,
node_ip1.parent_id,
node_ip1.tag_id,
node_ip1.label,
node_ip1.level,
node_ip1.path
FROM node AS node_i
JOIN node AS node_ip1 ON node_i.id=node_ip1.parent_id
WHERE node_i.id=:node_id;
-----------------------------------------------------------------------------
-- SQLQ_SELECT_NODE_CHILD
-- Find child labelled :child_label of node identified by :node_id
SELECT node_ip1.id,
node_ip1.parent_id,
node_ip1.tag_id,
node_ip1.label,
node_ip1.level,
node_ip1.path
FROM node AS node_i
JOIN node AS node_ip1 ON node_i.id=node_ip1.parent_id
WHERE node_i.id=:node_id AND node_ip1.label=:child_label;
-----------------------------------------------------------------------------
-- SQLQ_SELECT_NODE_DATASETS
-- Find datatsets attached to node identified by :node_id.
SELECT dataset.id,
dataset.hash,
dataset.alias
FROM dataset
JOIN dataset_node_membership ON dataset.id=dataset_node_membership.dataset_id
WHERE dataset_node_membership.node_id=:node_id;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment