Initial commit

This commit is contained in:
Michael Becker 2024-04-17 22:33:56 -04:00
parent 7141146cb8
commit 76540c574d
40 changed files with 1133 additions and 0 deletions

View File

@ -0,0 +1,7 @@
DROP FUNCTION IF EXISTS mocha_get_current_tenant;
CREATE FUNCTION mocha_get_current_tenant
(
)
RETURNS INT
RETURN @current_tenant_id;

View File

@ -0,0 +1,13 @@
DROP FUNCTION IF EXISTS mocha_get_instance_by_global_identifier;
CREATE FUNCTION mocha_get_instance_by_global_identifier
(
p_global_identifier CHAR(40)
)
RETURNS INT
DETERMINISTIC
RETURN (
SELECT id FROM mocha_instances
WHERE (tenant_id = mocha_get_current_tenant() OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = mocha_get_current_tenant()))
AND mocha_normalize_uuid(global_identifier) = mocha_normalize_uuid(p_global_identifier)
ORDER BY id DESC LIMIT 1);

View File

@ -0,0 +1,17 @@
DROP FUNCTION IF EXISTS mocha_get_instance_by_key;
CREATE FUNCTION mocha_get_instance_by_key
(
p_class_id INT,
p_inst_id INT
)
RETURNS INT
RETURN (SELECT id FROM mocha_instances
WHERE (tenant_id = mocha_get_current_tenant()
OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = mocha_get_current_tenant())
)
AND class_id = p_class_id
AND inst_id = p_inst_id
LIMIT 1
);

View File

@ -0,0 +1,14 @@
DROP FUNCTION IF EXISTS mocha_get_instance_key;
CREATE FUNCTION mocha_get_instance_key
(
p_inst_id INT
)
RETURNS VARCHAR(50)
BEGIN
RETURN (SELECT CONCAT(class_id, '$', inst_id) FROM mocha_instances WHERE tenant_id = mocha_get_current_tenant() AND id = p_inst_id);
END;

View File

@ -0,0 +1,15 @@
DROP FUNCTION IF EXISTS mocha_get_next_inst_id;
CREATE FUNCTION mocha_get_next_inst_id
(
p_class_id INT
)
RETURNS INT
BEGIN
DECLARE next_inst_id INT;
SET next_inst_id = (SELECT MAX(inst_id) + 1 FROM mocha_instances WHERE tenant_id = mocha_get_current_tenant() AND class_id = p_class_id);
IF NOT next_inst_id IS NULL THEN
RETURN next_inst_id;
END IF;
RETURN 1;
END;

View File

@ -0,0 +1,8 @@
DROP FUNCTION IF EXISTS mocha_get_tenant_by_name;
CREATE FUNCTION mocha_get_tenant_by_name
(
p_tenant_name VARCHAR(32)
)
RETURNS INT
RETURN (SELECT id FROM mocha_tenants WHERE tenant_name = p_tenant_name LIMIT 1);

View File

@ -0,0 +1,14 @@
DROP FUNCTION IF EXISTS mocha_get_user_by_username;
CREATE FUNCTION mocha_get_user_by_username
(
p_user_name VARCHAR(32)
)
RETURNS INT
RETURN (
SELECT src_inst_id FROM mocha_attributes
WHERE (tenant_id = mocha_get_current_tenant() OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = mocha_get_current_tenant()))
AND att_inst_id = mocha_get_instance_by_global_identifier('960FAF025C5940F791A720012A99D9ED')
AND att_value = p_user_name
ORDER BY att_effective_date DESC
LIMIT 1);

View File

@ -0,0 +1,10 @@
DROP FUNCTION IF EXISTS mocha_normalize_uuid;
CREATE FUNCTION mocha_normalize_uuid(p_uuid CHAR(40))
RETURNS CHAR(32) NO SQL
DETERMINISTIC
BEGIN
RETURN LOWER(REPLACE(REPLACE(REPLACE(p_uuid, '{', ''), '}', ''), '-', ''));
END

View File

