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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2012-03-15 21:05:49 UTC
  • mfrom: (1.4.19)
  • Revision ID: package-import@ubuntu.com-20120315210549-fiuynu6jue9keqlh
Tags: 0.7.6-1
* New upstream release
* debhelper's compatibility bumped to 7
* Standards-Version bumped to 3.9.3 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
database itself, especially if database reflection features are
85
85
to be used.
86
86
 
 
87
Transaction Isolation Level
 
88
---------------------------
 
89
 
 
90
:func:`.create_engine` accepts an ``isolation_level`` 
 
91
parameter which results in the command ``SET SESSION 
 
92
TRANSACTION ISOLATION LEVEL <level>`` being invoked for 
 
93
every new connection. Valid values for this parameter are
 
94
``READ COMMITTED``, ``READ UNCOMMITTED``, 
 
95
``REPEATABLE READ``, and ``SERIALIZABLE``::
 
96
 
 
97
    engine = create_engine(
 
98
                    "mysql://scott:tiger@localhost/test", 
 
99
                    isolation_level="READ UNCOMMITTED"
 
100
                )
 
101
 
 
102
(new in 0.7.6)
 
103
 
87
104
Keys
88
105
----
89
106
 
221
238
an integer. MySQL only allows a length for an index if it is for a CHAR,
222
239
VARCHAR, TEXT, BINARY, VARBINARY and BLOB.
223
240
 
 
241
Index Types
 
242
~~~~~~~~~~~~~
 
243
 
 
244
Some MySQL storage engines permit you to specify an index type when creating
 
245
an index or primary key constraint. SQLAlchemy provides this feature via the 
 
246
``mysql_using`` parameter on :class:`.Index`::
 
247
 
 
248
    Index('my_index', my_table.c.data, mysql_using='hash')
 
249
 
 
250
As well as the ``mysql_using`` parameter on :class:`.PrimaryKeyConstraint`::
 
251
 
 
252
    PrimaryKeyConstraint("data", mysql_using='hash')
 
253
 
 
254
The value passed to the keyword argument will be simply passed through to the
 
255
underlying CREATE INDEX or PRIMARY KEY clause, so it *must* be a valid index 
 
256
type for your MySQL storage engine.
 
257
 
224
258
More information can be found at:
 
259
 
225
260
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
 
261
 
 
262
http://dev.mysql.com/doc/refman/5.0/en/create-table.html
 
263
 
