diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index e0d3ac5cbe..d8adf29966 100644 --- a/django/db/backends/oracle/introspection.py +++ b/django/db/backends/oracle/introspection.py @@ -1,3 +1,4 @@ +from django.db.backends.oracle.base import quote_name import re foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)") @@ -8,8 +9,10 @@ def get_table_list(cursor): return [row[0].upper() for row in cursor] def get_table_description(cursor, table_name): - return table_name - + "Returns a description of the table, with the DB-API cursor.description interface." + cursor.execute("SELECT * FROM %s WHERE ROWNUM < 2" % quote_name(table_name)) + return cursor.description + def _name_to_index(cursor, table_name): """ Returns a dictionary of {field_name: field_index} for the given table. @@ -31,7 +34,39 @@ def get_indexes(cursor, table_name): {'primary_key': boolean representing whether it's the primary key, 'unique': boolean representing whether it's a unique index} """ - raise NotImplementedError + # This query retrieves each index on the given table, including the + # first associated field name + # "We were in the nick of time; you were in great peril!" + sql = """ +WITH primarycols AS ( + SELECT user_cons_columns.table_name, user_cons_columns.column_name, 1 AS PRIMARYCOL + FROM user_cons_columns, user_constraints + WHERE user_cons_columns.constraint_name = user_constraints.constraint_name AND + user_constraints.constraint_type = 'P' AND + user_cons_columns.table_name = %s), + uniquecols AS ( + SELECT user_ind_columns.table_name, user_ind_columns.column_name, 1 AS UNIQUECOL + FROM user_indexes, user_ind_columns + WHERE uniqueness = 'UNIQUE' AND + user_indexes.index_name = user_ind_columns.index_name AND + user_ind_columns.table_name = %s) +SELECT allcols.column_name, primarycols.primarycol, uniquecols.UNIQUECOL +FROM (SELECT column_name FROM primarycols UNION SELECT column_name FROM +uniquecols) allcols, + primarycols, uniquecols +WHERE allcols.column_name = primarycols.column_name (+) AND + allcols.column_name = uniquecols.column_name (+) + """ + cursor.execute(sql, [table_name, table_name]) + indexes = {} + for row in cursor.fetchall(): + # row[1] (idx.indkey) is stored in the DB as an array. It comes out as + # a string of space-separated integers. This designates the field + # indexes (1-based) of the fields that have indexes on the table. + # Here, we skip any indexes across multiple fields. + indexes[row[0]] = {'primary_key': row[1], 'unique': row[2]} + return indexes + # Maps type codes to Django Field types. DATA_TYPES_REVERSE = {