/**
  * Executes the statement with any supplied named parameters on the connection provided.
  *
  * If no connection is provided the default connection will be used.
  *
  * @param $statement
  * @param array $namedParameters
  * @param \PDO $connection
  * @param bool $isInsertQuery True if the query is an insert and the ID should be returned
  * @throws \Rhubarb\Stem\Exceptions\RepositoryStatementException
  * @return \PDOStatement
  */
 public static function executeStatement($statement, $namedParameters = [], $connection = null, $isInsertQuery = false)
 {
     if ($connection === null) {
         $connection = static::getDefaultConnection();
     }
     self::$secondLastStatement = self::$lastStatement;
     self::$lastStatement = $statement;
     self::$lastParams = $namedParameters;
     $pdoStatement = $connection->prepare($statement);
     Log::CreateEntry(Log::PERFORMANCE_LEVEL | Log::REPOSITORY_LEVEL, function () use($statement, $namedParameters, $connection) {
         $newStatement = $statement;
         array_walk($namedParameters, function ($value, $key) use(&$newStatement, &$params, $connection) {
             // Note this is not attempting to make secure queries - this is purely illustrative for the logs
             // However we do at least do addslashes so if you want to cut and paste a query from the log to
             // try it - it should work in most cases.
             $newStatement = str_replace(':' . $key, $connection->quote($value), $newStatement);
         });
         return "Executing PDO statement " . $newStatement;
     }, "PDO");
     if (!$pdoStatement->execute($namedParameters)) {
         $error = $pdoStatement->errorInfo();
         throw new RepositoryStatementException($error[2], $statement);
     }
     if ($isInsertQuery) {
         $pdoStatement = $connection->lastInsertId();
     }
     Log::CreateEntry(Log::PERFORMANCE_LEVEL | Log::REPOSITORY_LEVEL, "Statement successful", "PDO");
     return $pdoStatement;
 }