| | 1 | """ |
| | 2 | Firebird database backend for Django. |
| | 3 | |
| | 4 | Requires KInterbasDB 3.2: http://kinterbasdb.sourceforge.net/ |
| | 5 | The egenix mx (mx.DateTime) is NOT required |
| | 6 | |
| | 7 | Database charset should be UNICODE_FSS or UTF8 (FireBird 2.0+) |
| | 8 | To use UTF8 encoding add FIREBIRD_CHARSET = 'UTF8' to your settings.py |
| | 9 | UNICODE_FSS works with all versions and uses less memory |
| | 10 | """ |
| | 11 | |
| | 12 | from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util |
| | 13 | |
| | 14 | try: |
| | 15 | import kinterbasdb as Database |
| | 16 | except ImportError, e: |
| | 17 | from django.core.exceptions import ImproperlyConfigured |
| | 18 | raise ImproperlyConfigured, "Error loading KInterbasDB module: %s" % e |
| | 19 | |
| | 20 | DatabaseError = Database.DatabaseError |
| | 21 | IntegrityError = Database.IntegrityError |
| | 22 | |
| | 23 | class DatabaseFeatures(BaseDatabaseFeatures): |
| | 24 | needs_datetime_string_cast = False |
| | 25 | needs_default_null = True |
| | 26 | needs_upper_for_iops = True |
| | 27 | supports_constraints = False #some tests went strange without it |
| | 28 | uses_custom_icontains = True #CONTAINING <value> op instead of LIKE %<value>% |
| | 29 | uses_custom_startswith = True #STARTING WITH op. Faster than LIKE |
| | 30 | uses_custom_queryset = True |
| | 31 | |
| | 32 | class DatabaseOperations(BaseDatabaseOperations): |
| | 33 | _max_name_length = 31 |
| | 34 | def __init__(self): |
| | 35 | self._firebird_version = None |
| | 36 | |
| | 37 | def get_generator_name(self, name): |
| | 38 | return '%s_G' % util.truncate_name(name, self._max_name_length-2).upper() |
| | 39 | |
| | 40 | def get_trigger_name(self, name): |
| | 41 | return '%s_T' % util.truncate_name(name, self._max_name_length-2).upper() |
| | 42 | |
| | 43 | def _get_firebird_version(self): |
| | 44 | if self._firebird_version is None: |
| | 45 | from django.db import connection |
| | 46 | self._firebird_version = [int(val) for val in connection.server_version.split()[-1].split('.')] |
| | 47 | return self._firebird_version |
| | 48 | firebird_version = property(_get_firebird_version) |
| | 49 | |
| | 50 | def _autoinc_sql_with_style(self, style, table_name, column_name): |
| | 51 | """ |
| | 52 | To simulate auto-incrementing primary keys in Firebird, we have to |
| | 53 | create a generator and a trigger. |
| | 54 | |
| | 55 | Create the generators and triggers names based only on table name |
| | 56 | since django only support one auto field per model |
| | 57 | """ |
| | 58 | |
| | 59 | KWD = style.SQL_KEYWORD |
| | 60 | TBL = style.SQL_TABLE |
| | 61 | FLD = style.SQL_FIELD |
| | 62 | |
| | 63 | generator_name = self.get_generator_name(table_name) |
| | 64 | trigger_name = self.get_trigger_name(table_name) |
| | 65 | column_name = self.quote_name(column_name) |
| | 66 | table_name = self.quote_name(table_name) |
| | 67 | |
| | 68 | generator_sql = "%s %s;" % ( KWD('CREATE GENERATOR'), |
| | 69 | TBL(generator_name)) |
| | 70 | trigger_sql = "\n".join([ |
| | 71 | "%s %s %s %s" % ( \ |
| | 72 | KWD('CREATE TRIGGER'), TBL(trigger_name), KWD('FOR'), |
| | 73 | TBL(table_name)), |
| | 74 | "%s 0 %s" % (KWD('ACTIVE BEFORE INSERT POSITION'), KWD('AS')), |
| | 75 | KWD('BEGIN'), |
| | 76 | " %s ((%s.%s %s) %s (%s.%s = 0)) %s" % ( \ |
| | 77 | KWD('IF'), |
| | 78 | KWD('NEW'), FLD(column_name), KWD('IS NULL'), |
| | 79 | KWD('OR'), KWD('NEW'), FLD(column_name), |
| | 80 | KWD('THEN') |
| | 81 | ), |
| | 82 | " %s" % KWD('BEGIN'), |
| | 83 | " %s.%s = %s(%s, 1);" % ( \ |
| | 84 | KWD('NEW'), FLD(column_name), |
| | 85 | KWD('GEN_ID'), TBL(generator_name) |
| | 86 | ), |
| | 87 | " %s" % KWD('END'), |
| | 88 | KWD('END') |
| | 89 | ]) |
| | 90 | return (generator_sql, trigger_sql) |
| | 91 | |
| | 92 | def autoinc_sql(self, table_name, column_name): |
| | 93 | # style argument disappeared, so we'll just import django's dummy |
| | 94 | from django.core.management.color import no_style, color_style |
| | 95 | return self._autoinc_sql_with_style(no_style(), table_name, column_name) |
| | 96 | |
| | 97 | def max_name_length(self): |
| | 98 | return self._max_name_length |
| | 99 | |
| | 100 | def query_set_class(self, DefaultQuerySet): |
| | 101 | from django.db import connection |
| | 102 | from django.db.models.query import EmptyResultSet, GET_ITERATOR_CHUNK_SIZE, quote_only_if_word |
| | 103 | |
| | 104 | class FirebirdQuerySet(DefaultQuerySet): |
| | 105 | #TODO: Optimize for Firebird and take full advanatage of its power |
| | 106 | # Now it's just a copy of django.db.models.query._QuerySet |
| | 107 | # with LIMIT/OFFSET removed and FIRST/SKIP added |
| | 108 | def _get_sql_clause(self): |
| | 109 | from django.db.models.query import SortedDict, handle_legacy_orderlist, orderfield2column, fill_table_cache |
| | 110 | qn = connection.ops.quote_name |
| | 111 | opts = self.model._meta |
| | 112 | |
| | 113 | # Construct the fundamental parts of the query: SELECT X FROM Y WHERE Z. |
| | 114 | select = ["%s.%s" % (qn(opts.db_table), qn(f.column)) for f in opts.fields] |
| | 115 | tables = [quote_only_if_word(t) for t in self._tables] |
| | 116 | joins = SortedDict() |
| | 117 | where = self._where[:] |
| | 118 | params = self._params[:] |
| | 119 | |
| | 120 | # Convert self._filters into SQL. |
| | 121 | joins2, where2, params2 = self._filters.get_sql(opts) |
| | 122 | joins.update(joins2) |
| | 123 | where.extend(where2) |
| | 124 | params.extend(params2) |
| | 125 | |
| | 126 | # Add additional tables and WHERE clauses based on select_related. |
| | 127 | if self._select_related: |
| | 128 | fill_table_cache(opts, select, tables, where, |
| | 129 | old_prefix=opts.db_table, |
| | 130 | cache_tables_seen=[opts.db_table], |
| | 131 | max_depth=self._max_related_depth) |
| | 132 | |
| | 133 | # Add any additional SELECTs. |
| | 134 | if self._select: |
| | 135 | select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), qn(s[0])) for s in self._select.items()]) |
| | 136 | |
| | 137 | # Start composing the body of the SQL statement. |
| | 138 | sql = [" FROM", qn(opts.db_table)] |
| | 139 | |
| | 140 | # Compose the join dictionary into SQL describing the joins. |
| | 141 | if joins: |
| | 142 | sql.append(" ".join(["%s %s AS %s ON %s" % (join_type, table, alias, condition) |
| | 143 | for (alias, (table, join_type, condition)) in joins.items()])) |
| | 144 | |
| | 145 | # Compose the tables clause into SQL. |
| | 146 | if tables: |
| | 147 | sql.append(", " + ", ".join(tables)) |
| | 148 | |
| | 149 | # Compose the where clause into SQL. |
| | 150 | if where: |
| | 151 | sql.append(where and "WHERE " + " AND ".join(where)) |
| | 152 | |
| | 153 | # ORDER BY clause |
| | 154 | order_by = [] |
| | 155 | if self._order_by is not None: |
| | 156 | ordering_to_use = self._order_by |
| | 157 | else: |
| | 158 | ordering_to_use = opts.ordering |
| | 159 | for f in handle_legacy_orderlist(ordering_to_use): |
| | 160 | if f == '?': # Special case. |
| | 161 | order_by.append(connection.ops.random_function_sql()) |
| | 162 | else: |
| | 163 | if f.startswith('-'): |
| | 164 | col_name = f[1:] |
| | 165 | order = "DESC" |
| | 166 | else: |
| | 167 | col_name = f |
| | 168 | order = "ASC" |
| | 169 | if "." in col_name: |
| | 170 | table_prefix, col_name = col_name.split('.', 1) |
| | 171 | table_prefix = qn(table_prefix) + '.' |
| | 172 | else: |
| | 173 | # Use the database table as a column prefix if it wasn't given, |
| | 174 | # and if the requested column isn't a custom SELECT. |
| | 175 | if "." not in col_name and col_name not in (self._select or ()): |
| | 176 | table_prefix = qn(opts.db_table) + '.' |
| | 177 | else: |
| | 178 | table_prefix = '' |
| | 179 | order_by.append('%s%s %s' % (table_prefix, qn(orderfield2column(col_name, opts)), order)) |
| | 180 | if order_by: |
| | 181 | sql.append("ORDER BY " + ", ".join(order_by)) |
| | 182 | |
| | 183 | return select, " ".join(sql), params |
| | 184 | |
| | 185 | def iterator(self): |
| | 186 | "Performs the SELECT database lookup of this QuerySet." |
| | 187 | from django.db.models.query import get_cached_row |
| | 188 | |
| | 189 | try: |
| | 190 | select, sql, params = self._get_sql_clause() |
| | 191 | except EmptyResultSet: |
| | 192 | raise StopIteration |
| | 193 | |
| | 194 | # self._select is a dictionary, and dictionaries' key order is |
| | 195 | # undefined, so we convert it to a list of tuples. |
| | 196 | extra_select = self._select.items() |
| | 197 | |
| | 198 | cursor = connection.cursor() |
| | 199 | limit_offset_before = "" |
| | 200 | if self._limit is not None: |
| | 201 | limit_offset_before += "FIRST %s " % self._limit |
| | 202 | if self._offset: |
| | 203 | limit_offset_before += "SKIP %s " % self._offset |
| | 204 | else: |
| | 205 | assert self._offset is None, "'offset' is not allowed without 'limit'" |
| | 206 | cursor.execute("SELECT " + limit_offset_before + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) |
| | 207 | fill_cache = self._select_related |
| | 208 | fields = self.model._meta.fields |
| | 209 | index_end = len(fields) |
| | 210 | has_resolve_columns = hasattr(self, 'resolve_columns') |
| | 211 | while 1: |
| | 212 | rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) |
| | 213 | if not rows: |
| | 214 | raise StopIteration |
| | 215 | for row in rows: |
| | 216 | if has_resolve_columns: |
| | 217 | row = self.resolve_columns(row, fields) |
| | 218 | if fill_cache: |
| | 219 | obj, index_end = get_cached_row(klass=self.model, row=row, |
| | 220 | index_start=0, max_depth=self._max_related_depth) |
| | 221 | else: |
| | 222 | obj = self.model(*row[:index_end]) |
| | 223 | for i, k in enumerate(extra_select): |
| | 224 | setattr(obj, k[0], row[index_end+i]) |
| | 225 | yield obj |
| | 226 | return FirebirdQuerySet |
| | 227 | |
| | 228 | |
| | 229 | def quote_name(self, name): |
| | 230 | #Trancate and quote once. |
| | 231 | #Generally works without upper() but some tests fail? without it |
| | 232 | #So let it be like in oracle backend |
| | 233 | if not name.startswith('"') and not name.endswith('"'): |
| | 234 | name = '"%s"' % util.truncate_name(name, self._max_name_length) |
| | 235 | return name.upper() |
| | 236 | |
| | 237 | def last_insert_id(self, cursor, table_name, pk_name): |
| | 238 | stmt = 'SELECT GEN_ID(%s, 0) from RDB$DATABASE' |
| | 239 | cursor.execute(stmt % self.get_generator_name(table_name)) |
| | 240 | return cursor.fetchone()[0] |
| | 241 | |
| | 242 | def date_extract_sql(self, lookup_type, column_name): |
| | 243 | # lookup_type is 'year', 'month', 'day' |
| | 244 | return "EXTRACT(%s FROM %s)" % (lookup_type, column_name) |
| | 245 | |
| | 246 | def date_trunc_sql(self, lookup_type, column_name): |
| | 247 | if lookup_type == 'year': |
| | 248 | sql = "EXTRACT(year FROM %s)||'-01-01 00:00:00'" % column_name |
| | 249 | elif lookup_type == 'month': |
| | 250 | sql = "EXTRACT(year FROM %s)||'-'||EXTRACT(month FROM %s)||'-01 00:00:00'" % (column_name, column_name) |
| | 251 | elif lookup_type == 'day': |
| | 252 | sql = "EXTRACT(year FROM %s)||'-'||EXTRACT(month FROM %s)||'-'||EXTRACT(day FROM %s)||' 00:00:00'" % (column_name, column_name, column_name) |
| | 253 | return "CAST(%s AS TIMESTAMP)" % sql |
| | 254 | |
| | 255 | def cascade_delete_update_sql(self): |
| | 256 | # Solves FK problems with sql_flush |
| | 257 | return " ON DELETE CASCADE ON UPDATE CASCADE" |
| | 258 | |
| | 259 | def datetime_cast_sql(self): |
| | 260 | return None |
| | 261 | |
| | 262 | def limit_offset_sql(self, limit, offset=None): |
| | 263 | # limits are handled in custom FirebirdQuerySet |
| | 264 | assert False, 'Limits are handled in a different way in Firebird' |
| | 265 | return "" |
| | 266 | |
| | 267 | def random_function_sql(self): |
| | 268 | return "rand()" |
| | 269 | |
| | 270 | def pk_default_value(self): |
| | 271 | return "NULL" |
| | 272 | |
| | 273 | def start_transaction_sql(self): |
| | 274 | return "" |
| | 275 | |
| | 276 | def sequence_reset_sql(self, style, model_list): |
| | 277 | from django.db import models |
| | 278 | output = [] |
| | 279 | for model in model_list: |
| | 280 | for f in model._meta.fields: |
| | 281 | if isinstance(f, models.AutoField): |
| | 282 | generator_name = self.get_generator_name(model._meta.db_table) |
| | 283 | output.append("SET GENERATOR %s TO 0;" % generator_name) |
| | 284 | break # Only one AutoField is allowed per model, so don't bother continuing. |
| | 285 | for f in model._meta.many_to_many: |
| | 286 | generator_name = self.get_generator_name(f.m2m_db_table()) |
| | 287 | output.append("SET GENERATOR %s TO 0;" % generator_name) |
| | 288 | return output |
| | 289 | |
| | 290 | def sql_flush(self, style, tables, sequences): |
| | 291 | if tables: |
| | 292 | # FK constraints gave us a lot of trouble with default values |
| | 293 | # that was a reason behind very ugly and dangerous code here |
| | 294 | # Solved with "ON DELETE CASCADE" with all FK references |
| | 295 | sql = ['%s %s %s;' % \ |
| | 296 | (style.SQL_KEYWORD('DELETE'), |
| | 297 | style.SQL_KEYWORD('FROM'), |
| | 298 | style.SQL_FIELD(self.quote_name(table)) |
| | 299 | ) for table in tables] |
| | 300 | for generator_info in sequences: |
| | 301 | table_name = generator_info['table'] |
| | 302 | query = "SET GENERATOR %s TO 0;" % self.get_generator_name(table_name) |
| | 303 | sql.append(query) |
| | 304 | return sql |
| | 305 | else: |
| | 306 | return [] |
| | 307 | |
| | 308 | # def fulltext_search_sql(self, field_name): |
| | 309 | # return field_name + ' CONTAINING %s' |
| | 310 | |
| | 311 | def drop_sequence_sql(self, table): |
| | 312 | return "DROP GENERATOR %s;" % self.get_generator_name(table) |
| | 313 | |
| | 314 | def last_executed_query(self, cursor, sql, params): |
| | 315 | """ |
| | 316 | Returns a string of the query last executed by the given cursor, with |
| | 317 | placeholders replaced with actual values. |
| | 318 | |
| | 319 | `sql` is the raw query containing placeholders, and `params` is the |
| | 320 | sequence of parameters. These are used by default, but this method |
| | 321 | exists for database backends to provide a better implementation |
| | 322 | according to their own quoting schemes. |
| | 323 | """ |
| | 324 | from django.utils.encoding import smart_unicode, force_unicode |
| | 325 | |
| | 326 | # Convert params to contain Unicode values. |
| | 327 | to_unicode = lambda s: force_unicode(s, strings_only=True) |
| | 328 | if isinstance(params, (list, tuple)): |
| | 329 | u_params = tuple([to_unicode(val) for val in params]) |
| | 330 | else: |
| | 331 | u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()]) |
| | 332 | try: |
| | 333 | #Extracts sql right from KInterbasDB's prepared statement |
| | 334 | return smart_unicode(cursor.query) % u_params |
| | 335 | except TypeError: |
| | 336 | return smart_unicode(sql) % u_params |
| | 337 | |
| | 338 | class FirebirdCursorWrapper(object): |
| | 339 | """ |
| | 340 | Django uses "format" ('%s') style placeholders, but firebird uses "qmark" ('?') style. |
| | 341 | This fixes it -- but note that if you want to use a literal "%s" in a query, |
| | 342 | you'll need to use "%%s". |
| | 343 | |
| | 344 | We also do all automatic type conversions here. |
| | 345 | """ |
| | 346 | import kinterbasdb.typeconv_datetime_stdlib as tc_dt |
| | 347 | import kinterbasdb.typeconv_fixed_decimal as tc_fd |
| | 348 | import kinterbasdb.typeconv_text_unicode as tc_tu |
| | 349 | import django.utils.encoding as dj_ue |
| | 350 | |
| | 351 | def timestamp_conv_in(self, timestamp): |
| | 352 | if isinstance(timestamp, basestring): |
| | 353 | #Replaces 6 digits microseconds to 4 digits allowed in Firebird |
| | 354 | timestamp = timestamp[:24] |
| | 355 | return self.tc_dt.timestamp_conv_in(timestamp) |
| | 356 | |
| | 357 | def time_conv_in(self, value): |
| | 358 | import datetime |
| | 359 | if isinstance(value, datetime.datetime): |
| | 360 | value = datetime.time(value.hour, value.minute, value.second, value.micosecond) |
| | 361 | return self.tc_dt.time_conv_in(value) |
| | 362 | |
| | 363 | def ascii_conv_in(self, text): |
| | 364 | return self.dj_eu.smart_str(text, 'ascii') |
| | 365 | |
| | 366 | def unicode_conv_in(self, text): |
| | 367 | return self.tc_tu.unicode_conv_in((self.dj_ue.force_unicode(text[0]), self.FB_CHARSET_CODE)) |
| | 368 | |
| | 369 | def blob_conv_in(self, text): |
| | 370 | return self.tc_tu.unicode_conv_in((self.dj_ue.force_unicode(text), self.FB_CHARSET_CODE)) |
| | 371 | |
| | 372 | def blob_conv_out(self, text): |
| | 373 | return self.tc_tu.unicode_conv_out((text, self.FB_CHARSET_CODE)) |
| | 374 | |
| | 375 | def __init__(self, cursor): |
| | 376 | from django.conf import settings |
| | 377 | self.FB_CHARSET_CODE = 3 #UNICODE_FSS |
| | 378 | if hasattr(settings, 'FIREBIRD_CHARSET'): |
| | 379 | if settings.FIREBIRD_CHARSET == 'UTF8': |
| | 380 | self.FB_CHARSET_CODE = 4 # UTF-8 with Firebird 2.0+ |
| | 381 | self.cursor = cursor |
| | 382 | |
| | 383 | # Prepared Statement |
| | 384 | # http://kinterbasdb.sourceforge.net/dist_docs/usage.html#adv_prepared_statements |
| | 385 | # Need to decide wether they are useful or not |
| | 386 | # Maybe add prepare, execute_prep and executemany_pep methods here |
| | 387 | # and rewrite QuerySet to take advantage of them? |
| | 388 | # Could speed the things up |
| | 389 | self._statement = None |
| | 390 | self.cursor.set_type_trans_in({ |
| | 391 | 'DATE': self.tc_dt.date_conv_in, |
| | 392 | 'TIME': self.time_conv_in, |
| | 393 | 'TIMESTAMP': self.timestamp_conv_in, |
| | 394 | 'FIXED': self.tc_fd.fixed_conv_in_imprecise, |
| | 395 | 'TEXT': self.ascii_conv_in, |
| | 396 | 'TEXT_UNICODE': self.unicode_conv_in, |
| | 397 | 'BLOB': self.blob_conv_in |
| | 398 | }) |
| | 399 | self.cursor.set_type_trans_out({ |
| | 400 | 'DATE': self.tc_dt.date_conv_out, |
| | 401 | 'TIME': self.tc_dt.time_conv_out, |
| | 402 | 'TIMESTAMP': self.tc_dt.timestamp_conv_out, |
| | 403 | 'FIXED': self.tc_fd.fixed_conv_out_imprecise, |
| | 404 | 'TEXT': self.dj_ue.force_unicode, |
| | 405 | 'TEXT_UNICODE': self.tc_tu.unicode_conv_out, |
| | 406 | 'BLOB': self.blob_conv_out |
| | 407 | }) |
| | 408 | |
| | 409 | def _get_query(self): |
| | 410 | if self._statement: |
| | 411 | return self._statement.sql |
| | 412 | def _get_statement(self): |
| | 413 | if self._statement: |
| | 414 | return self._statement |
| | 415 | query = property(_get_query) |
| | 416 | statement = property(_get_statement) |
| | 417 | |
| | 418 | def execute(self, query, params=()): |
| | 419 | query = self.convert_query(query, len(params)) |
| | 420 | if self._get_query() != query: |
| | 421 | try: |
| | 422 | self._statement = self.cursor.prep(query) |
| | 423 | except Database.ProgrammingError, e: |
| | 424 | print query % params |
| | 425 | raise DatabaseError, e |
| | 426 | return self.cursor.execute(self._statement, params) |
| | 427 | |
| | 428 | def executemany(self, query, param_list): |
| | 429 | try: |
| | 430 | query = self.convert_query(query, len(param_list[0])) |
| | 431 | except IndexError: |
| | 432 | param_list = [] |
| | 433 | if self._get_query() != query: |
| | 434 | self._statement = self.cursor.prep(query) |
| | 435 | return self.cursor.executemany(self._statement, param_list) |
| | 436 | |
| | 437 | def convert_query(self, query, num_params): |
| | 438 | return query % tuple("?" * num_params) |
| | 439 | |
| | 440 | def __getattr__(self, attr): |
| | 441 | if attr in self.__dict__: |
| | 442 | return self.__dict__[attr] |
| | 443 | else: |
| | 444 | return getattr(self.cursor, attr) |
| | 445 | |
| | 446 | class DatabaseWrapper(BaseDatabaseWrapper): |
| | 447 | features = DatabaseFeatures() |
| | 448 | ops = DatabaseOperations() |
| | 449 | operators = { |
| | 450 | 'exact': '= %s', |
| | 451 | 'iexact': '= UPPER(%s)', |
| | 452 | 'contains': "LIKE %s ESCAPE'\\'", |
| | 453 | 'icontains': 'CONTAINING %s', #case is ignored |
| | 454 | 'gt': '> %s', |
| | 455 | 'gte': '>= %s', |
| | 456 | 'lt': '< %s', |
| | 457 | 'lte': '<= %s', |
| | 458 | 'startswith': 'STARTING WITH %s', #looks to be faster then LIKE |
| | 459 | 'endswith': "LIKE %s ESCAPE'\\'", |
| | 460 | 'istartswith': 'STARTING WITH UPPER(%s)', |
| | 461 | 'iendswith': "LIKE UPPER(%s) ESCAPE'\\'", |
| | 462 | } |
| | 463 | _current_cursor = None |
| | 464 | def _connect(self, settings): |
| | 465 | if settings.DATABASE_NAME == '': |
| | 466 | from django.core.exceptions import ImproperlyConfigured |
| | 467 | raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." |
| | 468 | charset = 'UNICODE_FSS' |
| | 469 | if hasattr(settings, 'FIREBIRD_CHARSET'): |
| | 470 | if settings.FIREBIRD_CHARSET == 'UTF8': |
| | 471 | charset = 'UTF8' |
| | 472 | kwargs = {'charset' : charset } |
| | 473 | if settings.DATABASE_HOST: |
| | 474 | kwargs['dsn'] = "%s:%s" % (settings.DATABASE_HOST, settings.DATABASE_NAME) |
| | 475 | else: |
| | 476 | kwargs['dsn'] = "localhost:%s" % settings.DATABASE_NAME |
| | 477 | if settings.DATABASE_USER: |
| | 478 | kwargs['user'] = settings.DATABASE_USER |
| | 479 | if settings.DATABASE_PASSWORD: |
| | 480 | kwargs['password'] = settings.DATABASE_PASSWORD |
| | 481 | self.connection = Database.connect(**kwargs) |
| | 482 | assert self.charset == charset |
| | 483 | try: |
| | 484 | self.connection.execute_immediate(""" |
| | 485 | DECLARE EXTERNAL FUNCTION rand |
| | 486 | RETURNS DOUBLE PRECISION |
| | 487 | BY VALUE ENTRY_POINT 'IB_UDF_rand' MODULE_NAME 'ib_udf'; |
| | 488 | """) |
| | 489 | except Database.ProgrammingError: |
| | 490 | pass #Already defined |
| | 491 | |
| | 492 | def cursor(self, name=None): |
| | 493 | #Cursors can be named |
| | 494 | #http://kinterbasdb.sourceforge.net/dist_docs/usage.html#adv_named_cursors |
| | 495 | #and maybe useful for scrolling updates and deletes |
| | 496 | from django.conf import settings |
| | 497 | cursor = self._cursor(settings, name) |
| | 498 | if settings.DEBUG: |
| | 499 | return self.make_debug_cursor(cursor) |
| | 500 | return cursor |
| | 501 | |
| | 502 | def _cursor(self, settings, name): |
| | 503 | if self.connection is None: |
| | 504 | self._connect(settings) |
| | 505 | cursor = self.connection.cursor() |
| | 506 | if name: |
| | 507 | cursor.name = name |
| | 508 | cursor = FirebirdCursorWrapper(cursor) |
| | 509 | self._current_cursor = cursor |
| | 510 | return cursor |
| | 511 | |
| | 512 | #Returns query from prepared statement |
| | 513 | def _get_query(self): |
| | 514 | if self._current_cursor: |
| | 515 | return self._current_cursor.query |
| | 516 | query = property(_get_query) |
| | 517 | #Returns prepared statement itself |
| | 518 | def _get_statement(self): |
| | 519 | if self._current_cursor: |
| | 520 | return self._current_cursor.statement |
| | 521 | statement = property(_get_statement) |
| | 522 | |
| | 523 | |
| | 524 | def __getattr__(self, attr): |
| | 525 | if attr in self.__dict__: |
| | 526 | return self.__dict__[attr] |
| | 527 | else: |
| | 528 | return getattr(self.connection, attr) |
| | 529 | |
| | 530 | |