| 
									
										
										
										
											2019-12-03 12:03:39 +01:00
										 |  |  | CREATE TABLE persons ( | 
					
						
							|  |  |  |     id INTEGER NOT NULL PRIMARY KEY, | 
					
						
							|  |  |  |     first_name TEXT NOT NULL, | 
					
						
							|  |  |  |     last_name TEXT NOT NULL | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  | -- This represents real instruments as well as other roles that can be played | 
					
						
							|  |  |  | -- in a recording. | 
					
						
							| 
									
										
										
										
											2019-12-03 12:03:39 +01:00
										 |  |  | CREATE TABLE instruments ( | 
					
						
							|  |  |  |     id INTEGER NOT NULL PRIMARY KEY, | 
					
						
							|  |  |  |     name TEXT NOT NULL | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CREATE TABLE works ( | 
					
						
							|  |  |  |     id INTEGER NOT NULL PRIMARY KEY, | 
					
						
							|  |  |  |     composer INTEGER REFERENCES persons(id), | 
					
						
							|  |  |  |     title TEXT NOT NULL, | 
					
						
							|  |  |  |     part_of INTEGER REFERENCES works(id) ON DELETE CASCADE, | 
					
						
							| 
									
										
										
										
											2020-03-21 14:34:45 +01:00
										 |  |  |     part_index INTEGER, | 
					
						
							|  |  |  |     part_level INTEGER | 
					
						
							| 
									
										
										
										
											2019-12-03 12:03:39 +01:00
										 |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CREATE TABLE instrumentations ( | 
					
						
							|  |  |  |     work INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE, | 
					
						
							|  |  |  |     instrument INTEGER NOT NULL REFERENCES instruments(id) ON DELETE CASCADE | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 14:49:40 +01:00
										 |  |  | CREATE TABLE ensembles ( | 
					
						
							|  |  |  |     id INTEGER NOT NULL PRIMARY KEY, | 
					
						
							|  |  |  |     name TEXT NOT NULL | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CREATE TABLE recordings ( | 
					
						
							|  |  |  |     id INTEGER NOT NULL PRIMARY KEY, | 
					
						
							|  |  |  |     work INTEGER REFERENCES works(id) | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CREATE TABLE performances ( | 
					
						
							|  |  |  |     recording INTEGER NOT NULL REFERENCES recordings(id) ON DELETE CASCADE, | 
					
						
							|  |  |  |     person INTEGER REFERENCES persons(id) ON DELETE CASCADE, | 
					
						
							|  |  |  |     ensemble INTEGER REFERENCES ensembles(id) ON DELETE CASCADE, | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  |     role INTEGER REFERENCES instruments(id) | 
					
						
							| 
									
										
										
										
											2020-03-22 14:49:40 +01:00
										 |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 12:03:39 +01:00
										 |  |  | allPersons: | 
					
						
							|  |  |  | SELECT * FROM persons ORDER BY last_name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | personById: | 
					
						
							|  |  |  | SELECT * FROM persons WHERE id = :id LIMIT 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | allInstruments: | 
					
						
							|  |  |  | SELECT * FROM instruments ORDER BY name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | instrumentById: | 
					
						
							|  |  |  | SELECT * FROM instruments WHERE id = :id LIMIT 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | workById: | 
					
						
							|  |  |  | SELECT * FROM works WHERE id = :id LIMIT 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 14:34:45 +01:00
										 |  |  | workParts: | 
					
						
							|  |  |  | SELECT * FROM works WHERE part_of = :id ORDER BY part_index; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 16:31:38 +01:00
										 |  |  | -- TODO: Maybe optimize. | 
					
						
							|  |  |  | worksByComposer: | 
					
						
							|  |  |  | SELECT DISTINCT A.* FROM works A LEFT JOIN works B ON A.id = B.part_of | 
					
						
							|  |  |  |     WHERE A.part_of IS NULL AND A.composer = :id OR B.composer = :id; | 
					
						
							| 
									
										
										
										
											2019-12-03 12:03:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 15:59:23 +01:00
										 |  |  | composersByWork: | 
					
						
							|  |  |  | SELECT DISTINCT persons.* FROM persons | 
					
						
							|  |  |  |     JOIN works ON works.composer = persons.id | 
					
						
							|  |  |  |     WHERE works.id = :id OR works.part_of = :id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 12:03:39 +01:00
										 |  |  | instrumentsByWork: | 
					
						
							|  |  |  | SELECT instruments.* FROM instrumentations | 
					
						
							|  |  |  |     JOIN instruments ON instrumentations.instrument=instruments.id | 
					
						
							|  |  |  |     WHERE instrumentations.work = :workId; | 
					
						
							| 
									
										
										
										
											2020-03-22 14:49:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | allEnsembles: | 
					
						
							|  |  |  | SELECT * FROM ensembles ORDER BY name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ensembleById: | 
					
						
							|  |  |  | SELECT * FROM ensembles WHERE id = :id LIMIT 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | recordingById: | 
					
						
							|  |  |  | SELECT * FROM recordings WHERE id = :id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | recordingsByWork: | 
					
						
							|  |  |  | SELECT * FROM recordings WHERE work = :id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | performancesByRecording: | 
					
						
							|  |  |  | SELECT * FROM performances WHERE recording = :id; |