Guest User

Untitled

a guest
Nov 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. class dbMainExec extends dbConn
  2. {
  3.  
  4.  
  5. private $_table;
  6. private $_column;
  7. private $_where;
  8. private $_join;
  9.  
  10. private $_errors = array();
  11. private $_stmt;
  12. private $_results;
  13. private $_countResults;
  14.  
  15. public function __construct($table = null, $column = null, $where = null, $join = null)
  16. {
  17. parent::__construct();
  18. //$this->_conn = $conn;
  19. $this->setTable($table);
  20. $this->setColumn($column);
  21. $this->setWhere($where);
  22. $this->setJoin($join);
  23. }
  24.  
  25. public function setTable($table)
  26. {
  27. $table = strtolower($table);
  28. $this->_table = $table;
  29. }
  30.  
  31. public function getTable()
  32. {
  33. return $this->_table;
  34. }
  35.  
  36. public function setColumn($column)
  37. {
  38. if (is_array($column)) {
  39. $this->_column = $column;
  40. } else {
  41. $this->_errors[] = "Columen is geen array!";
  42.  
  43. }
  44. }
  45.  
  46. public function getColumn()
  47. {
  48. return $this->_column;
  49. }
  50.  
  51. public function setWhere($where)
  52. {
  53.  
  54. if (is_array($where) || empty($where)) {
  55. $this->_where = $where;
  56. } else {
  57. $this->_errors[] = "where is geen array!";
  58.  
  59. }
  60. }
  61.  
  62. public function getWhere()
  63. {
  64. return $this->_where;
  65. }
  66.  
  67. public function setJoin($join)
  68. {
  69. if (is_array($join) || empty($join)) {
  70. $this->_join = $join;
  71. } else {
  72. $this->_errors[] = "join is geen array!";
  73.  
  74. }
  75. }
  76.  
  77. public function getJoin()
  78. {
  79. return $this->_join;
  80. }
  81.  
  82.  
  83. public function setResults($results)
  84. {
  85. if (is_array($results)) {
  86. $this->_results = $results;
  87. } else {
  88. $this->_errors[] = "Fout bij resultaten";
  89.  
  90. }
  91. }
  92.  
  93. public function getResults()
  94. {
  95. return $this->_results;
  96. }
  97.  
  98. public function setCountResults($countResults)
  99. {
  100. if (is_numeric($countResults)) {
  101. $this->_countResults = $countResults;
  102. } else {
  103. $this->_errors[] = "fout bij tellen resultaten";
  104.  
  105. }
  106. }
  107.  
  108. public function getCountResults()
  109. {
  110. return $this->_countResults;
  111. }
  112.  
  113. public function setStatement($stmt)
  114. {
  115. $this->_stmt = $stmt;
  116. }
  117.  
  118. public function getStatement()
  119. {
  120. return $this->_stmt;
  121. }
  122.  
  123. public function execute($sql)
  124. {
  125.  
  126. $this->setStatement($this->connect()->prepare($sql));
  127.  
  128. /*if (!empty($this->getColumn())) {
  129. foreach ($this->getColumn() as $singleColumn => $columnValue) {
  130.  
  131. $this->getStatement()->bindParam(':' . $singleColumn, $columnValue);
  132. }
  133. }*/
  134.  
  135.  
  136. if (!empty($this->getWhere())) {
  137. foreach ($this->getWhere() as $singleWhere => $whereValue) {
  138.  
  139. $this->getStatement()->bindParam(':' . $singleWhere, $whereValue);
  140. }
  141. }
  142.  
  143. if (!empty($this->getJoin())) {
  144. foreach ($this->getJoin() as $singleJoin => $joinValue) {
  145. $this->getStatement()->bindParam(':' . $singleJoin, $joinValue);
  146. }
  147. }
  148.  
  149. $this->getStatement()->execute();
  150.  
  151. $this->disconnect();
  152.  
  153. }
  154.  
  155. public function selectRow()
  156. {
  157.  
  158.  
  159. if ($this->_errors) {
  160. var_dump($this->_errors);
  161. die();
  162. }
  163.  
  164. $sql = "SELECT " . implode($this->getColumn(), ',') . " FROM " . $this->getTable() . " ";
  165.  
  166. if (!empty($this->getJoin())) {
  167. foreach ($this->getJoin() as $extraTable => $extraValues) {
  168. $sql .= "JOIN $extraTable";
  169. foreach ($extraValues as $singleValue => $singleTarget) {
  170. $sql .= " ON {$singleValue} = {$singleTarget} ";
  171. }
  172. }
  173. }
  174.  
  175. if (!empty($this->getWhere())) {
  176. $sql .= "WHERE ";
  177. $arrayKeys = array_keys($this->getWhere());
  178. foreach ($this->getWhere() as $option => $value) {
  179. if ($option != end($arrayKeys)) {
  180. $sql .= $option . " = :{$option} AND ";
  181. } else {
  182. $sql .= $option . " = :{$option}";
  183. }
  184. }
  185. }
  186.  
  187. $this->execute($sql);
  188.  
  189.  
  190. $this->setResults($this->getStatement()->fetch(PDO::FETCH_ASSOC));
  191. $this->setCountResults($this->getStatement()->rowCount());
  192.  
  193. }
  194.  
  195. public function selectMultiple()
  196. {
  197.  
  198.  
  199. if ($this->_errors) {
  200. var_dump($this->_errors);
  201. die();
  202. }
  203.  
  204. $sql = "SELECT " . implode($this->getColumn(), ',') . " FROM " . $this->getTable() . " ";
  205.  
  206. if (!empty($this->getJoin())) {
  207. foreach ($this->getJoin() as $extraTable => $extraValues) {
  208. $sql .= "JOIN $extraTable";
  209. foreach ($extraValues as $singleValue => $singleTarget) {
  210. $sql .= " ON {$singleValue} = {$singleTarget} ";
  211. }
  212. }
  213. }
  214.  
  215.  
  216. if (!empty($this->getWhere())) {
  217. $sql .= "WHERE ";
  218. $arrayKeys = array_keys($this->getWhere());
  219. foreach ($this->getWhere() as $option => $value) {
  220. if ($option != end($arrayKeys)) {
  221. $sql .= $option . " = :{$option} AND ";
  222. } else {
  223. $sql .= $option . " = :{$option}";
  224. }
  225. }
  226. }
  227.  
  228.  
  229. $this->execute($sql);
  230.  
  231. $this->setResults($this->getStatement()->fetchAll(PDO::FETCH_ASSOC));
  232. $this->setCountResults($this->getStatement()->rowCount());
  233.  
  234. }
  235.  
  236. public function insert()
  237. {
  238.  
  239. if ($this->_errors) {
  240. var_dump($this->_errors);
  241. die();
  242. }
  243.  
  244. $sql = "INSERT INTO {$this->getTable()} ( " . implode(', ', array_keys($this->getColumn())) . ") VALUES (";
  245. $arrayKeys = array_keys($this->getColumn());
  246. foreach ($this->getColumn() as $singleColum => $singleValue) {
  247. $sql .= ":" . $singleColum;
  248. if ($singleColum != end($arrayKeys)) {
  249. $sql .= ", ";
  250. }
  251.  
  252. }
  253. $sql .= ')';
  254. $this->execute($sql);
  255. }
  256.  
  257. public function update()
  258. {
  259.  
  260.  
  261.  
  262. if ($this->_errors) {
  263. var_dump($this->_errors);
  264. die();
  265. }
  266.  
  267. $sql = "UPDATE {$this->getTable()} SET ";
  268.  
  269.  
  270. $arrayKeys = array_keys($this->getColumn());
  271. foreach ($this->getColumn() as $singlecolum => $singleValue) {
  272. $sql .= $singlecolum . " = :" . $singlecolum;
  273. if ($singlecolum != end($arrayKeys)) {
  274. $sql .= ", ";
  275. }
  276. }
  277.  
  278.  
  279. if (!empty($this->getWhere())) {
  280. $sql .= " WHERE ";
  281. $arrayKeys = array_keys($this->getWhere());
  282. foreach ($this->getWhere() as $option => $value) {
  283. if ($option != end($arrayKeys)) {
  284. $sql .= $option . " = :{$option} AND ";
  285. } else {
  286. $sql .= $option . " = :{$option}";
  287. }
  288.  
  289. }
  290. }
  291.  
  292. $this->execute($sql);
  293. }
  294.  
  295. public function delete()
  296. {
  297.  
  298. if ($this->_errors) {
  299. var_dump($this->_errors);
  300. die();
  301. }
  302.  
  303. $sql = "DELETE FROM {$this->getTable()}";
  304. if (!empty($this->getWhere())) {
  305. $sql .= " WHERE ";
  306. $arrayKeys = array_keys($this->getWhere());
  307. foreach ($this->getWhere() as $option => $value) {
  308. if ($option != end($arrayKeys)) {
  309. $sql .= $option . " = :{$option} AND ";
  310. } else {
  311. $sql .= $option . " = :{$option}";
  312. }
  313.  
  314. }
  315. }
  316.  
  317. $this->execute($sql);
  318. }
  319.  
  320. }
Add Comment
Please, Sign In to add comment