Skip to content

Commit

Permalink
Updated database schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ciioprof0 committed Aug 6, 2024
1 parent 14fca71 commit a23ad5f
Show file tree
Hide file tree
Showing 51 changed files with 1,145 additions and 237 deletions.
13 changes: 10 additions & 3 deletions info579/scripts/bash/create_procedures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ DB_USER="your_username"
DB_PASSWORD="your_password"
DB_NAME="stixd_corpus"

# Array of stored procedure scripts in the order they should be run
procedures_scripts=(
"../../sql/procedures/sp_check_prolog_constraints.sql"
"../../sql/procedures/sp_check_lexicon_constraints.sql"
)

# Create procedures
echo "Creating procedures..."
mysql -u $DB_USER -p$DB_PASSWORD $DB_NAME < ../../sql/procedures/17_prc_check_prolog_constraints.sql

echo "Procedures created successfully."
for script in "${procedures_scripts[@]}"; do
echo "Running $script..."
mysql -u $DB_USER -p$DB_PASSWORD $DB_NAME < $script
done
1 change: 0 additions & 1 deletion info579/scripts/bash/create_tables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ tables_scripts=(
"../../sql/schema/tables/05_create_table_stix_objects.sql"
"../../sql/schema/tables/06_create_table_documents.sql"
"../../sql/schema/tables/07_create_table_sentences.sql"
"../../sql/schema/tables/08_create_table_validation_results.sql"
"../../sql/schema/tables/09_create_table_lexicon.sql"
"../../sql/schema/tables/20_create_table_obj_doc_jt.sql"
"../../sql/schema/tables/21_create_table_doc_sent_jt.sql"
Expand Down
8 changes: 1 addition & 7 deletions info579/scripts/bash/create_triggers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ DB_NAME="stixd_corpus"

# Array of trigger creation scripts in the order they should be run
triggers_scripts=(
"../../sql/triggers/11_trg_lexicon_preposition_gender.sql"
"../../sql/triggers/12_trg_lexicon_preposition_check.sql"
"../../sql/triggers/13_trg_lexicon_word_class_prep.sql"
"../../sql/triggers/14_trg_lexicon_word_class_gender.sql"
"../../sql/triggers/15_trg_lexicon_prohibited_words.sql"
"../../sql/triggers/16_trg_lexicon_prohibited_intersections.sql"
"../../sql/triggers/18_trg_lexicon_prolog_constraints.sql"
"../../sql/triggers/master_triggers_lexicon.sql"
)

# Create triggers
Expand Down
2 changes: 1 addition & 1 deletion info579/scripts/bash/setup_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ DB_NAME="stixd_corpus"

# Create the database
echo "Creating database..."
mysql -u $DB_USER -p$DB_PASSWORD < ../../sql/setup/00_create_database.sql
mysql -u $DB_USER -p$DB_PASSWORD < ../../sql/setup/create_database.sql

echo "Database created successfully."
1 change: 0 additions & 1 deletion info579/scripts/python/create_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def main():
"../../sql/schema/tables/05_create_table_stix_objects.sql",
"../../sql/schema/tables/06_create_table_documents.sql",
"../../sql/schema/tables/07_create_table_sentences.sql",
"../../sql/schema/tables/08_create_table_validation_results.sql",
"../../sql/schema/tables/09_create_table_lexicon.sql",
"../../sql/schema/tables/20_create_table_obj_doc_jt.sql",
"../../sql/schema/tables/21_create_table_doc_sent_jt.sql",
Expand Down
2 changes: 1 addition & 1 deletion info579/scripts/python/setup_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def run_sql_script(script_path):
subprocess.run(["mysql", "-u", "your_username", "-pyour_password", "<", script_path], shell=True)

def main():
script_path = "../../sql/setup/00_create_database.sql"
script_path = "../../sql/setup/create_database.sql"
print(f"Running {script_path}...")
run_sql_script(script_path)
print("Database created successfully.")
Expand Down
79 changes: 79 additions & 0 deletions info579/sql/procedures/sp_check_lexicon_constraints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-- Create procedure to check for prolog constraints
DELIMITER //
CREATE PROCEDURE stixd_corpus.check_prolog_constraints(IN base_form VARCHAR(255))
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE constraint_violation VARCHAR(255);
DECLARE constraint_message VARCHAR(255);

DECLARE cur CURSOR FOR SELECT pattern, message FROM stixd_corpus.prolog_constraints;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur;

read_loop: LOOP
FETCH cur INTO constraint_violation, constraint_message;
IF done THEN
LEAVE read_loop;
END IF;
IF NOT (base_form REGEXP constraint_violation) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = constraint_message;
END IF;
END LOOP;

CLOSE cur;
END;
//
DELIMITER ;

-- Create main procedure to check lexicon constraints
DELIMITER //
CREATE PROCEDURE stixd_corpus.sp_check_lexicon_constraints(
IN new_base_form VARCHAR(255),
IN new_word_class VARCHAR(31),
IN new_preposition INT,
IN new_gender VARCHAR(7)
)
BEGIN
-- Step 1: Check prolog constraints
CALL stixd_corpus.check_prolog_constraints(new_base_form);

-- Step 2: Check prohibited words
DECLARE prohibited_word_found INT DEFAULT 0;
SELECT COUNT(*)
INTO prohibited_word_found
FROM stixd_corpus.prohibited_words
WHERE word = new_base_form;

IF prohibited_word_found > 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'base_form contains a prohibited word';
END IF;

-- Step 3: Check prohibited intersections
CALL stixd_corpus.check_prohib_intersections(new_base_form);

-- Step 4: Ensure preposition is not null if word_class starts with adj_tr or dv_
IF (new_word_class LIKE 'adj_tr%' OR new_word_class LIKE 'dv_%') THEN
IF (new_preposition IS NULL) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'preposition must be NOT NULL if word_class starts with "adj_tr" or "dv_"';
END IF;
END IF;

-- Step 5: Ensure gender is not null if word_class starts with noun_ or pn
IF (new_word_class LIKE 'noun_%' OR new_word_class LIKE 'pn%') THEN
IF (new_gender IS NULL) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'gender must be NOT NULL if word_class starts with "noun_" or "pn"';
END IF;
END IF;

-- Step 6: Enforce the mutual exclusivity of preposition and gender
IF (new_preposition IS NOT NULL AND new_gender IS NOT NULL) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'preposition and gender cannot both be NOT NULL';
END IF;
END//
//
DELIMITER ;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Create procedure to check for prolog constraints (stix_corpus_18.sql)
-- Create procedure to check for prolog constraints )
DELIMITER //
CREATE PROCEDURE stixd_corpus.check_prolog_constraints (IN base_form VARCHAR(255))
BEGIN
Expand Down
42 changes: 42 additions & 0 deletions info579/sql/procedures/sp_run_seed_scripts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
DELIMITER //

