Commits

Anonymous committed 63d17ca

added back all the stuff that r44 accidentily removed

Comments (0)

Files changed (1)

         """
         qn = connection.ops.quote_name
         field.set_attributes_from_name(field_name)
+        
+        # hook for the field to do any resolution prior to it's attributes being queried
+        if hasattr(field, 'south_init'):
+            field.south_init()
+        
         sql = field.db_type()
         if not sql:
             return None
         # if the field is "NOT NULL" and a default value is provided, create the column with it
         # this allows the addition of a NOT NULL field to a table with existing rows
         if not field.null and field.has_default():
+            default = field.get_default()
+            if isinstance(default, basestring):
+                default = "'%s'" % default
             sql += " DEFAULT %s"
-            sqlparams = (field.get_default())
+            sqlparams = (default)
         
         if field.rel:
             self.add_deferred_sql(
                 )
             )
             
+        if field.db_index:
+            self.add_deferred_sql(self.create_index_sql(table_name, [field.column]))
+            
         return sql % sqlparams
         
     def foreign_key_sql(self, from_table_name, from_column_name, to_table_name, to_column_name):
             connection.ops.deferrable_sql() # Django knows this
         )
         
+    def create_index_name(self, table_name, column_names):
+        """
+        Generate a unique name for the index
+        """
+        index_unique_name = ''
+        if len(column_names) > 1:
+            index_unique_name = '_%x' % abs(hash((table_name, ','.join(column_names))))
 
+        return '%s_%s%s' % (table_name, column_names[0], index_unique_name)
+
+    def create_index_sql(self, table_name, column_names, unique=False, db_tablespace=''):
+        """
+        Generates a create index statement on 'table_name' for a list of 'column_names'
+        """
+        if not column_names:
+            print "No column names supplied on which to create an index"
+            return ''
+            
+        if db_tablespace and connection.features.supports_tablespaces:
+            tablespace_sql = ' ' + connection.ops.tablespace_sql(db_tablespace)
+        else:
+            tablespace_sql = ''
+        
+        index_name = self.create_index_name(table_name, column_names)
+        qn = connection.ops.quote_name
+        return 'CREATE %sINDEX %s ON %s (%s)%s;' % (
+            unique and 'UNIQUE ' or '',
+            index_name,
+            table_name,
+            ','.join([qn(field) for field in column_names]),
+            tablespace_sql
+            )
+        
+    def create_index(self, table_name, column_names, unique=False, db_tablespace=''):
+        """ Executes a create index statement """
+        sql = self.create_index_sql(table_name, column_names, unique, db_tablespace)
+        self.execute(sql)
+        
 
     def delete_column(self, table_name, name):
         """