@ -0,0 +1,25 @@
DROP FUNCTION IF EXISTS mocha_uuid_v4;
CREATE FUNCTION mocha_uuid_v4()
RETURNS CHAR(32) NO SQL
BEGIN
-- Generate 8 2-byte strings that we will combine into a UUIDv4
SET @h1 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h2 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h3 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h6 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h7 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h8 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
-- 4th section will start with a 4 indicating the version
SET @h4 = CONCAT('4', LPAD(HEX(FLOOR(RAND() * 0x0fff)), 3, '0'));
-- 5th section first half-byte can only be 8, 9 A or B
SET @h5 = CONCAT(HEX(FLOOR(RAND() * 4 + 8)),
LPAD(HEX(FLOOR(RAND() * 0x0fff)), 3, '0'));
-- Build the complete UUID
RETURN LOWER(CONCAT(
@h1, @h2, @h3, @h4, @h5, @h6, @h7, @h8
));
END

View File

@ -0,0 +1,30 @@
DROP PROCEDURE IF EXISTS mocha_assign_relationship;
CREATE PROCEDURE mocha_assign_relationship
(
IN p_source_inst_id INT,
IN p_relationship_inst_id INT,
IN p_destination_inst_id INT,
IN p_user_inst_id INT,
IN p_effective_date DATETIME
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
-- insert record first
INSERT INTO mocha_relationships
(tenant_id, source_inst_id, relationship_inst_id, destination_inst_id, user_inst_id, effective_date)
VALUES
(p_tenant_id, p_source_inst_id, p_relationship_inst_id, p_destination_inst_id, p_user_inst_id, z_effective_date);
END;

View File

@ -0,0 +1,54 @@
DROP PROCEDURE IF EXISTS mocha_create_class;
CREATE PROCEDURE mocha_create_class
(
IN p_class_id INT,
IN p_global_identifier CHAR(32),
IN p_user_inst_id INT,
IN p_effective_date DATETIME,
OUT p_assigned_inst_id INT
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE next_inst_id INT;
DECLARE p_class_index INT;
DECLARE z_global_identifier CHAR(32);
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_global_identifier IS NULL THEN
SET z_global_identifier=mocha_uuid_v4();
ELSE
SET z_global_identifier=p_global_identifier;
END IF;
-- insert record first
INSERT INTO mocha_instances
(tenant_id, class_id, inst_id, global_identifier)
VALUES
(p_tenant_id, 1, p_class_id, z_global_identifier);
SET p_assigned_inst_id = LAST_INSERT_ID();
-- then update relationship: Class.has Instance
CALL mocha_assign_relationship
(
mocha_get_instance_by_key(1, p_class_index),
mocha_get_instance_by_global_identifier('7EB41D3C2AE9488483A4E59441BCAEFB'),
mocha_get_instance_by_key(p_class_index, next_inst_id),
p_user_inst_id,
z_effective_date
);
-- then update relationship: Instance.for Class
CALL mocha_assign_relationship
(
mocha_get_instance_by_key(p_class_index, next_inst_id),
mocha_get_instance_by_global_identifier('494D5A6D04BE477B8763E3F57D0DD8C8'),
mocha_get_instance_by_key(1, p_class_index),
p_user_inst_id,
z_effective_date
);
END;

View File

@ -0,0 +1,49 @@
DROP PROCEDURE IF EXISTS mocha_create_instance;
CREATE PROCEDURE mocha_create_instance
(
IN p_global_identifier CHAR(32), -- the desired global identifier for the newly created instance
IN p_class_index INT,
IN p_inst_index INT,
IN p_user_inst_id INT,
IN p_effective_date DATETIME,
OUT p_assigned_inst_id INT
)
sp: BEGIN
DECLARE p_tenant_id INT;
DECLARE next_inst_id INT;
DECLARE z_global_identifier CHAR(32);
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_inst_index IS NULL THEN
SET next_inst_id = mocha_get_next_inst_id(p_class_index);
ELSE
SET next_inst_id = p_inst_index;
END IF;
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
IF p_global_identifier IS NULL THEN
SET z_global_identifier=mocha_uuid_v4();
ELSE
SET z_global_identifier=p_global_identifier;
END IF;
-- insert record first
INSERT INTO mocha_instances
(tenant_id, class_id, inst_id, global_identifier)
VALUES
(p_tenant_id, p_class_index, next_inst_id, z_global_identifier);
SET p_assigned_inst_id = LAST_INSERT_ID();
END;

View File

@ -0,0 +1,73 @@
DROP PROCEDURE IF EXISTS mocha_create_instance_of;
CREATE PROCEDURE mocha_create_instance_of
(
IN p_class_inst_id INT,
IN p_global_identifier CHAR(32), -- the desired global identifier for the newly created instance
IN p_user_inst_id INT,
IN p_effective_date DATETIME,
OUT p_assigned_inst_id INT
)
sp: BEGIN
DECLARE p_tenant_id INT;
DECLARE next_inst_id INT;
DECLARE p_class_index INT;
DECLARE z_global_identifier CHAR(32);
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
SET p_class_index = (SELECT inst_id FROM mocha_instances
WHERE (tenant_id = p_tenant_id OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = p_tenant_id))
AND class_id = 1 AND id = p_class_inst_id);
IF p_class_index IS NULL THEN
SELECT "cannot create an instance of something that is not a Class" AS error_description;
LEAVE sp;
END IF;
SET next_inst_id=mocha_get_next_inst_id(p_class_index);
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
IF p_global_identifier IS NULL THEN
SET z_global_identifier=mocha_uuid_v4();
ELSE
SET z_global_identifier=p_global_identifier;
END IF;
-- insert record first
INSERT INTO mocha_instances
(tenant_id, class_id, inst_id, global_identifier)
VALUES
(p_tenant_id, p_class_index, next_inst_id, z_global_identifier);
SET p_assigned_inst_id = LAST_INSERT_ID();
-- then update relationship: Class.has Instance
CALL mocha_assign_relationship
(
mocha_get_instance_by_key(1, p_class_index),
mocha_get_instance_by_global_identifier('7EB41D3C2AE9488483A4E59441BCAEFB'),
mocha_get_instance_by_key(p_class_index, next_inst_id),
p_user_inst_id,
z_effective_date
);
-- then update relationship: Instance.for Class
CALL mocha_assign_relationship
(
mocha_get_instance_by_key(p_class_index, next_inst_id),
mocha_get_instance_by_global_identifier('494D5A6D04BE477B8763E3F57D0DD8C8'),
mocha_get_instance_by_key(1, p_class_index),
p_user_inst_id,
z_effective_date
);
END;

View File

@ -0,0 +1,24 @@
DROP PROCEDURE IF EXISTS mocha_create_tenant;
CREATE PROCEDURE mocha_create_tenant
(
IN p_tenant_name VARCHAR(32),
IN p_global_identifier CHAR(32)
)
BEGIN
DECLARE z_global_identifier CHAR(32);
IF p_global_identifier IS NULL THEN
SET z_global_identifier = mocha_uuid_v4();
ELSE
SET z_global_identifier = p_global_identifier;
END IF;
IF NOT (SELECT COUNT(tenant_name) FROM mocha_tenants WHERE tenant_name = p_tenant_name) > 0 THEN
INSERT INTO mocha_tenants (tenant_name, global_identifier, effective_date)
VALUES (p_tenant_name, z_global_identifier, NOW());
END IF;
END;

View File

@ -0,0 +1,30 @@
DROP PROCEDURE IF EXISTS mocha_create_user;
CREATE PROCEDURE mocha_create_user
(
IN p_username VARCHAR(32),
IN p_global_identifier CHAR(32)
)
sp: BEGIN
DECLARE z_global_identifier CHAR(32);
IF p_global_identifier IS NULL THEN
SET z_global_identifier=mocha_uuid_v4();
ELSE
SET z_global_identifier=p_global_identifier;
END IF;
SET @user_iid = mocha_get_user_by_username(p_username);
IF @user_iid IS NULL THEN
CALL mocha_create_instance_of(mocha_get_instance_by_global_identifier('9C6871C19A7F4A3A900E69D1D9E24486'), z_global_identifier, NULL, NULL, @user_iid);
ELSE
SELECT "user with the specified name already exists on this tenant" AS error_description;
LEAVE sp;
END IF;
SELECT @user_iid;
CALL mocha_set_attribute_value(@user_iid, mocha_get_instance_by_global_identifier('960FAF025C5940F791A720012A99D9ED'), p_username, NULL, NULL);
END;

View File

@ -0,0 +1,29 @@
DROP PROCEDURE IF EXISTS mocha_get_attribute_value;
CREATE PROCEDURE mocha_get_attribute_value
(
IN p_source_inst_id INT,
IN p_attribute_inst_id INT,
IN p_effective_date DATETIME
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
SELECT att_value FROM mocha_attributes
WHERE (tenant_id = p_tenant_id OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = p_tenant_id))
AND src_inst_id = p_source_inst_id
AND att_inst_id = p_attribute_inst_id
AND att_effective_date <= z_effective_date
ORDER BY tenant_id DESC, att_effective_date DESC, id DESC
LIMIT 1;
END;

View File

@ -0,0 +1,28 @@
DROP PROCEDURE IF EXISTS mocha_get_attribute_values;
CREATE PROCEDURE mocha_get_attribute_values
(
IN p_source_inst_id INT,
IN p_attribute_inst_id INT,
IN p_effective_date DATETIME
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
SELECT att_value FROM mocha_attributes
WHERE tenant_id = p_tenant_id
AND src_inst_id = p_source_inst_id
AND att_inst_id = p_attribute_inst_id
AND att_effective_date <= z_effective_date
ORDER BY att_effective_date;
END;

View File

@ -0,0 +1,19 @@
DROP PROCEDURE IF EXISTS mocha_get_instance_by_global_identifier;
CREATE PROCEDURE mocha_get_instance_by_global_identifier
(
IN p_global_identifier CHAR(40)
)
BEGIN
DECLARE p_tenant_id INT;
SET p_tenant_id = mocha_get_current_tenant();
SELECT * FROM mocha_instances
WHERE (tenant_id = p_tenant_id OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = p_tenant_id))
AND UPPER(global_identifier) = UPPER(REPLACE(REPLACE(REPLACE(p_global_identifier, '{', ''), '}', ''), '-', ''))
ORDER BY id DESC
LIMIT 1;
END;

View File

@ -0,0 +1,18 @@
DROP PROCEDURE IF EXISTS mocha_get_instance_by_key;
CREATE PROCEDURE mocha_get_instance_by_key
(
IN p_class_id INT,
IN p_inst_id INT
)
BEGIN
DECLARE p_tenant_id INT;
SET p_tenant_id = mocha_get_current_tenant();
SELECT * FROM mocha_instances
WHERE (tenant_id = p_tenant_id OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = p_tenant_id))
AND class_id = p_class_id
AND inst_id = p_inst_id;
END;

