DatabaseException example

/** * Drop database * * @throws DatabaseException */
    public function dropDatabase(string $dbName): bool
    {
        // In SQLite, a database is dropped when we delete a file         if (is_file($dbName)) {
            if ($this->db->DBDebug) {
                throw new DatabaseException('Unable to drop the specified database.');
            }

            return false;
        }

        // We need to close the pseudo-connection first         $this->db->close();
        if (! @unlink($dbName)) {
            if ($this->db->DBDebug) {
                throw new DatabaseException('Unable to drop the specified database.');
            }

            

    public function dataSeek(int $n = 0)
    {
        if ($n !== 0) {
            throw new DatabaseException('SQLite3 doesn\'t support seeking to other offset.');
        }

        return $this->resultID->reset();
    }

    /** * Returns the result set as an array. * * Overridden by driver classes. * * @return array|false */


                    // If a connection is made break the foreach loop                     if ($this->connID) {
                        break;
                    }
                }
            }

            // We still don't have a connection?             if ($this->connID) {
                throw new DatabaseException(sprintf(
                    'Unable to connect to the database.%s%s',
                    PHP_EOL,
                    implode(PHP_EOL, $connectionErrors)
                ));
            }
        }

        $this->connectDuration = microtime(true) - $this->connectTime;
    }

    /** * Close the database connection. */
