annotate base/database/charm/database-setup.lisp @ 318:c4e792b9b898

Add some ideas for charm constituents. Not particularly useful in its own right, but contains some possibly useful ideas related to the generalisation of db-compositions.
author Jamie Forth <j.forth@gold.ac.uk>
date Thu, 30 Sep 2010 15:35:15 +0100
parents
children
rev   line source
j@318 1 (cl:in-package #:amuse-charm)
j@318 2
j@318 3 ;;;=====================================================================
j@318 4 ;;; CHARM database setup functions
j@318 5 ;;;=====================================================================
j@318 6
j@318 7 (defun create-charm-db-tables (database)
j@318 8 (%create-constituent-header-table database)
j@318 9 (%create-constituent-particle-table database)
j@318 10 (%create-constituent-stored-routines database))
j@318 11
j@318 12 (defun drop-charm-db-tables (database)
j@318 13 (%drop-constituent-header-table database)
j@318 14 (%drop-constituent-particle-table database)
j@318 15 (%drop-constituent-stored-routines database))
j@318 16
j@318 17
j@318 18 ;;;=====================================================================
j@318 19 ;;; Helper functions
j@318 20 ;;;=====================================================================
j@318 21
j@318 22 (defun %create-constituent-header-table (database)
j@318 23 (clsql:execute-command "
j@318 24 CREATE TABLE charm_constituent_headers (
j@318 25 constituent_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
j@318 26 implementation_id SMALLINT UNSIGNED NOT NULL,
j@318 27 parent_id INT UNSIGNED NOT NULL,
j@318 28 #start_time INTEGER UNSIGNED NOT NULL,
j@318 29 #duration INTEGER UNSIGNED NOT NULL,
j@318 30 ext_properties VARCHAR(255) NOT NULL,
j@318 31 int_properties VARCHAR(255) NOT NULL,
j@318 32 owner CHAR(16) NULL,
j@318 33 version SMALLINT UNSIGNED NOT NULL DEFAULT 1,
j@318 34 creation_timestamp TIMESTAMP NULL,
j@318 35 deletion_timestamp TIMESTAMP NULL,
j@318 36 INDEX impl_comp_index (implementation_id, parent_id))
j@318 37 engine = innodb;"
j@318 38 :database database)
j@318 39 (%create-constituent-header-triggers database))
j@318 40
j@318 41 (defun %create-constituent-header-triggers (database)
j@318 42 (clsql:execute-command "
j@318 43 CREATE TRIGGER pre_insert BEFORE INSERT ON charm_constituent_headers
j@318 44 FOR EACH ROW
j@318 45 BEGIN
j@318 46 SET NEW.owner = SUBSTRING_INDEX(USER(),'@',1);
j@318 47 SET NEW.creation_timestamp = CURRENT_TIMESTAMP;
j@318 48 #SET NEW.implementation_id = @current_impl;
j@318 49 END;"
j@318 50 :database database))
j@318 51
j@318 52 (defun %create-constituent-particle-table (database)
j@318 53 (clsql:execute-command "
j@318 54 CREATE TABLE charm_constituent_particles (
j@318 55 constituent_id BIGINT UNSIGNED NOT NULL,
j@318 56 particle_id BIGINT UNSIGNED NOT NULL,
j@318 57 particle_type SET('e', 'c') NOT NULL,
j@318 58 version_added SMALLINT UNSIGNED NOT NULL,
j@318 59 version_removed SMALLINT UNSIGNED NULL,
j@318 60 INDEX particle_index (constituent_id))
j@318 61 engine = innodb;"
j@318 62 :database database))
j@318 63
j@318 64 (defun %drop-constituent-header-table (database)
j@318 65 (clsql:drop-table "charm_constituent_headers"
j@318 66 :database database
j@318 67 :if-does-not-exist :ignore))
j@318 68
j@318 69 (defun %drop-constituent-particle-table (database)
j@318 70 (clsql:drop-table "charm_constituent_particles"
j@318 71 :database database
j@318 72 :if-does-not-exist :ignore))
j@318 73
j@318 74 (defun %drop-constituent-stored-routines (database)
j@318 75 (clsql:execute-command "
j@318 76 DROP FUNCTION constituent_header_exists"
j@318 77 :database database))
j@318 78
j@318 79
j@318 80 ;;;=====================================================================
j@318 81 ;;; Other server-side routines
j@318 82 ;;;=====================================================================
j@318 83
j@318 84 (defun %create-constituent-stored-routines (database)
j@318 85 (clsql:execute-command "
j@318 86 CREATE FUNCTION constituent_header_exists (
j@318 87 #start int unsigned,
j@318 88 #dur int unsigned,
j@318 89 impl_id SMALLINT UNSIGNED,
j@318 90 par_id INT UNSIGNED,
j@318 91 external varchar(255),
j@318 92 intrinsic varchar(255))
j@318 93 RETURNS boolean
j@318 94 RETURN EXISTS(SELECT constituent_id
j@318 95 FROM charm_constituent_headers
j@318 96 WHERE implementation_id = impl_id
j@318 97 AND parent_id = par_id
j@318 98 AND ext_properties = external
j@318 99 AND int_properties = intrinsic);"
j@318 100 :database database))