Move helpers.select_generator to PDB.sql_select.

This commit is contained in:
voussoir 2018-03-25 16:32:17 -07:00
parent cfa5d269d1
commit cca8837863
3 changed files with 17 additions and 18 deletions

View file

@ -343,19 +343,6 @@ def seconds_to_hms(seconds):
hms = ':'.join('%02d' % part for part in parts) hms = ':'.join('%02d' % part for part in parts)
return hms return hms
def select_generator(sql, query, bindings=None):
'''
Perform the query, and yield the results.
'''
bindings = bindings or []
cursor = sql.cursor()
cursor.execute(query, bindings)
while True:
fetch = cursor.fetchone()
if fetch is None:
break
yield fetch
def sql_listify(items): def sql_listify(items):
''' '''
Given a list of strings, return a string in the form of an SQL list. Given a list of strings, return a string in the form of an SQL list.

View file

@ -448,8 +448,7 @@ class Album(ObjectBase, GroupableMixin):
def get_photos(self): def get_photos(self):
photos = [] photos = []
generator = helpers.select_generator( generator = self.photodb.sql_select(
self.photodb.sql,
'SELECT photoid FROM album_photo_rel WHERE albumid == ?', 'SELECT photoid FROM album_photo_rel WHERE albumid == ?',
[self.id] [self.id]
) )
@ -874,8 +873,7 @@ class Photo(ObjectBase):
''' '''
Return the tags assigned to this Photo. Return the tags assigned to this Photo.
''' '''
generator = helpers.select_generator( generator = self.photodb.sql_select(
self.photodb.sql,
'SELECT tagid FROM photo_tag_rel WHERE photoid == ?', 'SELECT tagid FROM photo_tag_rel WHERE photoid == ?',
[self.id] [self.id]
) )

View file

@ -632,7 +632,7 @@ class PDBPhotoMixin:
#cur = self.sql.cursor() #cur = self.sql.cursor()
#cur.execute('EXPLAIN QUERY PLAN ' + query, bindings) #cur.execute('EXPLAIN QUERY PLAN ' + query, bindings)
#print('\n'.join(str(x) for x in cur.fetchall())) #print('\n'.join(str(x) for x in cur.fetchall()))
generator = helpers.select_generator(self.sql, query, bindings) generator = self.sql_select(query, bindings)
photos_received = 0 photos_received = 0
for row in generator: for row in generator:
photo = objects.Photo(self, row) photo = objects.Photo(self, row)
@ -758,6 +758,20 @@ class PDBSQLMixin:
if commit: if commit:
self.commit() self.commit()
def sql_select(self, query, bindings=None):
cur = self.sql.cursor()
cur.execute(query, bindings)
while True:
fetch = cur.fetchone()
if fetch is None:
break
yield fetch
def sql_select_one(self, query, bindings=None):
cur = self.sql.cursor()
cur.execute(query, bindings)
return cur.fetchone()
def sql_update(self, table, pairs, where_key, *, commit=False): def sql_update(self, table, pairs, where_key, *, commit=False):
cur = self.sql.cursor() cur = self.sql.cursor()