View File

@ -0,0 +1,17 @@
DROP PROCEDURE IF EXISTS mocha_get_instances;
CREATE PROCEDURE mocha_get_instances
(
IN p_class_id INT
)
BEGIN
DECLARE p_tenant_id INT;
SET p_tenant_id = mocha_get_current_tenant();
SELECT CONCAT(class_id, '$', inst_id) AS inst_key, global_identifier
FROM mocha_instances
WHERE tenant_id = p_tenant_id
AND (p_class_id IS NULL OR class_id = p_class_id);
END;

View File

@ -0,0 +1,32 @@
DROP PROCEDURE IF EXISTS mocha_get_related_instances;
CREATE PROCEDURE mocha_get_related_instances
(
IN p_src_inst_id INT,
IN p_rel_inst_id INT,
IN p_effective_date DATETIME
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
SELECT mocha_instances.*, mocha_relationships.remove_flag
FROM mocha_instances, mocha_relationships
WHERE mocha_instances.tenant_id = p_tenant_id
AND mocha_relationships.tenant_id = p_tenant_id
AND mocha_instances.id = mocha_relationships.destination_inst_id
AND mocha_relationships.source_inst_id = p_src_inst_id
AND mocha_relationships.relationship_inst_id = p_rel_inst_id
AND mocha_relationships.effective_date <= z_effective_date;
-- ORDER BY mocha_relationships.effective_date ASC;
END;

View File

@ -0,0 +1,21 @@
DROP PROCEDURE IF EXISTS mocha_get_tenant_by_name;
CREATE PROCEDURE mocha_get_tenant_by_name
(
IN p_tenant_name VARCHAR(32),
IN p_effective_date DATETIME
)
BEGIN
DECLARE z_effective_date DATETIME;
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
SELECT * FROM mocha_tenants
WHERE tenant_name = p_tenant_name
AND effective_date <= z_effective_date;
END;

View File

@ -0,0 +1,84 @@
DROP PROCEDURE IF EXISTS mocha_prepare_instance;
CREATE PROCEDURE mocha_prepare_instance
(
IN p_class_inst_id INT,
IN p_inst_id INT,
IN p_class_global_identifier CHAR(32), -- the global identifier of the class of the newly created instance
IN p_global_identifier CHAR(32), -- the desired global identifier for the newly created instance
IN p_user_inst_id INT,
IN p_effective_date DATETIME,
OUT p_assigned_inst_id INT
)
sp: BEGIN
DECLARE p_tenant_id INT;
DECLARE next_inst_id INT;
DECLARE p_class_index INT;
DECLARE z_global_identifier CHAR(32);
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
-- SET p_class_index = (SELECT inst_id FROM mocha_instances WHERE tenant_id = p_tenant_id AND class_id = 1 AND id = p_class_inst_id);
SET p_class_index = p_class_inst_id;
IF p_class_index IS NULL AND NOT p_class_global_identifier IS NULL THEN
SET p_class_index = (SELECT inst_id FROM mocha_instances WHERE tenant_id = p_tenant_id AND id = mocha_get_instance_by_global_identifier(p_class_global_identifier));
END IF;
-- IF p_class_index IS NULL THEN
-- SELECT "cannot create an instance of something that is not a Class" AS error_description;
-- LEAVE sp;
-- END IF;
SET next_inst_id=p_inst_id;
IF p_inst_id IS NULL THEN
SET next_inst_id = mocha_get_next_inst_id(p_class_index);
END IF;
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
IF p_global_identifier IS NULL THEN
SET z_global_identifier=mocha_uuid_v4();
ELSE
SET z_global_identifier=p_global_identifier;
END IF;
-- insert record first
INSERT INTO mocha_instances
(tenant_id, class_id, inst_id, global_identifier)
VALUES
(p_tenant_id, p_class_index, next_inst_id, z_global_identifier);
SET p_assigned_inst_id = LAST_INSERT_ID();
-- -- then update relationship: Class.has Instance
-- CALL mocha_assign_relationship
-- (
-- mocha_get_instance_by_key(1, p_class_index),
-- mocha_get_instance_by_global_identifier('7EB41D3C2AE9488483A4E59441BCAEFB'),
-- mocha_get_instance_by_key(p_class_index, next_inst_id),
-- p_user_inst_id,
-- z_effective_date
-- );
-- -- then update relationship: Instance.for Class
-- CALL mocha_assign_relationship
-- (
-- mocha_get_instance_by_key(p_class_index, next_inst_id),
-- mocha_get_instance_by_global_identifier('494D5A6D04BE477B8763E3F57D0DD8C8'),
-- mocha_get_instance_by_key(1, p_class_index),
-- p_user_inst_id,
-- z_effective_date
-- );
END;

View File

@ -0,0 +1,10 @@
DROP PROCEDURE IF EXISTS mocha_release_tenant;
CREATE PROCEDURE mocha_release_tenant
(
)
BEGIN
SET @current_tenant_id = NULL;
END;

View File

@ -0,0 +1,17 @@
DROP PROCEDURE IF EXISTS mocha_search_count;
CREATE PROCEDURE mocha_search_count
(
IN p_query VARCHAR(50)
)
BEGIN
DECLARE p_tenant_id INT;
SET p_tenant_id = mocha_get_current_tenant();
SELECT COUNT(id) FROM mocha_attributes
WHERE tenant_id = p_tenant_id
AND LOWER(att_value) LIKE CONCAT('%', LOWER(p_query), '%')
AND att_effective_date <= NOW()
ORDER BY att_effective_date;
END;

View File

@ -0,0 +1,19 @@
DROP PROCEDURE IF EXISTS mocha_search_query;
CREATE PROCEDURE mocha_search_query
(
IN p_query VARCHAR(50)
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
SELECT * FROM mocha_attributes
WHERE tenant_id = p_tenant_id
AND LOWER(att_value) LIKE CONCAT('%', LOWER(p_query), '%')
AND att_effective_date <= z_effective_date
ORDER BY att_effective_date;
END;

View File

@ -0,0 +1,19 @@
DROP PROCEDURE IF EXISTS mocha_select_tenant;
CREATE PROCEDURE mocha_select_tenant
(
IN p_tenant_id INT
)
BEGIN
IF p_tenant_id IS NULL THEN
SELECT @current_tenant_id;
ELSE
SET @current_tenant_id = p_tenant_id;
END IF;
END;

View File

@ -0,0 +1,42 @@
DROP PROCEDURE IF EXISTS mocha_set_attribute_value;
CREATE PROCEDURE mocha_set_attribute_value
(
IN p_source_inst_id INT,
IN p_attribute_inst_id INT,
IN p_value TEXT,
IN p_user_inst_id INT,
IN p_effective_date DATETIME
)
BEGIN
DECLARE p_tenant_id INT;
DECLARE z_effective_date DATETIME;
SET p_tenant_id = mocha_get_current_tenant();
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
INSERT INTO mocha_attributes
(
tenant_id,
src_inst_id,
att_inst_id,
att_value,
usr_inst_id,
att_effective_date
)
VALUES
(
p_tenant_id,
p_source_inst_id,
p_attribute_inst_id,
p_value,
p_user_inst_id,
z_effective_date
);
END;

View File

@ -0,0 +1,37 @@
DROP PROCEDURE IF EXISTS mocha_set_parent_class;
CREATE PROCEDURE mocha_set_parent_class
(
IN p_source_inst_id INT,
IN p_class_inst_id INT,
IN p_user_inst_id INT,
IN p_effective_date DATETIME
)
BEGIN
DECLARE z_effective_date DATETIME;
IF p_effective_date IS NULL THEN
SET z_effective_date = NOW();
ELSE
SET z_effective_date = p_effective_date;
END IF;
CALL mocha_assign_relationship
(
p_source_inst_id,
mocha_get_instance_by_global_identifier('494D5A6D04BE477B8763E3F57D0DD8C8'),
p_class_inst_id,
p_user_inst_id,
z_effective_date
);
CALL mocha_assign_relationship
(
p_class_inst_id,
mocha_get_instance_by_global_identifier('7EB41D3C2AE9488483A4E59441BCAEFB'),
p_source_inst_id,
p_user_inst_id,
z_effective_date
);
END;

View File

@ -0,0 +1,13 @@
DROP PROCEDURE IF EXISTS mocha_truncate_tenant;
CREATE PROCEDURE mocha_truncate_tenant
(
IN p_tenant_id INT
)
BEGIN
DELETE FROM mocha_instances WHERE tenant_id = p_tenant_id;
DELETE FROM mocha_attributes WHERE tenant_id = p_tenant_id;
DELETE FROM mocha_relationships WHERE tenant_id = p_tenant_id;
END;

View File

@ -0,0 +1,10 @@
DROP TABLE IF EXISTS mocha_tenants;
CREATE TABLE mocha_tenants
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tenant_name VARCHAR(32),
global_identifier CHAR(32),
effective_date DATETIME,
is_library INT NOT NULL DEFAULT 0
);

View File

@ -0,0 +1,41 @@
--
-- mocha_instances.sql - defines the mocha_instances table
--
-- Author:
-- Michael Becker <alcexhim@gmail.com>
--
-- Copyright (c) 2021 Mike Becker's Software
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
DROP TABLE IF EXISTS mocha_instances;
CREATE TABLE mocha_instances
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tenant_id INT,
class_id INT,
inst_id INT,
global_identifier CHAR(32),
INDEX `inst_index_FI_1`
(tenant_id, class_id, inst_id),
INDEX `inst_index_FI_2`
(tenant_id, global_identifier),
CONSTRAINT `inst_inst_id_FK_tenant`
FOREIGN KEY (`tenant_id`)
REFERENCES `mocha_tenants` (`id`)
);

