Ticket #2417: 11_z2_sql_placeholder_prefix.diff

File 11_z2_sql_placeholder_prefix.diff, 2.8 KB (added by prz@…, 16 years ago)

small patch to allow prefix/postfix on placeholders in SQL inserts, makes casts & typing of input in MySQL possible.

  • django/db/models/base.py

     
    241241            else:
    242242                record_exists = False
    243243        if not pk_set or not record_exists:
    244             field_names = [qn(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
    245             db_values = [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
     244            field_names = []
     245            db_values = []
     246            placeholders = []
     247            for f in self._meta.fields:
     248                if not isinstance(f, AutoField):
     249                    field_names += [ qn(f.column) ]
     250                    val = f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))
     251                    db_values += [ val ]
     252                    placeholders += [ (f.get_db_value_placeholder_prefix(val) or '') + '%s' + (f.get_db_value_placeholder_postfix(val) or '') ]
    246253            # If the PK has been manually set, respect that.
    247254            if pk_set:
    248                 field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)]
    249                 db_values += [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]
    250             placeholders = ['%s'] * len(field_names)
     255                for f in self._meta.fields:
     256                    if isinstance(f, AutoField):
     257                        field_names += [ f.column ]
     258                        val = f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))
     259                        db_values += [ val ]
     260                        placeholders += [ (f.get_db_value_placeholder_prefix(val) or '') + '%s' + (f.get_db_value_placeholder_postfix(val) or '') ]
     261
    251262            if self._meta.order_with_respect_to:
    252263                field_names.append(qn('_order'))
    253264                placeholders.append('%s')
  • django/db/models/fields/__init__.py

     
    207207        "Returns field's value prepared for saving into a database."
    208208        return value
Back to Top