|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The database object which helps to abstract database functions |
| 4 | + * |
| 5 | + * Uses and requires PDO, generally available after PHP 5.1 |
| 6 | + * |
| 7 | + * @package php-simple-sessions |
| 8 | + * @author Liam Kelly <https://github.com/likel> |
| 9 | + * @copyright 2017 Liam Kelly |
| 10 | + * @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE> |
| 11 | + * @link https://github.com/likel/php-simple-sessions |
| 12 | + * @version 1.0.0 |
| 13 | + */ |
| 14 | +namespace Likel; |
| 15 | + |
| 16 | +class DB |
| 17 | +{ |
| 18 | + private $database_handler; // Stores the database connection |
| 19 | + private $statement; // The MySQL query with prepared values |
| 20 | + private $table_prefix; // The table prefix from the credentials.ini file |
| 21 | + |
| 22 | + /** |
| 23 | + * Construct the database object |
| 24 | + * |
| 25 | + * @param string $credentials_location The location of the credential file |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function __construct($credentials_location) |
| 29 | + { |
| 30 | + try { |
| 31 | + $db_credentials = parse_ini_file($credentials_location, true); |
| 32 | + $this->database_handler = $this->loadDatabase($db_credentials["likel_db"]); |
| 33 | + $this->table_prefix = $db_credentials["likel_db"]["table_prefix"]; |
| 34 | + } catch (\Exception $ex) { |
| 35 | + throw $ex; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Attempt to retrieve the likel_db ini array and connect to the database |
| 41 | + * |
| 42 | + * @param array $credentials likel_db from the credentials.ini file |
| 43 | + * @return mixed |
| 44 | + * @throws \Exception If credentials empty or not found |
| 45 | + * @throws \PDOException If PDO connection is unsuccessful |
| 46 | + */ |
| 47 | + private function loadDatabase($credentials) |
| 48 | + { |
| 49 | + if(!empty($credentials)){ |
| 50 | + try { |
| 51 | + $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name']; |
| 52 | + |
| 53 | + $options = array( |
| 54 | + \PDO::ATTR_PERSISTENT => true, |
| 55 | + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION |
| 56 | + ); |
| 57 | + |
| 58 | + $pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options); |
| 59 | + |
| 60 | + return $pdo_object; |
| 61 | + |
| 62 | + } catch(\PDOException $e) { |
| 63 | + throw new \Exception($e->getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + } else { |
| 67 | + throw new \Exception('The credential file could not be located or is empty.'); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Prepare the query from a supplied query string |
| 73 | + * |
| 74 | + * @param string $query The prepared query |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function query($query) |
| 78 | + { |
| 79 | + $this->statement = $this->database_handler->prepare($query); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Bind properties to the statement |
| 84 | + * E.G. $DB->bind(':fname', 'Liam'); |
| 85 | + * |
| 86 | + * @param string $param The parameter to replace |
| 87 | + * @param mixed $value The value replacement |
| 88 | + * @param mixed $type Force the PDO::PARAM type |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function bind($param, $value, $type = null) |
| 92 | + { |
| 93 | + if (is_null($type)) { |
| 94 | + switch (true) { |
| 95 | + case is_int($value): |
| 96 | + $type = \PDO::PARAM_INT; |
| 97 | + break; |
| 98 | + case is_bool($value): |
| 99 | + $type = \PDO::PARAM_BOOL; |
| 100 | + break; |
| 101 | + case is_null($value): |
| 102 | + $type = \PDO::PARAM_NULL; |
| 103 | + break; |
| 104 | + default: |
| 105 | + $type = \PDO::PARAM_STR; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + $this->statement->bindValue($param, $value, $type); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Execute the statement |
| 114 | + * Use result()/results() for insert queries |
| 115 | + * |
| 116 | + * @return bool |
| 117 | + */ |
| 118 | + public function execute() |
| 119 | + { |
| 120 | + return $this->statement->execute(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Return multiple rows |
| 125 | + * |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + public function results() |
| 129 | + { |
| 130 | + $this->execute(); |
| 131 | + return $this->statement->fetchAll(\PDO::FETCH_ASSOC); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Return a single row |
| 136 | + * |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + public function result() |
| 140 | + { |
| 141 | + $this->execute(); |
| 142 | + return $this->statement->fetch(\PDO::FETCH_ASSOC); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Return the row count |
| 147 | + * |
| 148 | + * @return int |
| 149 | + */ |
| 150 | + public function rowCount() |
| 151 | + { |
| 152 | + return $this->statement->rowCount(); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Return if rows exists |
| 157 | + * |
| 158 | + * @return bool |
| 159 | + */ |
| 160 | + public function rowsExist() |
| 161 | + { |
| 162 | + return $this->rowCount() != 0; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Return the id of the last inserted row |
| 167 | + * |
| 168 | + * @return mixed |
| 169 | + */ |
| 170 | + public function lastInsertId() |
| 171 | + { |
| 172 | + return $this->database_handler->lastInsertId(); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Begin a transaction for multiple statements |
| 177 | + * |
| 178 | + * @return bool |
| 179 | + */ |
| 180 | + public function beginTransaction() |
| 181 | + { |
| 182 | + return $this->database_handler->beginTransaction(); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Commit the transaction for multiple statements |
| 187 | + * |
| 188 | + * @return bool |
| 189 | + */ |
| 190 | + public function endTransaction() |
| 191 | + { |
| 192 | + return $this->database_handler->commit(); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Roll back the transaction |
| 197 | + * |
| 198 | + * @return bool |
| 199 | + */ |
| 200 | + public function cancelTransaction() |
| 201 | + { |
| 202 | + return $this->database_handler->rollBack(); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Return the table name with prefix |
| 207 | + * |
| 208 | + * @param string $table_name The table name that's accessed |
| 209 | + * @return string |
| 210 | + */ |
| 211 | + public function getTableName($table_name) |
| 212 | + { |
| 213 | + return $this->table_prefix . $table_name; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Dump the statement's current parameters |
| 218 | + * |
| 219 | + * @return void |
| 220 | + */ |
| 221 | + public function dumpStatement() |
| 222 | + { |
| 223 | + $this->statement->debugDumpParams(); |
| 224 | + } |
| 225 | +} |
0 commit comments