1 <?php
2
3 namespace Alo\Db;
4
5 use Alo;
6 use Alo\Cache\AbstractCache;
7 use PDOStatement;
8
9 if(!defined('GEN_START')) {
10 http_response_code(404);
11 die();
12 }
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 * Default query options
52 *
53 * @var array
54 */
55 protected static $default_settings = [
56 self::V_CACHE => false,
57 self::V_TIME => 300,
58 self::V_PREFIX => null,
59 self::V_FETCH_NUM => false
60 ];
61 /**
62 * The cache object in use
63 *
64 * @var AbstractCache
65 */
66 protected $cache;
67 /**
68 * The prefix to use for cache keys
69 *
70 * @var string
71 */
72 protected $cache_prefix;
73 /**
74 * The last cache hash generated
75 *
76 * @var string
77 */
78 protected $last_hash;
79
80 /**
81 * Instantiates the database connection
82 *
83 * @author Art <a.molcanovas@gmail.com>
84 *
85 * @param string $cache Which cache interface to use
86 */
87 function __construct($cache) {
88 $this->cache = new $cache;
89
90 if(!Alo::$db) {
91 Alo::$db = &$this;
92 }
93 }
94
95 /**
96 * Returns the last hash generated
97 *
98 * @author Art <a.molcanovas@gmail.com>
99 * @return string
100 */
101 function getLastHash() {
102 return $this->last_hash;
103 }
104
105 /**
106 * Prepares a statement
107 *
108 * @author Art <a.molcanovas@gmail.com>
109 *
110 * @param string $sql A SQL statement to prepare
111 *
112 * @return PDOStatement
113 */
114 abstract function prepare($sql);
115
116 /**
117 * Checks whether a transaction is active
118 *
119 * @author Art <a.molcanovas@gmail.com>
120 * @return boolean
121 */
122 abstract function transactionActive();
123
124 /**
125 * Returns an aggregated one-column result set, e.g. from a count(*) query
126 *
127 * @author Art <a.molcanovas@gmail.com>
128 *
129 * @param string $sql The SQL code
130 * @param array $params Bound parameters
131 * @param array $settings Optional settings
132 *
133 * @return int|float
134 */
135 abstract function aggregate($sql, $params = null, array $settings = []);
136
137 /**
138 * Begins a transaction
139 *
140 * @author Art <a.molcanovas@gmail.com>
141 * @return AbstractDb
142 */
143 abstract function beginTransaction();
144
145 /**
146 * Rolls back a transaction
147 *
148 * @author Art <a.molcanovas@gmail.com>
149 * @return AbstractDb
150 */
151 abstract function rollBack();
152
153 /**
154 * Commits a transaction
155 *
156 * @author Art <a.molcanovas@gmail.com>
157 * @return AbstractDb
158 */
159 abstract function commit();
160
161 /**
162 * Executes a prepared query and returns the results
163 *
164 * @author Art <a.molcanovas@gmail.com>
165 *
166 * @param string $sql The SQL code
167 * @param array $params Bound parameters
168 * @param array $settings Optional settings
169 *
170 * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
171 */
172 abstract function prepQuery($sql, $params = null, array $settings = []);
173
174 /**
175 * Executes a quick unescaped query without preparing it
176 *
177 * @author Art <a.molcanovas@gmail.com>
178 *
179 * @param string $sql SQL code
180 *
181 * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
182 */
183 abstract function query($sql);
184
185 /**
186 * Creates a query hash for caching
187 *
188 * @author Art <a.molcanovas@gmail.com>
189 *
190 * @param string $sql QUery string
191 * @param array $params Query parameters
192 * @param string $prefix Optional prefix
193 *
194 * @return string An MD5 hash
195 */
196 protected function hash($sql, $params, $prefix = null) {
197 $this->last_hash = $this->cache_prefix . md5($prefix . $sql . json_encode($params));
198
199 return $this->last_hash;
200 }
201 }