Activate IDENTITY_INSERT if needed.
(self)
| 1855 | return statement |
| 1856 | |
| 1857 | def pre_exec(self): |
| 1858 | """Activate IDENTITY_INSERT if needed.""" |
| 1859 | |
| 1860 | if self.isinsert: |
| 1861 | if TYPE_CHECKING: |
| 1862 | assert is_sql_compiler(self.compiled) |
| 1863 | assert isinstance(self.compiled.compile_state, DMLState) |
| 1864 | assert isinstance( |
| 1865 | self.compiled.compile_state.dml_table, TableClause |
| 1866 | ) |
| 1867 | |
| 1868 | tbl = self.compiled.compile_state.dml_table |
| 1869 | id_column = tbl._autoincrement_column |
| 1870 | |
| 1871 | if id_column is not None and ( |
| 1872 | not isinstance(id_column.default, Sequence) |
| 1873 | ): |
| 1874 | insert_has_identity = True |
| 1875 | compile_state = self.compiled.dml_compile_state |
| 1876 | self._enable_identity_insert = ( |
| 1877 | id_column.key in self.compiled_parameters[0] |
| 1878 | ) or ( |
| 1879 | compile_state._dict_parameters |
| 1880 | and (id_column.key in compile_state._insert_col_keys) |
| 1881 | ) |
| 1882 | |
| 1883 | else: |
| 1884 | insert_has_identity = False |
| 1885 | self._enable_identity_insert = False |
| 1886 | |
| 1887 | self._select_lastrowid = ( |
| 1888 | not self.compiled.inline |
| 1889 | and insert_has_identity |
| 1890 | and not self.compiled.effective_returning |
| 1891 | and not self._enable_identity_insert |
| 1892 | and not self.executemany |
| 1893 | ) |
| 1894 | |
| 1895 | if self._enable_identity_insert: |
| 1896 | self.root_connection._cursor_execute( |
| 1897 | self.cursor, |
| 1898 | self._opt_encode( |
| 1899 | "SET IDENTITY_INSERT %s ON" |
| 1900 | % self.identifier_preparer.format_table(tbl) |
| 1901 | ), |
| 1902 | (), |
| 1903 | self, |
| 1904 | ) |
| 1905 | |
| 1906 | # don't embed the scope_identity select into an |
| 1907 | # "INSERT .. DEFAULT VALUES" |
| 1908 | if ( |
| 1909 | self._select_lastrowid |
| 1910 | and self.dialect.scope_identity_must_be_embedded |
| 1911 | and self.dialect.use_scope_identity |
| 1912 | and len(self.parameters[0]) |
| 1913 | ): |
| 1914 | self._embedded_scope_identity = True |
nothing calls this directly
no test coverage detected