{@inheritdoc}

@throws DBALException
333778
public function createDatabasePlatformForVersion($version)
    {
        $mariadb = stripos($version, 'mariadb') !== false;
        if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
            return new MariaDb1027Platform();
        }

        if (! $mariadb) {
            $oracleMysqlVersion = $this->getOracleMysqlVersionNumber($version);
            if (version_compare($oracleMysqlVersion, '8', '>=')) {
                return new MySQL80Platform();
            }
            if (version_compare($oracleMysqlVersion, '5.7.9', '>=')) {
                return new MySQL57Platform();
            }
        }

        return $this->getDatabasePlatform();
    }
749804
public function getDoctrineTypeMapping($dbType)
    {
        if ($this->doctrineTypeMapping === null) {
            $this->initializeAllDoctrineTypeMappings();
        }

        $dbType = strtolower($dbType);

        if (! isset($this->doctrineTypeMapping[$dbType])) {
            throw new DBALException('Unknown database type ' . $dbType . ' requested, ' . static::class . ' may not support it.');
        }

        return $this->doctrineTypeMapping[$dbType];
    }
498860
public static function query($sql) {
        self::init();
        try {
            $db = self::connect();
            if(is_array($db)) {
                throw new DbException('Connection error');
            }
            if(!$query = $db->query($sql)) {
                throw new DbException('Failed to execute query!');
            }
        } catch(mysqli_sql_exception $e) {
            self::$_ops->formatError([], $e->getMessage(), $sql);

        } catch(DbException $e) {
            self::$_ops->formatError([], $e->getMessage(), $sql);
        }
        if(isset($query) && isset($db)) {
            return ['result' => $query, 'link' => $db];
        }
    }
1281420
protected function prepareMysqlException(\Exception $databaseException, string $query, array $criteria = []): MysqlException {
		$code = $databaseException->getCode();
		
		$mysqlException = new MysqlException(
			$databaseException->getMessage(),
			is_numeric($code) ? $code : 0,
			$databaseException
		);
		
		$mysqlException->setQuery($this->getStatement($query, $criteria));
		return $mysqlException;
	}
244607
public function getRecord($tableName, $field, $value, $fields = array())
    {
        try {
            return $this->db->getRecord($tableName, $field, $value, $fields);
        } catch (\Exception $e) {
            throw new DbException($e->getMessage());
        }
    }
1128657
public function ExecuteMultiQuery($query)
    {
        $result = $this->db->multi_query($query);
        if ($result)
        {
            while ($this->db->next_result()) {;} //overgo the results!
        }
        if (!$result)
        {
            throw new DBExceptions\DatabaseException('Error in multi query', 101, $query);
        }
    }
452232
public function commit()
    {
        try {
            if (empty($this->_fields)) {
                $field = 'COUNT(1)';
            } else {
                $field = 'COUNT(';
                if ($this->_fields[static::FIELD_DISTINCT]) {
                    $field .= 'DISTINCT(';
                }
                $field .= '`' . $this->_fields[static::FIELD_NAME] . '`';
                if ($this->_fields[static::FIELD_DISTINCT]) {
                    $field .= ')';
                }
                $field = ')';
            }

            $query = "
                SELECT
                    $field AS nb
                FROM
                    `" . $this->getDbPrefix() . $this->_dbContainer . "`";

            if ($this->_conditions->count()) {
                $query .= "
                WHERE
                    " . $this->_conditions->getPreparedConditions($this->_dbContainer) . "";
            }

            $prepared = Agl::app()->getDb()->getConnection()->prepare($query);

            if ($prepared->execute($this->_conditions->getPreparedValues())) {
                $result = $prepared->fetchObject();
                if (! $result) {
                    throw new Exception("The count query failed (table '" . $this->getDbPrefix() . $this->_dbContainer . "')");
                }
            } else {
                $error = $prepared->errorInfo();
                throw new Exception("The count query failed (table '" . $this->getDbPrefix() . $this->_dbContainer . "') with message '" . $error[2] . "'");
            }

            if (Agl::app()->isDebugMode()) {
                Agl::app()->getDb()->incrementCounter();
            }

            return (int)$result->{'nb'};
        } catch (Exception $e) {
            throw new Exception($e);
        }
    }