226
264
"""
227
265
 
228
266
import datetime, inspect, re, sys
1331
1369
        return ', '.join(t._compiler_dispatch(self, asfrom=True, **kw) 
1332
1370
                    for t in [from_table] + list(extra_froms))
1333
1371
 
1334
 
    def update_from_clause(self, update_stmt, from_table, extra_froms, **kw):
 
1372
    def update_from_clause(self, update_stmt, from_table, 
 
1373
                                extra_froms, from_hints, **kw):
1335
1374
        return None
1336
1375
 
1337
1376
 
1421
1460
            table_opts.append(joiner.join((opt, arg)))
1422
1461
        return ' '.join(table_opts)
1423
1462
 
 
1463
 
1424
1464
    def visit_create_index(self, create):
1425
1465
        index = create.element
1426
1466
        preparer = self.preparer
 
1467
        table = preparer.format_table(index.table)
 
1468
        columns = [preparer.quote(c.name, c.quote) for c in index.columns]
 
1469
        name = preparer.quote(
 
1470
                    self._index_identifier(index.name), 
 
1471
                    index.quote)
 
1472
 
1427
1473
        text = "CREATE "
1428
1474
        if index.unique:
1429
1475
            text += "UNIQUE "
1430
 
        text += "INDEX %s ON %s " \
1431
 
                    % (preparer.quote(self._index_identifier(index.name), 
1432
 
                        index.quote),preparer.format_table(index.table))
 
1476
        text += "INDEX %s ON %s " % (name, table)
 
1477
 
 
1478
        columns = ', '.join(columns)
1433
1479
        if 'mysql_length' in index.kwargs:
1434
1480
            length = index.kwargs['mysql_length']
1435
 
        else:
1436
 
            length = None
1437
 
        if length is not None:
1438
 
            text+= "(%s(%d))" \
1439
 
                    % (', '.join(preparer.quote(c.name, c.quote)
1440
 
                                 for c in index.columns), length)
1441
 
        else:
1442
 
            text+= "(%s)" \
1443
 
                    % (', '.join(preparer.quote(c.name, c.quote)
1444
 
                                 for c in index.columns))
1445
 
        return text
1446
 
 
 
1481
            text += "(%s(%d))" % (columns, length)
 
1482
        else:
 
1483
            text += "(%s)" % (columns)
 
1484
 
 
1485
        if 'mysql_using' in index.kwargs:
 
1486
            using = index.kwargs['mysql_using']
 
1487
            text += " USING %s" % (preparer.quote(using, index.quote))
 
1488
 
 
1489
        return text
 
1490
 
 
1491
    def visit_primary_key_constraint(self, constraint):
 
1492
        text = super(MySQLDDLCompiler, self).\
 
1493
            visit_primary_key_constraint(constraint)
 
1494
        if "mysql_using" in constraint.kwargs:
 
1495
            using = constraint.kwargs['mysql_using']
 
1496
            text += " USING %s" % (
 
1497
                self.preparer.quote(using, constraint.quote))
 
1498
        return text
1447
1499
 
1448
1500
    def visit_drop_index(self, drop):
1449
1501
        index = drop.element
1450
1502
 
1451
1503
        return "\nDROP INDEX %s ON %s" % \
1452
 
                    (self.preparer.quote(self._index_identifier(index.name), index.quote),
 
1504
                    (self.preparer.quote(
 
1505
                        self._index_identifier(index.name), index.quote
 
1506
                    ),
1453
1507
                     self.preparer.format_table(index.table))
1454
1508
 
1455
1509
    def visit_drop_constraint(self, drop):
1768
1822
    _backslash_escapes = True
1769
1823
    _server_ansiquotes = False
1770
1824
 
1771
 
    def __init__(self, use_ansiquotes=None, **kwargs):
 
1825
    def __init__(self, use_ansiquotes=None, isolation_level=None, **kwargs):
1772
1826
        default.DefaultDialect.__init__(self, **kwargs)
 
1827
        self.isolation_level = isolation_level
 
1828
 
 
1829
    def on_connect(self):
 
1830
        if self.isolation_level is not None:
 
1831
            def connect(conn):
 
1832
                self.set_isolation_level(conn, self.isolation_level)
 
1833
            return connect
 
1834
        else:
 
1835
            return None
 
1836
 
 
1837
    _isolation_lookup = set(['SERIALIZABLE', 
 
1838
                'READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ'])
 
1839
 
 
1840
    def set_isolation_level(self, connection, level):
 
1841
        level = level.replace('_', ' ')
 
1842
        if level not in self._isolation_lookup:
 
1843
            raise exc.ArgumentError(
 
1844
                "Invalid value '%s' for isolation_level. "
 
1845
                "Valid isolation levels for %s are %s" % 
 
1846
                (level, self.name, ", ".join(self._isolation_lookup))
 
1847
                )
 
1848
        cursor = connection.cursor()
 
1849
        cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL %s" % level)
 
1850
        cursor.execute("COMMIT")
 
1851
        cursor.close()
 
1852
 
 
1853
    def get_isolation_level(self, connection):
 
1854
        cursor = connection.cursor()
 
1855
        cursor.execute('SELECT @@tx_isolation')
 
1856
        val = cursor.fetchone()[0]
 
1857
        cursor.close()
 
1858
        return val.upper().replace("-", " ")
1773
1859
 
1774
1860
    def do_commit(self, connection):
1775
1861
        """Execute a COMMIT."""