]> git.datanom.net - pwp.git/blob - app/DB/base.py
Half way through migration away from sqlalchemy
[pwp.git] / app / DB / base.py
1 import logging
2 logging.basicConfig(level=logging.INFO)
3 logger = logging.getLogger(__name__)
4
5 class Base(object):
6
7 def query(self, sql, placeholders):
8 res = None
9 cur = self.conn.cursor()
10 try:
11 cur.execute(sql, placeholders)
12 res = cur.fetchall()
13 except Exception as e:
14 logger.error("{0}".format(e))
15 pass
16 finally:
17 cur.close()
18
19 return res
20
21 def update(self, sql, placeholders):
22 rows = None
23 cur = self.conn.cursor()
24 try:
25 cur.execute(sql, placeholders)
26 self.conn.commit()
27 rows = cur.rowcount
28 except Exception as e:
29 self.conn.rollback()
30 raise Exception(e)
31 finally:
32 cur.close()
33
34 return rows
35
36 def insert(self, sql, placeholders):
37 lastid = None
38 cur = self.conn.cursor()
39 try:
40 cur.execute(sql, placeholders)
41 if self.__class__.__name__ == 'Postgres':
42 cur.execute('select lastval()')
43 lastid = cur.fetchone()[0]
44 else:
45 lastid = cur.lastrowid
46 self.conn.commit()
47 except Exception as e:
48 self.conn.rollback()
49 raise Exception(e)
50 finally:
51 cur.close()
52
53 return lastid
54
55 def ddl(self, sql):
56 cur = self.conn.cursor()
57 try:
58 if self.__class__.__name__ == 'Mysql':
59 for r in cur.execute(sql, multi=True):
60 pass
61 else:
62 cur.execute(sql)
63 self.conn.commit()
64 except Exception as e:
65 self.conn.rollback()
66 raise Exception(e)
67 finally:
68 cur.close()
69
70 def __del__(self):
71 #print("%s: deleted" % self.__class__.__name__)
72 try:
73 self.conn.close()
74 self.conn = None
75 except AttributeError:
76 pass
77 except:
78 raise
This page took 0.123186 seconds and 6 git commands to generate.