| | |
| | | } |
| | | |
| | | /** |
| | | * 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) |
| | |
| | | } |
| | | |
| | | /** |
| | | * Escapes a string so it can be safely used in a query |
| | | * |
| | | * @param string $str A string to escape |
| | | * |
| | | * @return string Escaped string for use in a query |
| | | */ |
| | | public function escape($str) |
| | | { |
| | | if (is_null($str)) { |
| | | return 'NULL'; |
| | | } |
| | | |
| | | return substr($this->quote($str), 1, -1); |
| | | } |
| | | |
| | | /** |
| | | * Quotes a string so it can be safely used as a table or column name |
| | | * |
| | | * @param string $str Value to quote |
| | |
| | | public function quoteIdentifier($str) |
| | | { |
| | | return $this->quote_identifier($str); |
| | | } |
| | | |
| | | /** |
| | | * Escapes a string so it can be safely used in a query |
| | | * |
| | | * @param string $str A string to escape |
| | | * |
| | | * @return string Escaped string for use in a query |
| | | * @deprecated Replaced by rcube_db::escape |
| | | * @see rcube_db::escape |
| | | */ |
| | | public function escapeSimple($str) |
| | | { |
| | | return $this->escape($str); |
| | | } |
| | | |
| | | /** |
| | |
| | | { |
| | | $rcube = rcube::get_instance(); |
| | | |
| | | // return table name if configured |
| | | $config_key = 'db_table_'.$table; |
| | | |
| | | if ($name = $rcube->config->get($config_key)) { |
| | | return $name; |
| | | // add prefix to the table name if configured |
| | | if ($prefix = $rcube->config->get('db_prefix')) { |
| | | return $prefix . $table; |
| | | } |
| | | |
| | | return $table; |