View File

@ -0,0 +1,31 @@
DROP TABLE IF EXISTS mocha_attributes;
CREATE TABLE mocha_attributes
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tenant_id INT,
src_inst_id INT,
att_inst_id INT,
usr_inst_id INT,
att_effective_date DATETIME,
att_value TEXT,
INDEX `att_index_FI_1`
(tenant_id, src_inst_id, att_inst_id),
CONSTRAINT `att_inst_id_FK_tenant`
FOREIGN KEY (`tenant_id`)
REFERENCES `mocha_tenants` (`id`),
CONSTRAINT `att_inst_id_FK_src`
FOREIGN KEY (`src_inst_id`)
REFERENCES `mocha_instances` (`id`),
CONSTRAINT `att_inst_id_FK_att`
FOREIGN KEY (`att_inst_id`)
REFERENCES `mocha_instances` (`id`),
CONSTRAINT `att_inst_id_FK_usr`
FOREIGN KEY (`usr_inst_id`)
REFERENCES `mocha_instances` (`id`)
);

View File

@ -0,0 +1,36 @@
DROP TABLE IF EXISTS mocha_relationships;
CREATE TABLE mocha_relationships
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tenant_id INT,
source_inst_id INT,
relationship_inst_id INT,
destination_inst_id INT,
user_inst_id INT,
effective_date DATETIME,
remove_flag INT,
INDEX `rel_index_FI_1`
(tenant_id, source_inst_id, relationship_inst_id),
CONSTRAINT `rel_inst_id_FK_tenant`
FOREIGN KEY (`tenant_id`)
REFERENCES `mocha_tenants` (`id`),
CONSTRAINT `rel_inst_id_FK_src`
FOREIGN KEY (`source_inst_id`)
REFERENCES `mocha_instances` (`id`),
CONSTRAINT `rel_inst_id_FK_rel`
FOREIGN KEY (`relationship_inst_id`)
REFERENCES `mocha_instances` (`id`),
CONSTRAINT `rel_inst_id_FK_dst`
FOREIGN KEY (`destination_inst_id`)
REFERENCES `mocha_instances` (`id`),
CONSTRAINT `rel_inst_id_FK_usr`
FOREIGN KEY (`user_inst_id`)
REFERENCES `mocha_instances` (`id`)
);

