Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refs #4299 -- Removed some code duplication in the postgresql introsp…

…ection modules by letting the `postgresql_psycopg2` backend use identical functions from the `postgresql` backend.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6165 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 30b24a6ccece2131aa2fd931ac157cda5a2bb1b6 1 parent e70d7e6
Gary Wilson Jr. authored September 14, 2007
63  django/db/backends/postgresql_psycopg2/introspection.py
... ...
@@ -1,23 +1,8 @@
1 1
 from django.db.backends.postgresql_psycopg2.base import DatabaseOperations
  2
+from django.db.backends.postgresql.introspection import get_table_list, get_table_description, get_indexes, DATA_TYPES_REVERSE
2 3
 
3 4
 quote_name = DatabaseOperations().quote_name
4 5
 
5  
-def get_table_list(cursor):
6  
-    "Returns a list of table names in the current database."
7  
-    cursor.execute("""
8  
-        SELECT c.relname
9  
-        FROM pg_catalog.pg_class c
10  
-        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
11  
-        WHERE c.relkind IN ('r', 'v', '')
12  
-            AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
13  
-            AND pg_catalog.pg_table_is_visible(c.oid)""")
14  
-    return [row[0] for row in cursor.fetchall()]
15  
-
16  
-def get_table_description(cursor, table_name):
17  
-    "Returns a description of the table, with the DB-API cursor.description interface."
18  
-    cursor.execute("SELECT * FROM %s LIMIT 1" % quote_name(table_name))
19  
-    return cursor.description
20  
-
21 6
 def get_relations(cursor, table_name):
22 7
     """
23 8
     Returns a dictionary of {field_index: (field_index_other_table, other_table)}
@@ -35,49 +20,3 @@ def get_relations(cursor, table_name):
35 20
         # row[0] and row[1] are single-item lists, so grab the single item.
36 21
         relations[row[0][0] - 1] = (row[1][0] - 1, row[2])
37 22
     return relations
38  
-
39  
-def get_indexes(cursor, table_name):
40  
-    """
41  
-    Returns a dictionary of fieldname -> infodict for the given table,
42  
-    where each infodict is in the format:
43  
-        {'primary_key': boolean representing whether it's the primary key,
44  
-         'unique': boolean representing whether it's a unique index}
45  
-    """
46  
-    # This query retrieves each index on the given table, including the
47  
-    # first associated field name
48  
-    cursor.execute("""
49  
-        SELECT attr.attname, idx.indkey, idx.indisunique, idx.indisprimary
50  
-        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
51  
-            pg_catalog.pg_index idx, pg_catalog.pg_attribute attr
52  
-        WHERE c.oid = idx.indrelid
53  
-            AND idx.indexrelid = c2.oid
54  
-            AND attr.attrelid = c.oid
55  
-            AND attr.attnum = idx.indkey[0]
56  
-            AND c.relname = %s""", [table_name])
57  
-    indexes = {}
58  
-    for row in cursor.fetchall():
59  
-        # row[1] (idx.indkey) is stored in the DB as an array. It comes out as
60  
-        # a string of space-separated integers. This designates the field
61  
-        # indexes (1-based) of the fields that have indexes on the table.
62  
-        # Here, we skip any indexes across multiple fields.
63  
-        if ' ' in row[1]:
64  
-            continue
65  
-        indexes[row[0]] = {'primary_key': row[3], 'unique': row[2]}
66  
-    return indexes
67  
-
68  
-# Maps type codes to Django Field types.
69  
-DATA_TYPES_REVERSE = {
70  
-    16: 'BooleanField',
71  
-    21: 'SmallIntegerField',
72  
-    23: 'IntegerField',
73  
-    25: 'TextField',
74  
-    701: 'FloatField',
75  
-    869: 'IPAddressField',
76  
-    1043: 'CharField',
77  
-    1082: 'DateField',
78  
-    1083: 'TimeField',
79  
-    1114: 'DateTimeField',
80  
-    1184: 'DateTimeField',
81  
-    1266: 'TimeField',
82  
-    1700: 'DecimalField',
83  
-}

0 notes on commit 30b24a6

Please sign in to comment.
Something went wrong with that request. Please try again.