Example #1
0
 /**
  * Prepares the specified statement if the $queries entry is not already a statement instance
  * @param string $name Name of the statement to prepare
  * @return bool
  */
 function prepareStatement($name, $params = array())
 {
     static $patterns = null;
     if (!@$this->queries[$name]) {
         trigger_error("Cannot execute unknown/invalid query {$name}", E_USER_WARNING);
         return false;
     }
     if (!is_object($this->queries[$name])) {
         if (!isset($patterns)) {
             $patterns = array();
             // Replace the real table names with their prefixed equivalent
             foreach ($this->tables as $k => $v) {
                 $patterns[] = '{' . $k . '}';
             }
         }
         $this->queries[$name] = $this->db->prepare(str_replace($patterns, $this->tables, $this->queries[$name]));
     }
     if (!empty($params)) {
         if (isset($this->parameters[$name])) {
             foreach ($params as $k => $v) {
                 $this->queries[$name]->bindValue(is_int($k) ? $k + 1 : $k, $v, $this->parameters[$name][$k]);
             }
         } else {
             foreach ($params as $k => $v) {
                 $this->queries[$name]->bindValue(is_int($k) ? $k + 1 : $k, $v);
             }
         }
     }
     return true;
 }
Example #2
0
 /**
  * Cast a variable according to a PDO_PARAM_* type
  *
  * @param mixed $val The variable whom type must be set
  * @param int $type A PDO_PARAM_* data type
  * @return mixed
  * @access protected
  */
 function castValue($val, $type = PDO_PARAM_INT)
 {
     switch ($type) {
         case PDO_PARAM_BOOL:
             return $val ? 1 : 0;
         case PDO_PARAM_STR:
             return $this->db->quote($val);
         case PDO_PARAM_INT:
         default:
             return intval($val);
     }
 }