Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/db.py
Show First 20 Lines • Show All 590 Lines • ▼ Show 20 Lines | def origin_add(self, type, url, cur=None): | ||||
insert = """INSERT INTO origin (type, url) values (%s, %s) | insert = """INSERT INTO origin (type, url) values (%s, %s) | ||||
RETURNING id""" | RETURNING id""" | ||||
cur.execute(insert, (type, url)) | cur.execute(insert, (type, url)) | ||||
return cur.fetchone()[0] | return cur.fetchone()[0] | ||||
origin_cols = ['id', 'type', 'url'] | origin_cols = ['id', 'type', 'url'] | ||||
def origin_get_with(self, type, url, cur=None): | def origin_get_with(self, origins, cur=None): | ||||
"""Retrieve the origin id from its type and url if found.""" | """Retrieve the origin id from its type and url if found.""" | ||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
query = """SELECT %s | query = """SELECT %s FROM (VALUES %%s) as t(type, url) | ||||
FROM origin | LEFT JOIN origin | ||||
WHERE type=%%s AND url=%%s | ON (t.type=origin.type AND t.url=origin.url) | ||||
""" % ','.join(self.origin_cols) | """ % ','.join('origin.' + col for col in self.origin_cols) | ||||
cur.execute(query, (type, url)) | yield from execute_values_generator( | ||||
return cur.fetchone() | cur, query, origins) | ||||
def origin_get(self, id, cur=None): | def origin_get(self, ids, cur=None): | ||||
"""Retrieve the origin per its identifier. | """Retrieve the origin per its identifier. | ||||
""" | """ | ||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
query = """SELECT %s | query = """SELECT %s FROM (VALUES %%s) as t(id) | ||||
FROM origin WHERE id=%%s | LEFT JOIN origin ON t.id = origin.id | ||||
""" % ','.join(self.origin_cols) | """ % ','.join('origin.' + col for col in self.origin_cols) | ||||
cur.execute(query, (id,)) | yield from execute_values_generator( | ||||
return cur.fetchone() | cur, query, ((id,) for id in ids)) | ||||
def origin_search(self, url_pattern, offset=0, limit=50, | def origin_search(self, url_pattern, offset=0, limit=50, | ||||
regexp=False, with_visit=False, cur=None): | regexp=False, with_visit=False, cur=None): | ||||
"""Search for origins whose urls contain a provided string pattern | """Search for origins whose urls contain a provided string pattern | ||||
or match a provided regular expression. | or match a provided regular expression. | ||||
The search is performed in a case insensitive way. | The search is performed in a case insensitive way. | ||||
Args: | Args: | ||||
▲ Show 20 Lines • Show All 203 Lines • Show Last 20 Lines |