Skip to content

Commit 2444ddd

Browse files
committed
Use execute_values from psycopg 2.7+
1 parent bd203c0 commit 2444ddd

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

addok_pg/plugin.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from psycopg2 import connect
2+
from psycopg2.extras import execute_values
23

34
from addok.config import config
45

@@ -31,9 +32,6 @@ def fetch(self, *keys):
3132

3233
def upsert(self, *docs):
3334
"""
34-
Once psycopg2.7 is released, switch to `extras.execute_values`
35-
which avoids the template dance.
36-
3735
Potential performance boost, using copy_from:
3836
* https://gist.github.com/jsheedy/efa9a69926a754bebf0e9078fd085df6
3937
* https://gist.github.com/jsheedy/ed81cdf18190183b3b7d
@@ -42,11 +40,11 @@ def upsert(self, *docs):
4240
* http://stackoverflow.com/a/8150329
4341
"""
4442
insert_into_query = '''
45-
INSERT INTO {PG_TABLE} (key, data) VALUES {template}
43+
INSERT INTO {PG_TABLE} (key, data) VALUES %s
4644
ON CONFLICT DO NOTHING
47-
'''.format(template=','.join(['%s'] * len(docs)), **config)
45+
'''.format(**config)
4846
with self.conn.cursor() as curs:
49-
curs.execute(insert_into_query, docs)
47+
execute_values(curs, insert_into_query, docs)
5048
self.conn.commit()
5149

5250
def remove(self, *keys):

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
psycopg2
1+
psycopg2==2.7.1

0 commit comments

Comments
 (0)