mirror of
https://github.com/beetbox/beets.git
synced 2025-12-29 03:52:51 +01:00
Add Type.to_sql() and simplify model code
Instead of encoding the conversion behaviour in the model class (via the `_bytes_keys` attribute) we define it on the type. This gives us a more extensible interface and separates logic. This should not change any behaviour (as one can see by closely staring at the code).
This commit is contained in:
parent
4134a5173a
commit
4cfb59bfba
3 changed files with 12 additions and 11 deletions
|
|
@ -115,11 +115,6 @@ class Model(object):
|
|||
keys are field names and the values are `Type` objects.
|
||||
"""
|
||||
|
||||
_bytes_keys = ()
|
||||
"""Keys whose values should be stored as raw bytes blobs rather than
|
||||
strings.
|
||||
"""
|
||||
|
||||
_search_fields = ()
|
||||
"""The fields that should be queried by default by unqualified query
|
||||
terms.
|
||||
|
|
@ -338,11 +333,7 @@ class Model(object):
|
|||
if key != 'id' and key in self._dirty:
|
||||
self._dirty.remove(key)
|
||||
assignments += key + '=?,'
|
||||
value = self[key]
|
||||
# Wrap path strings in buffers so they get stored
|
||||
# "in the raw".
|
||||
if key in self._bytes_keys and isinstance(value, str):
|
||||
value = buffer(value)
|
||||
value = self._type(key).to_sql(self[key])
|
||||
subvars.append(value)
|
||||
assignments = assignments[:-1] # Knock off last ,
|
||||
|
||||
|
|
|
|||
|
|
@ -75,6 +75,12 @@ class Type(object):
|
|||
"""
|
||||
return self.normalize(sql_value)
|
||||
|
||||
def to_sql(self, model_value):
|
||||
"""Convert a value as stored in the model object to a value used
|
||||
by the database adapter.
|
||||
"""
|
||||
return model_value
|
||||
|
||||
|
||||
# Reusable types.
|
||||
|
||||
|
|
|
|||
|
|
@ -109,6 +109,11 @@ class PathType(types.Type):
|
|||
else:
|
||||
return value
|
||||
|
||||
def to_sql(self, value):
|
||||
if isinstance(value, str):
|
||||
value = buffer(value)
|
||||
return value
|
||||
|
||||
|
||||
class MusicalKey(types.String):
|
||||
"""String representing the musical key of a song.
|
||||
|
|
@ -188,7 +193,6 @@ class WriteError(FileOperationError):
|
|||
class LibModel(dbcore.Model):
|
||||
"""Shared concrete functionality for Items and Albums.
|
||||
"""
|
||||
_bytes_keys = ('path', 'artpath')
|
||||
|
||||
def _template_funcs(self):
|
||||
funcs = DefaultTemplateFunctions(self, self._db).functions()
|
||||
|
|
|
|||
Loading…
Reference in a new issue