Blame db/types.py

4656ad
4656ad
4656ad
import datetime
4656ad
4656ad
4656ad
dateformat = '%Y-%m-%dT%H:%M:%S'
4656ad
datepattern = '0000-00-00T00:00:00'
4656ad
4656ad
def date_to_str(date):
4656ad
    return date.strftime(dateformat).rjust(len(datepattern), '0')
4656ad
4656ad
def set_timezone(date, tz = datetime.timezone.utc):
4656ad
    return datetime.datetime(
4656ad
        year        = date.year,
4656ad
        month       = date.month,
4656ad
        day         = date.day,
4656ad
        hour        = date.hour,
4656ad
        minute      = date.minute,
4656ad
        second      = date.second,
4656ad
        microsecond = date.microsecond,
4656ad
        tzinfo      = tz )
4656ad
    
4656ad
def str_to_date(s):
4656ad
    return set_timezone( datetime.datetime.strptime(s, date_format) )
4656ad
4656ad
4656ad
class Type:
4656ad
  def from_raw(self, value):
4656ad
    return None
4656ad
  def from_db(self, connection, value):
4656ad
    return value
4656ad
  def to_db(self, connection, value):
4656ad
    return connection.escape(self.from_raw(value))
4656ad
4656ad
4656ad
class Int(Type):
4656ad
  def from_raw(self, value):
4656ad
    return int(value if value else 0)
4656ad
4656ad
4656ad
class String(Type):
4656ad
  def from_raw(self, value):
e5b0ac
    return '' if value is None else str(value)
4656ad
  def to_db(self, connection, value):
4656ad
    return "'" + connection.escape_string(self.from_raw(value)) + "'"
4656ad
4656ad
4656ad
class Float(Type):
4656ad
  def from_raw(self, value):
4656ad
    return float(value if value else 0)
4656ad
4656ad
4656ad
class Date(Type):
4656ad
  def from_raw(self, value):
e5b0ac
    return str_to_date(date_to_str(value) if type(value) is datetime.datetime else str(value))
4656ad
  def from_db(self, connection, value):
4656ad
    return set_timezone(value)
4656ad
4656ad
4656ad
class Field(Type):
4656ad
  def from_raw(self, value):
4656ad
    result = str(value)
4656ad
    assert(result.isidentifier())
4656ad
    return result
4656ad
  def to_db(self, connection, value):
4656ad
    return '`' + connection.escape_string(self.from_raw(value)) + '`'
4656ad
4656ad
4656ad
class Table(Type):
4656ad
  def from_raw(self, value):
4656ad
    result = str(value)
4656ad
    assert(result.isidentifier())
4656ad
    return result
4656ad
  def from_db(self, connection, value):
4656ad
    value = str(value)
4656ad
    prefix = connection.pool.server.config['db']['prefix']
4656ad
    assert value.startswith(prefix)
4656ad
    return self.from_raw(value[len(prefix):])
4656ad
  def to_db(self, connection, value):
4656ad
    return '`' + connection.escape_string( connection.pool.server.config['db']['prefix'] + self.from_raw(value)) + '`'
4656ad
4656ad
4656ad
tint    = Int()
4656ad
tstring = String()
4656ad
tfloat  = Float()
4656ad
tdate   = Date()
4656ad
tfield  = Field()
4656ad
ttable  = Table()
4656ad
4656ad
4656ad
bytype = {
4656ad
  int   : tint,
4656ad
  str   : tstring,
4656ad
  float : tfloat,
4656ad
  datetime.datetime: tdate,
4656ad
}
4656ad
4656ad
bychar = {
4656ad
  'd': tint,
4656ad
  's': tstring,
4656ad
  'f': tfloat,
4656ad
  'D': tdate,
4656ad
  'F': tfield,
4656ad
  'T': ttable,
4656ad
}