database_schema.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 'changelog';
  100. CREATE TABLE IF NOT EXISTS 'changelog' (
  101. 'revision' INTEGER NOT NULL,
  102. 'comment' TEXT,
  103. 'date' NUMERIC,
  104. PRIMARY KEY('revision' AUTOINCREMENT)
  105. );
  106. DROP TABLE IF EXISTS 'simulators';
  107. CREATE TABLE IF NOT EXISTS 'simulators' (
  108. 'session_id' INTEGER NOT NULL,
  109. 'date' NUMERIC NOT NULL,
  110. 'totalTime' INTEGER NOT NULL,
  111. 'deviceType' TEXT NOT NULL,
  112. 'aircraftType' TEXT,
  113. 'registration' TEXT,
  114. 'remarks' TEXT,
  115. PRIMARY KEY('session_id' AUTOINCREMENT)
  116. );
  117. DROP TABLE IF EXISTS 'previousExperience';
  118. CREATE TABLE 'previousExperience' (
  119. 'tblk' INTEGER,
  120. 'tSPSE' INTEGER,
  121. 'tSPME' INTEGER,
  122. 'tMP' INTEGER,
  123. 'tNIGHT' INTEGER,
  124. 'tIFR' INTEGER,
  125. 'tPIC' INTEGER,
  126. 'tPICUS' INTEGER,
  127. 'tSIC' INTEGER,
  128. 'tDUAL' INTEGER,
  129. 'tFI' INTEGER,
  130. 'tSIM' INTEGER,
  131. 'toDay' INTEGER,
  132. 'toNight' INTEGER,
  133. 'ldgDay' INTEGER,
  134. 'ldgNight' INTEGER,
  135. 'autoland' INTEGER
  136. );
  137. DROP VIEW IF EXISTS 'viewDefault';
  138. CREATE VIEW viewDefault AS
  139. SELECT flight_id,
  140. doft,
  141. dept,
  142. tofb,
  143. dest,
  144. tonb,
  145. tblk,
  146. pilots.pilot_id,
  147. tails.tail_id,
  148. tails.registration,
  149. flightNumber,
  150. remarks
  151. FROM flights
  152. INNER JOIN pilots on flights.pic = pilots.pilot_id
  153. INNER JOIN tails on flights.acft = tails.tail_id
  154. ORDER BY doft DESC
  155. DROP VIEW IF EXISTS 'viewDefaultSim';
  156. CREATE VIEW viewDefaultSim AS
  157. SELECT flights.flight_id,
  158. flights.doft,
  159. flights.dept,
  160. flights.tofb,
  161. flights.dest,
  162. flights.tonb,
  163. flights.tblk,
  164. pilots.pilot_id ,
  165. tails.tail_id,
  166. tails.registration,
  167. null AS 'deviceType',
  168. null AS 'SimTime',
  169. flights.remarks
  170. FROM flights
  171. INNER JOIN pilots on flights.pic = pilots.pilot_id
  172. INNER JOIN tails on flights.acft = tails.tail_id
  173. UNION
  174. SELECT (simulators.session_id * -1),
  175. simulators.date,
  176. null, null, null, null, null, null,
  177. simulators.aircraftType,
  178. simulators.registration,
  179. simulators.deviceType,
  180. simulators.totalTime,
  181. remarks
  182. FROM simulators
  183. ORDER BY date DESC
  184. DROP VIEW IF EXISTS 'viewEasa';
  185. CREATE VIEW viewEasa AS
  186. SELECT
  187. flight_id,
  188. doft,
  189. dept,
  190. tofb,
  191. dest,
  192. tonb,
  193. tail_id,
  194. registration,
  195. tSPSE,
  196. tSPME,
  197. tMP,
  198. tblk,
  199. pilot_id,
  200. ldgDay,
  201. ldgNight,
  202. tNight,
  203. tIFR,
  204. tPIC,
  205. tSIC,
  206. tDUAL,
  207. tFI,
  208. remarks
  209. FROM flights
  210. INNER JOIN pilots on flights.pic = pilots.pilot_id
  211. INNER JOIN tails on flights.acft = tails.tail_id ORDER BY doft DESC
  212. DROP VIEW IF EXISTS 'viewEasaSim';
  213. CREATE VIEW viewEasaSim AS
  214. SELECT flight_id,
  215. flights.doft as 'Date',
  216. flights.dept,
  217. flights.tofb,
  218. flights.dest,
  219. flights.tonb,
  220. tails.tail_id AS 'Type',
  221. tails.registration AS 'Registration',
  222. flights.tSPSE,
  223. flights.tSPME,
  224. flights.tMP,
  225. flights.tblk,
  226. pilots.pilot_id AS 'PIC',
  227. flights.ldgDay,
  228. flights.ldgNight,
  229. flights.tNight,
  230. flights.tIFR,
  231. flights.tPIC,
  232. flights.tSIC,
  233. flights.tDual,
  234. flights.tFI,
  235. null AS 'deviceType',
  236. null AS 'simTime',
  237. flights.remarks
  238. FROM flights
  239. INNER JOIN pilots on flights.pic = pilots.pilot_id
  240. INNER JOIN tails on flights.acft = tails.tail_id
  241. UNION
  242. SELECT (session_id * -1),
  243. simulators.date,
  244. null, null, null, null,
  245. simulators.aircraftType,
  246. simulators.registration,
  247. null, null, null, null,
  248. null, null, null, null,
  249. null, null, null, null,
  250. null,
  251. simulators.deviceType,
  252. simulators.totalTime,
  253. simulators.remarks
  254. FROM simulators
  255. ORDER BY date DESC
  256. DROP VIEW IF EXISTS 'viewSimulators';
  257. CREATE VIEW viewSimulators AS
  258. SELECT (session_id * -1),
  259. date,
  260. registration,
  261. aircraftType,
  262. deviceType,
  263. totalTime,
  264. remarks
  265. FROM simulators
  266. ORDER BY date DESC
  267. DROP VIEW IF EXISTS 'viewTails';
  268. CREATE VIEW viewTails AS
  269. SELECT tail_id AS 'ID',
  270. registration AS 'Registration',
  271. make||' '||model AS 'Type',
  272. company AS 'Company'
  273. FROM tails WHERE model IS NOT NULL AND variant IS NULL
  274. UNION
  275. SELECT tail_id AS 'ID',
  276. registration AS 'Registration',
  277. make||' '||model||'-'||variant AS 'Type',
  278. company AS 'Company'
  279. FROM tails WHERE variant IS NOT NULL;
  280. DROP VIEW IF EXISTS 'viewPilots';
  281. CREATE VIEW viewPilots AS
  282. SELECT pilot_id AS 'ID',
  283. lastname AS 'Last Name',
  284. firstname AS 'First Name',
  285. company AS 'Company'
  286. FROM pilots;
  287. DROP VIEW IF EXISTS 'viewTotals';
  288. CREATE VIEW viewTotals AS
  289. SELECT printf('%02d',CAST(SUM(tblk) AS INT)/60)||':'||printf('%02d',CAST(SUM(tblk) AS INT)%60) AS 'TOTAL',
  290. printf('%02d',CAST(SUM(tSPSE) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSPSE) AS INT)%60) AS 'SP SE',
  291. printf('%02d',CAST(SUM(tSPME) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSPME) AS INT)%60) AS 'SP ME',
  292. printf('%02d',CAST(SUM(tNIGHT) AS INT)/60)||':'||printf('%02d',CAST(SUM(tNIGHT) AS INT)%60) AS 'NIGHT',
  293. printf('%02d',CAST(SUM(tIFR) AS INT)/60)||':'||printf('%02d',CAST(SUM(tIFR) AS INT)%60) AS 'IFR',
  294. printf('%02d',CAST(SUM(tPIC) AS INT)/60)||':'||printf('%02d',CAST(SUM(tPIC) AS INT)%60) AS 'PIC',
  295. printf('%02d',CAST(SUM(tPICUS) AS INT)/60)||':'||printf('%02d',CAST(SUM(tPICUS) AS INT)%60) AS 'PICUS',
  296. printf('%02d',CAST(SUM(tSIC) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSIC) AS INT)%60) AS 'SIC',
  297. printf('%02d',CAST(SUM(tDual) AS INT)/60)||':'||printf('%02d',CAST(SUM(tDual) AS INT)%60) AS 'DUAL',
  298. printf('%02d',CAST(SUM(tFI) AS INT)/60)||':'||printf('%02d',CAST(SUM(tFI) AS INT)%60) AS 'INSTRUCTOR',
  299. printf('%02d',CAST(SUM(tSIM) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSIM) AS INT)%60) AS 'SIMULATOR',
  300. printf('%02d',CAST(SUM(tMP) AS INT)/60)||':'||printf('%02d',CAST(SUM(tMP) AS INT)%60) AS 'MultPilot',
  301. CAST(SUM(toDay) AS INT) AS 'TO Day',
  302. CAST(SUM(toNight) AS INT) AS 'TO Night',
  303. CAST(SUM(ldgDay) AS INT) AS 'LDG Day',
  304. CAST(SUM(ldgNight) AS INT) AS 'LDG Night'
  305. FROM flights;
  306. DROP VIEW IF EXISTS 'viewExport';
  307. CREATE VIEW viewExport AS
  308. SELECT flight_id,
  309. doft as 'Date',
  310. dept AS 'Dept',
  311. printf('%02d',(tofb/60))||':'||printf('%02d',(tofb%60)) AS 'Time Out',
  312. dest AS 'Dest',
  313. printf('%02d',(tonb/60))||':'||printf('%02d',(tonb%60)) AS 'Time In ',
  314. CASE WHEN variant IS NOT NULL THEN make||' '||model||'-'||variant ELSE make||' '||model END AS 'Type',
  315. registration AS 'Registration',
  316. (SELECT printf('%02d',(tSPSE/60))||':'||printf('%02d',(tSPSE%60)) WHERE tSPSE IS NOT NULL) AS 'SP SE',
  317. (SELECT printf('%02d',(tSPME/60))||':'||printf('%02d',(tSPME%60)) WHERE tSPME IS NOT NULL) AS 'SP ME',
  318. (SELECT printf('%02d',(tMP/60))||':'||printf('%02d',(tMP%60)) WHERE tMP IS NOT NULL) AS 'MP',
  319. printf('%02d',(tblk/60))||':'||printf('%02d',(tblk%60)) AS 'Total',
  320. CASE WHEN pilot_id = 1 THEN alias ELSE lastname||', '||substr(firstname, 1, 1)||'.' END AS 'Name PIC',
  321. toDay AS 'Take-Off Day',
  322. ldgDay AS 'Landings Day',
  323. toNight AS 'Take-Off Night',
  324. ldgNight AS 'Landings Night',
  325. (SELECT printf('%02d',(tNight/60))||':'||printf('%02d',(tNight%60)) WHERE tNight IS NOT NULL) AS 'Night',
  326. (SELECT printf('%02d',(tIFR/60))||':'||printf('%02d',(tIFR%60)) WHERE tIFR IS NOT NULL) AS 'IFR',
  327. (SELECT printf('%02d',(tPIC/60))||':'||printf('%02d',(tPIC%60)) WHERE tPIC IS NOT NULL) AS 'PIC',
  328. (SELECT printf('%02d',(tSIC/60))||':'||printf('%02d',(tSIC%60)) WHERE tSIC IS NOT NULL) AS 'SIC',
  329. (SELECT printf('%02d',(tDual/60))||':'||printf('%02d',(tDual%60)) WHERE tDual IS NOT NULL) AS 'Dual',
  330. (SELECT printf('%02d',(tFI/60))||':'||printf('%02d',(tFI%60)) WHERE tFI IS NOT NULL) AS 'FI',
  331. null AS 'Sim Type',
  332. null AS 'Time of Session',
  333. remarks AS 'Remarks'
  334. FROM flights
  335. INNER JOIN pilots on flights.pic = pilots.pilot_id
  336. INNER JOIN tails on flights.acft = tails.tail_id
  337. UNION
  338. SELECT (session_id * -1),
  339. date,
  340. null, null, null, null,
  341. aircraftType,
  342. registration,
  343. null, null, null,
  344. 'SIM',
  345. null, null, null, null, null, null, null, null, null, null, null,
  346. deviceType, printf('%02d',(totalTime/60))||':'||printf('%02d',(totalTime%60)),
  347. remarks
  348. FROM simulators
  349. ORDER BY date DESC;