|
1 | | -import edgedb |
| 1 | +import psycopg2 |
| 2 | +from config import DB_HOST, DB_PORT, DB_USER, DB_PASS, DB_NAME |
2 | 3 |
|
3 | | -class EdgeDB: |
| 4 | +class PostgresDB: |
4 | 5 | def __init__(self): |
5 | | - self.client = edgedb.create_client() |
| 6 | + self.conn = psycopg2.connect( |
| 7 | + dbname=DB_NAME, |
| 8 | + host=DB_HOST, |
| 9 | + port=DB_PORT, |
| 10 | + user=DB_USER, |
| 11 | + password=DB_PASS, |
| 12 | + ) |
| 13 | + self.cursor = self.conn.cursor() |
6 | 14 |
|
7 | 15 | def create_sponsor(self, gh_id, gh_username, discord_id=0, discord_name='', discord_code='', contributed_to_repos=None, is_currently_sponsoring=False): |
8 | 16 | if self.get_sponsor_by_gh_id(gh_id): |
9 | 17 | return |
10 | 18 | if contributed_to_repos is None: |
11 | 19 | contributed_to_repos = [] |
12 | | - self.client.execute(''' |
13 | | - INSERT Sponsor { |
14 | | - gh_id := <int32>$gh_id, |
15 | | - discord_id := <int64>$discord_id, |
16 | | - gh_username := <str>$gh_username, |
17 | | - discord_name := <str>$discord_name, |
18 | | - discord_code := <str>$discord_code, |
19 | | - contributed_to_repos := <array<str>>$contributed_to_repos, |
20 | | - is_currently_sponsoring := <bool>$is_currently_sponsoring |
21 | | - }; |
22 | | - ''', gh_id=int(gh_id), discord_id=int(discord_id), gh_username=gh_username, discord_name=discord_name, discord_code=discord_code, contributed_to_repos=contributed_to_repos, is_currently_sponsoring=is_currently_sponsoring) |
| 20 | + self.cursor.execute(''' |
| 21 | + INSERT INTO Sponsor ( |
| 22 | + gh_id, discord_id, gh_username, discord_name, discord_code, |
| 23 | + contributed_to_repos, is_currently_sponsoring |
| 24 | + ) VALUES (%s, %s, %s, %s, %s, %s, %s) |
| 25 | + ''', (gh_id, discord_id, gh_username, discord_name, discord_code, contributed_to_repos, is_currently_sponsoring)) |
| 26 | + self.conn.commit() |
23 | 27 |
|
24 | 28 | def get_sponsor_by_gh_id(self, gh_id): |
25 | | - return self.client.query_single(''' |
26 | | - SELECT DISTINCT Sponsor { |
27 | | - gh_id, |
28 | | - gh_username, |
29 | | - gh_url, |
30 | | - discord_name, |
31 | | - discord_id, |
32 | | - discord_code, |
33 | | - contributed_to_repos, |
34 | | - is_contributor, |
35 | | - is_currently_sponsoring |
36 | | - } |
37 | | - FILTER .gh_id = <int32>$gh_id |
38 | | - LIMIT 1; |
39 | | - ''', gh_id=int(gh_id)) |
| 29 | + self.cursor.execute(''' |
| 30 | + SELECT * FROM Sponsor WHERE gh_id = %s LIMIT 1 |
| 31 | + ''', (gh_id,)) |
| 32 | + return self.cursor.fetchone() |
40 | 33 |
|
41 | 34 | def get_sponsor_by_gh_username(self, gh_username): |
42 | | - return self.client.query_single(''' |
43 | | - SELECT Sponsor { |
44 | | - gh_id, |
45 | | - gh_username, |
46 | | - gh_url, |
47 | | - discord_name, |
48 | | - discord_id, |
49 | | - discord_code, |
50 | | - contributed_to_repos, |
51 | | - is_contributor, |
52 | | - is_currently_sponsoring |
53 | | - } |
54 | | - FILTER str_lower(.gh_username) = str_lower(<str>$gh_username) |
55 | | - LIMIT 1; |
56 | | - ''', gh_username=gh_username) |
| 35 | + self.cursor.execute(''' |
| 36 | + SELECT * FROM Sponsor WHERE LOWER(gh_username) = LOWER(%s) LIMIT 1 |
| 37 | + ''', (gh_username,)) |
| 38 | + return self.cursor.fetchone() |
57 | 39 |
|
58 | 40 | def get_sponsor_by_discord_id(self, discord_id): |
59 | | - return self.client.query_single(''' |
60 | | - SELECT Sponsor { |
61 | | - gh_id, |
62 | | - gh_username, |
63 | | - gh_url, |
64 | | - is_contributor, |
65 | | - contributed_to_repos, |
66 | | - discord_name, |
67 | | - discord_id, |
68 | | - discord_code, |
69 | | - is_currently_sponsoring |
70 | | - } |
71 | | - FILTER .discord_id = <int64>$discord_id |
72 | | - LIMIT 1; |
73 | | - ''', discord_id=int(discord_id)) |
74 | | - |
| 41 | + self.cursor.execute(''' |
| 42 | + SELECT * FROM Sponsor WHERE discord_id = %s LIMIT 1 |
| 43 | + ''', (discord_id,)) |
| 44 | + return self.cursor.fetchone() |
| 45 | + |
75 | 46 | def update_sponsor_gh_username(self, gh_id, gh_username): |
76 | | - sponsor = self.get_sponsor_by_gh_id(gh_id) |
77 | | - if not sponsor: |
| 47 | + if not self.get_sponsor_by_gh_id(gh_id): |
78 | 48 | return |
79 | | - self.client.execute(''' |
80 | | - UPDATE Sponsor |
81 | | - FILTER .gh_id = <int32>$gh_id |
82 | | - SET { |
83 | | - gh_username := <str>$gh_username |
84 | | - }; |
85 | | - ''', gh_id=int(gh_id), gh_username=gh_username) |
| 49 | + self.cursor.execute(''' |
| 50 | + UPDATE Sponsor SET gh_username = %s WHERE gh_id = %s |
| 51 | + ''', (gh_username, gh_id)) |
| 52 | + self.conn.commit() |
86 | 53 |
|
87 | 54 | def update_sponsor_discord_id(self, gh_id, discord_id): |
88 | 55 | if not self.get_sponsor_by_gh_id(gh_id): |
89 | 56 | return |
90 | | - self.client.execute(''' |
91 | | - UPDATE Sponsor |
92 | | - FILTER .gh_id = <int32>$gh_id |
93 | | - SET { |
94 | | - discord_id := <int64>$discord_id |
95 | | - }; |
96 | | - ''', gh_id=int(gh_id), discord_id=int(discord_id)) |
| 57 | + self.cursor.execute(''' |
| 58 | + UPDATE Sponsor SET discord_id = %s WHERE gh_id = %s |
| 59 | + ''', (discord_id, gh_id)) |
| 60 | + self.conn.commit() |
97 | 61 |
|
98 | 62 | def update_sponsor_discord_name(self, gh_id, discord_name): |
99 | 63 | if not self.get_sponsor_by_gh_id(gh_id): |
100 | 64 | return |
101 | | - self.client.execute(''' |
102 | | - UPDATE Sponsor |
103 | | - FILTER .gh_id = <int32>$gh_id |
104 | | - SET { |
105 | | - discord_name := <str>$discord_name |
106 | | - }; |
107 | | - ''', gh_id=int(gh_id), discord_name=discord_name) |
| 65 | + self.cursor.execute(''' |
| 66 | + UPDATE Sponsor SET discord_name = %s WHERE gh_id = %s |
| 67 | + ''', (discord_name, gh_id)) |
| 68 | + self.conn.commit() |
108 | 69 |
|
109 | 70 | def update_sponsor_discord_code(self, gh_id, discord_code): |
110 | 71 | if not self.get_sponsor_by_gh_id(gh_id): |
111 | 72 | return |
112 | | - self.client.execute(''' |
113 | | - UPDATE Sponsor |
114 | | - FILTER .gh_id = <int32>$gh_id |
115 | | - SET { |
116 | | - discord_code := <str>$discord_code |
117 | | - }; |
118 | | - ''', gh_id=int(gh_id), discord_code=discord_code) |
| 73 | + self.cursor.execute(''' |
| 74 | + UPDATE Sponsor SET discord_code = %s WHERE gh_id = %s |
| 75 | + ''', (discord_code, gh_id)) |
| 76 | + self.conn.commit() |
119 | 77 |
|
120 | 78 | def update_sponsor_contributed_to_repos(self, gh_id, contributed_to_repos): |
121 | 79 | if not self.get_sponsor_by_gh_id(gh_id): |
122 | 80 | return |
123 | | - self.client.execute(''' |
124 | | - UPDATE Sponsor |
125 | | - FILTER .gh_id = <int32>$gh_id |
126 | | - SET { |
127 | | - contributed_to_repos := <array<str>>$contributed_to_repos |
128 | | - }; |
129 | | - ''', gh_id=int(gh_id), contributed_to_repos=contributed_to_repos) |
| 81 | + self.cursor.execute(''' |
| 82 | + UPDATE Sponsor SET contributed_to_repos = %s WHERE gh_id = %s |
| 83 | + ''', (contributed_to_repos, gh_id)) |
| 84 | + self.conn.commit() |
130 | 85 |
|
131 | 86 | def update_sponsor_is_currently_sponsoring(self, gh_id, is_currently_sponsoring): |
132 | 87 | if not self.get_sponsor_by_gh_id(gh_id): |
133 | 88 | return |
134 | | - self.client.execute(''' |
135 | | - UPDATE Sponsor |
136 | | - FILTER .gh_id = <int32>$gh_id |
137 | | - SET { |
138 | | - is_currently_sponsoring := <bool>$is_currently_sponsoring |
139 | | - }; |
140 | | - ''', gh_id=int(gh_id), is_currently_sponsoring=is_currently_sponsoring) |
| 89 | + self.cursor.execute(''' |
| 90 | + UPDATE Sponsor SET is_currently_sponsoring = %s WHERE gh_id = %s |
| 91 | + ''', (is_currently_sponsoring, gh_id)) |
| 92 | + self.conn.commit() |
141 | 93 |
|
142 | 94 | def get_sponsors(self): |
143 | | - return self.client.query(''' |
144 | | - SELECT Sponsor { |
145 | | - gh_id, |
146 | | - gh_username, |
147 | | - gh_url, |
148 | | - is_contributor, |
149 | | - contributed_to_repos, |
150 | | - discord_name, |
151 | | - discord_id, |
152 | | - discord_code, |
153 | | - is_currently_sponsoring |
154 | | - }; |
155 | | - ''') |
| 95 | + self.cursor.execute('SELECT * FROM Sponsor') |
| 96 | + return self.cursor.fetchall() |
0 commit comments