I have some reservation with start and end (reference to resource is remove to make example more clear):
class Reservation(db.Model):
fromHour = db.DateTimeProperty()
toHour = db.DateTimeProperty()
fromToRange = db.ComputedProperty(lambda x: [x.fromHour, x.toHour])
And want to add another reservation with check if it not overlaps previous one - how to express such query in Google App Engine.
1st I try this query with list property but double inequality filters not works. It should do two matches from1 < to2 and from1 >= from2 and one results - whatever it could be costly if there will be more data.
fromHour = datetime.datetime(2012, 04, 18, 0, 0, 0)
toHour = datetime.datetime(2012, 04, 18, 2, 0, 0)
reservation = Reservation(fromHour = fromHour, toHour = toHour, colors = ['white', 'black'])
reservation.put()
self.response.out.write('<p>Both %s</p>' % fromHour)
self.response.out.write('<ol>')
for reservation in Reservation.all()\
.filter('fromToRange >', fromHour)\
.filter('fromToRange <=', fromHour):
self.response.out.write('<li>%s</li>' % (reservation.fromToRange))
self.response.out.write('</ol>')
I found another solution that I could use additional property containing days (this will be list of days in range per reservation) than I could hit days need to be checked to narrow scan of data and check every record if not overlapping new reservation.
Please help and provide some answer how to do optimal query to detect overlapping reservations - maybe there is quick 3rd solution for time ranges queries in Google App Engine or is not supported.