2
0

database_schema.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. DROP TABLE IF EXISTS 'pilots';
  2. CREATE TABLE IF NOT EXISTS 'pilots' (
  3. 'pilot_id' INTEGER NOT NULL,
  4. 'lastname' TEXT NOT NULL,
  5. 'firstname' TEXT,
  6. 'alias' TEXT,
  7. 'company' TEXT,
  8. 'employeeid' TEXT,
  9. 'phone' TEXT,
  10. 'email' TEXT,
  11. PRIMARY KEY('pilot_id' AUTOINCREMENT)
  12. );
  13. DROP TABLE IF EXISTS 'tails';
  14. CREATE TABLE IF NOT EXISTS 'tails' (
  15. 'tail_id' INTEGER NOT NULL,
  16. 'registration' TEXT NOT NULL,
  17. 'company' TEXT,
  18. 'make' TEXT,
  19. 'model' TEXT,
  20. 'variant' TEXT,
  21. 'multipilot' INTEGER,
  22. 'multiengine' INTEGER,
  23. 'engineType' INTEGER,
  24. 'weightClass' INTEGER,
  25. 'typeString' TEXT,
  26. PRIMARY KEY('tail_id' AUTOINCREMENT)
  27. );
  28. DROP TABLE IF EXISTS 'flights';
  29. CREATE TABLE IF NOT EXISTS 'flights' (
  30. 'flight_id' INTEGER NOT NULL,
  31. 'doft' NUMERIC NOT NULL,
  32. 'dept' TEXT NOT NULL,
  33. 'dest' TEXT NOT NULL,
  34. 'tofb' INTEGER NOT NULL,
  35. 'tonb' INTEGER NOT NULL,
  36. 'pic' INTEGER NOT NULL,
  37. 'acft' INTEGER NOT NULL,
  38. 'tblk' INTEGER NOT NULL,
  39. 'tSPSE' INTEGER,
  40. 'tSPME' INTEGER,
  41. 'tMP' INTEGER,
  42. 'tNIGHT' INTEGER,
  43. 'tIFR' INTEGER,
  44. 'tPIC' INTEGER,
  45. 'tPICUS' INTEGER,
  46. 'tSIC' INTEGER,
  47. 'tDUAL' INTEGER,
  48. 'tFI' INTEGER,
  49. 'tSIM' INTEGER,
  50. 'pilotFlying' INTEGER,
  51. 'toDay' INTEGER,
  52. 'toNight' INTEGER,
  53. 'ldgDay' INTEGER,
  54. 'ldgNight' INTEGER,
  55. 'autoland' INTEGER,
  56. 'secondPilot' INTEGER,
  57. 'thirdPilot' INTEGER,
  58. 'approachType' TEXT,
  59. 'flightNumber' TEXT,
  60. 'remarks' TEXT,
  61. FOREIGN KEY('pic') REFERENCES 'pilots'('pilot_id') ON DELETE RESTRICT,
  62. FOREIGN KEY('acft') REFERENCES 'tails'('tail_id') ON DELETE RESTRICT,
  63. PRIMARY KEY('flight_id' AUTOINCREMENT)
  64. );
  65. DROP TABLE IF EXISTS 'aircraft';
  66. CREATE TABLE IF NOT EXISTS 'aircraft' (
  67. 'aircraft_id' INTEGER NOT NULL,
  68. 'make' TEXT,
  69. 'model' TEXT,
  70. 'variant' TEXT,
  71. 'name' TEXT,
  72. 'iata' TEXT,
  73. 'icao' TEXT,
  74. 'multipilot' INTEGER,
  75. 'multiengine' INTEGER,
  76. 'engineType' INTEGER,
  77. 'weightClass' INTEGER,
  78. PRIMARY KEY('aircraft_id' AUTOINCREMENT)
  79. );
  80. DROP TABLE IF EXISTS 'airports';
  81. CREATE TABLE IF NOT EXISTS 'airports' (
  82. 'airport_id' INTEGER NOT NULL,
  83. 'icao' TEXT NOT NULL,
  84. 'iata' TEXT,
  85. 'name' TEXT,
  86. 'lat' REAL,
  87. 'long' REAL,
  88. 'country' TEXT,
  89. 'tzolson' TEXT,
  90. PRIMARY KEY('airport_id' AUTOINCREMENT)
  91. );
  92. DROP TABLE IF EXISTS 'currencies';
  93. CREATE TABLE IF NOT EXISTS "currencies" (
  94. "currency_id" INTEGER NOT NULL,
  95. "currencyName" TEXT,
  96. "expiryDate" NUMERIC,
  97. PRIMARY KEY('currency_id' AUTOINCREMENT)
  98. );
  99. DROP TABLE IF EXISTS 'simulators';
  100. CREATE TABLE IF NOT EXISTS 'simulators' (
  101. 'session_id' INTEGER NOT NULL,
  102. 'date' NUMERIC NOT NULL,
  103. 'totalTime' INTEGER NOT NULL,
  104. 'deviceType' TEXT NOT NULL,
  105. 'aircraftType' TEXT,
  106. 'registration' TEXT,
  107. 'remarks' TEXT,
  108. PRIMARY KEY('session_id' AUTOINCREMENT)
  109. );
  110. DROP TABLE IF EXISTS 'previousExperience';
  111. CREATE TABLE 'previousExperience' (
  112. 'tblk' INTEGER,
  113. 'tSPSE' INTEGER,
  114. 'tSPME' INTEGER,
  115. 'tMP' INTEGER,
  116. 'tNIGHT' INTEGER,
  117. 'tIFR' INTEGER,
  118. 'tPIC' INTEGER,
  119. 'tPICUS' INTEGER,
  120. 'tSIC' INTEGER,
  121. 'tDUAL' INTEGER,
  122. 'tFI' INTEGER,
  123. 'tSIM' INTEGER,
  124. 'toDay' INTEGER,
  125. 'toNight' INTEGER,
  126. 'ldgDay' INTEGER,
  127. 'ldgNight' INTEGER,
  128. 'autoland' INTEGER
  129. );
  130. DROP VIEW IF EXISTS 'viewDefault';
  131. CREATE VIEW viewDefault AS
  132. SELECT flight_id,
  133. doft,
  134. dept,
  135. tofb,
  136. dest,
  137. tonb,
  138. tblk,
  139. pilots.pilot_id,
  140. tails.tail_id,
  141. tails.registration,
  142. flightNumber,
  143. remarks
  144. FROM flights
  145. INNER JOIN pilots on flights.pic = pilots.pilot_id
  146. INNER JOIN tails on flights.acft = tails.tail_id
  147. ORDER BY doft DESC;
  148. DROP VIEW IF EXISTS 'viewDefaultSim';
  149. CREATE VIEW viewDefaultSim AS
  150. SELECT flights.flight_id,
  151. flights.doft,
  152. flights.dept,
  153. flights.tofb,
  154. flights.dest,
  155. flights.tonb,
  156. flights.tblk,
  157. pilots.pilot_id ,
  158. tails.tail_id,
  159. tails.registration,
  160. null AS 'deviceType',
  161. null AS 'SimTime',
  162. flights.remarks
  163. FROM flights
  164. INNER JOIN pilots on flights.pic = pilots.pilot_id
  165. INNER JOIN tails on flights.acft = tails.tail_id
  166. UNION
  167. SELECT (simulators.session_id * -1),
  168. simulators.date,
  169. null, null, null, null, null, null,
  170. simulators.aircraftType,
  171. simulators.registration,
  172. simulators.deviceType,
  173. simulators.totalTime,
  174. remarks
  175. FROM simulators
  176. ORDER BY date DESC;
  177. DROP VIEW IF EXISTS 'viewEasa';
  178. CREATE VIEW viewEasa AS
  179. SELECT
  180. flight_id,
  181. doft,
  182. dept,
  183. tofb,
  184. dest,
  185. tonb,
  186. tail_id,
  187. registration,
  188. tSPSE,
  189. tSPME,
  190. tMP,
  191. tblk,
  192. pilot_id,
  193. ldgDay,
  194. ldgNight,
  195. tNight,
  196. tIFR,
  197. tPIC,
  198. tSIC,
  199. tDUAL,
  200. tFI,
  201. remarks
  202. FROM flights
  203. INNER JOIN pilots on flights.pic = pilots.pilot_id
  204. INNER JOIN tails on flights.acft = tails.tail_id ORDER BY doft DESC;
  205. DROP VIEW IF EXISTS 'viewEasaSim';
  206. CREATE VIEW viewEasaSim AS
  207. SELECT flight_id,
  208. flights.doft as 'Date',
  209. flights.dept,
  210. flights.tofb,
  211. flights.dest,
  212. flights.tonb,
  213. tails.tail_id AS 'Type',
  214. tails.registration AS 'Registration',
  215. flights.tSPSE,
  216. flights.tSPME,
  217. flights.tMP,
  218. flights.tblk,
  219. pilots.pilot_id AS 'PIC',
  220. flights.ldgDay,
  221. flights.ldgNight,
  222. flights.tNight,
  223. flights.tIFR,
  224. flights.tPIC,
  225. flights.tSIC,
  226. flights.tDual,
  227. flights.tFI,
  228. null AS 'deviceType',
  229. null AS 'simTime',
  230. flights.remarks
  231. FROM flights
  232. INNER JOIN pilots on flights.pic = pilots.pilot_id
  233. INNER JOIN tails on flights.acft = tails.tail_id
  234. UNION
  235. SELECT (session_id * -1),
  236. simulators.date,
  237. null, null, null, null,
  238. simulators.aircraftType,
  239. simulators.registration,
  240. null, null, null, null,
  241. null, null, null, null,
  242. null, null, null, null,
  243. null,
  244. simulators.deviceType,
  245. simulators.totalTime,
  246. simulators.remarks
  247. FROM simulators
  248. ORDER BY date DESC;
  249. DROP VIEW IF EXISTS 'viewSimulators';
  250. CREATE VIEW viewSimulators AS
  251. SELECT (session_id * -1),
  252. date,
  253. registration,
  254. aircraftType,
  255. deviceType,
  256. totalTime,
  257. remarks
  258. FROM simulators
  259. ORDER BY date DESC;
  260. DROP VIEW IF EXISTS 'viewTails';
  261. CREATE VIEW viewTails AS
  262. SELECT tail_id AS 'ID',
  263. registration AS 'Registration',
  264. make||' '||model AS 'Type',
  265. company AS 'Company'
  266. FROM tails WHERE model IS NOT NULL AND variant IS NULL
  267. UNION
  268. SELECT tail_id AS 'ID',
  269. registration AS 'Registration',
  270. make||' '||model||'-'||variant AS 'Type',
  271. company AS 'Company'
  272. FROM tails WHERE variant IS NOT NULL;
  273. DROP VIEW IF EXISTS 'viewPilots';
  274. CREATE VIEW viewPilots AS
  275. SELECT pilot_id AS 'ID',
  276. lastname AS 'Last Name',
  277. firstname AS 'First Name',
  278. company AS 'Company'
  279. FROM pilots;
  280. DROP VIEW IF EXISTS 'viewTotals';
  281. CREATE VIEW viewTotals AS
  282. SELECT printf('%02d',CAST(SUM(tblk) AS INT)/60)||':'||printf('%02d',CAST(SUM(tblk) AS INT)%60) AS 'TOTAL',
  283. printf('%02d',CAST(SUM(tSPSE) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSPSE) AS INT)%60) AS 'SP SE',
  284. printf('%02d',CAST(SUM(tSPME) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSPME) AS INT)%60) AS 'SP ME',
  285. printf('%02d',CAST(SUM(tNIGHT) AS INT)/60)||':'||printf('%02d',CAST(SUM(tNIGHT) AS INT)%60) AS 'NIGHT',
  286. printf('%02d',CAST(SUM(tIFR) AS INT)/60)||':'||printf('%02d',CAST(SUM(tIFR) AS INT)%60) AS 'IFR',
  287. printf('%02d',CAST(SUM(tPIC) AS INT)/60)||':'||printf('%02d',CAST(SUM(tPIC) AS INT)%60) AS 'PIC',
  288. printf('%02d',CAST(SUM(tPICUS) AS INT)/60)||':'||printf('%02d',CAST(SUM(tPICUS) AS INT)%60) AS 'PICUS',
  289. printf('%02d',CAST(SUM(tSIC) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSIC) AS INT)%60) AS 'SIC',
  290. printf('%02d',CAST(SUM(tDual) AS INT)/60)||':'||printf('%02d',CAST(SUM(tDual) AS INT)%60) AS 'DUAL',
  291. printf('%02d',CAST(SUM(tFI) AS INT)/60)||':'||printf('%02d',CAST(SUM(tFI) AS INT)%60) AS 'INSTRUCTOR',
  292. printf('%02d',CAST(SUM(tSIM) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSIM) AS INT)%60) AS 'SIMULATOR',
  293. printf('%02d',CAST(SUM(tMP) AS INT)/60)||':'||printf('%02d',CAST(SUM(tMP) AS INT)%60) AS 'MultPilot',
  294. CAST(SUM(toDay) AS INT) AS 'TO Day',
  295. CAST(SUM(toNight) AS INT) AS 'TO Night',
  296. CAST(SUM(ldgDay) AS INT) AS 'LDG Day',
  297. CAST(SUM(ldgNight) AS INT) AS 'LDG Night'
  298. FROM flights;
  299. DROP VIEW IF EXISTS 'viewExport';
  300. CREATE VIEW viewExport AS
  301. SELECT flight_id,
  302. doft as 'Date',
  303. dept AS 'Dept',
  304. printf('%02d',(tofb/60))||':'||printf('%02d',(tofb%60)) AS 'Time Out',
  305. dest AS 'Dest',
  306. printf('%02d',(tonb/60))||':'||printf('%02d',(tonb%60)) AS 'Time In ',
  307. CASE WHEN variant IS NOT NULL THEN make||' '||model||'-'||variant ELSE make||' '||model END AS 'Type',
  308. registration AS 'Registration',
  309. (SELECT printf('%02d',(tSPSE/60))||':'||printf('%02d',(tSPSE%60)) WHERE tSPSE IS NOT NULL) AS 'SP SE',
  310. (SELECT printf('%02d',(tSPME/60))||':'||printf('%02d',(tSPME%60)) WHERE tSPME IS NOT NULL) AS 'SP ME',
  311. (SELECT printf('%02d',(tMP/60))||':'||printf('%02d',(tMP%60)) WHERE tMP IS NOT NULL) AS 'MP',
  312. printf('%02d',(tblk/60))||':'||printf('%02d',(tblk%60)) AS 'Total',
  313. CASE WHEN pilot_id = 1 THEN alias ELSE lastname||', '||substr(firstname, 1, 1)||'.' END AS 'Name PIC',
  314. toDay AS 'Take-Off Day',
  315. ldgDay AS 'Landings Day',
  316. toNight AS 'Take-Off Night',
  317. ldgNight AS 'Landings Night',
  318. (SELECT printf('%02d',(tNight/60))||':'||printf('%02d',(tNight%60)) WHERE tNight IS NOT NULL) AS 'Night',
  319. (SELECT printf('%02d',(tIFR/60))||':'||printf('%02d',(tIFR%60)) WHERE tIFR IS NOT NULL) AS 'IFR',
  320. (SELECT printf('%02d',(tPIC/60))||':'||printf('%02d',(tPIC%60)) WHERE tPIC IS NOT NULL) AS 'PIC',
  321. (SELECT printf('%02d',(tSIC/60))||':'||printf('%02d',(tSIC%60)) WHERE tSIC IS NOT NULL) AS 'SIC',
  322. (SELECT printf('%02d',(tDual/60))||':'||printf('%02d',(tDual%60)) WHERE tDual IS NOT NULL) AS 'Dual',
  323. (SELECT printf('%02d',(tFI/60))||':'||printf('%02d',(tFI%60)) WHERE tFI IS NOT NULL) AS 'FI',
  324. null AS 'Sim Type',
  325. null AS 'Time of Session',
  326. remarks AS 'Remarks'
  327. FROM flights
  328. INNER JOIN pilots on flights.pic = pilots.pilot_id
  329. INNER JOIN tails on flights.acft = tails.tail_id
  330. UNION
  331. SELECT (session_id * -1),
  332. date,
  333. null, null, null, null,
  334. aircraftType,
  335. registration,
  336. null, null, null,
  337. 'SIM',
  338. null, null, null, null, null, null, null, null, null, null, null,
  339. deviceType, printf('%02d',(totalTime/60))||':'||printf('%02d',(totalTime%60)),
  340. remarks
  341. FROM simulators
  342. ORDER BY date DESC;