CREATE PROCEDURE stixd_corpus.run_seed_scripts()
BEGIN
-- Seed Genders
SET @sql = 'SOURCE ../seeds/seed_genders.sql';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Seed Prohibited Words
SET @sql = 'SOURCE ../seeds/seed_prohib_words.sql';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Seed Prohibited Intersections
SET @sql = 'SOURCE ../seeds/seed_prohib_intersections.sql';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Seed Prolog Constraints
SET @sql = 'SOURCE ../seeds/seed_prolog_constraints.sql';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Seed Word Tags
SET @sql = 'SOURCE ../seeds/seed_word_tags.sql';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Seed Special Characters
SET @sql = 'SOURCE ../seeds/seed_spec_chars.sql';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //

DELIMITER ;
6 changes: 0 additions & 6 deletions info579/sql/schema/tables/00_create_table_genders.sql

This file was deleted.

8 changes: 0 additions & 8 deletions info579/sql/schema/tables/01_create_table_special_chars.sql

This file was deleted.

6 changes: 0 additions & 6 deletions info579/sql/schema/tables/02_create_table_prohib_words.sql

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions info579/sql/schema/tables/06_create_table_documents.sql

This file was deleted.

10 changes: 0 additions & 10 deletions info579/sql/schema/tables/07_create_table_sentences.sql

This file was deleted.

14 changes: 14 additions & 0 deletions info579/sql/schema/tables/creat_table_word_tags.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Create the stixd_corpus.word_tags table in the specified database
-- DROP TABLE IF EXISTS stixd_corpus.word_tags;
CREATE TABLE IF NOT EXISTS stixd_corpus.word_tags (
id INT AUTO_INCREMENT PRIMARY KEY,
word_tag VARCHAR(12) NOT NULL,
first_arg VARCHAR(31) NOT NULL,
second_arg VARCHAR(31) NOT NULL,
third_arg VARCHAR(31) DEFAULT NULL,
word_class VARCHAR(31) NOT NULL
);

-- SHOW CREATE TABLE stixd_corpus.word_tags

