|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * This file is part of simple-mvc-rest-api for PHP. |
| 6 | + * |
| 7 | + */ |
| 8 | +namespace Database\DB; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class PDO |
| 12 | + */ |
| 13 | +final class PDO { |
| 14 | + private $pdo = null; |
| 15 | + private $statement = null; |
| 16 | + |
| 17 | + public function __construct($hostname, $username, $password, $database, $port) { |
| 18 | + try { |
| 19 | + $this->pdo = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true)); |
| 20 | + } catch(\PDOException $e) { |
| 21 | + trigger_error('Error: Could not make a database link ( ' . $e->getMessage() . '). Error Code : ' . $e->getCode() . ' <br />'); |
| 22 | + exit(); |
| 23 | + } |
| 24 | + |
| 25 | + $this->pdo->exec("SET NAMES 'utf8'"); |
| 26 | + $this->pdo->exec("SET CHARACTER SET utf8"); |
| 27 | + $this->pdo->exec("SET CHARACTER_SET_CONNECTION=utf8"); |
| 28 | + $this->pdo->exec("SET SQL_MODE = ''"); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + public function prepare($sql) { |
| 33 | + $this->statement = $this->pdo->prepare($sql); |
| 34 | + } |
| 35 | + |
| 36 | + public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) { |
| 37 | + if ($length) { |
| 38 | + $this->statement->bindParam($parameter, $variable, $data_type, $length); |
| 39 | + } else { |
| 40 | + $this->statement->bindParam($parameter, $variable, $data_type); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public function execute() { |
| 45 | + try { |
| 46 | + if ($this->statement && $this->statement->execute()) { |
| 47 | + $data = array(); |
| 48 | + |
| 49 | + while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { |
| 50 | + $data[] = $row; |
| 51 | + } |
| 52 | + |
| 53 | + $result = new \stdClass(); |
| 54 | + $result->row = (isset($data[0])) ? $data[0] : array(); |
| 55 | + $result->rows = $data; |
| 56 | + $result->num_rows = $this->statement->rowCount(); |
| 57 | + } |
| 58 | + } catch(\PDOException $e) { |
| 59 | + trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public function query($sql, $params = array()) { |
| 64 | + $this->statement = $this->pdo->prepare($sql); |
| 65 | + $result = false; |
| 66 | + |
| 67 | + try { |
| 68 | + if ($this->statement && $this->statement->execute($params)) { |
| 69 | + $data = array(); |
| 70 | + |
| 71 | + while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { |
| 72 | + $data[] = $row; |
| 73 | + } |
| 74 | + |
| 75 | + $result = new \stdClass(); |
| 76 | + $result->row = (isset($data[0]) ? $data[0] : array()); |
| 77 | + $result->rows = $data; |
| 78 | + $result->num_rows = $this->statement->rowCount(); |
| 79 | + } |
| 80 | + } catch (\PDOException $e) { |
| 81 | + trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql); |
| 82 | + exit(); |
| 83 | + } |
| 84 | + |
| 85 | + if ($result) { |
| 86 | + return $result; |
| 87 | + } else { |
| 88 | + $result = new \stdClass(); |
| 89 | + $result->row = array(); |
| 90 | + $result->rows = array(); |
| 91 | + $result->num_rows = 0; |
| 92 | + return $result; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public function escape($value) { |
| 97 | + $search = array("\\", "0円", "\n", "\r", "\x1a", "'", '"'); |
| 98 | + $replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'); |
| 99 | + return str_replace($search, $replace, $value); |
| 100 | + } |
| 101 | + |
| 102 | + public function getLastId() { |
| 103 | + return $this->pdo->lastInsertId(); |
| 104 | + } |
| 105 | + |
| 106 | + public function __destruct() { |
| 107 | + $this->pdo = null; |
| 108 | + } |
| 109 | +} |
0 commit comments