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

Namespaces

  • Alo
    • Cache
    • Controller
    • Db
    • Exception
    • Session
    • Statics
    • Test
    • Validators
  • Controller
  • None
  • PHP

Classes

  • AbstractDb
  • MySQL
  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       /**
 52        * The cache object in use
 53        *
 54        * @var AbstractCache
 55        */
 56       protected $cache;
 57 
 58       /**
 59        * The prefix to use for cache keys
 60        *
 61        * @var string
 62        */
 63       protected $cache_prefix;
 64 
 65       /**
 66        * The last cache hash generated
 67        *
 68        * @var string
 69        */
 70       protected $last_hash;
 71 
 72       /**
 73        * Default query options
 74        *
 75        * @var array
 76        */
 77       protected static $default_settings = [
 78          self::V_CACHE     => false,
 79          self::V_TIME      => 300,
 80          self::V_PREFIX    => null,
 81          self::V_FETCH_NUM => false
 82       ];
 83 
 84       /**
 85        * Instantiates the database connection
 86        *
 87        * @author Art <a.molcanovas@gmail.com>
 88        * @param string $cache Which cache interface to use
 89        */
 90       function __construct($cache) {
 91          $this->cache = new $cache;
 92 
 93          if (!Alo::$db) {
 94             Alo::$db = &$this;
 95          }
 96       }
 97 
 98       /**
 99        * Creates a query hash for caching
100        *
101        * @author Art <a.molcanovas@gmail.com>
102        * @param string $sql    QUery string
103        * @param array  $params Query parameters
104        * @param string $prefix Optional prefix
105        * @return string An MD5 hash
106        */
107       protected function hash($sql, $params, $prefix = null) {
108          $this->last_hash = $this->cache_prefix . md5($prefix . $sql . json_encode($params));
109 
110          return $this->last_hash;
111       }
112 
113       /**
114        * Returns the last hash generated
115        *
116        * @author Art <a.molcanovas@gmail.com>
117        * @return string
118        */
119       function getLastHash() {
120          return $this->last_hash;
121       }
122 
123       /**
124        * Prepares a statement
125        *
126        * @author Art <a.molcanovas@gmail.com>
127        * @param string $sql A SQL statement to prepare
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        * @param string $sql      The SQL code
145        * @param array  $params   Bound parameters
146        * @param array  $settings Optional settings
147        * @return int|float
148        */
149       abstract function aggregate($sql, $params = null, array $settings = []);
150 
151       /**
152        * Begins a transaction
153        *
154        * @author Art <a.molcanovas@gmail.com>
155        * @return AbstractDb
156        */
157       abstract function beginTransaction();
158 
159       /**
160        * Rolls back a transaction
161        *
162        * @author Art <a.molcanovas@gmail.com>
163        * @return AbstractDb
164        */
165       abstract function rollBack();
166 
167       /**
168        * Commits a transaction
169        *
170        * @author Art <a.molcanovas@gmail.com>
171        * @return AbstractDb
172        */
173       abstract function commit();
174 
175       /**
176        * Executes a prepared query and returns the results
177        *
178        * @author Art <a.molcanovas@gmail.com>
179        * @param string $sql      The SQL code
180        * @param array  $params   Bound parameters
181        * @param array  $settings Optional settings
182        * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
183        */
184       abstract function prepQuery($sql, $params = null, array $settings = []);
185 
186       /**
187        * Executes a quick unescaped query without preparing it
188        *
189        * @author Art <a.molcanovas@gmail.com>
190        * @param string $sql SQL code
191        * @return array|boolean Result array on SELECT queries, TRUE/FALSE for others
192        */
193       abstract function query($sql);
194    }
AloFramework documentation API documentation generated by ApiGen 2.8.0