|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This is the DB object |
| 4 | + * |
| 5 | + * @package php-simple-sessions |
| 6 | + * @author Liam Kelly <https://github.com/likel> |
| 7 | + * @copyright 2017 Liam Kelly |
| 8 | + * @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE> |
| 9 | + * @link https://github.com/likel/php-simple-sessions |
| 10 | + * @version 1.0.0 |
| 11 | + */ |
| 12 | +namespace Likel\Session; |
| 13 | + |
| 14 | +class DB |
| 15 | +{ |
| 16 | + private $dbh; |
| 17 | + private $stmt; |
| 18 | + |
| 19 | + public function __construct($credentials_location = __DIR__ . '/../ini/credentials.ini') |
| 20 | + { |
| 21 | + try { |
| 22 | + $db_credentials = parse_ini_file($credentials_location, true); |
| 23 | + $this->dbh = loadDatabase($db_credentials["likel_db"]); |
| 24 | + } catch (Exception $ex) { |
| 25 | + throw $ex; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private function loadDatabase($credentials) { |
| 30 | + if(!empty($credentials["likel_db"])){ |
| 31 | + try { |
| 32 | + $dsn = 'mysql:host=' . $credentials["likel_db"]['host'] . ';dbname=' . $credentials["likel_db"]['db_name']; |
| 33 | + |
| 34 | + $options = array( |
| 35 | + PDO::ATTR_PERSISTENT => true, |
| 36 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
| 37 | + ); |
| 38 | + |
| 39 | + $pdo_object = new PDO($dsn, $credentials["likel_db"]['username'], $credentials["likel_db"]['password'], $options); |
| 40 | + |
| 41 | + return $pdo_object; |
| 42 | + |
| 43 | + } catch(PDOException $e) { |
| 44 | + throw new Exception('An error occured when attempting to connect to the database.'); |
| 45 | + } |
| 46 | + |
| 47 | + } else { |
| 48 | + throw new Exception('The credential file could not be located or is empty.'); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Prepare the query from a supplied query string. |
| 54 | + */ |
| 55 | + public function query($query) |
| 56 | + { |
| 57 | + $this->stmt = $this->dbh->prepare($query); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Bind properties to the statement. |
| 62 | + * E.G. $DB->bind(':fname', 'Liam'); |
| 63 | + */ |
| 64 | + public function bind($param, $value, $type = null) |
| 65 | + { |
| 66 | + if (is_null($type)) { |
| 67 | + switch (true) { |
| 68 | + case is_int($value): |
| 69 | + $type = PDO::PARAM_INT; |
| 70 | + break; |
| 71 | + case is_bool($value): |
| 72 | + $type = PDO::PARAM_BOOL; |
| 73 | + break; |
| 74 | + case is_null($value): |
| 75 | + $type = PDO::PARAM_NULL; |
| 76 | + break; |
| 77 | + default: |
| 78 | + $type = PDO::PARAM_STR; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $this->stmt->bindValue($param, $value, $type); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Execute the statement. |
| 87 | + * Use result()/results() for insert queries. |
| 88 | + */ |
| 89 | + public function execute() |
| 90 | + { |
| 91 | + return $this->stmt->execute(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Return multiple rows. |
| 96 | + */ |
| 97 | + public function results() |
| 98 | + { |
| 99 | + $this->execute(); |
| 100 | + return $this->stmt->fetchAll(PDO::FETCH_ASSOC); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Return a single row. |
| 105 | + */ |
| 106 | + public function result() |
| 107 | + { |
| 108 | + $this->execute(); |
| 109 | + return $this->stmt->fetch(PDO::FETCH_ASSOC); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Return the row count. |
| 114 | + */ |
| 115 | + public function rowCount() |
| 116 | + { |
| 117 | + return $this->stmt->rowCount(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Return if rows exists. |
| 122 | + */ |
| 123 | + public function rowsExist() |
| 124 | + { |
| 125 | + return $this->rowCount() != 0; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Return the id of the last inserted row. |
| 130 | + */ |
| 131 | + public function lastInsertId() |
| 132 | + { |
| 133 | + return $this->dbh->lastInsertId(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Begin a transaction for multiple statements. |
| 138 | + */ |
| 139 | + public function beginTransaction() |
| 140 | + { |
| 141 | + return $this->dbh->beginTransaction(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Commit the transaction for multiple statements. |
| 146 | + */ |
| 147 | + public function endTransaction() |
| 148 | + { |
| 149 | + return $this->dbh->commit(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Roll back the transaction. |
| 154 | + */ |
| 155 | + public function cancelTransaction() |
| 156 | + { |
| 157 | + return $this->dbh->rollBack(); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Return the table name with prefix |
| 162 | + */ |
| 163 | + public function tb($tablename) |
| 164 | + { |
| 165 | + return $this->tableprefix.$tablename; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Dump the statement's current parameters. |
| 170 | + */ |
| 171 | + public function dumpStatement() |
| 172 | + { |
| 173 | + return $this->stmt->debugDumpParams(); |
| 174 | + } |
| 175 | +} |
0 commit comments