906250
public function commit() {
        try {
            $this->getTransaction()->commit();
        } catch(DbException $e) {
            if($e->getMessage() != 'There is no active transaction') {
                throw $e;
            }
        }

        $transactions = $this->getTransactions();
        unset($transactions[$this->getDbService()]);
        $this->setTransactions($transactions);

        $this->findEventsManager()->fire(self::EVENT_COMMIT, $this);
    }
521412
public static function update(array $data, array $where): int
    {
        $self = static::getInstance();

        $data = static::filterColumns($data);

        if (!\count($data)) {
            throw new DbException(
                "Invalid field names of table `{$self->name}`. Please check use of `update()` method"
            );
        }

        $where = static::filterColumns($where);

        if (!\count($where)) {
            throw new DbException(
                "Method `Table::update()` can't update all records in the table `{$self->name}`,\n" .
                "please use `Db::query()` instead (of cause if you know what are you doing)"
            );
        }

        $table = DbProxy::quoteIdentifier($self->name);

        $sql = "UPDATE $table SET " . implode(',', self::prepareStatement($data))
            . " WHERE " . implode(' AND ', self::prepareStatement($where));

        return DbProxy::query($sql, array_merge(array_values($data), array_values($where)));
    }
955313
public function execute()
	{
		$this->connect();

		if (!is_object($this->connection))
		{
			$this->log(
				Log\LogLevel::ERROR,
				'Database query failed (error #{code}): {message}',
				array('code' => $this->errorNum, 'message' => $this->errorMsg)
			);
			throw new \RuntimeException($this->errorMsg, $this->errorNum);
		}

		// Take a local copy so that we don't modify the original query and cause issues later
		$sql = $this->replacePrefix((string) $this->sql);

		if ($this->limit > 0 || $this->offset > 0)
		{
			$sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
		}

		// Increment the query counter.
		$this->count++;

		// If debugging is enabled then let's log the query.
		if ($this->debug)
		{
			// Add the query to the object queue.
			$this->log(
				Log\LogLevel::DEBUG,
				'{sql}',
				array('sql' => $sql, 'category' => 'databasequery', 'trace' => debug_backtrace())
			);
		}

		// Reset the error values.
		$this->errorNum = 0;
		$this->errorMsg = '';

		// Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost.
		$this->cursor = @mysqli_query($this->connection, $sql);

		// If an error occurred handle it.
		if (!$this->cursor)
		{
			$this->errorNum = (int) mysqli_errno($this->connection);
			$this->errorMsg = (string) mysqli_error($this->connection) . "\n-- SQL --\n" . $sql;

			// Check if the server was disconnected.
			if (!$this->connected())
			{
				try
				{
					// Attempt to reconnect.
					$this->connection = null;
					$this->connect();
				}
				catch (\RuntimeException $e)
				// If connect fails, ignore that exception and throw the normal exception.
				{
					$this->log(
						Log\LogLevel::ERROR,
						'Database query failed (error #{code}): {message}',
						array('code' => $this->errorNum, 'message' => $this->errorMsg)
					);
					throw new \RuntimeException($this->errorMsg, $this->errorNum);
				}

				// Since we were able to reconnect, run the query again.
				return $this->execute();
			}
			else
			// The server was not disconnected.
			{
				$this->log(
					Log\LogLevel::ERROR,
					'Database query failed (error #{code}): {message}',
					array('code' => $this->errorNum, 'message' => $this->errorMsg)
				);
				throw new \RuntimeException($this->errorMsg, $this->errorNum);
			}
		}

		return $this->cursor;
	}
