48 return result |
51 return result |
49 else: |
52 else: |
50 return tuple([smart_str(p, self.charset, True) for p in params]) |
53 return tuple([smart_str(p, self.charset, True) for p in params]) |
51 |
54 |
52 def execute(self, sql, params=()): |
55 def execute(self, sql, params=()): |
53 return self.cursor.execute(smart_str(sql, self.charset), self.format_params(params)) |
56 try: |
|
57 return self.cursor.execute(smart_str(sql, self.charset), self.format_params(params)) |
|
58 except Database.IntegrityError, e: |
|
59 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] |
|
60 except Database.DatabaseError, e: |
|
61 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] |
54 |
62 |
55 def executemany(self, sql, param_list): |
63 def executemany(self, sql, param_list): |
56 new_param_list = [self.format_params(params) for params in param_list] |
64 try: |
57 return self.cursor.executemany(sql, new_param_list) |
65 new_param_list = [self.format_params(params) for params in param_list] |
|
66 return self.cursor.executemany(sql, new_param_list) |
|
67 except Database.IntegrityError, e: |
|
68 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] |
|
69 except Database.DatabaseError, e: |
|
70 raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] |
58 |
71 |
59 def __getattr__(self, attr): |
72 def __getattr__(self, attr): |
60 if attr in self.__dict__: |
73 if attr in self.__dict__: |
61 return self.__dict__[attr] |
74 return self.__dict__[attr] |
62 else: |
75 else: |
63 return getattr(self.cursor, attr) |
76 return getattr(self.cursor, attr) |
64 |
77 |
65 def __iter__(self): |
78 def __iter__(self): |
66 return iter(self.cursor) |
79 return iter(self.cursor.fetchall()) |
67 |
80 |
68 class DatabaseFeatures(BaseDatabaseFeatures): |
81 class DatabaseFeatures(BaseDatabaseFeatures): |
69 uses_savepoints = True |
82 uses_savepoints = True |
70 |
83 |
71 class DatabaseWrapper(BaseDatabaseWrapper): |
84 class DatabaseWrapper(BaseDatabaseWrapper): |
87 } |
100 } |
88 |
101 |
89 def __init__(self, *args, **kwargs): |
102 def __init__(self, *args, **kwargs): |
90 super(DatabaseWrapper, self).__init__(*args, **kwargs) |
103 super(DatabaseWrapper, self).__init__(*args, **kwargs) |
91 |
104 |
|
105 import warnings |
|
106 warnings.warn( |
|
107 'The "postgresql" backend has been deprecated. Use "postgresql_psycopg2" instead.', |
|
108 PendingDeprecationWarning |
|
109 ) |
|
110 |
92 self.features = DatabaseFeatures() |
111 self.features = DatabaseFeatures() |
93 self.ops = DatabaseOperations() |
112 self.ops = DatabaseOperations(self) |
94 self.client = DatabaseClient(self) |
113 self.client = DatabaseClient(self) |
95 self.creation = DatabaseCreation(self) |
114 self.creation = DatabaseCreation(self) |
96 self.introspection = DatabaseIntrospection(self) |
115 self.introspection = DatabaseIntrospection(self) |
97 self.validation = BaseDatabaseValidation() |
116 self.validation = BaseDatabaseValidation(self) |
98 |
117 |
99 def _cursor(self): |
118 def _cursor(self): |
|
119 new_connection = False |
100 set_tz = False |
120 set_tz = False |
101 settings_dict = self.settings_dict |
121 settings_dict = self.settings_dict |
102 if self.connection is None: |
122 if self.connection is None: |
103 set_tz = True |
123 new_connection = True |
104 if settings_dict['DATABASE_NAME'] == '': |
124 set_tz = settings_dict.get('TIME_ZONE') |
|
125 if settings_dict['NAME'] == '': |
105 from django.core.exceptions import ImproperlyConfigured |
126 from django.core.exceptions import ImproperlyConfigured |
106 raise ImproperlyConfigured("You need to specify DATABASE_NAME in your Django settings file.") |
127 raise ImproperlyConfigured("You need to specify NAME in your Django settings file.") |
107 conn_string = "dbname=%s" % settings_dict['DATABASE_NAME'] |
128 conn_string = "dbname=%s" % settings_dict['NAME'] |
108 if settings_dict['DATABASE_USER']: |
129 if settings_dict['USER']: |
109 conn_string = "user=%s %s" % (settings_dict['DATABASE_USER'], conn_string) |
130 conn_string = "user=%s %s" % (settings_dict['USER'], conn_string) |
110 if settings_dict['DATABASE_PASSWORD']: |
131 if settings_dict['PASSWORD']: |
111 conn_string += " password='%s'" % settings_dict['DATABASE_PASSWORD'] |
132 conn_string += " password='%s'" % settings_dict['PASSWORD'] |
112 if settings_dict['DATABASE_HOST']: |
133 if settings_dict['HOST']: |
113 conn_string += " host=%s" % settings_dict['DATABASE_HOST'] |
134 conn_string += " host=%s" % settings_dict['HOST'] |
114 if settings_dict['DATABASE_PORT']: |
135 if settings_dict['PORT']: |
115 conn_string += " port=%s" % settings_dict['DATABASE_PORT'] |
136 conn_string += " port=%s" % settings_dict['PORT'] |
116 self.connection = Database.connect(conn_string, **settings_dict['DATABASE_OPTIONS']) |
137 self.connection = Database.connect(conn_string, **settings_dict['OPTIONS']) |
117 self.connection.set_isolation_level(1) # make transactions transparent to all cursors |
138 self.connection.set_isolation_level(1) # make transactions transparent to all cursors |
118 connection_created.send(sender=self.__class__) |
139 connection_created.send(sender=self.__class__) |
119 cursor = self.connection.cursor() |
140 cursor = self.connection.cursor() |
120 if set_tz: |
141 if new_connection: |
121 cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']]) |
142 if set_tz: |
|
143 cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']]) |
122 if not hasattr(self, '_version'): |
144 if not hasattr(self, '_version'): |
123 self.__class__._version = get_version(cursor) |
145 self.__class__._version = get_version(cursor) |
124 if self._version[0:2] < (8, 0): |
146 if self._version[0:2] < (8, 0): |
125 # No savepoint support for earlier version of PostgreSQL. |
147 # No savepoint support for earlier version of PostgreSQL. |
126 self.features.uses_savepoints = False |
148 self.features.uses_savepoints = False |
127 cursor.execute("SET client_encoding to 'UNICODE'") |
149 cursor.execute("SET client_encoding to 'UNICODE'") |
128 cursor = UnicodeCursorWrapper(cursor, 'utf-8') |
150 return UnicodeCursorWrapper(cursor, 'utf-8') |
129 return cursor |
|
130 |
151 |
131 def typecast_string(s): |
152 def typecast_string(s): |
132 """ |
153 """ |
133 Cast all returned strings to unicode strings. |
154 Cast all returned strings to unicode strings. |
134 """ |
155 """ |
140 # in Python's native (standard-library) datetime/time format, whereas psycopg |
161 # in Python's native (standard-library) datetime/time format, whereas psycopg |
141 # use mx.DateTime by default. |
162 # use mx.DateTime by default. |
142 try: |
163 try: |
143 Database.register_type(Database.new_type((1082,), "DATE", util.typecast_date)) |
164 Database.register_type(Database.new_type((1082,), "DATE", util.typecast_date)) |
144 except AttributeError: |
165 except AttributeError: |
145 raise Exception("You appear to be using psycopg version 2. Set your DATABASE_ENGINE to 'postgresql_psycopg2' instead of 'postgresql'.") |
166 raise Exception("You appear to be using psycopg version 2. Set your DATABASES.ENGINE to 'postgresql_psycopg2' instead of 'postgresql'.") |
146 Database.register_type(Database.new_type((1083,1266), "TIME", util.typecast_time)) |
167 Database.register_type(Database.new_type((1083,1266), "TIME", util.typecast_time)) |
147 Database.register_type(Database.new_type((1114,1184), "TIMESTAMP", util.typecast_timestamp)) |
168 Database.register_type(Database.new_type((1114,1184), "TIMESTAMP", util.typecast_timestamp)) |
148 Database.register_type(Database.new_type((16,), "BOOLEAN", util.typecast_boolean)) |
169 Database.register_type(Database.new_type((16,), "BOOLEAN", util.typecast_boolean)) |
149 Database.register_type(Database.new_type((1700,), "NUMERIC", util.typecast_decimal)) |
170 Database.register_type(Database.new_type((1700,), "NUMERIC", util.typecast_decimal)) |
150 Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string)) |
171 Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string)) |