# -*- coding: utf-8 -*-1# This program is free software; you can redistribute it and/or modify2# it under the terms of the GNU General Public License as published by3# the Free Software Foundation; either version 2 of the License, or4# (at your option) any later version.5#6# This program is distributed in the hope that it will be useful,7# but WITHOUT ANY WARRANTY; without even the implied warranty of8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9# GNU General Public License for more details.10#11# You should have received a copy of the GNU General Public License12# along with this program; if not, write to the Free Software13# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,14# MA 02110-1301, USA.15#16# Author: Mauro Soria1718import mysql.connector1920from mysql.connector.constants import SQLMode21from urllib.parse import urlparse2223from lib.core.exceptions import InvalidURLException24from lib.core.settings import DB_CONNECTION_TIMEOUT25from lib.report.factory import BaseReport, SQLReportMixin262728class MySQLReport(SQLReportMixin, BaseReport):29__format__ = "sql"30__extension__ = None31_reuse = True3233def is_valid(self, url):34return url.startswith("mysql://")3536def connect(self, url):37if not self.is_valid(url):38raise InvalidURLException("Provided MySQL URL does not start with mysql://")3940parsed = urlparse(url)41conn = mysql.connector.connect(42host=parsed.hostname,43port=parsed.port or 3306,44user=parsed.username,45password=parsed.password,46database=parsed.path.lstrip("/"),47connection_timeout=DB_CONNECTION_TIMEOUT,48)49conn.sql_mode = [SQLMode.ANSI_QUOTES]5051return conn525354