]>
Commit | Line | Data |
---|---|---|
e5424f29 MR |
1 | class Base(object): |
2 | ||
3 | def query(self, sql): | |
4 | res = None | |
5 | cur = self.conn.cursor() | |
6 | try: | |
7 | cur.execute(sql) | |
8 | res = cur.fetchall() | |
9 | except: | |
10 | pass | |
11 | finally: | |
12 | cur.close() | |
13 | ||
14 | return res | |
15 | ||
16 | def update(self, sql, placeholders): | |
17 | rows = None | |
18 | cur = self.conn.cursor() | |
19 | try: | |
20 | cur.execute(sql, placeholders) | |
21 | self.conn.commit() | |
22 | rows = cur.rowcount | |
23 | except Exception as e: | |
24 | self.conn.rollback() | |
25 | raise Exception(e) | |
26 | finally: | |
27 | cur.close() | |
28 | ||
29 | return rows | |
30 | ||
31 | def insert(self, sql, placeholders): | |
32 | lastid = None | |
33 | cur = self.conn.cursor() | |
34 | try: | |
35 | cur.execute(sql, placeholders) | |
36 | if self.__class__.__name__ == 'Postgres': | |
37 | cur.execute('select lastval()') | |
38 | lastid = cur.fetchone()[0] | |
39 | else: | |
40 | lastid = cur.lastrowid | |
41 | self.conn.commit() | |
42 | except Exception as e: | |
43 | self.conn.rollback() | |
44 | raise Exception(e) | |
45 | finally: | |
46 | cur.close() | |
47 | ||
48 | return lastid | |
49 | ||
50 | def ddl(self, sql): | |
51 | cur = self.conn.cursor() | |
52 | try: | |
53 | if self.__class__.__name__ == 'Mysql': | |
54 | for r in cur.execute(sql, multi=True): | |
55 | pass | |
56 | else: | |
57 | cur.execute(sql) | |
58 | self.conn.commit() | |
59 | except Exception as e: | |
60 | self.conn.rollback() | |
61 | raise Exception(e) | |
62 | finally: | |
63 | cur.close() | |
64 | ||
65 | def __del__(self): | |
66 | #print("%s: deleted" % self.__class__.__name__) | |
67 | try: | |
68 | self.conn.close() | |
69 | self.conn = None | |
70 | except AttributeError: | |
71 | pass | |
72 | except: | |
73 | raise |