ticket_mule icon indicating copy to clipboard operation
ticket_mule copied to clipboard

Database agnostic query?

Open ghost opened this issue 14 years ago • 1 comments

It would be nice to have these two queries work in all three db flavors (MySQL, SQLite and Postgresql). In the meantime, here are the queries that work for the associated databases:

Postgresql

Ticket.timeline_opened_tickets: self.count(:group => 'date(created_at)', :having => ['date(created_at) >= ? and date(created_at) <= ?', (Time.zone.now.beginning_of_day - 30.days).to_date.to_s, (Time.zone.now.end_of_day - 1.day).to_date.to_s])

Ticket.timeline_closed_tickets: self.count(:group => 'date(closed_at)', :having => ['date(closed_at) >= ? and date(closed_at) <= ?', (Time.zone.now.beginning_of_day - 30.days).to_date.to_s, (Time.zone.now.end_of_day - 1.day).to_date.to_s])

MySQL & SQLite

Ticket.timeline_opened_tickets: self.count(:group => 'date(created_at)', :having => ['date_created_at >= ? and date_created_at <= ?', (Time.zone.now.beginning_of_day - 30.days).to_date.to_s, (Time.zone.now.end_of_day - 1.day).to_date.to_s])

Ticket.timeline_closed_tickets: self.count(:group => 'date(closed_at)', :having => ['date_closed_at >= ? and date_closed_at <= ?', (Time.zone.now.beginning_of_day - 30.days).to_date.to_s, (Time.zone.now.end_of_day - 1.day).to_date.to_s])

ghost avatar Jan 22 '11 02:01 ghost

Could you detect the type of Database Adapter being used and use alternate queries?

postmodern avatar May 14 '11 22:05 postmodern