-
Notifications
You must be signed in to change notification settings - Fork 0
/
define_views.sql
211 lines (181 loc) · 10.7 KB
/
define_views.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `database`
-- Prefix: `prefix_`
--
-- --------------------------------------------------------
--
-- You can do a bulk replace of 'prefix_'
-- to convert to your local prefix.
--
-- This defines the MySQL views across the
-- the tables defined by defined_db.sql
--
-- It should be executed after any changes to the
-- table structure.
--
-- --------------------------------------------------------
--
-- View section, done last to ensure tables exist
--
-- Layout tours, plus any contained layouts
CREATE OR REPLACE VIEW prefix_eventtools_layout_tour_with_layouts
AS SELECT prefix_eventtools_layout_tours.*, prefix_eventtools_layouts.*,
prefix_eventtools_layout_tour_links.layout_tour_link_order, prefix_eventtools_event_status_values.*,
prefix_eventtools_accessibility_codes.*
FROM ((
(prefix_eventtools_layout_tours
LEFT JOIN prefix_eventtools_layout_tour_links
ON prefix_eventtools_layout_tour_links.tour_number = prefix_eventtools_layout_tours.id
)
LEFT JOIN prefix_eventtools_layouts
ON prefix_eventtools_layouts.layout_id = prefix_eventtools_layout_tour_links.layout_id
)
LEFT JOIN prefix_eventtools_event_status_values
ON prefix_eventtools_layout_tours.status_code = prefix_eventtools_event_status_values.event_status_code
)
LEFT JOIN prefix_eventtools_accessibility_codes
ON layout_accessibility = accessibility_code;
-- Layouts, plus any tours they're on, and accessibility
CREATE OR REPLACE VIEW prefix_eventtools_layout_with_layout_tours
AS SELECT prefix_eventtools_layout_tours.*,
prefix_eventtools_layouts.*, prefix_eventtools_accessibility_codes.*, prefix_eventtools_event_status_values.*
FROM ((
(prefix_eventtools_layouts
LEFT JOIN prefix_eventtools_layout_tour_links
ON prefix_eventtools_layouts.layout_id = prefix_eventtools_layout_tour_links.layout_id
)
LEFT JOIN prefix_eventtools_layout_tours
ON prefix_eventtools_layout_tour_links.tour_number = prefix_eventtools_layout_tours.id
)
LEFT JOIN prefix_eventtools_accessibility_codes
ON layout_accessibility = accessibility_code
)
LEFT JOIN prefix_eventtools_event_status_values
ON prefix_eventtools_layouts.layout_status_code = prefix_eventtools_event_status_values.event_status_code;
-- General tours, plus any contained status
CREATE OR REPLACE VIEW prefix_eventtools_general_tour_with_status
AS SELECT prefix_eventtools_general_tours.*, prefix_eventtools_event_status_values.*
FROM prefix_eventtools_general_tours
LEFT JOIN prefix_eventtools_event_status_values
ON prefix_eventtools_general_tours.status_code = prefix_eventtools_event_status_values.event_status_code;
-- Clinics, plus any applied tags
CREATE OR REPLACE VIEW prefix_eventtools_clinics_with_tags
AS SELECT prefix_eventtools_clinics.*, prefix_eventtools_clinic_tags.tag_name, prefix_eventtools_clinic_locations.location_name
FROM (
prefix_eventtools_clinics
LEFT JOIN prefix_eventtools_clinic_tags
ON prefix_eventtools_clinics.id = clinic_tag_clinic_number
)
LEFT JOIN prefix_eventtools_clinic_locations
ON clinic_location_code = location_code
;
-- Misc Events, plus any applied tags
CREATE OR REPLACE VIEW prefix_eventtools_misc_events_with_tags
AS SELECT prefix_eventtools_misc_events.*, prefix_eventtools_misc_event_tags.tag_name, prefix_eventtools_clinic_locations.location_name
FROM (
prefix_eventtools_misc_events
LEFT JOIN prefix_eventtools_misc_event_tags
ON prefix_eventtools_misc_events.id = misc_event_tag_misc_event_number
)
LEFT JOIN prefix_eventtools_clinic_locations
ON misc_location_code = location_code
;
-- Person, plus any availability tags
CREATE OR REPLACE VIEW prefix_eventtools_person_with_availability
AS SELECT * FROM prefix_eventtools_people LEFT JOIN prefix_eventtools_availability
ON prefix_eventtools_people.person_id = prefix_eventtools_availability.availability_person_id;
-- Op Session, plus layout info if present
CREATE OR REPLACE VIEW prefix_eventtools_opsession_with_layouts
AS SELECT * FROM prefix_eventtools_opsession LEFT JOIN prefix_eventtools_layouts
ON prefix_eventtools_opsession.ops_layout_id = prefix_eventtools_layouts.layout_id;
-- Op Session name for use in requests
CREATE OR REPLACE VIEW prefix_eventtools_opsession_name
AS SELECT ops_id, start_date, presenting_time, spaces, distance, travel_time, location, ops_layout_id, status_code,
IF((ops_layout_id2!=0),CONCAT(l1.layout_owner_lastname,' / ',l2.layout_owner_lastname),CONCAT(l1.layout_owner_lastname,' ',l1.layout_name)) AS show_name,
l1.layout_owner_lastname AS layout_owner_lastname1, l2.layout_owner_lastname AS layout_owner_lastname2,
l1.layout_owner_firstname AS layout_owner_firstname1, l2.layout_owner_firstname AS layout_owner_firstname2,
l1.layout_local_url AS layout_local_url1, l2.layout_local_url AS layout_local_url2,
l1.layout_photo_url AS layout_photo_url1, l2.layout_photo_url AS layout_photo_url2,
l1.layout_name AS layout_name1, l2.layout_name AS layout_name2,
l1.layout_id AS layout_id1, l2.layout_id AS layout_id2
FROM (
prefix_eventtools_opsession LEFT JOIN prefix_eventtools_layouts l1
ON prefix_eventtools_opsession.ops_layout_id = l1.layout_id
)
LEFT JOIN prefix_eventtools_layouts l2
ON prefix_eventtools_opsession.ops_layout_id2 = l2.layout_id;
-- Not UPDATABLE
CREATE OR REPLACE VIEW prefix_eventtools_ops_group_names
AS SELECT customers_firstname, customers_lastname, customers_create_date, customers_updated_date,
opsreq_person_email, opsreq_priority, prefix_eventtools_opsreq_group.opsreq_group_id,
opsreq_group_cycle_name, opsreq_comment, prefix_eventtools_opsreq_group_req_link.opsreq_id,
opsreq_group_req_link_id, entry_city, entry_state, opsreq_number, opsreq_any
FROM (((
prefix_eventtools_opsession_req LEFT JOIN prefix_customers
ON prefix_eventtools_opsession_req.opsreq_person_email = prefix_customers.customers_email_address
) JOIN prefix_eventtools_opsreq_group_req_link
ON prefix_eventtools_opsession_req.opsreq_id = prefix_eventtools_opsreq_group_req_link.opsreq_id
) JOIN prefix_eventtools_opsreq_group
ON prefix_eventtools_opsreq_group_req_link.opsreq_group_id = prefix_eventtools_opsreq_group.opsreq_group_id
) LEFT JOIN prefix_address_book
ON prefix_customers.customers_default_address_id = prefix_address_book.address_book_id
;
-- op session request assignment with session name info
CREATE OR REPLACE VIEW prefix_eventtools_ops_group_session_assignments
AS SELECT customers_firstname, customers_lastname, customers_create_date, customers_updated_date,
opsreq_person_email, opsreq_priority, prefix_eventtools_ops_group_names.opsreq_group_id,
opsreq_group_cycle_name, opsreq_comment, prefix_eventtools_ops_group_names.opsreq_id, opsreq_req_status_id, status,
prefix_eventtools_ops_group_names.opsreq_group_req_link_id, req_num, prefix_eventtools_opsreq_req_status.ops_id,
start_date, spaces, show_name, entry_city, entry_state, opsreq_number, opsreq_any, ops_layout_id,
layout_owner_lastname1, layout_owner_lastname2, layout_name1, layout_name2
FROM (prefix_eventtools_ops_group_names
LEFT JOIN prefix_eventtools_opsreq_req_status
ON prefix_eventtools_ops_group_names.opsreq_group_req_link_id = prefix_eventtools_opsreq_req_status.opsreq_group_req_link_id
) LEFT JOIN prefix_eventtools_opsession_name
ON prefix_eventtools_opsreq_req_status.ops_id = prefix_eventtools_opsession_name.ops_id
;
-- CREATE OR REPLACE VIEW prefix_eventtools_opsession_req_with_user_info
-- AS SELECT prefix_eventtools_opsession_req.*, prefix_customers.*,
-- prefix_address_book.entry_street_address, prefix_address_book.entry_city, prefix_address_book.entry_state, prefix_address_book.entry_postcode
-- FROM (
-- prefix_eventtools_opsession_req LEFT JOIN prefix_customers
-- ON prefix_eventtools_opsession_req.opsreq_person_email = prefix_customers.customers_email_address AND
-- prefix_eventtools_opsession_req.opsreq_person_password = prefix_customers.customers_password
-- ) LEFT JOIN prefix_address_book
-- ON prefix_customers.customers_id = prefix_address_book.customers_id
-- ;
-- customer name and address with request values - the JOINS
-- below are to make it UPDATEABLE; this wasn't when those were LEFT JOINs
CREATE OR REPLACE VIEW prefix_eventtools_opsession_req_with_user_info
AS SELECT prefix_eventtools_opsession_req.*, prefix_customers.*,
prefix_address_book.entry_street_address, prefix_address_book.entry_city, prefix_address_book.entry_state, prefix_address_book.entry_postcode
FROM (
prefix_eventtools_opsession_req JOIN prefix_customers
ON prefix_eventtools_opsession_req.opsreq_person_email = prefix_customers.customers_email_address
) JOIN prefix_address_book
ON prefix_customers.customers_id = prefix_address_book.customers_id
;
-- customer name and address with option values
CREATE OR REPLACE VIEW prefix_eventtools_customer_cross_options_and_values
AS SELECT prefix_customers.*, prefix_eventtools_customer_options.*,
prefix_eventtools_customer_option_values.customer_option_value_value, prefix_eventtools_customer_option_values.customer_option_value_date,
prefix_eventtools_customer_option_values.customer_option_value_id
FROM (
prefix_customers LEFT JOIN prefix_eventtools_customer_option_values
ON prefix_customers.customers_id = prefix_eventtools_customer_option_values.customers_id
) LEFT JOIN prefix_eventtools_customer_options
ON prefix_eventtools_customer_option_values.customer_option_id = prefix_eventtools_customer_options.customer_option_id
;
--
-- Trigger ensures availability is always available for view
--
-- DROP TRIGGER IF EXISTS prefix_eventtools_trigger_person_gets_availability;
-- delimiter |
-- CREATE TRIGGER prefix_eventtools_trigger_person_gets_availability
-- AFTER INSERT ON prefix_eventtools_people
-- FOR EACH ROW
-- BEGIN
-- INSERT INTO prefix_eventtools_availability SET availability_person_id = NEW.person_id;
-- END;
-- |