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

Namespaces

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

Classes

  • AbstractQuery
  • MySQLQuery
  1 <?php
  2 
  3     namespace Alo\Db\Query;
  4 
  5     use PDO;
  6     use Alo\Db\MySQL;
  7     use Alo\Exception\LibraryException as Libex;
  8     use Alo\Exception\ORMException as ORMEx;
  9     use Alo;
 10 
 11     if (!defined('GEN_START')) {
 12         http_response_code(404);
 13     } else {
 14 
 15         /**
 16          * MySQL ORM
 17          * @author Art <a.molcanovas@gmail.com>
 18          */
 19         class MySQLQuery extends AbstractQuery {
 20 
 21             /**
 22              * Reference to database connection
 23              * @var MySQL|PDO
 24              */
 25             protected $db;
 26 
 27             /**
 28              * Instantiates the MySQL ORM
 29              * @author Art <a.molcanovas@gmail.com>
 30              * @throws Libex When $db isn't supplied & Alo::$db isn't an instance of MySQL or PDO
 31              *
 32              * @param MySQL|PDO $db
 33              *
 34              * @throws Libex
 35              */
 36             function __construct(&$db = null) {
 37                 if ($db && ($db instanceof MySQL || $db instanceof PDO)) {
 38                     $this->db = &$db;
 39                 } elseif (Alo::$db && (Alo::$db instanceof MySQL || Alo::$db instanceof PDO)) {
 40                     $this->db = &Alo::$db;
 41                 } else {
 42                     throw new Libex('Database instance not found', Libex::E_REQUIRED_LIB_NOT_FOUND);
 43                 }
 44             }
 45 
 46             /**
 47              * Returns a string representation of the built query
 48              * @author Art <a.molcanovas@gmail.com>
 49              * @return string
 50              * @throws ORMEx When there are no columns to select or there is no source table
 51              */
 52             function getSQL() {
 53                 $sql = '';
 54                 if (empty($this->select)) {
 55                     throw new ORMEx('No columns to select', ORMEx::E_INVALID_QUERY);
 56                 } elseif (!$this->from) {
 57                     throw new ORMEx('No source table', ORMEx::E_INVALID_QUERY);
 58                 } else {
 59                     $sql .= 'SELECT ';
 60 
 61                     foreach ($this->select as $i) {
 62                         $sql .= $i . ',';
 63                     }
 64 
 65                     $sql = rtrim($sql, ',') . ' FROM ' . $this->from;
 66 
 67                     foreach ($this->joins as $j) {
 68                         $sql .= ' ' . $j['type'] . ' JOIN ' . $j['table'];
 69                         if ($j['on']) {
 70                             $sql .= ' ON ' . $j['on'];
 71                         }
 72                     }
 73 
 74                     if (!empty($this->where)) {
 75                         $sql .= ' WHERE';
 76 
 77                         $insertBracket = null;
 78                         $count         = 0;
 79                         $max           = count($this->where) - 1;
 80 
 81                         foreach ($this->where as $w) {
 82                             if (is_string($w)) {
 83                                 if ($w == ')' && $count == $max) {
 84                                     $sql .= ')';
 85                                 } else {
 86                                     $insertBracket = $w;
 87                                 }
 88                             } else {
 89                                 if ($insertBracket == ')') {
 90                                     $sql .= ')';
 91                                 }
 92 
 93                                 if ($count != 0) {
 94                                     $sql .= ' ' . $w['kind'];
 95                                 }
 96 
 97                                 if ($insertBracket == '(') {
 98                                     $sql .= '(';
 99                                 }
100 
101                                 $sql .= ' ' . $w['col'] . $w['mod'] . $w['val'];
102 
103                                 $insertBracket = null;
104                             }
105 
106                             $count++;
107                         }
108                     }
109                 }
110 
111                 return $sql;
112             }
113         }
114     }
115 
AloFramework documentation API documentation generated byApiGen 2.8.0