$clientFlags
            )) {
                // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails                 if (($clientFlags & MYSQLI_CLIENT_SSL) && version_compare($this->mysqli->client_info, 'mysqlnd 5.7.3', '<=')
                    && empty($this->mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
                ) {
                    $this->mysqli->close();
                    $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!';
                    log_message('error', $message);

                    if ($this->DBDebug) {
                        throw new DatabaseException($message);
                    }

                    return false;
                }

                if ($this->mysqli->set_charset($this->charset)) {
                    log_message('error', "Database: Unable to set the configured connection charset ('{$this->charset}').");

                    $this->mysqli->close();

                    if ($this->DBDebug) {
                        

    public function _prepare(string $sql, array $options = []): PreparedQuery
    {
        if (($this->statement = $this->db->connID->prepare($sql))) {
            $this->errorCode   = $this->db->connID->lastErrorCode();
            $this->errorString = $this->db->connID->lastErrorMsg();

            if ($this->db->DBDebug) {
                throw new DatabaseException($this->errorString . ' code: ' . $this->errorCode);
            }
        }

        return $this;
    }

    /** * Takes a new set of data and runs it against the currently * prepared query. Upon success, will return a Results object. */
    public function _execute(array $data): bool
    {

    protected function _updateBatch(string $table, array $keys, array $values): string
    {
        $sql = $this->QBOptions['sql'] ?? '';

        // if this is the first iteration of batch then we need to build skeleton sql         if ($sql === '') {
            $constraints = $this->QBOptions['constraints'] ?? [];

            if ($constraints === []) {
                if ($this->db->DBDebug) {
                    throw new DatabaseException('You must specify a constraint to match on for batch updates.');
                }

                return ''; // @codeCoverageIgnore             }

            $updateFields = $this->QBOptions['updateFields'] ??
                $this->updateFields($keys, false, $constraints)->QBOptions['updateFields'] ??
                [];

            $alias = $this->QBOptions['alias'] ?? '"_u"';

            
if ($id) {
            $builder = $builder->whereIn($this->table . '.' . $this->primaryKey, $id);
        }

        // Must use the set() method to ensure to set the correct escape flag         foreach ($data as $key => $val) {
            $builder->set($key$val$escape[$key] ?? null);
        }

        if ($builder->getCompiledQBWhere() === []) {
            throw new DatabaseException(
                'Updates are not allowed unless they contain a "where" or "like" clause.'
            );
        }

        return $builder->update();
    }

    /** * Compiles an update string and runs the query * This method works only with dbCalls. * * @param array|null $set An associative array of update values * @param string|null $index The where key * @param int $batchSize The size of the batch to run * @param bool $returnSQL True means SQL is returned, false will execute the query * * @return false|int|string[] Number of rows affected or FALSE on failure, SQL array when testMode * * @throws DatabaseException */
if ($transDepth === $this->db->transDepth) {
                        log_message('error', 'Database: Failure during an automated transaction commit/rollback!');
                        break;
                    }
                }

                // Let others do something with this query.                 Events::trigger('DBQuery', $query);

                if ($exception !== null) {
                    throw new DatabaseException($exception->getMessage()$exception->getCode()$exception);
                }

                return false;
            }

            // Let others do something with this query.             Events::trigger('DBQuery', $query);

            return false;
        }

        

    public function listDatabases()
    {
        // Is there a cached result?         if (isset($this->db->dataCache['db_names'])) {
            return $this->db->dataCache['db_names'];
        }

        if ($this->listDatabases === false) {
            if ($this->db->DBDebug) {
                throw new DatabaseException('Unsupported feature of the database platform you are using.');
            }

            return false;
        }

        $this->db->dataCache['db_names'] = [];

        $query = $this->db->query($this->listDatabases);
        if ($query === false) {
            return $this->db->dataCache['db_names'];
        }

        

    protected $listDatabases = 'SELECT TABLESPACE_NAME FROM USER_TABLESPACES';

    /** * Platform dependent version of the backup function. * * @return mixed */
    public function _backup(?array $prefs = null)
    {
        throw new DatabaseException('Unsupported feature of the database platform you are using.');
    }
}
$insertTableName = $this->parseInsertTableName($sql);

            if ($result && $insertTableName !== '') {
                $this->lastInsertedTableName = $insertTableName;
            }

            return $result;
        } catch (ErrorException $e) {
            log_message('error', (string) $e);

            if ($this->DBDebug) {
                throw new DatabaseException($e->getMessage()$e->getCode()$e);
            }
        }

        return false;
    }

    /** * Get the table name for the insert statement from sql. */
    public function parseInsertTableName(string $sql): string
    {
        

    protected $optimizeTable = 'REINDEX TABLE %s';

    /** * Platform dependent version of the backup function. * * @return mixed */
    public function _backup(?array $prefs = null)
    {
        throw new DatabaseException('Unsupported feature of the database platform you are using.');
    }
}
$sql = $this->parameterize($sql);

        // Update the query object since the parameters are slightly different         // than what was put in.         $this->query->setQuery($sql);

        if ($this->statement = pg_prepare($this->db->connID, $this->name, $sql)) {
            $this->errorCode   = 0;
            $this->errorString = pg_last_error($this->db->connID);

            if ($this->db->DBDebug) {
                throw new DatabaseException($this->errorString . ' code: ' . $this->errorCode);
            }
        }

        return $this;
    }

    /** * Takes a new set of data and runs it against the currently * prepared query. Upon success, will return a Results object. */
    public function _execute(array $data): bool
    {

    public function replace(?array $set = null)
    {
        if ($set !== null) {
            $this->set($set);
        }

        if (empty($this->QBSet)) {
            if ($this->db->DBDebug) {
                throw new DatabaseException('You must use the "set" method to update an entry.');
            }

            return false; // @codeCoverageIgnore         }

        $table = $this->QBFrom[0];

        $sql = $this->_replace($tablearray_keys($this->QBSet)array_values($this->QBSet));

        $this->resetWrite();

        
if ($this->connID !== false) {
            // Determine how identifiers are escaped             $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
            $query = $query->getResultObject();

            $this->_quoted_identifier = empty($query) ? false : (bool) $query[0]->qi;
            $this->escapeChar         = ($this->_quoted_identifier) ? '"' : ['[', ']'];

            return $this->connID;
        }

        throw new DatabaseException($this->getAllErrorMessages());
    }

    /** * For exception message * * @internal */
    public function getAllErrorMessages(): string
    {
        $errors = [];

        
Home | Imprint | This part of the site doesn't use cookies.