protected function exec(\PDOStatement $statement, $type = self::SELECT) {
        // Execute
        try {
            $statement->execute();
        } catch (\PDOException $pe) {
            $statement->closeCursor();
            throw new \Exception("Error while executing query:: sql: ".$statement->queryString.PHP_EOL."Message: ".$pe->getMessage());
        }

        // Process Result based on the query type
        if ($type == self::SELECT) {
            $result = array();
            foreach($statement as $row) {
                $result[] = $row;
            }
        } else if ($type == self::INSERT) {
            // No need to check connection here
            $result = (int)$this->getConnection(false)->lastInsertId();
        } else {
            $result = true;
        }

        $statement->closeCursor();

        return $result;
    }