1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Postgresql support

This commit is contained in:
spl0k 2013-07-16 11:30:19 +02:00
parent f3eed12e1b
commit b324e1da29
2 changed files with 23 additions and 6 deletions

View File

@ -125,6 +125,8 @@ def album_list_id3():
def now_playing(): def now_playing():
if engine.name == 'sqlite': if engine.name == 'sqlite':
query = User.query.join(Track).filter(func.strftime('%s', now()) - func.strftime('%s', User.last_play_date) < Track.duration * 2) 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: else:
query = User.query.join(Track).filter(func.timediff(func.now(), User.last_play_date) < Track.duration * 2) query = User.query.join(Track).filter(func.timediff(func.now(), User.last_play_date) < Track.duration * 2)

27
db.py
View File

@ -8,27 +8,42 @@ from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import TypeDecorator, BINARY 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 uuid, datetime, time
import os.path import os.path
class UUID(TypeDecorator): 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 impl = BINARY
def __init__(self): def load_dialect_impl(self, dialect):
self.impl.length = 16 if dialect.name == 'postgresql':
TypeDecorator.__init__(self, length = self.impl.length) 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 value and isinstance(value, uuid.UUID):
if dialect.name == 'postgresql':
return str(value)
return value.bytes return value.bytes
if value and not isinstance(value, uuid.UUID): if value and not isinstance(value, uuid.UUID):
raise ValueError, 'value %s is not a valid uuid.UUID' % value raise ValueError, 'value %s is not a valid uuid.UUID' % value
return None return None
def process_result_value(self, value, dialect = None): def process_result_value(self, value, dialect):
if value: if value:
if dialect.name == 'postgresql':
return uuid.UUID(value)
return uuid.UUID(bytes = value) return uuid.UUID(bytes = value)
return None return None