918827
public function execute()
	{
		$this->connect();

		if (!is_resource($this->connection))
		{
			$this->log(
				Log\LogLevel::ERROR,
				'Database query failed (error #{code}): {message}',
				array('code' => $this->errorNum, 'message' => $this->errorMsg)
			);
			throw new \RuntimeException($this->errorMsg, $this->errorNum);
		}

		// Take a local copy so that we don't modify the original query and cause issues later
		$sql = $this->replacePrefix((string) $this->sql);

		if ($this->limit > 0 || $this->offset > 0)
		{
			$sql .= ' LIMIT ' . $this->limit . ' OFFSET ' . $this->offset;
		}

		// Increment the query counter.
		$this->count++;

		// If debugging is enabled then let's log the query.
		if ($this->debug)
		{
			// Add the query to the object queue.
			$this->log(
				Log\LogLevel::DEBUG,
				'{sql}',
				array('sql' => $sql, 'category' => 'databasequery', 'trace' => debug_backtrace())
			);
		}

		// Reset the error values.
		$this->errorNum = 0;
		$this->errorMsg = '';

		// Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost.
		$this->cursor = @pg_query($this->connection, $sql);

		// If an error occurred handle it.
		if (!$this->cursor)
		{
			// Check if the server was disconnected.
			if (!$this->connected())
			{
				try
				{
					// Attempt to reconnect.
					$this->connection = null;
					$this->connect();
				}
				catch (\RuntimeException $e)
				// If connect fails, ignore that exception and throw the normal exception.
				{
					// Get the error number and message.
					$this->errorNum = (int) pg_result_error_field($this->cursor, PGSQL_DIAG_SQLSTATE) . ' ';
					$this->errorMsg = pg_last_error($this->connection) . "\nSQL=$sql";

					// Throw the normal query exception.
					$this->log(
						Log\LogLevel::ERROR,
						'Database query failed (error #{code}): {message}',
						array('code' => $this->errorNum, 'message' => $this->errorMsg)
					);
					throw new \RuntimeException($this->errorMsg);
				}

				// Since we were able to reconnect, run the query again.
				return $this->execute();
			}
			else
			// The server was not disconnected.
			{
				// Get the error number and message.
				$this->errorNum = (int) pg_result_error_field($this->cursor, PGSQL_DIAG_SQLSTATE) . ' ';
				$this->errorMsg = pg_last_error($this->connection) . "\nSQL=$sql";

				// Throw the normal query exception.
				$this->log(
					Log\LogLevel::ERROR,
					'Database query failed (error #{code}): {message}',
					array('code' => $this->errorNum, 'message' => $this->errorMsg)
				);
				throw new \RuntimeException($this->errorMsg);
			}
		}

		return $this->cursor;
	}
946212
public function registerDoctrineTypeMapping($dbType, $doctrineType)
    {
        if ($this->doctrineTypeMapping === null) {
            $this->initializeAllDoctrineTypeMappings();
        }

        if (! Types\Type::hasType($doctrineType)) {
            throw DBALException::typeNotFound($doctrineType);
        }

        $dbType                             = strtolower($dbType);
        $this->doctrineTypeMapping[$dbType] = $doctrineType;

        $doctrineType = Type::getType($doctrineType);

        if (! $doctrineType->requiresSQLCommentHint($this)) {
            return;
        }

        $this->markDoctrineTypeCommented($doctrineType);
    }
750581
protected function _refresh()
    {
        $this->_statement->execute($this->_params);
        if ($this->_statement->errorCode() !== '00000') {
            throw new DbException($this->_statement->errorInfo(), $this->_statement->queryString);
        }
        $this->_currentRowIndex = 0;
    }
869991
public function prepare($statement)
    {
        try {
            $stmt = new Statement($statement, $this);
        } catch (Throwable $ex) {
            throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
        }

        $stmt->setFetchMode($this->defaultFetchMode);

        return $stmt;
    }
1253097
public function free(): bool
    {
        try {
            return $this->pdoStatement->closeCursor();
        } catch (PDOException $pdoe) {
            throw new DatabaseException($pdoe->getMessage(), $pdoe);
        }
    }
213042
public function query(string $sql)
    {
        $this->lastSql = $sql;
        $this->lastStmt = $this->instance->query($sql);
        if(false === $this->lastStmt)
        {
            throw new DbException('sql prepare error: [' . $this->errorCode() . '] ' . $this->errorInfo() . PHP_EOL . 'sql: ' . $sql . PHP_EOL);
        }
        return BeanFactory::newInstance(Statement::class, $this, $this->lastStmt);
    }
324219
public static function find(int $id): ORM
    {
        if (! ($instance = self::instance()->getById($id))) {
            throw new DatabaseException('Could not find ' . __CLASS__ .
                ' where primary key is ' . $id);
        }
        return $instance;
    }