def recent_ids(self, maxdate=None): """ Returns the ids of most recent interactions for clients as of a date. Arguments: maxdate -- datetime object. Most recent date to pull. (default None) """ from django.db import connections cursor = connections[get_db_label('Reporting')].cursor() cfilter = "expiration is null" sql = 'select ri.id, x.client_id from ' + \ '(select client_id, MAX(timestamp) as timer from ' + \ _quote('Reporting_interaction') if maxdate: if not isinstance(maxdate, datetime): raise ValueError('Expected a datetime object') sql = sql + " where timestamp <= '%s' " % maxdate cfilter = "(expiration is null or expiration > '%s') and creation <= '%s'" % (maxdate, maxdate) sql = sql + ' GROUP BY client_id) x, ' + \ _quote('Reporting_interaction') + \ ' ri where ri.client_id = x.client_id AND' + \ ' ri.timestamp = x.timer and x.client_id in' + \ ' (select id from %s where %s)' % \ (_quote('Reporting_client'), cfilter) try: cursor.execute(sql) return [item[0] for item in cursor.fetchall()] except: '''FIXME - really need some error handling''' pass return []
def recent_ids(self, maxdate=None): """ Returns the ids of most recent interactions for clients as of a date. Arguments: maxdate -- datetime object. Most recent date to pull. (default None) """ from django.db import connections cursor = connections[get_db_label('Reporting')].cursor() cfilter = "expiration is null" sql = 'select ri.id, x.client_id from ' + \ '(select client_id, MAX(timestamp) as timer from ' + \ _quote('Reporting_interaction') if maxdate: if not isinstance(maxdate, datetime): raise ValueError('Expected a datetime object') sql = sql + " where timestamp <= '%s' " % maxdate cfilter = "(expiration is null or expiration > '%s') and creation <= '%s'" % ( maxdate, maxdate) sql = sql + ' GROUP BY client_id) x, ' + \ _quote('Reporting_interaction') + \ ' ri where ri.client_id = x.client_id AND' + \ ' ri.timestamp = x.timer and x.client_id in' + \ ' (select id from %s where %s)' % \ (_quote('Reporting_client'), cfilter) try: cursor.execute(sql) return [item[0] for item in cursor.fetchall()] except: '''FIXME - really need some error handling''' pass return []
def _quote(value): """ Quote a string to use as a table name or column Newer versions and various drivers require an argument https://code.djangoproject.com/ticket/13630 """ global _our_backend if not _our_backend: if django.VERSION[0] == 1 and django.VERSION[1] >= 7: _our_backend = connections[get_db_label('Reporting')].ops else: from django.db import backend try: _our_backend = backend.DatabaseOperations( connections[get_db_label('Reporting')]) except TypeError: _our_backend = backend.DatabaseOperations() return _our_backend.quote_name(value)
def _shove(old_table, new_table, columns): cols = ",".join([_quote(f) for f in columns]) sql = "insert into %s(%s) select %s from %s" % ( _quote(new_table), cols, cols, _quote(old_table)) cursor = connections[get_db_label('Reporting')].cursor() cursor.execute(sql) cursor.close()