~sidnei/storm/mssql-support

« back to all changes in this revision

Viewing changes to storm/databases/mysql.py

  • Committer: Gustavo Niemeyer
  • Date: 2007-08-04 21:20:41 UTC
  • Revision ID: gustavo@niemeyer.net-20070804212041-fc2tjgpe2l9wvxx1
- The reserved-words branch had a bug: MySQL's default escaping
  character is actually ` (backtick), not " (double quote).  The
  bug went unnoticed because MySQL understands double quotes as
  strings, and it accepts strings in places such as aliases (e.g.
  foo as "str").
- A new test was introduced to verify that quoting actually works
  in all backends.
- This change also inverts the logic for deciding when to quote.
  Rather than looking for unaccepted characters, we quote anything
  that doesn't look like a nice word ([a-z][a-z0-9_]+).

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
except ImportError:
31
31
    MySQLdb = dummy
32
32
 
33
 
from storm.expr import compile, Select, compile_select, Undef, And, Eq, SQLRaw
 
33
from storm.expr import (compile, Select, compile_select, Undef, And, Eq,
 
34
                        SQLRaw, SQLToken, is_safe_token)
34
35
from storm.database import *
35
36
from storm.exceptions import install_exceptions, DatabaseModuleError
36
37
 
46
47
        select.limit = sys.maxint
47
48
    return compile_select(compile, select, state)
48
49
 
 
50
@compile.when(SQLToken)
 
51
def compile_sql_token_mysql(compile, expr, state):
 
52
    """MySQL uses ` as the escape character by default."""
 
53
    if is_safe_token(expr) and not compile.is_reserved_word(expr):
 
54
        return expr
 
55
    return '`%s`' % expr.replace('`', '``')
 
56
 
49
57
 
50
58
class MySQLResult(Result):
51
59