You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1388 lines
57 KiB

2 years ago
  1. /**
  2. * Implemented from here https://www.w3resource.com/sqlite/sqlite-subqueries.php
  3. */
  4. #include <sqlite_orm/sqlite_orm.h>
  5. #include <string>
  6. #include <memory>
  7. #include <iostream>
  8. using std::cout;
  9. using std::endl;
  10. struct Employee {
  11. int id;
  12. std::string firstName;
  13. std::string lastName;
  14. std::string email;
  15. std::string phoneNumber;
  16. std::string hireDate;
  17. std::string jobId;
  18. long salary;
  19. std::unique_ptr<double> commission_pct;
  20. std::unique_ptr<decltype(id)> managerId;
  21. int departmentId;
  22. };
  23. struct Department {
  24. int id;
  25. std::string name;
  26. int managerId;
  27. int locationId;
  28. };
  29. struct JobHistory {
  30. decltype(Employee::id) employeeId;
  31. std::string startDate;
  32. std::string endDate;
  33. decltype(Employee::jobId) jobId;
  34. decltype(Employee::departmentId) departmentId;
  35. };
  36. int main(int, char**) {
  37. using namespace sqlite_orm;
  38. auto storage = make_storage("subquery.sqlite",
  39. make_table("employees",
  40. make_column("EMPLOYEE_ID", &Employee::id, primary_key()),
  41. make_column("FIRST_NAME", &Employee::firstName),
  42. make_column("LAST_NAME", &Employee::lastName),
  43. make_column("EMAIL", &Employee::email),
  44. make_column("PHONE_NUMBER", &Employee::phoneNumber),
  45. make_column("HIRE_DATE", &Employee::hireDate),
  46. make_column("JOB_ID", &Employee::jobId),
  47. make_column("SALARY", &Employee::salary),
  48. make_column("COMMISSION_PCT", &Employee::commission_pct),
  49. make_column("MANAGER_ID", &Employee::managerId),
  50. make_column("DEPARTMENT_ID", &Employee::departmentId)),
  51. make_table("departments",
  52. make_column("DEPARTMENT_ID", &Department::id, primary_key()),
  53. make_column("DEPARTMENT_NAME", &Department::name),
  54. make_column("MANAGER_ID", &Department::managerId),
  55. make_column("LOCATION_ID", &Department::locationId)),
  56. make_table("job_history",
  57. make_column("employee_id", &JobHistory::employeeId),
  58. make_column("start_date", &JobHistory::startDate),
  59. make_column("end_date", &JobHistory::endDate),
  60. make_column("job_id", &JobHistory::jobId),
  61. make_column("department_id", &JobHistory::departmentId)));
  62. storage.sync_schema();
  63. storage.remove_all<Employee>();
  64. storage.replace(
  65. Employee{100, "Steven", "King", "SKING", "515.123.4567", "17-Jun-87", "AD_PRES", 24000, {}, {}, 90});
  66. storage.replace(Employee{101,
  67. "Neena",
  68. "Kochhar",
  69. "NKOCHHAR",
  70. "515.123.4568",
  71. "21-Sep-89",
  72. "AD_VP",
  73. 17000,
  74. {},
  75. std::make_unique<int>(100),
  76. 90});
  77. storage.replace(Employee{102,
  78. "Lex",
  79. "De Haan",
  80. "LDEHAAN",
  81. "515.123.4569",
  82. "13-Jan-93",
  83. "AD_VP",
  84. 17000,
  85. {},
  86. std::make_unique<int>(100),
  87. 90});
  88. storage.replace(Employee{103,
  89. "Alexander",
  90. "Hunold",
  91. "AHUNOLD",
  92. "590.423.4567",
  93. "3-Jan-90",
  94. "IT_PROG",
  95. 9000,
  96. {},
  97. std::make_unique<int>(102),
  98. 60});
  99. storage.replace(Employee{104,
  100. "Bruce",
  101. "Ernst",
  102. "BERNST",
  103. "590.423.4568",
  104. "21-May-91",
  105. "IT_PROG",
  106. 6000,
  107. {},
  108. std::make_unique<int>(103),
  109. 60});
  110. storage.replace(Employee{105,
  111. "David",
  112. "Austin",
  113. "DAUSTIN",
  114. "590.423.4569",
  115. "25-Jun-97",
  116. "IT_PROG",
  117. 4800,
  118. {},
  119. std::make_unique<int>(103),
  120. 60});
  121. storage.replace(Employee{106,
  122. "Valli",
  123. "Pataballa",
  124. "VPATABAL",
  125. "590.423.4560",
  126. "5-Feb-98",
  127. "IT_PROG",
  128. 4800,
  129. {},
  130. std::make_unique<int>(103),
  131. 60});
  132. storage.replace(Employee{107,
  133. "Diana",
  134. "Lorentz",
  135. "DLORENTZ",
  136. "590.423.5567",
  137. "7-Feb-99",
  138. "IT_PROG",
  139. 4200,
  140. {},
  141. std::make_unique<int>(103),
  142. 60});
  143. storage.replace(Employee{108,
  144. "Nancy",
  145. "Greenberg",
  146. "NGREENBE",
  147. "515.124.4569",
  148. "17-Aug-94",
  149. "FI_MGR",
  150. 12000,
  151. {},
  152. std::make_unique<int>(101),
  153. 100});
  154. storage.replace(Employee{109,
  155. "Daniel",
  156. "Faviet",
  157. "DFAVIET",
  158. "515.124.4169",
  159. "16-Aug-94",
  160. "FI_ACCOUNT",
  161. 9000,
  162. {},
  163. std::make_unique<int>(108),
  164. 100});
  165. storage.replace(Employee{110,
  166. "John",
  167. "Chen",
  168. "JCHEN",
  169. "515.124.4269",
  170. "28-Sep-97",
  171. "FI_ACCOUNT",
  172. 8200,
  173. {},
  174. std::make_unique<int>(108),
  175. 100});
  176. storage.replace(Employee{111,
  177. "Ismael",
  178. "Sciarra",
  179. "ISCIARRA",
  180. "515.124.4369",
  181. "30-Sep-97",
  182. "FI_ACCOUNT",
  183. 7700,
  184. {},
  185. std::make_unique<int>(108),
  186. 100});
  187. storage.replace(Employee{112,
  188. "Jose Manuel",
  189. "Urman",
  190. "JMURMAN",
  191. "515.124.4469",
  192. "7-Mar-98",
  193. "FI_ACCOUNT",
  194. 7800,
  195. {},
  196. std::make_unique<int>(108),
  197. 100});
  198. storage.replace(Employee{113,
  199. "Luis",
  200. "Popp",
  201. "LPOPP",
  202. "515.124.4567",
  203. "7-Dec-99",
  204. "FI_ACCOUNT",
  205. 6900,
  206. {},
  207. std::make_unique<int>(108),
  208. 100});
  209. storage.replace(Employee{114,
  210. "Den",
  211. "Raphaely",
  212. "DRAPHEAL",
  213. "515.127.4561",
  214. "7-Dec-94",
  215. "PU_MAN",
  216. 11000,
  217. {},
  218. std::make_unique<int>(100),
  219. 30});
  220. storage.replace(Employee{115,
  221. "Alexander",
  222. "Khoo",
  223. "AKHOO",
  224. "515.127.4562",
  225. "18-May-95",
  226. "PU_CLERK",
  227. 3100,
  228. {},
  229. std::make_unique<int>(114),
  230. 30});
  231. storage.replace(Employee{116,
  232. "Shelli",
  233. "Baida",
  234. "SBAIDA",
  235. "515.127.4563",
  236. "24-Dec-97",
  237. "PU_CLERK",
  238. 2900,
  239. {},
  240. std::make_unique<int>(114),
  241. 30});
  242. storage.replace(Employee{117,
  243. "Sigal",
  244. "Tobias",
  245. "STOBIAS",
  246. "515.127.4564",
  247. "24-Jul-97",
  248. "PU_CLERK",
  249. 2800,
  250. {},
  251. std::make_unique<int>(114),
  252. 30});
  253. storage.replace(Employee{118,
  254. "Guy",
  255. "Himuro",
  256. "GHIMURO",
  257. "515.127.4565",
  258. "15-Nov-98",
  259. "PU_CLERK",
  260. 2600,
  261. {},
  262. std::make_unique<int>(114),
  263. 30});
  264. storage.replace(Employee{119,
  265. "Karen",
  266. "Colmenares",
  267. "KCOLMENA",
  268. "515.127.4566",
  269. "10-Aug-99",
  270. "PU_CLERK",
  271. 2500,
  272. {},
  273. std::make_unique<int>(114),
  274. 30});
  275. storage.replace(Employee{120,
  276. "Matthew",
  277. "Weiss",
  278. "MWEISS",
  279. "650.123.1234",
  280. "18-Jul-96",
  281. "ST_MAN",
  282. 8000,
  283. {},
  284. std::make_unique<int>(100),
  285. 50});
  286. storage.replace(Employee{121,
  287. "Adam",
  288. "Fripp",
  289. "AFRIPP",
  290. "650.123.2234",
  291. "10-Apr-97",
  292. "ST_MAN",
  293. 8200,
  294. {},
  295. std::make_unique<int>(100),
  296. 50});
  297. storage.replace(Employee{122,
  298. "Payam",
  299. "Kaufling",
  300. "PKAUFLIN",
  301. "650.123.3234",
  302. "1-May-95",
  303. "ST_MAN",
  304. 7900,
  305. {},
  306. std::make_unique<int>(100),
  307. 50});
  308. storage.replace(Employee{123,
  309. "Shanta",
  310. "Vollman",
  311. "SVOLLMAN",
  312. "650.123.4234",
  313. "10-Oct-97",
  314. "ST_MAN",
  315. 6500,
  316. {},
  317. std::make_unique<int>(100),
  318. 50});
  319. storage.replace(Employee{124,
  320. "Kevin",
  321. "Mourgos",
  322. "KMOURGOS",
  323. "650.123.5234",
  324. "16-Nov-99",
  325. "ST_MAN",
  326. 5800,
  327. {},
  328. std::make_unique<int>(100),
  329. 50});
  330. storage.replace(Employee{125,
  331. "Julia",
  332. "Nayer",
  333. "JNAYER",
  334. "650.124.1214",
  335. "16-Jul-97",
  336. "ST_CLERK",
  337. 3200,
  338. {},
  339. std::make_unique<int>(120),
  340. 50});
  341. storage.replace(Employee{126,
  342. "Irene",
  343. "Mikkilineni",
  344. "IMIKKILI",
  345. "650.124.1224",
  346. "28-Sep-98",
  347. "ST_CLERK",
  348. 2700,
  349. {},
  350. std::make_unique<int>(120),
  351. 50});
  352. storage.replace(Employee{127,
  353. "James",
  354. "Landry",
  355. "JLANDRY",
  356. "650.124.1334",
  357. "14-Jan-99",
  358. "ST_CLERK",
  359. 2400,
  360. {},
  361. std::make_unique<int>(120),
  362. 50});
  363. storage.replace(Employee{128,
  364. "Steven",
  365. "Markle",
  366. "SMARKLE",
  367. "650.124.1434",
  368. "8-Mar-00",
  369. "ST_CLERK",
  370. 2200,
  371. {},
  372. std::make_unique<int>(120),
  373. 50});
  374. storage.replace(Employee{129,
  375. "Laura",
  376. "Bissot",
  377. "LBISSOT",
  378. "650.124.5234",
  379. "20-Aug-97",
  380. "ST_CLERK",
  381. 3300,
  382. {},
  383. std::make_unique<int>(121),
  384. 50});
  385. storage.replace(Employee{130,
  386. "Mozhe",
  387. "Atkinson",
  388. "MATKINSO",
  389. "650.124.6234",
  390. "30-Oct-97",
  391. "ST_CLERK",
  392. 2800,
  393. {},
  394. std::make_unique<int>(121),
  395. 50});
  396. storage.replace(Employee{131,
  397. "James",
  398. "Marlow",
  399. "JAMRLOW",
  400. "650.124.7234",
  401. "16-Feb-97",
  402. "ST_CLERK",
  403. 2500,
  404. {},
  405. std::make_unique<int>(121),
  406. 50});
  407. storage.replace(Employee{132,
  408. "TJ",
  409. "Olson",
  410. "TJOLSON",
  411. "650.124.8234",
  412. "10-Apr-99",
  413. "ST_CLERK",
  414. 2100,
  415. {},
  416. std::make_unique<int>(121),
  417. 50});
  418. storage.replace(Employee{133,
  419. "Jason",
  420. "Mallin",
  421. "JMALLIN",
  422. "650.127.1934",
  423. "14-Jun-96",
  424. "ST_CLERK",
  425. 3300,
  426. {},
  427. std::make_unique<int>(122),
  428. 50});
  429. storage.replace(Employee{134,
  430. "Michael",
  431. "Rogers",
  432. "MROGERS",
  433. "650.127.1834",
  434. "26-Aug-98",
  435. "ST_CLERK",
  436. 2900,
  437. {},
  438. std::make_unique<int>(122),
  439. 50});
  440. storage.replace(Employee{135,
  441. "Ki",
  442. "Gee",
  443. "KGEE",
  444. "650.127.1734",
  445. "12-Dec-99",
  446. "ST_CLERK",
  447. 2400,
  448. {},
  449. std::make_unique<int>(122),
  450. 50});
  451. storage.replace(Employee{136,
  452. "Hazel",
  453. "Philtanker",
  454. "HPHILTAN",
  455. "650.127.1634",
  456. "6-Feb-00",
  457. "ST_CLERK",
  458. 2200,
  459. {},
  460. std::make_unique<int>(122),
  461. 50});
  462. storage.replace(Employee{137,
  463. "Renske",
  464. "Ladwig",
  465. "RLADWIG",
  466. "650.121.1234",
  467. "14-Jul-95",
  468. "ST_CLERK",
  469. 3600,
  470. {},
  471. std::make_unique<int>(123),
  472. 50});
  473. storage.replace(Employee{138,
  474. "Stephen",
  475. "Stiles",
  476. "SSTILES",
  477. "650.121.2034",
  478. "26-Oct-97",
  479. "ST_CLERK",
  480. 3200,
  481. {},
  482. std::make_unique<int>(123),
  483. 50});
  484. storage.replace(Employee{139,
  485. "John",
  486. "Seo",
  487. "JSEO",
  488. "650.121.2019",
  489. "12-Feb-98",
  490. "ST_CLERK",
  491. 2700,
  492. {},
  493. std::make_unique<int>(123),
  494. 50});
  495. storage.replace(Employee{140,
  496. "Joshua",
  497. "Patel",
  498. "JPATEL",
  499. "650.121.1834",
  500. "6-Apr-98",
  501. "ST_CLERK",
  502. 2500,
  503. {},
  504. std::make_unique<int>(123),
  505. 50});
  506. storage.replace(Employee{141,
  507. "Trenna",
  508. "Rajs",
  509. "TRAJS",
  510. "650.121.8009",
  511. "17-Oct-95",
  512. "ST_CLERK",
  513. 3500,
  514. {},
  515. std::make_unique<int>(124),
  516. 50});
  517. storage.replace(Employee{142,
  518. "Curtis",
  519. "Davies",
  520. "CDAVIES",
  521. "650.121.2994",
  522. "29-Jan-97",
  523. "ST_CLERK",
  524. 3100,
  525. {},
  526. std::make_unique<int>(124),
  527. 50});
  528. storage.replace(Employee{143,
  529. "Randall",
  530. "Matos",
  531. "RMATOS",
  532. "650.121.2874",
  533. "15-Mar-98",
  534. "ST_CLERK",
  535. 2600,
  536. {},
  537. std::make_unique<int>(124),
  538. 50});
  539. storage.replace(Employee{144,
  540. "Peter",
  541. "Vargas",
  542. "PVARGAS",
  543. "650.121.2004",
  544. "9-Jul-98",
  545. "ST_CLERK",
  546. 2500,
  547. {},
  548. std::make_unique<int>(124),
  549. 50});
  550. storage.replace(Employee{145,
  551. "John",
  552. "Russell",
  553. "JRUSSEL",
  554. "011.44.1344.429268",
  555. "1-Oct-96",
  556. "SA_MAN",
  557. 14000,
  558. std::make_unique<double>(0.4),
  559. std::make_unique<int>(100),
  560. 80});
  561. storage.replace(Employee{146,
  562. "Karen",
  563. "Partners",
  564. "KPARTNER",
  565. "011.44.1344.467268",
  566. "5-Jan-97",
  567. "SA_MAN",
  568. 13500,
  569. std::make_unique<double>(0.3),
  570. std::make_unique<int>(100),
  571. 80});
  572. storage.replace(Employee{147,
  573. "Alberto",
  574. "Errazuriz",
  575. "AERRAZUR",
  576. "011.44.1344.429278",
  577. "10-Mar-97",
  578. "SA_MAN",
  579. 12000,
  580. std::make_unique<double>(0.3),
  581. std::make_unique<int>(100),
  582. 80});
  583. storage.replace(Employee{148,
  584. "Gerald",
  585. "Cambrault",
  586. "GCAMBRAU",
  587. "011.44.1344.619268",
  588. "15-Oct-99",
  589. "SA_MAN",
  590. 11000,
  591. std::make_unique<double>(0.3),
  592. std::make_unique<int>(100),
  593. 80});
  594. storage.replace(Employee{149,
  595. "Eleni",
  596. "Zlotkey",
  597. "EZLOTKEY",
  598. "011.44.1344.429018",
  599. "29-Jan-00",
  600. "SA_MAN",
  601. 10500,
  602. std::make_unique<double>(0.2),
  603. std::make_unique<int>(100),
  604. 80});
  605. storage.replace(Employee{150,
  606. "Peter",
  607. "Tucker",
  608. "PTUCKER",
  609. "011.44.1344.129268",
  610. "30-Jan-97",
  611. "SA_REP",
  612. 10000,
  613. std::make_unique<double>(0.3),
  614. std::make_unique<int>(145),
  615. 80});
  616. storage.replace(Employee{151,
  617. "David",
  618. "Bernstein",
  619. "DBERNSTE",
  620. "011.44.1344.345268",
  621. "24-Mar-97",
  622. "SA_REP",
  623. 9500,
  624. std::make_unique<double>(0.25),
  625. std::make_unique<int>(145),
  626. 80});
  627. storage.replace(Employee{152,
  628. "Peter",
  629. "Hall",
  630. "PHALL",
  631. "011.44.1344.478968",
  632. "20-Aug-97",
  633. "SA_REP",
  634. 9000,
  635. std::make_unique<double>(0.25),
  636. std::make_unique<int>(145),
  637. 80});
  638. storage.replace(Employee{153,
  639. "Christopher",
  640. "Olsen",
  641. "COLSEN",
  642. "011.44.1344.498718",
  643. "30-Mar-98",
  644. "SA_REP",
  645. 8000,
  646. std::make_unique<double>(0.2),
  647. std::make_unique<int>(145),
  648. 80});
  649. storage.replace(Employee{154,
  650. "Nanette",
  651. "Cambrault",
  652. "NCAMBRAU",
  653. "011.44.1344.987668",
  654. "9-Dec-98",
  655. "SA_REP",
  656. 7500,
  657. std::make_unique<double>(0.2),
  658. std::make_unique<int>(145),
  659. 80});
  660. storage.replace(Employee{155,
  661. "Oliver",
  662. "Tuvault",
  663. "OTUVAULT",
  664. "011.44.1344.486508",
  665. "23-Nov-99",
  666. "SA_REP",
  667. 7000,
  668. std::make_unique<double>(0.15),
  669. std::make_unique<int>(145),
  670. 80});
  671. storage.replace(Employee{156,
  672. "Janette",
  673. "King",
  674. "JKING",
  675. "011.44.1345.429268",
  676. "30-Jan-96",
  677. "SA_REP",
  678. 10000,
  679. std::make_unique<double>(0.35),
  680. std::make_unique<int>(146),
  681. 80});
  682. storage.replace(Employee{157,
  683. "Patrick",
  684. "Sully",
  685. "PSULLY",
  686. "011.44.1345.929268",
  687. "4-Mar-96",
  688. "SA_REP",
  689. 9500,
  690. std::make_unique<double>(0.35),
  691. std::make_unique<int>(146),
  692. 80});
  693. storage.replace(Employee{158,
  694. "Allan",
  695. "McEwen",
  696. "AMCEWEN",
  697. "011.44.1345.829268",
  698. "1-Aug-96",
  699. "SA_REP",
  700. 9000,
  701. std::make_unique<double>(0.35),
  702. std::make_unique<int>(146),
  703. 80});
  704. storage.replace(Employee{159,
  705. "Lindsey",
  706. "Smith",
  707. "LSMITH",
  708. "011.44.1345.729268",
  709. "10-Mar-97",
  710. "SA_REP",
  711. 8000,
  712. std::make_unique<double>(0.3),
  713. std::make_unique<int>(146),
  714. 80});
  715. storage.replace(Employee{160,
  716. "Louise",
  717. "Doran",
  718. "LDORAN",
  719. "011.44.1345.629268",
  720. "15-Dec-97",
  721. "SA_REP",
  722. 7500,
  723. std::make_unique<double>(0.3),
  724. std::make_unique<int>(146),
  725. 80});
  726. storage.replace(Employee{161,
  727. "Sarath",
  728. "Sewall",
  729. "SSEWALL",
  730. "011.44.1345.529268",
  731. "3-Nov-98",
  732. "SA_REP",
  733. 7000,
  734. std::make_unique<double>(0.25),
  735. std::make_unique<int>(146),
  736. 80});
  737. storage.replace(Employee{162,
  738. "Clara",
  739. "Vishney",
  740. "CVISHNEY",
  741. "011.44.1346.129268",
  742. "11-Nov-97",
  743. "SA_REP",
  744. 10500,
  745. std::make_unique<double>(0.25),
  746. std::make_unique<int>(147),
  747. 80});
  748. storage.replace(Employee{163,
  749. "Danielle",
  750. "Greene",
  751. "DGREENE",
  752. "011.44.1346.229268",
  753. "19-Mar-99",
  754. "SA_REP",
  755. 9500,
  756. std::make_unique<double>(0.15),
  757. std::make_unique<int>(147),
  758. 80});
  759. storage.replace(Employee{164,
  760. "Mattea",
  761. "Marvins",
  762. "MMARVINS",
  763. "011.44.1346.329268",
  764. "24-Jan-00",
  765. "SA_REP",
  766. 7200,
  767. std::make_unique<double>(0.1),
  768. std::make_unique<int>(147),
  769. 80});
  770. storage.replace(Employee{165,
  771. "David",
  772. "Lee",
  773. "DLEE",
  774. "011.44.1346.529268",
  775. "23-Feb-00",
  776. "SA_REP",
  777. 6800,
  778. std::make_unique<double>(0.1),
  779. std::make_unique<int>(147),
  780. 80});
  781. storage.replace(Employee{166,
  782. "Sundar",
  783. "Ande",
  784. "SANDE",
  785. "011.44.1346.629268",
  786. "24-Mar-00",
  787. "SA_REP",
  788. 6400,
  789. std::make_unique<double>(0.1),
  790. std::make_unique<int>(147),
  791. 80});
  792. storage.replace(Employee{167,
  793. "Amit",
  794. "Banda",
  795. "ABANDA",
  796. "011.44.1346.729268",
  797. "21-Apr-00",
  798. "SA_REP",
  799. 6200,
  800. std::make_unique<double>(0.1),
  801. std::make_unique<int>(147),
  802. 80});
  803. storage.replace(Employee{168,
  804. "Lisa",
  805. "Ozer",
  806. "LOZER",
  807. "011.44.1343.929268",
  808. "11-Mar-97",
  809. "SA_REP",
  810. 11500,
  811. std::make_unique<double>(0.25),
  812. std::make_unique<int>(148),
  813. 80});
  814. storage.replace(Employee{169,
  815. "Harrison",
  816. "Bloom",
  817. "HBLOOM",
  818. "011.44.1343.829268",
  819. "23-Mar-98",
  820. "SA_REP",
  821. 10000,
  822. std::make_unique<double>(0.2),
  823. std::make_unique<int>(148),
  824. 80});
  825. storage.replace(Employee{170,
  826. "Tayler",
  827. "Fox",
  828. "TFOX",
  829. "011.44.1343.729268",
  830. "24-Jan-98",
  831. "SA_REP",
  832. 9600,
  833. std::make_unique<double>(0.2),
  834. std::make_unique<int>(148),
  835. 80});
  836. storage.replace(Employee{171,
  837. "William",
  838. "Smith",
  839. "WSMITH",
  840. "011.44.1343.629268",
  841. "23-Feb-99",
  842. "SA_REP",
  843. 7400,
  844. std::make_unique<double>(0.15),
  845. std::make_unique<int>(148),
  846. 80});
  847. storage.replace(Employee{172,
  848. "Elizabeth",
  849. "Bates",
  850. "EBATES",
  851. "011.44.1343.529268",
  852. "24-Mar-99",
  853. "SA_REP",
  854. 7300,
  855. std::make_unique<double>(0.15),
  856. std::make_unique<int>(148),
  857. 80});
  858. storage.replace(Employee{173,
  859. "Sundita",
  860. "Kumar",
  861. "SKUMAR",
  862. "011.44.1343.329268",
  863. "21-Apr-00",
  864. "SA_REP",
  865. 6100,
  866. std::make_unique<double>(0.1),
  867. std::make_unique<int>(148),
  868. 80});
  869. storage.replace(Employee{174,
  870. "Ellen",
  871. "Abel",
  872. "EABEL",
  873. "011.44.1644.429267",
  874. "11-May-96",
  875. "SA_REP",
  876. 11000,
  877. std::make_unique<double>(0.3),
  878. std::make_unique<int>(149),
  879. 80});
  880. storage.replace(Employee{175,
  881. "Alyssa",
  882. "Hutton",
  883. "AHUTTON",
  884. "011.44.1644.429266",
  885. "19-Mar-97",
  886. "SA_REP",
  887. 8800,
  888. std::make_unique<double>(0.25),
  889. std::make_unique<int>(149),
  890. 80});
  891. storage.replace(Employee{176,
  892. "Jonathon",
  893. "Taylor",
  894. "JTAYLOR",
  895. "011.44.1644.429265",
  896. "24-Mar-98",
  897. "SA_REP",
  898. 8600,
  899. std::make_unique<double>(0.2),
  900. std::make_unique<int>(149),
  901. 80});
  902. storage.replace(Employee{177,
  903. "Jack",
  904. "Livingston",
  905. "JLIVINGS",
  906. "011.44.1644.429264",
  907. "23-Apr-98",
  908. "SA_REP",
  909. 8400,
  910. std::make_unique<double>(0.2),
  911. std::make_unique<int>(149),
  912. 80});
  913. storage.replace(Employee{178,
  914. "Kimberely",
  915. "Grant",
  916. "KGRANT",
  917. "011.44.1644.429263",
  918. "24-May-99",
  919. "SA_REP",
  920. 7000,
  921. std::make_unique<double>(0.15),
  922. std::make_unique<int>(149),
  923. 80});
  924. storage.replace(Employee{179,
  925. "Charles",
  926. "Johnson",
  927. "CJOHNSON",
  928. "011.44.1644.429262",
  929. "4-Jan-00",
  930. "SA_REP",
  931. 6200,
  932. std::make_unique<double>(0.1),
  933. std::make_unique<int>(149),
  934. 80});
  935. storage.replace(Employee{180,
  936. "Winston",
  937. "Taylor",
  938. "WTAYLOR",
  939. "650.507.9876",
  940. "24-Jan-98",
  941. "SH_CLERK",
  942. 3200,
  943. {},
  944. std::make_unique<int>(120),
  945. 50});
  946. storage.replace(Employee{181,
  947. "Jean",
  948. "Fleaur",
  949. "JFLEAUR",
  950. "650.507.9877",
  951. "23-Feb-98",
  952. "SH_CLERK",
  953. 3100,
  954. {},
  955. std::make_unique<int>(120),
  956. 50});
  957. storage.replace(Employee{182,
  958. "Martha",
  959. "Sullivan",
  960. "MSULLIVA",
  961. "650.507.9878",
  962. "21-Jun-99",
  963. "SH_CLERK",
  964. 2500,
  965. {},
  966. std::make_unique<int>(120),
  967. 50});
  968. storage.replace(Employee{183,
  969. "Girard",
  970. "Geoni",
  971. "GGEONI",
  972. "650.507.9879",
  973. "3-Feb-00",
  974. "SH_CLERK",
  975. 2800,
  976. {},
  977. std::make_unique<int>(120),
  978. 50});
  979. storage.replace(Employee{184,
  980. "Nandita",
  981. "Sarchand",
  982. "NSARCHAN",
  983. "650.509.1876",
  984. "27-Jan-96",
  985. "SH_CLERK",
  986. 4200,
  987. {},
  988. std::make_unique<int>(121),
  989. 50});
  990. storage.replace(Employee{185,
  991. "Alexis",
  992. "Bull",
  993. "ABULL",
  994. "650.509.2876",
  995. "20-Feb-97",
  996. "SH_CLERK",
  997. 4100,
  998. {},
  999. std::make_unique<int>(121),
  1000. 50});
  1001. storage.replace(Employee{186,
  1002. "Julia",
  1003. "Dellinger",
  1004. "JDELLING",
  1005. "650.509.3876",
  1006. "24-Jun-98",
  1007. "SH_CLERK",
  1008. 3400,
  1009. {},
  1010. std::make_unique<int>(121),
  1011. 50});
  1012. storage.replace(Employee{187,
  1013. "Anthony",
  1014. "Cabrio",
  1015. "ACABRIO",
  1016. "650.509.4876",
  1017. "7-Feb-99",
  1018. "SH_CLERK",
  1019. 3000,
  1020. {},
  1021. std::make_unique<int>(121),
  1022. 50});
  1023. storage.replace(Employee{188,
  1024. "Kelly",
  1025. "Chung",
  1026. "KCHUNG",
  1027. "650.505.1876",
  1028. "14-Jun-97",
  1029. "SH_CLERK",
  1030. 3800,
  1031. {},
  1032. std::make_unique<int>(122),
  1033. 50});
  1034. storage.replace(Employee{189,
  1035. "Jennifer",
  1036. "Dilly",
  1037. "JDILLY",
  1038. "650.505.2876",
  1039. "13-Aug-97",
  1040. "SH_CLERK",
  1041. 3600,
  1042. {},
  1043. std::make_unique<int>(122),
  1044. 50});
  1045. storage.replace(Employee{190,
  1046. "Timothy",
  1047. "Gates",
  1048. "TGATES",
  1049. "650.505.3876",
  1050. "11-Jul-98",
  1051. "SH_CLERK",
  1052. 2900,
  1053. {},
  1054. std::make_unique<int>(122),
  1055. 50});
  1056. storage.replace(Employee{191,
  1057. "Randall",
  1058. "Perkins",
  1059. "RPERKINS",
  1060. "650.505.4876",
  1061. "19-Dec-99",
  1062. "SH_CLERK",
  1063. 2500,
  1064. {},
  1065. std::make_unique<int>(122),
  1066. 50});
  1067. storage.replace(Employee{192,
  1068. "Sarah",
  1069. "Bell",
  1070. "SBELL",
  1071. "650.501.1876",
  1072. "4-Feb-96",
  1073. "SH_CLERK",
  1074. 4000,
  1075. {},
  1076. std::make_unique<int>(123),
  1077. 50});
  1078. storage.replace(Employee{193,
  1079. "Britney",
  1080. "Everett",
  1081. "BEVERETT",
  1082. "650.501.2876",
  1083. "3-Mar-97",
  1084. "SH_CLERK",
  1085. 3900,
  1086. {},
  1087. std::make_unique<int>(123),
  1088. 50});
  1089. storage.replace(Employee{194,
  1090. "Samuel",
  1091. "McCain",
  1092. "SMCCAIN",
  1093. "650.501.3876",
  1094. "1-Jul-98",
  1095. "SH_CLERK",
  1096. 3200,
  1097. {},
  1098. std::make_unique<int>(123),
  1099. 50});
  1100. storage.replace(Employee{195,
  1101. "Vance",
  1102. "Jones",
  1103. "VJONES",
  1104. "650.501.4876",
  1105. "17-Mar-99",
  1106. "SH_CLERK",
  1107. 2800,
  1108. {},
  1109. std::make_unique<int>(123),
  1110. 50});
  1111. storage.replace(Employee{196,
  1112. "Alana",
  1113. "Walsh",
  1114. "AWALSH",
  1115. "650.507.9811",
  1116. "24-Apr-98",
  1117. "SH_CLERK",
  1118. 3100,
  1119. {},
  1120. std::make_unique<int>(124),
  1121. 50});
  1122. storage.replace(Employee{197,
  1123. "Kevin",
  1124. "Feeney",
  1125. "KFEENEY",
  1126. "650.507.9822",
  1127. "23-May-98",
  1128. "SH_CLERK",
  1129. 3000,
  1130. {},
  1131. std::make_unique<int>(124),
  1132. 50});
  1133. storage.replace(Employee{198,
  1134. "Donald",
  1135. "OConnell",
  1136. "DOCONNEL",
  1137. "650.507.9833",
  1138. "21-Jun-99",
  1139. "SH_CLERK",
  1140. 2600,
  1141. {},
  1142. std::make_unique<int>(124),
  1143. 50});
  1144. storage.replace(Employee{199,
  1145. "Douglas",
  1146. "Grant",
  1147. "DGRANT",
  1148. "650.507.9844",
  1149. "13-Jan-00",
  1150. "SH_CLERK",
  1151. 2600,
  1152. {},
  1153. std::make_unique<int>(124),
  1154. 50});
  1155. storage.replace(Employee{200,
  1156. "Jennifer",
  1157. "Whalen",
  1158. "JWHALEN",
  1159. "515.123.4444",
  1160. "17-Sep-87",
  1161. "AD_ASST",
  1162. 4400,
  1163. {},
  1164. std::make_unique<int>(101),
  1165. 10});
  1166. storage.replace(Employee{201,
  1167. "Michael",
  1168. "Hartstein",
  1169. "MHARTSTE",
  1170. "515.123.5555",
  1171. "17-Feb-96",
  1172. "MK_MAN",
  1173. 13000,
  1174. {},
  1175. std::make_unique<int>(100),
  1176. 20});
  1177. storage.replace(Employee{202,
  1178. "Pat",
  1179. "Fay",
  1180. "PFAY",
  1181. "603.123.6666",
  1182. "17-Aug-97",
  1183. "MK_REP",
  1184. 6000,
  1185. {},
  1186. std::make_unique<int>(201),
  1187. 20});
  1188. storage.replace(Employee{203,
  1189. "Susan",
  1190. "Mavris",
  1191. "SMAVRIS",
  1192. "515.123.7777",
  1193. "7-Jun-94",
  1194. "HR_REP",
  1195. 6500,
  1196. {},
  1197. std::make_unique<int>(101),
  1198. 40});
  1199. storage.replace(Employee{204,
  1200. "Hermann",
  1201. "Baer",
  1202. "HBAER",
  1203. "515.123.8888",
  1204. "7-Jun-94",
  1205. "PR_REP",
  1206. 10000,
  1207. {},
  1208. std::make_unique<int>(101),
  1209. 70});
  1210. storage.replace(Employee{205,
  1211. "Shelley",
  1212. "Higgins",
  1213. "SHIGGINS",
  1214. "515.123.8080",
  1215. "7-Jun-94",
  1216. "AC_MGR",
  1217. 12000,
  1218. {},
  1219. std::make_unique<int>(101),
  1220. 110});
  1221. storage.replace(Employee{206,
  1222. "William",
  1223. "Gietz",
  1224. "WGIETZ",
  1225. "515.123.8181",
  1226. "7-Jun-94",
  1227. "AC_ACCOUNT",
  1228. 8300,
  1229. {},
  1230. std::make_unique<int>(205),
  1231. 110});
  1232. storage.replace(Department{10, "Administration", 200, 1700});
  1233. storage.replace(Department{20, "Marketing", 201, 1800});
  1234. storage.replace(Department{30, "Purchasing", 114, 1700});
  1235. storage.replace(Department{40, "Human Resources", 203, 2400});
  1236. storage.replace(Department{50, "Shipping", 121, 1500});
  1237. storage.replace(Department{60, "IT", 103, 1400});
  1238. storage.replace(Department{70, "Public Relations", 204, 2700});
  1239. storage.replace(Department{80, "Sales", 145, 2500});
  1240. storage.replace(Department{90, "Executive", 100, 1700});
  1241. storage.replace(Department{100, "Finance", 108, 1700});
  1242. storage.replace(Department{110, "Accounting", 205, 1700});
  1243. storage.replace(Department{120, "Treasury", 0, 1700});
  1244. storage.replace(Department{130, "Corporate Tax", 0, 1700});
  1245. storage.replace(Department{140, "Control And Credit", 0, 1700});
  1246. storage.replace(Department{150, "Shareholder Services", 0, 1700});
  1247. storage.replace(Department{160, "Benefits", 0, 1700});
  1248. storage.replace(Department{170, "Manufacturing", 0, 1700});
  1249. storage.replace(Department{180, "Construction", 0, 1700});
  1250. storage.replace(Department{190, "Contracting", 0, 1700});
  1251. storage.replace(Department{200, "Operations", 0, 1700});
  1252. storage.replace(Department{210, "IT Support", 0, 1700});
  1253. storage.replace(Department{220, "NOC", 0, 1700});
  1254. storage.replace(Department{230, "IT Helpdesk", 0, 1700});
  1255. storage.replace(Department{240, "Government Sales", 0, 1700});
  1256. storage.replace(Department{250, "Retail Sales", 0, 1700});
  1257. storage.replace(Department{260, "Recruiting", 0, 1700});
  1258. storage.replace(Department{270, "Payroll", 0, 1700});
  1259. storage.replace(JobHistory{102, "1993-01-13", "1998-07-24", "IT_PROG", 60});
  1260. storage.replace(JobHistory{101, "1989-09-21", "1993-10-27", "AC_ACCOUNT", 110});
  1261. storage.replace(JobHistory{101, "1993-10-28", "1997-03-15", "AC_MGR", 110});
  1262. storage.replace(JobHistory{201, "1996-02-17", "1999-12-19", "MK_REP", 20});
  1263. storage.replace(JobHistory{114, "1998-03-24", "1999-12-31", "ST_CLERK", 50});
  1264. storage.replace(JobHistory{122, "1999-01-01", "1999-12-31", "ST_CLERK", 50});
  1265. storage.replace(JobHistory{200, "1987-09-17", "1993-06-17", "AD_ASST", 90});
  1266. storage.replace(JobHistory{176, "1998-03-24", "1998-12-31", "SA_REP", 80});
  1267. storage.replace(JobHistory{176, "1999-01-01", "1999-12-31", "SA_MAN", 80});
  1268. storage.replace(JobHistory{200, "1994-07-01", "1998-12-31", "AC_ACCOUNT", 90});
  1269. {
  1270. // SELECT first_name, last_name, salary
  1271. // FROM employees
  1272. // WHERE salary >(
  1273. // SELECT salary
  1274. // FROM employees
  1275. // WHERE first_name='Alexander');
  1276. auto rows = storage.select(
  1277. columns(&Employee::firstName, &Employee::lastName, &Employee::salary),
  1278. where(greater_than(&Employee::salary,
  1279. select(&Employee::salary, where(is_equal(&Employee::firstName, "Alexander"))))));
  1280. cout << "first_name last_name salary" << endl;
  1281. cout << "---------- ---------- ----------" << endl;
  1282. for(auto& row: rows) {
  1283. cout << std::get<0>(row) << '\t' << std::get<1>(row) << '\t' << std::get<2>(row) << endl;
  1284. }
  1285. }
  1286. {
  1287. // SELECT employee_id,first_name,last_name,salary
  1288. // FROM employees
  1289. // WHERE salary > (SELECT AVG(SALARY) FROM employees);
  1290. auto rows = storage.select(columns(&Employee::id, &Employee::firstName, &Employee::lastName, &Employee::salary),
  1291. where(greater_than(&Employee::salary, select(avg(&Employee::salary)))));
  1292. cout << "employee_id first_name last_name salary" << endl;
  1293. cout << "----------- ---------- ---------- ----------" << endl;
  1294. for(auto& row: rows) {
  1295. cout << std::get<0>(row) << '\t' << std::get<1>(row) << '\t' << std::get<2>(row) << '\t' << std::get<3>(row)
  1296. << endl;
  1297. }
  1298. }
  1299. {
  1300. // SELECT first_name, last_name, department_id
  1301. // FROM employees
  1302. // WHERE department_id IN
  1303. // (SELECT DEPARTMENT_ID FROM departments
  1304. // WHERE location_id=1700);
  1305. auto rows = storage.select(
  1306. columns(&Employee::firstName, &Employee::lastName, &Employee::departmentId),
  1307. from<Employee>(),
  1308. where(in(&Employee::departmentId, select(&Department::id, where(c(&Department::locationId) == 1700)))));
  1309. cout << "first_name last_name department_id" << endl;
  1310. cout << "---------- ---------- -------------" << endl;
  1311. for(auto& row: rows) {
  1312. cout << std::get<0>(row) << '\t' << std::get<1>(row) << '\t' << std::get<2>(row) << endl;
  1313. }
  1314. }
  1315. {
  1316. // SELECT first_name, last_name, department_id
  1317. // FROM employees
  1318. // WHERE department_id NOT IN
  1319. // (SELECT DEPARTMENT_ID FROM departments
  1320. // WHERE manager_id
  1321. // BETWEEN 100 AND 200);
  1322. auto rows =
  1323. storage.select(columns(&Employee::firstName, &Employee::lastName, &Employee::departmentId),
  1324. from<Employee>(),
  1325. where(not_in(&Employee::departmentId,
  1326. select(&Department::id, where(between(&Department::managerId, 100, 200))))));
  1327. cout << "first_name last_name department_id" << endl;
  1328. cout << "---------- ---------- -------------" << endl;
  1329. for(auto& row: rows) {
  1330. cout << std::get<0>(row) << '\t' << std::get<1>(row) << '\t' << std::get<2>(row) << endl;
  1331. }
  1332. }
  1333. {
  1334. // SELECT last_name, salary, department_id
  1335. // FROM employees e
  1336. // WHERE salary >(SELECT AVG(salary)
  1337. // FROM employees
  1338. // WHERE department_id = e.department_id);
  1339. using als = alias_e<Employee>;
  1340. auto rows = storage.select(
  1341. columns(alias_column<als>(&Employee::lastName),
  1342. alias_column<als>(&Employee::salary),
  1343. alias_column<als>(&Employee::departmentId)),
  1344. from<als>(),
  1345. where(greater_than(
  1346. alias_column<als>(&Employee::salary),
  1347. select(avg(&Employee::salary),
  1348. from<Employee>(),
  1349. where(is_equal(&Employee::departmentId, alias_column<als>(&Employee::departmentId)))))));
  1350. cout << "last_name salary department_id" << endl;
  1351. cout << "---------- ---------- -------------" << endl;
  1352. for(auto& row: rows) {
  1353. cout << std::get<0>(row) << '\t' << std::get<1>(row) << '\t' << std::get<2>(row) << endl;
  1354. }
  1355. }
  1356. {
  1357. // SELECT first_name, last_name, employee_id, job_id
  1358. // FROM employees
  1359. // WHERE 1 <=
  1360. // (SELECT COUNT(*) FROM Job_history
  1361. // WHERE employee_id = employees.employee_id);
  1362. auto rows =
  1363. storage.select(columns(&Employee::firstName, &Employee::lastName, &Employee::id, &Employee::jobId),
  1364. from<Employee>(),
  1365. where(lesser_or_equal(1,
  1366. select(count<JobHistory>(),
  1367. from<JobHistory>(),
  1368. where(is_equal(&Employee::id, &JobHistory::employeeId))))));
  1369. cout << "first_name last_name employee_id job_id" << endl;
  1370. cout << "---------- ---------- ----------- ----------" << endl;
  1371. for(auto& row: rows) {
  1372. cout << std::get<0>(row) << '\t' << std::get<1>(row) << '\t' << std::get<2>(row) << '\t' << std::get<3>(row)
  1373. << endl;
  1374. }
  1375. }
  1376. return 0;
  1377. }