View File

@ -0,0 +1,15 @@
DROP TABLE IF EXISTS mocha_tenant_references;
CREATE TABLE mocha_tenant_references
(
source_tenant_id INT NOT NULL,
target_tenant_id INT NOT NULL,
CONSTRAINT `tenantref_inst_id_FK_tenant_source`
FOREIGN KEY (`source_tenant_id`)
REFERENCES `mocha_tenants` (`id`),
CONSTRAINT `tenantref_inst_id_FK_tenant_target`
FOREIGN KEY (`target_tenant_id`)
REFERENCES `mocha_tenants` (`id`)
);

View File

@ -0,0 +1,21 @@
INSERT INTO mocha_tenants
(
tenant_name,
global_identifier,
effective_date,
is_library
)
VALUES
(
'net.alcetech.Mocha.System',
'2826E41F763A413FB2393D9698AB629F',
NOW(),
1
),
(
'default',
'2552F66B0DBE41EB8A8076DE8575A468',
NOW(),
0
)

View File

@ -0,0 +1,9 @@
INSERT INTO mocha_tenant_references
(
source_tenant_id,
target_tenant_id
)
VALUES
(
2, 1
)

View File

@ -0,0 +1,50 @@
<?php
$argv = $_SERVER["argv"];
// install_mysql.php databasename username password
if (count($argv) > 1)
{
$DatabaseName = $argv[1];
$HostName = "127.0.0.1";
$PortNumber = 23306;
if (count($argv) >= 4)
{
$UserName = $argv[2];
$Password = $argv[3];
}
else
{
echo( "enter database credentials\n\nuser name: ");
$UserName = trim(fgets(STDIN));
if ($UserName === false)
{
return;
}
system("stty -echo");
echo("password: ");
$Password = trim(fgets(STDIN));
if ($Password === false)
{
return;
}
system("stty echo");
}
echo("uninstalling mocha from `" . $DatabaseName . "` with user `" . $UserName . "` and password\n");
$pdo = new \PDO("mysql:host=" . $HostName . ";port=" . $PortNumber . ";dbname=" . $DatabaseName, $UserName, $Password);
$query = "DROP DATABASE :database_name";
$stmt = $pdo->prepare($query);
$stmt->execute(array("database_name" => $DatabaseName));
echo("\n\n");
}
?>

