1 | <?php |
||
11 | class Database |
||
12 | { |
||
13 | private $dsn; |
||
14 | private $database_handler; |
||
15 | private $statement; |
||
16 | |||
17 | /** |
||
18 | * Sets the DSN for the database and creates a connection |
||
19 | */ |
||
20 | public function __construct() |
||
21 | { |
||
22 | $this->setDSN(); |
||
23 | |||
24 | //set options |
||
25 | $options = [ |
||
26 | PDO::ATTR_PERSISTENT => true, |
||
27 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
||
28 | ]; |
||
29 | |||
30 | //create a new pdo instance |
||
31 | $this->database_handler = new PDO( |
||
32 | $this->dsn, |
||
33 | getenv("USERNAME"), |
||
34 | getenv("PASSWORD"), |
||
35 | $options |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Sets the DSN as set in the environment variable file |
||
41 | */ |
||
42 | private function setDSN() |
||
43 | { |
||
44 | $env = new Dotenv(__DIR__ . "/../"); |
||
45 | $env->load(); |
||
46 | |||
47 | $this->dsn = getenv("DATABASE_TYPE") |
||
48 | . ":host=" . getenv("HOST") |
||
49 | . ";dbname=" . getenv("DATABASE_NAME") |
||
50 | . ";port=" . getenv("PORT"); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Prepares the statement |
||
55 | * |
||
56 | * @param string $query The SQL query to be prepared |
||
57 | */ |
||
58 | public function prepare($query) |
||
62 | |||
63 | /** |
||
64 | * Binds the values of the parameters in the statement * |
||
65 | * @param mixed $param The parameter that the value will be bound to |
||
66 | * @param mixed $value The value to be bound to the parameter |
||
67 | * @param $type The PDO SQL data type of the value being bound to the parameter |
||
68 | */ |
||
69 | public function bind($param, $value, $type = null) |
||
85 | |||
86 | /** |
||
87 | * Executes the SQL statement |
||
88 | */ |
||
89 | public function execute() |
||
93 | |||
94 | /** |
||
95 | * Prepares a select query to be executed by the execute() method |
||
96 | * @param string $table The name of the table to query |
||
97 | * @param $where The condition to be used when querying |
||
98 | */ |
||
99 | public function select($table, $where = "") |
||
100 | { |
||
101 | $query = "SELECT * FROM $table" |
||
102 | . ($where ? " WHERE $where " : ""); |
||
103 | |||
104 | $this->prepare($query); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Inserts a new record in the database and returns the last inserted ID |
||
109 | * @param string $table The name of the table to insert the record into |
||
110 | * @param array $data The information to insert |
||
111 | * @return int |
||
112 | */ |
||
113 | public function insert($table, $data) |
||
130 | |||
131 | /** |
||
132 | * Updates a record in the database |
||
133 | * @param string $table The table to update |
||
134 | * @param array $data The updated information in the form of an array |
||
135 | * @param $where The condition that needs to be true in order to update |
||
136 | */ |
||
137 | public function update($table, array $data, $where = "") |
||
157 | |||
158 | /** |
||
159 | * Deletes a record or records from the database |
||
160 | * @param string $table The table to delete the record(s) from |
||
161 | * @param $where The condition that needs to be true in order to delete |
||
162 | * @param int $limit |
||
163 | */ |
||
164 | public function delete($table, $where = "", $limit = 1) |
||
175 | |||
176 | /** |
||
177 | * Retrieves all records from the corresponding table in the |
||
178 | * database and returns them as an array of objects |
||
179 | * @return array |
||
180 | */ |
||
181 | public function objectSet($entityClass) |
||
187 | |||
188 | /** |
||
189 | * Retrieves a record from the database based on its ID |
||
190 | * and returns it in the form of the corresponding object |
||
191 | * @return object |
||
192 | */ |
||
193 | public function singleObject($entityClass) |
||
199 | } |
||
200 |