diff --git a/api/albums_songs.py b/api/albums_songs.py index f57b624..7348651 100755 --- a/api/albums_songs.py +++ b/api/albums_songs.py @@ -125,6 +125,8 @@ def album_list_id3(): def now_playing(): if engine.name == 'sqlite': query = User.query.join(Track).filter(func.strftime('%s', now()) - func.strftime('%s', User.last_play_date) < Track.duration * 2) + elif engine.name == 'postgresql': + query = User.query.join(Track).filter(func.date_part('epoch', func.now() - User.last_play_date) < Track.duration * 2) else: query = User.query.join(Track).filter(func.timediff(func.now(), User.last_play_date) < Track.duration * 2) diff --git a/db.py b/db.py index 2b55f73..c011c6e 100755 --- a/db.py +++ b/db.py @@ -8,27 +8,42 @@ from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.types import TypeDecorator, BINARY -from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.dialects.postgresql import UUID as pgUUID import uuid, datetime, time import os.path class UUID(TypeDecorator): + """Platform-somewhat-independent UUID type + + Uses Postgresql's UUID type, otherwise uses BINARY(16), + should be more efficient than a CHAR(32). + + Mix of http://stackoverflow.com/a/812363 + and http://www.sqlalchemy.org/docs/core/types.html#backend-agnostic-guid-type + """ + impl = BINARY - def __init__(self): - self.impl.length = 16 - TypeDecorator.__init__(self, length = self.impl.length) + def load_dialect_impl(self, dialect): + if dialect.name == 'postgresql': + return dialect.type_descriptor(pgUUID()) + else: + return dialect.type_descriptor(BINARY(16)) - def process_bind_param(self, value, dialect = None): + def process_bind_param(self, value, dialect): if value and isinstance(value, uuid.UUID): + if dialect.name == 'postgresql': + return str(value) return value.bytes if value and not isinstance(value, uuid.UUID): raise ValueError, 'value %s is not a valid uuid.UUID' % value return None - def process_result_value(self, value, dialect = None): + def process_result_value(self, value, dialect): if value: + if dialect.name == 'postgresql': + return uuid.UUID(value) return uuid.UUID(bytes = value) return None