~ubuntu-branches/debian/sid/sqlalchemy/sid

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/dialects/mssql/base.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2012-05-06 20:25:44 UTC
  • mfrom: (1.4.20)
  • Revision ID: package-import@ubuntu.com-20120506202544-ah8xa6gwcsqavrz5
Tags: 0.7.7-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
107
107
--------------------
108
108
MSSQL supports the notion of setting compatibility levels at the
109
109
database level. This allows, for instance, to run a database that
110
 
is compatibile with SQL2000 while running on a SQL2005 database
 
110
is compatible with SQL2000 while running on a SQL2005 database
111
111
server. ``server_version_info`` will always return the database
112
112
server version information (in this case SQL2005) and not the
113
 
compatibiility level information. Because of this, if running under
 
113
compatibility level information. Because of this, if running under
114
114
a backwards compatibility mode SQAlchemy may attempt to use T-SQL
115
115
statements that are unable to be parsed by the database server.
116
116
 
146
146
 
147
147
Not necessarily specific to SQLAlchemy, SQL Server has a default transaction 
148
148
isolation mode that locks entire tables, and causes even mildly concurrent
149
 
applications to have long held locks and frequent deadlocks.   
 
149
applications to have long held locks and frequent deadlocks.
150
150
Enabling snapshot isolation for the database as a whole is recommended 
151
151
for modern levels of concurrency support.  This is accomplished via the 
152
152
following ALTER DATABASE commands executed at the SQL prompt::
168
168
should be used when IN/NOT IN are desired.
169
169
 
170
170
For the time being, the existing behavior prevents a comparison
171
 
between scalar select and another value that actually wants to use ``=``.  
 
171
between scalar select and another value that actually wants to use ``=``.
172
172
To remove this behavior in a forwards-compatible way, apply this
173
173
compilation rule by placing the following code at the module import
174
174
level::
176
176
    from sqlalchemy.ext.compiler import compiles
177
177
    from sqlalchemy.sql.expression import _BinaryExpression
178
178
    from sqlalchemy.sql.compiler import SQLCompiler
179
 
    
 
179
 
180
180
    @compiles(_BinaryExpression, 'mssql')
181
181
    def override_legacy_binary(element, compiler, **kw):
182
182
        return SQLCompiler.visit_binary(compiler, element, **kw)
689
689
                                        not self.executemany
690
690
 
691
691
            if self._enable_identity_insert:
692
 
                self.cursor.execute("SET IDENTITY_INSERT %s ON" % 
693
 
                    self.dialect.identifier_preparer.format_table(tbl))
 
692
                self.root_connection._cursor_execute(self.cursor, 
 
693
                    "SET IDENTITY_INSERT %s ON" % 
 
694
                    self.dialect.identifier_preparer.format_table(tbl),
 
695
                    ())
694
696
 
695
697
    def post_exec(self):
696
698
        """Disable IDENTITY_INSERT if enabled."""
697
699
 
 
700
        conn = self.root_connection
698
701
        if self._select_lastrowid:
699
702
            if self.dialect.use_scope_identity:
700
 
                self.cursor.execute(
701
 
                "SELECT scope_identity() AS lastrowid", ())
 
703
                conn._cursor_execute(self.cursor, 
 
704
                    "SELECT scope_identity() AS lastrowid", ())
702
705
            else:
703
 
                self.cursor.execute("SELECT @@identity AS lastrowid", ())
 
706
                conn._cursor_execute(self.cursor, 
 
707
                    "SELECT @@identity AS lastrowid", ())
704
708
            # fetchall() ensures the cursor is consumed without closing it
705
709
            row = self.cursor.fetchall()[0]
706
710
            self._lastrowid = int(row[0])
710
714
            self._result_proxy = base.FullyBufferedResultProxy(self)
711
715
 
712
716
        if self._enable_identity_insert:
713
 
            self.cursor.execute(
 
717
            conn._cursor_execute(self.cursor, 
714
718
                        "SET IDENTITY_INSERT %s OFF" %
715
719
                            self.dialect.identifier_preparer.
716
 
                                format_table(self.compiled.statement.table)
 
720
                                format_table(self.compiled.statement.table),
 
721
                        ()
717
722
                        )
718
723
 
719
724
    def get_lastrowid(self):
980
985
        else:
981
986
            return ""
982
987
 
 
988
    def update_from_clause(self, update_stmt,
 
989
                                from_table, extra_froms,
 
990
                                from_hints,
 
991
                                **kw):
 
992
        """Render the UPDATE..FROM clause specific to MSSQL.
 
993
        
 
994
        In MSSQL, if the UPDATE statement involves an alias of the table to
 
995
        be updated, then the table itself must be added to the FROM list as
 
996
        well. Otherwise, it is optional. Here, we add it regardless.
 
997
        
 
998
        """
 
999
        return "FROM " + ', '.join(
 
1000
                    t._compiler_dispatch(self, asfrom=True,
 
1001
                                    fromhints=from_hints, **kw)
 
1002
                    for t in [from_table] + extra_froms)
 
1003
 
983
1004
class MSSQLStrictCompiler(MSSQLCompiler):
984
1005
    """A subclass of MSSQLCompiler which disables the usage of bind
985
1006
    parameters where not allowed natively by MS-SQL.