def execute(self, statement, params=None, noresult=False): if (isinstance(statement, Insert) and statement.primary_variables is not Undef): result = Connection.execute(self, statement, params) # The lastrowid value will be set if: # - the table had an AUTO INCREMENT column, and # - the column was not set during the insert or set to 0 # # If these conditions are met, then lastrowid will be the # value of the first such column set. We assume that it # is the first undefined primary key variable. if result._raw_cursor.lastrowid: for variable in statement.primary_variables: if not variable.is_defined(): variable.set(result._raw_cursor.lastrowid, from_db=True) break if noresult: result = None return result return Connection.execute(self, statement, params, noresult)
def execute(self, statement, params=None, noresult=False): """Execute a statement with the given parameters. This extends the L{Connection.execute} method to add support for automatic retrieval of inserted primary keys to link in-memory objects with their specific rows. """ if (isinstance(statement, Insert) and self._database._version >= 80200 and statement.primary_variables is not Undef and statement.primary_columns is not Undef): # Here we decorate the Insert statement with a Returning # expression, so that we get back in the result the values # for the primary key just inserted. This prevents a round # trip to the database for obtaining these values. result = Connection.execute(self, Returning(statement), params) for variable, value in zip(statement.primary_variables, result.get_one()): result.set_variable(variable, value) return result return Connection.execute(self, statement, params, noresult)