Unified construction of WITH SQL in contrib.postgres.indexes.

This commit is contained in:
Tim Graham 2018-01-10 14:59:15 -05:00 committed by GitHub
parent 5bf62825b5
commit db9cd1b37e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 18 deletions

View File

@ -14,6 +14,19 @@ class PostgresIndex(Index):
# indexes. # indexes.
return Index.max_name_length - len(Index.suffix) + len(self.suffix) return Index.max_name_length - len(Index.suffix) + len(self.suffix)
def create_sql(self, model, schema_editor, using=''):
statement = super().create_sql(model, schema_editor, using=' USING %s' % self.suffix)
with_params = self.get_with_params()
if with_params:
statement.parts['extra'] = 'WITH (%s) %s' % (
', '.join(with_params),
statement.parts['extra'],
)
return statement
def get_with_params(self):
return []
class BrinIndex(PostgresIndex): class BrinIndex(PostgresIndex):
suffix = 'brin' suffix = 'brin'
@ -30,13 +43,11 @@ class BrinIndex(PostgresIndex):
kwargs['pages_per_range'] = self.pages_per_range kwargs['pages_per_range'] = self.pages_per_range
return path, args, kwargs return path, args, kwargs
def create_sql(self, model, schema_editor, using=''): def get_with_params(self):
statement = super().create_sql(model, schema_editor, using=' USING brin') with_params = []
if self.pages_per_range is not None: if self.pages_per_range is not None:
statement.parts['extra'] = ' WITH (pages_per_range={})'.format( with_params.append('pages_per_range = %d' % self.pages_per_range)
schema_editor.quote_value(self.pages_per_range) return with_params
) + statement.parts['extra']
return statement
class GinIndex(PostgresIndex): class GinIndex(PostgresIndex):
@ -55,16 +66,13 @@ class GinIndex(PostgresIndex):
kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
return path, args, kwargs return path, args, kwargs
def create_sql(self, model, schema_editor, using=''): def get_with_params(self):
statement = super().create_sql(model, schema_editor, using=' USING gin')
with_params = [] with_params = []
if self.gin_pending_list_limit is not None: if self.gin_pending_list_limit is not None:
with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit) with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
if self.fastupdate is not None: if self.fastupdate is not None:
with_params.append('fastupdate = {}'.format('on' if self.fastupdate else 'off')) with_params.append('fastupdate = %s' % ('on' if self.fastupdate else 'off'))
if with_params: return with_params
statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
return statement
class GistIndex(PostgresIndex): class GistIndex(PostgresIndex):
@ -83,13 +91,10 @@ class GistIndex(PostgresIndex):
kwargs['fillfactor'] = self.fillfactor kwargs['fillfactor'] = self.fillfactor
return path, args, kwargs return path, args, kwargs
def create_sql(self, model, schema_editor): def get_with_params(self):
statement = super().create_sql(model, schema_editor, using=' USING gist')
with_params = [] with_params = []
if self.buffering is not None: if self.buffering is not None:
with_params.append('buffering = {}'.format('on' if self.buffering else 'off')) with_params.append('buffering = %s' % ('on' if self.buffering else 'off'))
if self.fillfactor is not None: if self.fillfactor is not None:
with_params.append('fillfactor = %s' % self.fillfactor) with_params.append('fillfactor = %s' % self.fillfactor)
if with_params: return with_params
statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
return statement