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 Exception;
  6    use PDO;
  7 
  8    if (!defined('GEN_START')) {
  9       http_response_code(404);
 10       die();
 11    }
 12 
 13    \Alo::loadConfig('db' . DIRECTORY_SEPARATOR . 'mysql');
 14 
 15    /**
 16     * MySQL database manager
 17     *
 18     * @author Art <a.molcanovas@gmail.com>
 19     * @author Art <a.molcanovas@gmail.com>
 20     */
 21    class MySQL extends AbstractDb {
 22 
 23       /**
 24        * The PDO instance
 25        *
 26        * @var PDO
 27        */
 28       protected $pdo;
 29 
 30       /**
 31        * Instantiates the database connection
 32        *
 33        * @author Art <a.molcanovas@gmail.com>
 34        * @param string $ip      The IP address to use
 35        * @param int    $port    The port to use
 36        * @param string $user    The username
 37        * @param string $pw      The password
 38        * @param string $db      The database to use
 39        * @param string $cache   Which cache interface to use
 40        * @param array  $options Connection options
 41        */
 42       function __construct($ip = ALO_MYSQL_SERVER, $port = ALO_MYSQL_PORT, $user = ALO_MYSQL_USER, $pw = ALO_MYSQL_PW, $db = ALO_MYSQL_DATABASE, $cache = ALO_MYSQL_CACHE, array $options = null) {
 43          $this->pdo = new PDO('mysql:dbname=' . $db . ';host=' . $ip . ';port=' . $port, $user, $pw, $options);
 44 
 45          $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 46          $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 47 
 48          $this->cache_prefix = ALO_MYSQL_CACHE_PREFIX;
 49          parent::__construct($cache);
 50          \Log::debug('Initialised MySQL database connection');
 51       }
 52 
 53       function aggregate($sql, $params = null, array $settings = []) {
 54          $settings = \array_merge(self::$default_settings, $settings);
 55          $hash = $this->hash($sql, $params, $settings[self::V_PREFIX]);
 56          $cache = $settings[self::V_CACHE];
 57 
 58          if ($settings[self::V_CACHE] && $get = $this->cache->get($hash)) {
 59             return $get;
 60          } else {
 61             $settings[self::V_FETCH_NUM] = true;
 62             $settings[self::V_CACHE] = false;
 63             $prep = $this->prepQuery($sql, $params, $settings);
 64             $result = null;
 65 
 66             if ($prep) {
 67                $result = strpos($prep[0][0], '.') === false ? (int)$prep[0][0] : (float)$prep[0][0];
 68             }
 69 
 70             if ($cache) {
 71                $this->cache->set($hash, $result);
 72             }
 73 
 74             return $result;
 75          }
 76       }
 77 
 78       function beginTransaction() {
 79          return $this->pdo->inTransaction() ? true : $this->pdo->beginTransaction();
 80       }
 81 
 82       function commit() {
 83          return $this->pdo->inTransaction() ? $this->pdo->commit() : true;
 84       }
 85 
 86       function prepQuery($sql, $params = null, array $settings = []) {
 87          $settings = \array_merge(self::$default_settings, $settings);
 88          $hash = $this->hash($sql, $params, $settings[self::V_PREFIX]);
 89 
 90          if (stripos($sql, 'insert into') !== false || stripos($sql, 'replace into') !== false) {
 91             $settings[self::V_CACHE] = false;
 92          }
 93 
 94          if ($settings[self::V_CACHE] && $get = $this->cache->get($hash)) {
 95             return $get;
 96          } else {
 97             $pdo = $this->pdo->prepare($sql);
 98             $exec = $pdo->execute($params);
 99             $res = stripos($sql, 'select') !== false ? $pdo->fetchAll($settings[self::V_FETCH_NUM] ? PDO::FETCH_NUM : PDO::FETCH_ASSOC) : $exec;
100 
101             if ($settings[self::V_CACHE]) {
102                $this->cache->set($hash, $res, $settings[self::V_TIME]);
103             }
104 
105             return $res;
106          }
107       }
108 
109       function query($sql) {
110          $s = $this->pdo->query($sql);
111 
112          return stripos($sql, 'select') !== false ? $s->fetchAll(PDO::FETCH_ASSOC) : $s !== false;
113 
114       }
115 
116       function rollBack() {
117          return $this->pdo->inTransaction() ? $this->pdo->rollBack() : true;
118       }
119 
120       function prepare($sql) {
121          return $this->pdo->prepare($sql);
122       }
123 
124       function transactionActive() {
125          return $this->pdo->inTransaction();
126       }
127 
128       /**
129        * Handles direct calls to PDO
130        *
131        * @author Art <a.molcanovas@gmail.com>
132        * @param string $name      Method name
133        * @param array  $arguments Array of parameters
134        * @return mixed
135        */
136       function __call($name, $arguments) {
137          return call_user_func_array([$this->pdo, $name], $arguments);
138       }
139 
140    }
AloFramework documentation API documentation generated by ApiGen 2.8.0