db.t.f.epoch(), thanks Niphlod

This commit is contained in:
mdipierro
2012-09-14 15:51:58 -05:00
parent 47bcab6b26
commit 5f503d0427
3 changed files with 78 additions and 58 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.9 (2012-09-14 09:42:11) stable Version 2.0.9 (2012-09-14 15:51:53) stable
+33 -14
View File
@@ -1073,6 +1073,9 @@ class BaseAdapter(ConnectionPool):
def EXTRACT(self, first, what): def EXTRACT(self, first, what):
return "EXTRACT(%s FROM %s)" % (what, self.expand(first)) return "EXTRACT(%s FROM %s)" % (what, self.expand(first))
def EPOCH(self, first):
return self.EXTRACT(first, 'epoch')
def AGGREGATE(self, first, what): def AGGREGATE(self, first, what):
return "%s(%s)" % (what, self.expand(first)) return "%s(%s)" % (what, self.expand(first))
@@ -2027,8 +2030,11 @@ class SQLiteAdapter(BaseAdapter):
'second': (17, 19), 'second': (17, 19),
} }
try: try:
(i, j) = table[lookup] if lookup != 'epoch':
return int(s[i:j]) (i, j) = table[lookup]
return int(s[i:j])
else:
return time.mktime(datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S').timetuple())
except: except:
return None return None
@@ -2280,6 +2286,9 @@ class MySQLAdapter(BaseAdapter):
return 'SUBSTRING(%s,%s,%s)' % (self.expand(field), return 'SUBSTRING(%s,%s,%s)' % (self.expand(field),
parameters[0], parameters[1]) parameters[0], parameters[1])
def EPOCH(self, first):
return "UNIX_TIMESTAMP(%s)" % self.expand(first)
def _drop(self,table,mode): def _drop(self,table,mode):
# breaks db integrity but without this mysql does not drop table # breaks db integrity but without this mysql does not drop table
return ['SET FOREIGN_KEY_CHECKS=0;','DROP TABLE %s;' % table, return ['SET FOREIGN_KEY_CHECKS=0;','DROP TABLE %s;' % table,
@@ -3009,6 +3018,9 @@ class MSSQLAdapter(BaseAdapter):
return rows[minimum:] return rows[minimum:]
return rows[minimum:maximum] return rows[minimum:maximum]
def EPOCH(self, first):
return "DATEDIFF(second, '1970-01-01 00:00:00', %s)" % self.expand(first)
# GIS functions # GIS functions
# No STAsGeoJSON in MSSQL # No STAsGeoJSON in MSSQL
@@ -3228,6 +3240,9 @@ class FireBirdAdapter(BaseAdapter):
def RANDOM(self): def RANDOM(self):
return 'RAND()' return 'RAND()'
def EPOCH(self, first):
return "DATEDIFF(second, '1970-01-01 00:00:00', %s)" % self.expand(first)
def NOT_NULL(self,default,field_type): def NOT_NULL(self,default,field_type):
return 'DEFAULT %s NOT NULL' % self.represent(default,field_type) return 'DEFAULT %s NOT NULL' % self.represent(default,field_type)
@@ -5212,27 +5227,27 @@ class MongoDBAdapter(NoSQLAdapter):
return result return result
def ADD(self, first, second): def ADD(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '%s + %s' % (self.expand(first), self.expand(second, first.type)) return '%s + %s' % (self.expand(first), self.expand(second, first.type))
def SUB(self, first, second): def SUB(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s - %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s - %s)' % (self.expand(first), self.expand(second, first.type))
def MUL(self, first, second): def MUL(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s * %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s * %s)' % (self.expand(first), self.expand(second, first.type))
def DIV(self, first, second): def DIV(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s / %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s / %s)' % (self.expand(first), self.expand(second, first.type))
def MOD(self, first, second): def MOD(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s %% %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s %% %s)' % (self.expand(first), self.expand(second, first.type))
def AS(self, first, second): def AS(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '%s AS %s' % (self.expand(first), second) return '%s AS %s' % (self.expand(first), second)
#We could implement an option that simulates a full featured SQL database. But I think the option should be set explicit or implemented as another library. #We could implement an option that simulates a full featured SQL database. But I think the option should be set explicit or implemented as another library.
@@ -5344,31 +5359,31 @@ class MongoDBAdapter(NoSQLAdapter):
#TODO javascript has math #TODO javascript has math
def ADD(self, first, second): def ADD(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '%s + %s' % (self.expand(first), self.expand(second, first.type)) return '%s + %s' % (self.expand(first), self.expand(second, first.type))
#TODO javascript has math #TODO javascript has math
def SUB(self, first, second): def SUB(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s - %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s - %s)' % (self.expand(first), self.expand(second, first.type))
#TODO javascript has math #TODO javascript has math
def MUL(self, first, second): def MUL(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s * %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s * %s)' % (self.expand(first), self.expand(second, first.type))
#TODO javascript has math #TODO javascript has math
def DIV(self, first, second): def DIV(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s / %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s / %s)' % (self.expand(first), self.expand(second, first.type))
#TODO javascript has math #TODO javascript has math
def MOD(self, first, second): def MOD(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '(%s %% %s)' % (self.expand(first), self.expand(second, first.type)) return '(%s %% %s)' % (self.expand(first), self.expand(second, first.type))
#TODO javascript can do this #TODO javascript can do this
def AS(self, first, second): def AS(self, first, second):
raise NotImplementedError, "This must yet be replaced with javescript in order to accomplish this. Sorry" raise NotImplementedError, "This must yet be replaced with javascript in order to accomplish this. Sorry"
return '%s AS %s' % (self.expand(first), second) return '%s AS %s' % (self.expand(first), second)
#We could implement an option that simulates a full featured SQL database. But I think the option should be set explicit or implemented as another library. #We could implement an option that simulates a full featured SQL database. But I think the option should be set explicit or implemented as another library.
@@ -8075,6 +8090,10 @@ class Expression(object):
db = self.db db = self.db
return Expression(db, db._adapter.EXTRACT, self, 'second', 'integer') return Expression(db, db._adapter.EXTRACT, self, 'second', 'integer')
def epoch(self):
db = self.db
return Expression(db, db._adapter.EPOCH, self, None, 'integer')
def __getslice__(self, start, stop): def __getslice__(self, start, stop):
db = self.db db = self.db
if start < 0: if start < 0:
+8 -7
View File
@@ -311,12 +311,13 @@ class TestDatetime(unittest.TestCase):
9, 30)), 3) 9, 30)), 3)
self.assertEqual(len(db(db.t.a == datetime.datetime(1971, 12, self.assertEqual(len(db(db.t.a == datetime.datetime(1971, 12,
21, 11, 30)).select()), 1) 21, 11, 30)).select()), 1)
self.assertEqual(len(db(db.t.a.year() == 1971).select()), 2) self.assertEqual(db(db.t.a.year() == 1971).count(), 2)
self.assertEqual(len(db(db.t.a.month() == 12).select()), 2) self.assertEqual(db(db.t.a.month() == 12).count(), 2)
self.assertEqual(len(db(db.t.a.day() == 21).select()), 3) self.assertEqual(db(db.t.a.day() == 21).count(), 3)
self.assertEqual(len(db(db.t.a.hour() == 11).select()), 1) self.assertEqual(db(db.t.a.hour() == 11).count(), 1)
self.assertEqual(len(db(db.t.a.minutes() == 30).select()), 3) self.assertEqual(db(db.t.a.minutes() == 30).count(), 3)
self.assertEqual(len(db(db.t.a.seconds() == 0).select()), 3) self.assertEqual(db(db.t.a.seconds() == 0).count(), 3)
self.assertEqual(db(db.t.a.epoch()<365*24*3600).count(),1)
db.t.drop() db.t.drop()
@@ -329,7 +330,7 @@ class TestExpressions(unittest.TestCase):
self.assertEqual(db.t.insert(a=2), 2) self.assertEqual(db.t.insert(a=2), 2)
self.assertEqual(db.t.insert(a=3), 3) self.assertEqual(db.t.insert(a=3), 3)
self.assertEqual(db(db.t.a == 3).update(a=db.t.a + 1), 1) self.assertEqual(db(db.t.a == 3).update(a=db.t.a + 1), 1)
self.assertEqual(len(db(db.t.a == 4).select()), 1) self.assertEqual(db(db.t.a == 4).count(), 1)
db.t.drop() db.t.drop()