-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_utils.py
89 lines (71 loc) · 2.78 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#
# skaer media streamer
# Copyright (c) 2019 Emil Penchev
#
# Project page:
# http://skaermedia.org
#
# licensed under GNU GPL version 3 (or later)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
import sqlite3
import psycopg2
from contextlib import closing
class Sqlite(object):
""" Sqlite common set of database utils. """
def __init__(self, db_url, in_mem=False):
self._db_url = db_url
# If DB is in memory create a persistent connection.
if in_mem:
self._db_con = sqlite3.connect(self._db_url)
def execute(self, query, args=[]):
"""
Executes an SQL statement and return all the results.
Wrapper around sqlite3.Cursor.execute.
"""
with closing(sqlite3.connect(self._db_url)) as con, con:
with closing(con.cursor()) as cursor:
cursor.execute(query, args)
return cursor.fetchall()
def executescript(self, sql_script):
"""
Method for executing multiple SQL statements at once.
Wrapper around sqlite3.Cursor.executescript.
"""
with closing(sqlite3.connect(self._db_url)) as con, con:
with closing(con.cursor()) as cursor:
cursor.executescript(sql_script)
def executemany(self, query, seq_of_parameters):
"""
Executes an SQL command against all parameter sequences
or mappings found in the sequence seq_of_parameters.
Wrapper around sqlite3.Cursor.executemany.
"""
with closing(sqlite3.connect(self._db_url)) as con, con:
with closing(con.cursor()) as cursor:
cursor.executemany(query, seq_of_parameters)
class PostgreSQL(object):
""" PostgreSQL python api (psycopg2) wrapper. """
@staticmethod
def connect():
local_dsn = "dbname=skaer host=localhost user=emil passowrd=emil"
DATABASE_URL = os.environ.get("DATABASE_URL", local_dsn)
return psycopg2.connect(DATABASE_URL, sslmode="require")
@staticmethod
def execute(query, args=[]):
with closing(PostgreSQL.connect()) as con, con:
with con.cursor() as cur:
cur.execute(query, args)
return cur.fetchall()