| | |
| | | |
| | | |
| | | /** |
| | | * Database independent query interface |
| | | * Database independent query interface. |
| | | * This is a wrapper for the PHP PDO. |
| | | * |
| | | * This is a wrapper for the PHP PDO |
| | | * |
| | | * @package Database |
| | | * @version 1.0 |
| | | * @package Framework |
| | | * @sbpackage Database |
| | | */ |
| | | class rcube_db |
| | | { |
| | | public $db_provider; |
| | | |
| | | protected $db_dsnw; // DSN for write operations |
| | | protected $db_dsnr; // DSN for read operations |
| | | protected $db_connected = false; // Already connected ? |
| | |
| | | /** |
| | | * Getter for error state |
| | | * |
| | | * @return boolean True on error |
| | | * @param int $res_id Optional query result identifier |
| | | * |
| | | * @return string Error message |
| | | */ |
| | | public function is_error() |
| | | public function is_error($res_id = null) |
| | | { |
| | | return $this->db_error ? $this->db_error_msg : false; |
| | | if ($res_id !== null) { |
| | | return $this->_get_result($res_id) === false ? $this->db_error_msg : null; |
| | | } |
| | | |
| | | return $this->db_error ? $this->db_error_msg : null; |
| | | } |
| | | |
| | | /** |
| | |
| | | $idx = 0; |
| | | |
| | | while ($pos = strpos($query, '?', $pos)) { |
| | | $val = $this->quote($params[$idx++]); |
| | | unset($params[$idx-1]); |
| | | $query = substr_replace($query, $val, $pos, 1); |
| | | $pos += strlen($val); |
| | | if ($query[$pos+1] == '?') { // skip escaped ? |
| | | $pos += 2; |
| | | } |
| | | else { |
| | | $val = $this->quote($params[$idx++]); |
| | | unset($params[$idx-1]); |
| | | $query = substr_replace($query, $val, $pos, 1); |
| | | $pos += strlen($val); |
| | | } |
| | | } |
| | | |
| | | $query = rtrim($query, ';'); |
| | | // replace escaped ? back to normal |
| | | $query = rtrim(strtr($query, array('??' => '?')), ';'); |
| | | |
| | | $this->debug($query); |
| | | |
| | |
| | | return intval($input); |
| | | } |
| | | |
| | | if (is_null($input)) { |
| | | return 'NULL'; |
| | | } |
| | | |
| | | // create DB handle if not available |
| | | if (!$this->dbh) { |
| | | $this->db_connect('r'); |
| | |
| | | 'integer' => PDO::PARAM_INT, |
| | | ); |
| | | $type = isset($map[$type]) ? $map[$type] : PDO::PARAM_STR; |
| | | return $this->dbh->quote($input, $type); |
| | | return strtr($this->dbh->quote($input, $type), array('?' => '??')); // escape ? |
| | | } |
| | | |
| | | return 'NULL'; |