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: try: _our_backend = backend.DatabaseOperations(connection) except TypeError: _our_backend = backend.DatabaseOperations() return _our_backend.quote_name(value)
def get_operations_and_introspection_classes(style): from django.db import backend, connection try: # v0.96 compatibility v0_96_quote_name = backend.quote_name class dummy: pass setattr(connection, 'ops', dummy()) setattr(connection.ops, 'quote_name', v0_96_quote_name) except: pass backend_type = str(connection.__class__).split('.')[3] if backend_type == 'mysql': import deseb.backends.mysql as backend elif backend_type == 'postgresql': import deseb.backends.postgresql as backend elif backend_type == 'postgresql_psycopg2': import deseb.backends.postgresql_psycopg2 as backend elif backend_type == 'sqlite3': import deseb.backends.sqlite3 as backend else: raise Exception('backend ' + backend_type + ' not supported yet - sorry!') ops = backend.DatabaseOperations(connection, style) introspection = backend.DatabaseIntrospection(connection) return ops, introspection
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 set_sequence(models): # postgresql: reset sequence for model in models: cursor = connection.cursor() autofields = [field for field in model._meta.fields if isinstance(field, AutoField)] for f in autofields: seq = backend.DatabaseOperations(connection).quote_name('%s_%s_seq' % (model._meta.db_table, f.name)) cursor.execute("SELECT count(%s) from %s;" % (f.name, model._meta.db_table)) nb = cursor.fetchall()[0][0] cursor.execute('ALTER SEQUENCE %s RESTART WITH %d;' % (seq, nb + 1))
def archive(request): base_qs = Entry.objects.filter(published=True) in_archive = base_qs.aggregate(count=Count('pk'), max=Max('pk'), min=Min('pk'), avg=Avg('pk')) month = backend.DatabaseOperations(connections).date_trunc_sql('month', 'created') per_month_count = base_qs.extra({'date':month}).values('date').annotate(count=Count('pk')).order_by('date') ctx = { 'in_archive':in_archive, 'per_month_count':per_month_count } return render_to_response('homepage/archive.html', ctx, context_instance=RequestContext(request))
def archive(request, month=None): base_qs = Entry.objects.published_entries() in_archive_count = base_qs.count() page = None if month == None: month = backend.DatabaseOperations(connections).date_trunc_sql( 'month', 'created') per_month_count = base_qs.extra({ 'date': month }).values('date').annotate(count=Count('pk')).order_by('date') month = '' year = '' else: per_month_count = [] today = datetime.datetime.today() year = today.year entries = base_qs.filter(created__year=today.year, created__month=today.month) paginator = Paginator(entries, 5) # get page number from url ?page=# page_num = request.GET.get('page', 1) try: page = paginator.page(page_num) except EmptyPage: page = paginator.page(paginator.num_pages) except PageNotAnInteger: page = paginator.page(1) ctx = { 'page': page, 'month': month, 'year': year, "in_archive_count": in_archive_count, "per_month_count": per_month_count, } return render_to_response('archive.html', ctx, context_instance=RequestContext(request))
def quote_name(s): from django.db import backend if quote_flag == 'old': return backend.quote_name(s) else: return backend.DatabaseOperations().quote_name(s)
def quote_name(name): """Quote database field or table name.""" return backend.DatabaseOperations().quote_name(name)
def col(model, field): qn = backend.DatabaseOperations().quote_name return '%s.%s' % (qn( model._meta.db_table), qn(model._meta.get_field(field).column))