AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Traversables
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • AbstractDb
  • MySQL
  • Resultset
  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 ' . get_class($this) .
 95                                  ' was instantiated and got assigned a ' . $cache);
 96                 } elseif (!is_a(Alo::$cache, $cache)) {
 97                     $this->cache = new $cache;
 98                     Log::warning('Alo::$cache wasn\'t an instance of ' . $cache . ' when ' . get_class($this) .
 99                                  ' was instantiates and was overwritten.');
100                 } else {
101                     $this->cache = &\Alo::$cache;
102                 }
103 
104                 if (!Alo::$db) {
105                     Alo::$db = &$this;
106                 }
107 
108                 self::$this = &$this;
109             }
110 
111             /**
112              * Returns the last hash generated
113              *
114              * @author Art <a.molcanovas@gmail.com>
115              * @return string
116              */
117             function getLastHash() {
118                 return $this->lastHash;
119             }
120 
121             /**
122              * Prepares a statement
123              *
124              * @author Art <a.molcanovas@gmail.com>
125              *
126              * @param string $sql A SQL statement to prepare
127              *
128              * @return PDOStatement
129              */
130             abstract function prepare($sql);
131 
132             /**
133              * Checks whether a transaction is active
134              *
135              * @author Art <a.molcanovas@gmail.com>
136              * @return boolean
137              */
138             abstract function transactionActive();
139 
140             /**
141              * Returns an aggregated one-column result set, e.g. from a count(*) query
142              *
143              * @author Art <a.molcanovas@gmail.com>
144              *
145              * @param string $sql      The SQL code
146              * @param array  $params   Bound parameters
147              * @param array  $settings Optional settings
148              *
149              * @return int|float
150              */
151             abstract function aggregate($sql, $params = null, array $settings = []);
152 
153             /**
154              * Begins a transaction
155              *
156              * @author Art <a.molcanovas@gmail.com>
157              * @return AbstractDb
158              */
159             abstract function beginTransaction();
160 
161             /**
162              * Rolls back a transaction
163              *
164              * @author Art <a.molcanovas@gmail.com>
165              * @return AbstractDb
166              */
167             abstract function rollBack();
168 
169             /**
170              * Commits a transaction
171              *
172              * @author Art <a.molcanovas@gmail.com>
173              * @return AbstractDb
174              */
175             abstract function commit();
176 
177             /**
178              * Executes a prepared query and returns the results
179              *
180              * @author Art <a.molcanovas@gmail.com>
181              *
182              * @param string $sql      The SQL code
183              * @param array  $params   Bound parameters
184              * @param array  $settings Optional settings
185              *
186              * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
187              */
188             abstract function prepQuery($sql, $params = null, array $settings = []);
189 
190             /**
191              * Executes a quick unescaped query without preparing it
192              *
193              * @author Art <a.molcanovas@gmail.com>
194              *
195              * @param string $sql SQL code
196              *
197              * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
198              */
199             abstract function query($sql);
200 
201             /**
202              * Creates a query hash for caching
203              *
204              * @author Art <a.molcanovas@gmail.com>
205              *
206              * @param string $sql    QUery string
207              * @param array  $params Query parameters
208              * @param string $prefix Optional prefix
209              *
210              * @return string An MD5 hash
211              */
212             protected function hash($sql, $params, $prefix = null) {
213                 $this->lastHash = $this->cachePrefix . md5($prefix . $sql . json_encode($params));
214 
215                 return $this->lastHash;
216             }
217         }
218     }
219 
AloFramework documentation API documentation generated byApiGen 2.8.0