-- Populating the table done with SQL seed script.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the doc_lex_jt table in the specified database
DROP TABLE IF EXISTS stixd_corpus.doc_lex_jt;
CREATE TABLE stixd_corpus.doc_lex_jt (
-- Create the stixd_corpus.doc_lex_jt table
-- DROP TABLE IF EXISTS stixd_corpus.doc_lex_jt;
CREATE TABLE IF NOT EXISTS stixd_corpus.doc_lex_jt (
doc_id INT,
lex_id INT,
PRIMARY KEY (doc_id, lex_id),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the doc_sent_jt table in the specified database (stix_corpus_20.sql)
DROP TABLE IF EXISTS stixd_corpus.doc_sent_jt;
CREATE TABLE stixd_corpus.doc_sent_jt (
-- Create the stixd_corpus.doc_sent_jt table
-- DROP TABLE IF EXISTS stixd_corpus.doc_sent_jt;
CREATE TABLE IF NOT EXISTS stixd_corpus.doc_sent_jt (
doc_id INT,
sent_id INT,
PRIMARY KEY (doc_id, sent_id),
Expand Down
12 changes: 12 additions & 0 deletions info579/sql/schema/tables/create_table_documents.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Create the stixd_corpus.documents table
-- DROP TABLE IF EXISTS stixd_corpus.documents;
CREATE TABLE IF NOT EXISTS stixd_corpus.documents (
doc_id INT AUTO_INCREMENT PRIMARY KEY,
raw_text LONGTEXT,
raw_text_hash CHAR(64) UNIQUE,
proc_text JSON,
metadata JSON
);
-- SHOW CREATE TABLE stixd_corpus.documents;

-- Populating table done from Python script.
8 changes: 8 additions & 0 deletions info579/sql/schema/tables/create_table_genders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Create the stixd_corpus.genders table
-- DROP TABLE IF EXISTS stixd_corpus.genders;
CREATE TABLE IF NOT EXISTS stixd_corpus.genders (
gender VARCHAR(7) PRIMARY KEY
);
-- SHOW CREATE TABLE stixd_corpus.genders;

-- Populating the table done with SQL seed script.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the lexicon table in the specified database
DROP TABLE IF EXISTS stixd_corpus.lexicon;
CREATE TABLE stixd_corpus.lexicon (
-- Create the stixd_corpus.lexicon table
-- DROP TABLE IF EXISTS stixd_corpus.lexicon;
CREATE TABLE IF NOT EXISTS stixd_corpus.lexicon (
lex_id INT AUTO_INCREMENT PRIMARY KEY,
word_class VARCHAR(31),
base_form VARCHAR(255),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the obj_doc_jt table in the specified database
DROP TABLE IF EXISTS stixd_corpus.obj_doc_jt;
CREATE TABLE stixd_corpus.obj_doc_jt (
-- Create the stixd_corpus.obj_doc_jt table
-- DROP TABLE IF EXISTS stixd_corpus.obj_doc_jt;
CREATE TABLE IF NOT EXISTS stixd_corpus.obj_doc_jt (
obj_id VARCHAR(292),
doc_id INT,
PRIMARY KEY (obj_id, doc_id),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the obj_lex_jt table in the specified database
DROP TABLE IF EXISTS stixd_corpus.obj_lex_jt;
CREATE TABLE stixd_corpus.obj_lex_jt (
-- Create the stixd_corpus.obj_lex_jt table
-- DROP TABLE IF EXISTS stixd_corpus.obj_lex_jt;
CREATE TABLE IF NOT EXISTS stixd_corpus.obj_lex_jt (
obj_id VARCHAR(292),
lex_id INT,
PRIMARY KEY (obj_id, lex_id),
Expand Down
10 changes: 10 additions & 0 deletions info579/sql/schema/tables/create_table_prohib_intersections.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Create the stixd_corpus.prohibited_intersections table
-- DROP TABLE IF EXISTS stixd_corpus.prohibited_intersections;
CREATE TABLE IF NOT EXISTS stixd_corpus.prohibited_intersections (
class1 VARCHAR(31),
class2 VARCHAR(31),
PRIMARY KEY (class1, class2)
);
-- SHOW CREATE TABLE stixd_corpus.prohibited_intersections;

-- Populating the table done with SQL seed script.
8 changes: 8 additions & 0 deletions info579/sql/schema/tables/create_table_prohib_words.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Create the stixd_corpus.prohibited_words table
-- DROP TABLE IF EXISTS stixd_corpus.prohibited_words;
CREATE TABLE IF NOT EXISTS stixd_corpus.prohibited_words (
word VARCHAR(20) PRIMARY KEY
);
-- SHOW CREATE TABLE stixd_corpus.prohibited_words;

-- Populating the table done with SQL seed script.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the prolog_constraints table in the specified database
DROP TABLE IF EXISTS stixd_corpus.prolog_constraints;
CREATE TABLE stixd_corpus.prolog_constraints (
-- Create the stixd_corpus.prolog_constraints table
-- DROP TABLE IF EXISTS stixd_corpus.prolog_constraints;
CREATE TABLE IF NOT EXISTS stixd_corpus.prolog_constraints (
constraint_id INT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(511),
pattern VARCHAR(255),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Create the sent_lex_jt table in the specified database
DROP TABLE IF EXISTS stixd_corpus.sent_lex_jt;
CREATE TABLE stixd_corpus.sent_lex_jt (
-- Create the stixd_corpus.sent_lex_jt table
-- DROP TABLE IF EXISTS stixd_corpus.sent_lex_jt;
CREATE TABLE IF NOT EXISTS stixd_corpus.sent_lex_jt (
sent_id INT,
lex_id INT,
PRIMARY KEY (sent_id, lex_id),
Expand Down
Loading

0 comments on commit a23ad5f

Please sign in to comment.