Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MGmodule\crud2\config;
  4.  
  5. class Database extends \PDO
  6. {
  7.  
  8. private $dsn = 'mysql:dbname=todo;host=localhost';
  9. private $username = 'root';
  10. private $pass = 'Samolot132';
  11. private static $_instance = null;
  12.  
  13. //musi być public, bo dziedziczy po PDO, też nad tym ubolewam :(
  14. public function __construct()
  15. {
  16. parent::__construct($this->dsn, $this->username, $this->pass);
  17. }
  18.  
  19. public static function getInstance()
  20. {
  21. try {
  22. if (!(self::$_instance instanceof Database)) {
  23. self::$_instance = new Database();
  24. }
  25. } catch (\PdoException $e) {
  26. echo "Database doesn't want to work with Us";
  27. }
  28. return self::$_instance;
  29. }
  30.  
  31. /*
  32. * Select from database WHERE
  33. * string @quantity
  34. * string @table
  35. * array assoc @data
  36. * return array
  37. *
  38. */
  39.  
  40. public static function select($quantity, $table, array $data)
  41. {
  42. $query = ('SELECT ' . $quantity . ' FROM ' . $table . ' WHERE ' . self::arrayToQuery($data));
  43.  
  44. $prepared = self::getReady($query);
  45. $stmt = self::bind($data, $prepared);
  46.  
  47. return $row = self::getRow($stmt);
  48. }
  49.  
  50. /*
  51. * Select all from database
  52. * string @quantity
  53. * string @table
  54. */
  55.  
  56. public static function selectAll($quantity, $table)
  57. {
  58. $query = ('SELECT' . $quantity . 'FROM ' . $table);
  59.  
  60. $stmt = self::getReady($query);
  61.  
  62. return $row = self::getRow($stmt);
  63. }
  64.  
  65. /*
  66. *
  67. * string @table
  68. * array assoc $data
  69. */
  70.  
  71. public static function Insert($table, array $data)
  72. {
  73. $query = ('INSERT INTO ' . $table . ' (' . self::getParamsIndex($data) . ') VALUES (' . self::getParamsIndexToBind($data) . ')');
  74.  
  75. $ready = self::getReady($query);
  76. $stmt = self::bind($data, $ready);
  77.  
  78. return $execute = $stmt->execute();
  79. }
  80.  
  81. /*
  82. * string @table
  83. * int @id
  84. */
  85.  
  86. public static function delete($table, $id)
  87. {
  88. $query = ('DELETE FROM ' . $table . ' WHERE id = :id');
  89. $stmt = self::getReady($query);
  90. $stmt->bindValue(':id', $id);
  91.  
  92. return $result = $stmt->execute();
  93. }
  94.  
  95. /*
  96. * string @table
  97. * array assoc @data
  98. * int @id
  99. */
  100.  
  101. public static function update($table, array $data, $id)
  102. {
  103. $query = ('UPDATE ' . $table . ' SET ' . self::arrayToQuery($data) . ' WHERE id = :id');
  104. $ready = self::getReady($query);
  105. $stmt = self::bind($data, $ready);
  106. $stmt->bindValue(':id', $id);
  107.  
  108. $row = $stmt->execute();
  109.  
  110.  
  111. return 1;
  112. }
  113.  
  114. private static function getReady($query)
  115. {
  116. return $stmt = self::getInstance()->prepare($query);
  117. }
  118.  
  119. private static function getRow($stmt)
  120. {
  121. $stmt->execute();
  122. $row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  123.  
  124. if (!$row) {
  125. return false;
  126. }
  127.  
  128. return $row[0];
  129. }
  130.  
  131. private static function bind(array $data, $stmt)
  132. {
  133. $statement = $stmt;
  134.  
  135. foreach ($data as $db_index => $value) {
  136. $statement->bindValue(':' . $db_index, $value);
  137. }
  138.  
  139. return $statement;
  140. }
  141.  
  142. /*
  143. * return string 'index=:index, index2=:index2, index3=:index3'
  144. */
  145.  
  146. private static function arrayToQuery(array $data)
  147. {
  148. $query_params = '';
  149. $i = 0;
  150.  
  151. foreach ($data as $db_name => $key) {
  152. if (0 == $i) {
  153. $query_params = $db_name . "=:" . $db_name;
  154. $i++;
  155. } else {
  156. $query_params = $query_params . ', ' . $db_name . "=:" . $db_name . ' ';
  157. }
  158. }
  159.  
  160. return $query_params;
  161. }
  162.  
  163. /*
  164. * return string 'index, index2, index3'
  165. */
  166.  
  167. private static function getParamsIndex(array $data)
  168. {
  169. $db_index = '';
  170. $i = 0;
  171.  
  172. foreach ($data as $db_name => $key) {
  173. if (0 == $i) {
  174. $db_index = $db_name;
  175. $i++;
  176. } else {
  177. $db_index = $db_index . ', ' . $db_name;
  178. }
  179. }
  180.  
  181. return $db_index;
  182. }
  183.  
  184. /*
  185. * return string ':index, :index2, :index3'
  186. */
  187.  
  188. private static function getParamsIndexToBind(array $data)
  189. {
  190. $db_index = '';
  191. $i = 0;
  192.  
  193. foreach ($data as $db_name => $key) {
  194. if (0 == $i) {
  195. $db_index = ':' . $db_name;
  196. $i++;
  197. } else {
  198. $db_index = $db_index . ', :' . $db_name;
  199. }
  200. }
  201.  
  202. return $db_index;
  203. }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement