private static function determinePlaceholderPosition($query, $index, array $placeholdersPositions = array())
    {
        // The placeholder position.
        $placeholderPosition = null;

        // Find the previous cached placeholder position in the stack to optimize the current placeholder position
        // search.

        // The previous parameter index cached.
        $previousIndex = $index - 1;

        // Iterate each cached placeholder positions to find the previous one & the previous parameter index.
        while (($previousIndex >= 0) && ($placeholderPosition === null)) {

            // Check if the previous cached placeholder positions exists.
            if (isset($placeholdersPositions[$previousIndex])) {
                $placeholderPosition = $placeholdersPositions[$previousIndex];
                $previousIndex++;
            } else {
                $previousIndex--;
            }
        }

        // Check if the previous placeholder position has been found.
        if ($placeholderPosition === null) {
            $previousIndex = 0;
            $placeholderPosition = 0;
        }

        // Determine the query length.
        $queryLength = strlen($query);

        // Iterate from the previous cached parameter index to the current one.
        for ($placeholderIndex = $previousIndex; $placeholderIndex <= $index; $placeholderIndex++) {

            // TRUE if we are in an escaped section else FALSE.
            $escaped = false;

            // Iterate each query char from the previous placeholder position to the end in order to find the current
            // placeholder.
            while ($placeholderPosition < $queryLength) {

                // Switch the escaped flag if the current statement char is an escape delimiter.
                if (in_array($query[$placeholderPosition], array('\'', '"'))) {
                    $escaped = !$escaped;
                }

                // Check if we are not in an escaped section and if the current char is a placeholder.
                if (!$escaped && ($query[$placeholderPosition] === '?')) {

                    // If we are not on the current placeholder, we increment the placeholder position.
                    if ($placeholderIndex < $index) {
                        $placeholderPosition++;
                    }

                    break;
                }

                $placeholderPosition++;
            }

            // Check if we have found the placeholder position.
            if ($placeholderPosition === $queryLength) {
                throw QueryRewriterException::positionalPlaceholderDoesNotExist($index, $query);
            }
        }

        return $placeholderPosition;
    }