1 <?php
2
3 namespace Alo\Db;
4
5 use Alo;
6 use Alo\Cache\AbstractCache;
7 use Log;
8 use PDOStatement;
9
10 if (!defined('GEN_START')) {
11 http_response_code(404);
12 } else {
13
14 /**
15 * The framework database interface
16 *
17 * @author Art <a.molcanovas@gmail.com>
18 * @package Database
19 */
20 abstract class AbstractDb {
21
22 /**
23 * Defines a parameter as "whether to cache"
24 *
25 * @var string
26 */
27 const V_CACHE = 'c';
28
29 /**
30 * Defines a parameter as "cache time" in seconds
31 *
32 * @var string
33 */
34 const V_TIME = 't';
35
36 /**
37 * Defines a parameter as "cache hash prefix"
38 *
39 * @var string
40 */
41 const V_PREFIX = 'p';
42
43 /**
44 * Defines a variable as "whether to fetch as a numeric array instead of
45 * assoc"
46 *
47 * @var string
48 */
49 const V_FETCH_NUM = 'n';
50 /**
51 * Static reference to the last instance of the class
52 *
53 * @var AbstractDb
54 */
55 static $this;
56 /**
57 * Default query options
58 *
59 * @var array
60 */
61 protected static $defaultSettings = [self::V_CACHE => false,
62 self::V_TIME => 300,
63 self::V_PREFIX => null,
64 self::V_FETCH_NUM => false];
65 /**
66 * The cache object in use
67 *
68 * @var AbstractCache
69 */
70 protected $cache;
71 /**
72 * The prefix to use for cache keys
73 *
74 * @var string
75 */
76 protected $cachePrefix;
77 /**
78 * The last cache hash generated
79 *
80 * @var string
81 */
82 protected $lastHash;
83
84 /**
85 * Instantiates the database connection
86 *
87 * @author Art <a.molcanovas@gmail.com>
88 *
89 * @param string $cache Which cache interface to use
90 */
91 function __construct($cache) {
92 if (!\Alo::$cache) {
93 $this->cache = new $cache;
94 Log::warning('Alo::$cache was not defined when ' .
95 get_class($this) .
96 ' was instantiated and got assigned a ' .
97 $cache);
98 } elseif (!is_a(Alo::$cache, $cache)) {
99 $this->cache = new $cache;
100 Log::warning('Alo::$cache wasn\'t an instance of ' .
101 $cache .
102 ' when ' .
103 get_class($this) .
104 ' was instantiates and was overwritten.');
105 } else {
106 $this->cache = &\Alo::$cache;
107 }
108
109 if (!Alo::$db) {
110 Alo::$db = &$this;
111 }
112
113 self::$this = &$this;
114 }
115
116 /**
117 * Returns the last inserted auto-increment ID
118 * @author Art <a.molcanovas@gmail.com>
119 *
120 * @return int
121 */
122 abstract function lastInsertID();
123
124 /**
125 * Returns the last hash generated
126 *
127 * @author Art <a.molcanovas@gmail.com>
128 * @return string
129 */
130 function getLastHash() {
131 return $this->lastHash;
132 }
133
134 /**
135 * Prepares a statement
136 *
137 * @author Art <a.molcanovas@gmail.com>
138 *
139 * @param string $sql A SQL statement to prepare
140 *
141 * @return PDOStatement
142 */
143 abstract function prepare($sql);
144
145 /**
146 * Checks whether a transaction is active
147 *
148 * @author Art <a.molcanovas@gmail.com>
149 * @return boolean
150 */
151 abstract function transactionActive();
152
153 /**
154 * Returns an aggregated one-column result set, e.g. from a count(*) query
155 *
156 * @author Art <a.molcanovas@gmail.com>
157 *
158 * @param string $sql The SQL code
159 * @param array $params Bound parameters
160 * @param array $settings Optional settings
161 *
162 * @return int|float
163 */
164 abstract function aggregate($sql, $params = null, array $settings = []);
165
166 /**
167 * Begins a transaction
168 *
169 * @author Art <a.molcanovas@gmail.com>
170 * @return AbstractDb
171 */
172 abstract function beginTransaction();
173
174 /**
175 * Rolls back a transaction
176 *
177 * @author Art <a.molcanovas@gmail.com>
178 * @return AbstractDb
179 */
180 abstract function rollBack();
181
182 /**
183 * Commits a transaction
184 *
185 * @author Art <a.molcanovas@gmail.com>
186 * @return AbstractDb
187 */
188 abstract function commit();
189
190 /**
191 * Executes a prepared query and returns the results
192 *
193 * @author Art <a.molcanovas@gmail.com>
194 *
195 * @param string $sql The SQL code
196 * @param array $params Bound parameters
197 * @param array $settings Optional settings
198 *
199 * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
200 */
201 abstract function prepQuery($sql, $params = null, array $settings = []);
202
203 /**
204 * Executes a quick unescaped query without preparing it
205 *
206 * @author Art <a.molcanovas@gmail.com>
207 *
208 * @param string $sql SQL code
209 *
210 * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
211 */
212 abstract function query($sql);
213
214 /**
215 * Creates a query hash for caching
216 *
217 * @author Art <a.molcanovas@gmail.com>
218 *
219 * @param string $sql QUery string
220 * @param array $params Query parameters
221 * @param string $prefix Optional prefix
222 *
223 * @return string An MD5 hash
224 */
225 protected function hash($sql, $params, $prefix = null) {
226 $this->lastHash = $this->cachePrefix . md5($prefix . $sql . json_encode($params));
227
228 return $this->lastHash;
229 }
230 }
231 }
232