| | |
| | | $driver = isset($driver_map[$driver]) ? $driver_map[$driver] : $driver; |
| | | $class = "rcube_db_$driver"; |
| | | |
| | | if (!class_exists($class)) { |
| | | if (!$driver || !class_exists($class)) { |
| | | rcube::raise_error(array('code' => 600, 'type' => 'db', |
| | | 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => "Configuration error. Unsupported database driver: $driver"), |
| | |
| | | $this->db_connected = is_object($this->dbh); |
| | | |
| | | // use write-master when read-only fails |
| | | if (!$this->db_connected && $mode == 'r') { |
| | | if (!$this->db_connected && $mode == 'r' && $this->is_replicated()) { |
| | | $mode = 'w'; |
| | | $this->dbh = $this->dsn_connect($this->db_dsnw_array); |
| | | $this->db_connected = is_object($this->dbh); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Get number of rows for a SQL query |
| | | * If no query handle is specified, the last query will be taken as reference |
| | | * |
| | | * @param mixed $result Optional query handle |
| | | * @return mixed Number of rows or false on failure |
| | | * @deprecated This method shows very poor performance and should be avoided. |
| | | */ |
| | | public function num_rows($result = null) |
| | | { |
| | | if ($result || ($result === null && ($result = $this->last_result))) { |
| | | // repeat query with SELECT COUNT(*) ... |
| | | if (preg_match('/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/ims', $result->queryString, $m)) { |
| | | $query = $this->dbh->query('SELECT COUNT(*) FROM ' . $m[1], PDO::FETCH_NUM); |
| | | return $query ? intval($query->fetchColumn(0)) : false; |
| | | } |
| | | else { |
| | | $num = count($result->fetchAll()); |
| | | $result->execute(); // re-execute query because there's no seek(0) |
| | | return $num; |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * Get last inserted record ID |
| | | * |
| | | * @param string $table Table name (to find the incremented sequence) |
| | |
| | | * Formats input so it can be safely used in a query |
| | | * |
| | | * @param mixed $input Value to quote |
| | | * @param string $type Type of data |
| | | * @param string $type Type of data (integer, bool, ident) |
| | | * |
| | | * @return string Quoted/converted string for use in query |
| | | */ |
| | |
| | | |
| | | if (is_null($input)) { |
| | | return 'NULL'; |
| | | } |
| | | |
| | | if ($type == 'ident') { |
| | | return $this->quote_identifier($input); |
| | | } |
| | | |
| | | // create DB handle if not available |
| | |
| | | * @param string $str Value to quote |
| | | * |
| | | * @return string Quoted string for use in query |
| | | * @deprecated Replaced by rcube_db::quote |
| | | * @see rcube_db::quote |
| | | */ |
| | | public function simpleEscape($str) |
| | | { |
| | | return $this->quote($str); |
| | | } |
| | | |
| | | /** |
| | | * Quotes a string so it can be safely used as a table or column name |
| | | * |
| | | * @param string $str Value to quote |
| | | * |
| | | * @return string Quoted string for use in query |
| | | */ |
| | | public function quote_identifier($str) |
| | | { |
| | |
| | | $name[] = $start . $elem . $end; |
| | | } |
| | | |
| | | return implode($name, '.'); |
| | | return implode($name, '.'); |
| | | } |
| | | |
| | | /** |
| | |
| | | * Return list of elements for use with SQL's IN clause |
| | | * |
| | | * @param array $arr Input array |
| | | * @param string $type Type of data |
| | | * @param string $type Type of data (integer, bool, ident) |
| | | * |
| | | * @return string Comma-separated list of quoted values for use in query |
| | | */ |