witten
/
luminotes
Archived
1
0
Fork 0
This repository has been archived on 2023-12-16. You can view files and clone it, but cannot push or open issues or pull requests.
luminotes/model/Persistent.py

115 lines
3.3 KiB
Python

import sys
import time
from datetime import datetime
from pytz import utc
class Persistent( object ):
POSTGRESQL_BACKEND = 0
SQLITE_BACKEND = 1
"""
A persistent database object with a unique id.
"""
def __init__( self, object_id, revision = None ):
self.__object_id = object_id
self.__revision = revision
if not revision:
self.update_revision()
@staticmethod
def sql_load( object_id, revision = None ):
"""
Return a SQL string to load an object with the given information from the database. If a
revision is not provided, then the most current version of the given object will be loaded.
@type object_id: unicode
@param object_id: id of object to load
@type revision: unicode or NoneType
@param revision: revision of the object to load (optional)
"""
raise NotImplementedError()
@staticmethod
def sql_id_exists( object_id, revision = None ):
"""
Return a SQL string to determine whether the given object is present in the database. If a
revision is not provided, then the most current version of the given object will be used.
@type object_id: unicode
@param object_id: id of object to check for existence
@type revision: unicode or NoneType
@param revision: revision of the object to check (optional)
"""
raise NotImplementedError()
def sql_exists( self ):
"""
Return a SQL string to determine whether the current revision of this object is present in the
database.
"""
raise NotImplementedError()
def sql_create( self ):
"""
Return a SQL string to save this object to the database for the first time. This should be in
the form of a SQL insert.
"""
raise NotImplementedError()
def sql_update( self ):
"""
Return a SQL string to save an updated revision of this object to the database. Note that,
because of the retention of old row revisions in the database, this SQL string will usually
be in the form of an insert rather than an update to an existing row.
"""
raise NotImplementedError()
def to_dict( self ):
return dict(
object_id = self.__object_id,
revision = self.__revision,
)
def update_revision( self ):
revision = datetime.now( tz = utc )
# as a work around for the low-precision timer on Windows, replace the microseconds with a value
# from a higher-precision timer
if sys.platform.startswith( "win" ):
MICROSECONDS = 1000000
revision = revision.replace( microsecond = int( time.clock() * MICROSECONDS ) % MICROSECONDS )
self.__revision = revision
@staticmethod
def make_cache_key( Object_type, object_id ):
return "%s_%s" % ( object_id, Object_type.__name__ )
object_id = property( lambda self: self.__object_id )
revision = property( lambda self: self.__revision )
cache_key = property( lambda self: Persistent.make_cache_key( type( self ), self.object_id ) )
def quote( value ):
if value is None:
return "null"
if isinstance( value, bool ):
value = value and "t" or "f"
else:
value = unicode( value )
return "'%s'" % value.replace( "'", "''" ).replace( "\\", "\\\\" )
def quote_fuzzy( value ):
if value is None:
return "null"
value = unicode( value )
value = value.replace( "'", "''" ).replace( "\\", "\\\\" )
return "'%" + value + "%'"