web/lib/django/db/backends/postgresql/version.py
changeset 38 77b6da96e6f1
parent 0 0d40e90630ef
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 """
       
     2 Extracts the version of the PostgreSQL server.
       
     3 """
       
     4 
       
     5 import re
       
     6 
       
     7 # This reg-exp is intentionally fairly flexible here.
       
     8 # Needs to be able to handle stuff like:
       
     9 #   PostgreSQL 8.3.6
       
    10 #   EnterpriseDB 8.3
       
    11 #   PostgreSQL 8.3 beta4
       
    12 #   PostgreSQL 8.4beta1
       
    13 VERSION_RE = re.compile(r'\S+ (\d+)\.(\d+)\.?(\d+)?')
       
    14 
       
    15 def _parse_version(text):
       
    16     "Internal parsing method. Factored out for testing purposes."
       
    17     major, major2, minor = VERSION_RE.search(text).groups()
       
    18     try:
       
    19         return int(major), int(major2), int(minor)
       
    20     except (ValueError, TypeError):
       
    21         return int(major), int(major2), None
       
    22 
       
    23 def get_version(cursor):
       
    24     """
       
    25     Returns a tuple representing the major, minor and revision number of the
       
    26     server. For example, (7, 4, 1) or (8, 3, 4). The revision number will be
       
    27     None in the case of initial releases (e.g., 'PostgreSQL 8.3') or in the
       
    28     case of beta and prereleases ('PostgreSQL 8.4beta1').
       
    29     """
       
    30     cursor.execute("SELECT version()")
       
    31     return _parse_version(cursor.fetchone()[0])