Debian Codegraph @

lib/report/mysql_report.py

raw
1 # -*- coding: utf-8 -*-
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15 # MA 02110-1301, USA.
16 #
17 # Author: Mauro Soria
18
19 import mysql.connector
20
21 from mysql.connector.constants import SQLMode
22 from urllib.parse import urlparse
23
24 from lib.core.exceptions import InvalidURLException
25 from lib.core.settings import DB_CONNECTION_TIMEOUT
26 from lib.report.factory import BaseReport, SQLReportMixin
27
28
29 class MySQLReport(SQLReportMixin, BaseReport):
30 __format__ = "sql"
31 __extension__ = None
32 _reuse = True
33
34 def is_valid(self, url):
35 return url.startswith("mysql://")
36
37 def connect(self, url):
38 if not self.is_valid(url):
39 raise InvalidURLException("Provided MySQL URL does not start with mysql://")
40
41 parsed = urlparse(url)
42 conn = mysql.connector.connect(
43 host=parsed.hostname,
44 port=parsed.port or 3306,
45 user=parsed.username,
46 password=parsed.password,
47 database=parsed.path.lstrip("/"),
48 connection_timeout=DB_CONNECTION_TIMEOUT,
49 )
50 conn.sql_mode = [SQLMode.ANSI_QUOTES]
51
52 return conn