Guest User

Untitled

a guest
Aug 9th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. <?php
  2. class Database{
  3. private $dbc;
  4.  
  5. public function __construct($host, $db, $user, $pass){
  6. $this->dbc = pg_connect("host=$host dbname=$db user=$user password=$pass");
  7. }
  8.  
  9. public function getCustomers($view, $limit, $start){
  10. $query = "SELECT DISTINCT last_name, first_name, customer_id FROM $view ORDER BY last_name LIMIT $limit OFFSET $start";
  11. return @pg_query($this->dbc, $query);
  12. }
  13.  
  14. public function getActors($view, $limit, $start){
  15. $query = "SELECT DISTINCT last_name, first_name, actor_id FROM $view ORDER BY last_name LIMIT $limit OFFSET $start";
  16. return @pg_query($this->dbc, $query);
  17. }
  18.  
  19. public function getFilms($cat, $fsk, $limit, $start){
  20. $query = "SELECT f.film_id, f.title, f.description FROM film f, category c WHERE f.category_id = c.category_id AND c.name LIKE '$cat' AND f.rating LIKE '$fsk' ORDER BY title LIMIT $limit OFFSET $start";
  21. return @pg_query($this->dbc, $query);
  22. }
  23.  
  24. public function getCategories(){
  25. $query = "SELECT DISTINCT name FROM category";
  26. return @pg_query($this->dbc, $query);
  27. }
  28.  
  29. public function getFsk(){
  30. $query = "SELECT DISTINCT rating FROM film";
  31. return @pg_query($this->dbc, $query);
  32. }
  33.  
  34. public function formularSuche($eingabe, $view, $limit, $start, $sort, $order){
  35. $query = "";
  36. if($view === "customer"){
  37. $query = "SELECT * FROM customer NATURAL JOIN p_address NATURAL JOIN city NATURAL JOIN country";
  38. }
  39. if($view === "film"){
  40. $query = "SELECT * FROM film NATURAL JOIN category";
  41. }
  42. $a = array();
  43. $addExt = False;
  44. if(count($eingabe > 0)){
  45. foreach ($eingabe as $key=>$value) {
  46. if (!empty($value)) {
  47. $addExt = True;
  48. if($key === "film_id" || $key === "length" || $key === "customer_id"){
  49. $a[] = "CAST(".$key." as TEXT) LIKE '".$value."'";
  50.  
  51. }else{
  52. $a[] = $key." ILIKE '%".$value."%'";
  53.  
  54. }
  55. }
  56. }
  57. }
  58. $ext = implode(" AND ", $a);
  59. if($addExt){
  60. $query .= " WHERE ";
  61. }
  62.  
  63. if($sort != ""){
  64. $ext .= " ORDER BY ".$sort." ".$order;
  65. }
  66. $query .= $ext." LIMIT $limit OFFSET $start";
  67.  
  68. return pg_query($this->dbc, $query);
  69. }
  70.  
  71. public function freiTextSuche($eingabe, $view, $limit, $start, $sort, $order){
  72. $query = "";
  73. if($view === "customer"){
  74. $query = "SELECT * FROM customer NATURAL JOIN p_address NATURAL JOIN city NATURAL JOIN country";
  75. }
  76. if($view === "film"){
  77. $query = "SELECT * FROM film NATURAL JOIN category";
  78. }
  79.  
  80. $res = pg_query($this->dbc, $query);
  81. $tableArray = pg_fetch_all($res);
  82.  
  83. if($eingabe){
  84. #tabellenkopf ermitteln
  85. $conds = array();
  86. foreach($tableArray[0] as $key=>$value){
  87. $conds[] = "CAST(".$key." as TEXT) ILIKE '%".$eingabe."%'";
  88. }
  89.  
  90. #WHERE-Bedingungs-String bauen
  91. $ext = implode(" OR ", $conds);
  92. $query .= " WHERE ".$ext;
  93. }
  94.  
  95. if($sort != ""){
  96. $query .= " ORDER BY ".$sort." ".$order;
  97. }
  98.  
  99. $query .= " LIMIT $limit OFFSET $start";
  100.  
  101. return pg_query($this->dbc, $query);
  102. }
  103. /**
  104. * EXERCISE 4
  105. * login-function, returns 1 if authorization was succesfully, else 0
  106. */
  107. public function checkUser($username){
  108. $query = "select customer_id, first_name, customer_id from customer where customer_id = '$username';";
  109. return pg_query($this->dbc, $query);
  110. }
  111.  
  112. /**
  113. * EXERCISE 4
  114. * function insert values in rental, used in exercise four
  115. */
  116. public function setRental($inventory_id, $customer_id, $staff_id){
  117. $query = "INSERT INTO rental VALUES(now(), ".$inventory_id.", ".$customer_id.", null, ".$staff_id.");";
  118. pg_query($this->dbc, $query);
  119. }
  120.  
  121. /**
  122. * EXERCISE 4
  123. * function searches all movies
  124. */
  125. public function getAllFilms($limit, $start){
  126. $query = "SELECT DISTINCT title FROM FILM LIMIT $limit OFFSET $start;";
  127. return pg_query($this->dbc, $query);
  128. }
  129.  
  130. /**
  131. * EXERCISE 4
  132. * function searches all available film-copies
  133. */
  134. public function getAvailableFilms(){
  135. $query = "SELECT DISTINCT f.title FROM film f, rental r, inventory i WHERE r.return_date IS NOT NULL AND r.inventory_id = i.inventory_id AND i.film_id = f.film_id;";
  136. return pg_query($this->dbc, $query);
  137. }
  138.  
  139. /**
  140. * EXERCISE 4
  141. * function returns all available inventory_id's
  142. */
  143. public function getInventoryID($filmID){
  144. $query = "SELECT DISTINCT inventory_id FROM inventory WHERE film_id = $filmID AND inventory_id NOT IN (SELECT inventory_id FROM rental WHERE return_date IS NULL);";
  145. $id = pg_fetch_array(pg_query($this->dbc, $query));
  146. return $id[0];
  147. }
  148.  
  149. /**
  150. * EXERCISE 4
  151. * function returns film_id by title
  152. */
  153. public function getFilmID($name){
  154. $query ="select film_id from film where title = '$name';";
  155. $id = pg_fetch_array(pg_query($this->dbc, $query));
  156. return $id[0];
  157. }
  158. /**
  159. * EXERCISE 4
  160. */
  161. public function getNumberOfAvailableCopies($filmID){
  162. $query = "SELECT count(DISTINCT inventory_id) FROM inventory WHERE film_id = $filmID AND inventory_id NOT IN (SELECT inventory_id FROM rental WHERE return_date IS NULL);";
  163. $ret = pg_fetch_array(pg_query($this->dbc, $query));
  164. return $ret[0];
  165. }
  166.  
  167. /**
  168. * EXERCISE 4
  169. */
  170. public function checkCommit($invID){
  171. $query = "SELECT * FROM rental WHERE inventory_id = $invID AND return_date IS NULL FOR UPDATE;";
  172. $ret = pg_fetch_array(pg_query($query));
  173. if($ret == null){
  174. return True;
  175. }
  176. return False;
  177. }
  178. /**
  179. * EXERCISE 4
  180. */
  181. public function checkIfAlreadyBorrowed($filmID, $custID){
  182. $query = "SELECT * FROM rental NATURAL JOIN inventory WHERE customer_id = $custID AND film_id = $filmID AND return_date IS NULL;";
  183. $ret = pg_fetch_array(pg_query($query));
  184. if($ret == null){
  185. return False;
  186. }
  187. return True;
  188. }
  189.  
  190. public function getBorrowedMovies($custID){
  191. $query = "SELECT title,rent_date, ((date(CURRENT_TIMESTAMP) + interval '1 day')-date(rent_date)) as days FROM rental NATURAL JOIN customer INNER JOIN inventory ON (rental.inventory_id = inventory.inventory_id) NATURAL JOIN film WHERE return_date IS NULL AND customer_id = $custID;";
  192. return pg_query($this->dbc, $query);
  193. }
Add Comment
Please, Sign In to add comment