src-empire/Empire/Legacy/db.php line 133

Open in your IDE?
  1. <?php
  2. namespace Empire\Legacy;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Driver\PDOStatement;
  5. use Doctrine\DBAL\FetchMode;
  6. use Doctrine\DBAL\Driver\Statement;
  7. use Empire\Core\Core;
  8. use Empire\Core\Entity;
  9. /**
  10.  * Class db
  11.  * @package Empire\Legacy
  12.  * @deprecated Use the Doctrine DBAL instead.
  13.  */
  14. Class db {
  15.     
  16.     /**
  17.      * Does a mysqli query
  18.      * run this instead of ::prepare and I might kill you
  19.      *
  20.      * @param string $query
  21.      * @return Statement
  22.      * @throws \Exception
  23.      * @deprecated
  24.      */
  25.     static function query($query) {
  26.         $mysql Core::db();
  27.         $res $mysql->query($query);
  28.         
  29.         if(!$res) {
  30.             throw new \Exception($mysql->errorInfo() . " -" $query);
  31.         }
  32.         
  33.         return $res;
  34.     }
  35.     
  36.     /**
  37.      * Returns the number of affected rows
  38.      * 
  39.      * @param Statement $link_identifier
  40.      * @return int
  41.      */
  42.     static function affectedRows($link_identifier null) {
  43.         if (!is_null($link_identifier)) {
  44.             return $link_identifier->rowCount();
  45.         }
  46.         return -1;
  47.     } 
  48.     
  49.     /**
  50.      * Fetches an object from a result
  51.      * 
  52.      * @param Statement $result
  53.      * @param string $class
  54.      * @return mixed
  55.      */
  56.     static function fetchObject($result$class 'stdClass') {
  57.         if($result instanceof PDOStatement) {
  58.             if (!is_null($class)) {
  59.                 $obj $result->fetchObject($class);
  60.                 if ($obj instanceof Entity) {
  61.                     $obj->initialize();
  62.                 }
  63.             } else {
  64.                 return $result->fetchObject();
  65.             }
  66.             return $obj;
  67.         } else {
  68.             return null;
  69.         }
  70.     }
  71.     
  72.     /**
  73.      * Fetches an array from a result
  74.      *
  75.      * @param Statement $result
  76.      * @return array
  77.      */
  78.     static function fetchArray($result) {
  79.         return $result->fetch(FetchMode::ASSOCIATIVE);
  80.     }
  81.     /**
  82.      * @param Statement $result
  83.      * @param string $class
  84.      * @return array
  85.      */
  86.     static function fetchArrayOfObjects($result$class 'stdClass') {
  87.         $array = array();
  88.         
  89.         if (method_exists($result'rowCount') && $result->rowCount() == 0) {
  90.             return $array;
  91.         }
  92.         
  93.         while ($row self::fetchObject($result$class)) {
  94.             $array[] = $row;
  95.         }
  96.         return $array;
  97.     }
  98.     
  99.     /**
  100.      * Gets the number of rows in a result
  101.      * 
  102.      * @param Statement $result
  103.      * @return int
  104.      */
  105.     static function numRows($result) {
  106.         return $result->rowCount();
  107.     }
  108.     
  109.     /**
  110.      * Gets the most recent mysqli insert ID
  111.      *
  112.      * @return int
  113.      */
  114.     static function insertID() {
  115.         $mysql Core::db();
  116.         return $mysql->lastInsertId();
  117.     }
  118.     
  119.     /**
  120.      * Escapes a string for mysql
  121.      * 
  122.      * @param mixed $strToBeEscaped
  123.      * @return mixed
  124.      */
  125.     static function escape($strToBeEscaped) {
  126.         $mysql Core::db();
  127.         if(!is_numeric($strToBeEscaped)) {
  128.            //return $mysql->real_escape_string($strToBeEscaped);
  129.            return $mysql->quote($strToBeEscaped);
  130.         } else {
  131.            return $strToBeEscaped;
  132.         }
  133.     }
  134.     
  135.     /**
  136.      * Escapes a value with quotes
  137.      * 
  138.      * @param mixed $value
  139.      * @return int|string
  140.      */
  141.     public static function quoteEscape($value) {
  142.         if($value === null){
  143.             return 'null';
  144.         } else if($value === true){
  145.             return 1;
  146.         } else if($value === false){
  147.             return 0;
  148.         } else if ($value === "NOW()") {
  149.             return $value;
  150.         }
  151.         if(is_array($value)){
  152.             $value array_map(array('Empire\Legacy\db''quoteEscape'), $value);
  153.             return '(' implode(', '$value) . ')';
  154.         }
  155.         if(is_numeric($value)){
  156.             return $value;
  157.         }
  158.         return self::escape($value);
  159.     }
  160.     
  161.     /**
  162.      * Used as a callback for prepare
  163.      * 
  164.      * @param string $query
  165.      * @param array $assoc
  166.      * @return array
  167.      */
  168.     private static function prepareAssoc($query, array $assoc = array()){
  169.         $numeric 0;
  170.         $callback = function($match) use($assoc, &$numeric){
  171.             $modifier $match[1];
  172.             $name $match[2];
  173.             if($modifier == '#'){
  174.                 if($name == 'UNIX_NOW'){
  175.                     return time();
  176.                 } else if($name == 'CGT_NOW'){
  177.                     return cgt::getCGTSeconds();
  178.                 }
  179.                 return self::quoteEscape(constant($name));
  180.             }
  181.             if($modifier == '@'){
  182.                 if(is_array($assoc[0])){
  183.                     return self::quoteEscape($assoc[0][$name]);
  184.                 } else if(is_object($assoc[0])){
  185.                     return self::quoteEscape($assoc[0]->$name);
  186.                 }
  187.                 throw new \Exception('Invalid argument provided as assoc');
  188.             }
  189.             if($modifier == '!!'){
  190.                 if(!array_key_exists($numeric$assoc)){
  191.                     throw new \Exception('Looking for array argument: ' $numeric ' but not found');
  192.                 }
  193.                 return $assoc[$numeric++];
  194.             }
  195.             if($modifier == '?'){
  196.                 if(!array_key_exists($numeric$assoc)){
  197.                     throw new \Exception('Looking for array argument: ' $numeric ' but not found.');
  198.                 }
  199.                 return self::quoteEscape($assoc[$numeric++]);
  200.             }
  201.             if($modifier == '&&') {
  202.                 return $assoc[$numeric++];
  203.             }
  204.             throw new \Exception('Invalid query modified provided.');
  205.         };
  206.         return preg_replace_callback('/([@#?]|!!|&&)(\w*)/'$callback$query);
  207.     }
  208.     
  209.     /**
  210.      * Prepares a sql query, uses various args as needed
  211.      * 
  212.      * ? = any arg, gets quote-escaped
  213.      * # = a constant, also accepts UNIX_TIME and CGT_TIME
  214.      * @ = named replacement
  215.      * !! = raw injection
  216.      * && = raw injection, can be empty/ignored
  217.      * 
  218.      * @return Statement self::query
  219.      * @throws \Exception
  220.      */
  221.     public static function prepare(){
  222.         $intArgs func_num_args() - 1;
  223.         $arrArgs func_get_args();
  224.         //some initial checking
  225.         if($intArgs == -1){
  226.             throw new \Exception('prepare called with no arguments');
  227.         }
  228.         //wildcard query string
  229.         $query array_shift($arrArgs);
  230.         //fill in the query
  231.         $query self::prepareAssoc($query$arrArgs);
  232.         //run the generated query
  233.         return self::query($query);
  234.     }
  235.     
  236.     /**
  237.     * Inserts associative $array into $table. Field names are DB field names and values are values.
  238.     *
  239.     * @param string $table table name
  240.     * @param array $array associative array of fields
  241.     * @param array $duplicate associative array of fields for duplicate key handling
  242.     * @return int insert ID
  243.     * @throws \Exception
  244.     */
  245.     public static function insert($table$array$duplicate = array()) {
  246.        if(empty($array)){
  247.            throw new \Exception('Array is empty');
  248.        }
  249.        $sets = array();
  250.        foreach($array as $key=>$value){
  251.             $sets[] = '`' $key '` = ' self::quoteEscape($value);
  252.        }
  253.        $sql 'INSERT INTO `' $table '` SET ' implode(', '$sets);
  254.        
  255.        if(count($duplicate) > 0) {
  256.            $dupes = array();
  257.            foreach($duplicate as $key=>$value){
  258.                $dupes[] = '`' $key '` = ' self::quoteEscape($value);
  259.            }
  260.            $sql .= ' ON DUPLICATE KEY UPDATE ' implode(', '$dupes);
  261.        }
  262.        
  263.        self::query($sql);
  264.        return self::insertID();
  265.    }
  266. }