View File

@ -0,0 +1,7 @@
<?php
$argv = $_SERVER["argv"];
if (count($argv) > 1)
{
echo (hash('sha512', $argv[1]));
}
?>

View File

@ -0,0 +1,125 @@
<?php
const RSCANDIR_INCLUDE_FILES = 1;
const RSCANDIR_INCLUDE_DIRECTORIES = 2;
const RSCANDIR_INCLUDE_ALL = 3;
function rscandir($dir, $flags = RSCANDIR_INCLUDE_ALL)
{
if (!is_dir($dir))
{
return false;
}
$result = array();
$items = scandir($dir);
foreach ($items as $item)
{
if ($item == "." || $item == "..") continue;
if (($flags & RSCANDIR_INCLUDE_FILES) == RSCANDIR_INCLUDE_FILES && !is_dir($dir . "/" . $item))
{
$result[] = $dir . "/" . $item;
}
if (is_dir($dir . "/" . $item))
{
if (($flags & RSCANDIR_INCLUDE_DIRECTORIES) == RSCANDIR_INCLUDE_DIRECTORIES)
{
$result[] = $dir . "/" . $item;
}
$items2 = rscandir($dir . "/" . $item, $flags);
if ($items2 !== false)
{
$result = array_merge($result, $items2);
}
}
}
return $result;
}
$argv = $_SERVER["argv"];
// install_mysql.php databasename username password
if (count($argv) > 1)
{
// usage: mocha oms install
// php install_mysql.php DatabaseName UserName Password PortNumber
$DatabaseName = $argv[1];
$HostName = "127.0.0.1";
$PortNumber = 3306;
if (count($argv) >= 4)
{
$UserName = $argv[2];
$Password = $argv[3];
if (count($argv) >= 5)
{
$PortNumber = $argv[4];
}
}
else
{
echo( "enter database credentials\n\nuser name: ");
$UserName = trim(fgets(STDIN));
if ($UserName === false)
{
return;
}
system("stty -echo");
echo("password: ");
$Password = trim(fgets(STDIN));
if ($Password === false)
{
return;
}
system("stty echo");
}
echo("\n\n");
echo("installing mocha on `" . $DatabaseName . "` with user `" . $UserName . "` and password\n");
$pdo = new \PDO("mysql:host=" . $HostName . ";port=" . $PortNumber . ";dbname=" . $DatabaseName, $UserName, $Password);
// import all the sql files
$sql_files = rscandir("../mysql", RSCANDIR_INCLUDE_FILES);
foreach ($sql_files as $sql_file)
{
echo("executing `" . $sql_file . "`\n");
$sql = file_get_contents($sql_file);
$pdo->exec($sql);
}
/*
echo ("enter new name for super tenant? [super]: ");
$TenantName = trim(fgets(STDIN));
if ($TenantName === false)
{
return;
}
if ($TenantName == "")
{
$TenantName = "super";
}
print ("tenant name: '" . $TenantName . "'\n");
*/
$TenantName = "super";
// generate new UUID for default tenant
$query = "UPDATE mocha_tenants SET global_identifier = mocha_uuid_v4(), tenant_name = :tenant_name WHERE global_identifier = '2552F66B0DBE41EB8A8076DE8575A468'";
$stmt = $pdo->prepare($query);
$stmt->execute(array("tenant_name" => trim($TenantName)));
}
else
{
echo("usage: install_mysql.php DatabaseName [UserName] [Password]\n");
}
?>