本文整理汇总了Python中sqlalchemy.dialects.mysql.dialect方法的典型用法代码示例。如果您正苦于以下问题:Python mysql.dialect方法的具体用法?Python mysql.dialect怎么用?Python mysql.dialect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.dialects.mysql
的用法示例。
在下文中一共展示了mysql.dialect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compile_query
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def compile_query(query):
""" 把一个 sqlalchemy query object 编译成mysql风格的 sql 语句 """
from sqlalchemy.sql import compiler
from sqlalchemy.dialects import mysql as mysql_dialetct
from pymysql.converters import conversions, escape_item, encoders
dialect = mysql_dialetct.dialect()
statement = query.statement
comp = compiler.SQLCompiler(dialect, statement)
comp.compile()
enc = dialect.encoding
comp_params = comp.params
params = []
for k in comp.positiontup:
v = comp_params[k]
if six.PY2 and isinstance(v, six.string_types) and not isinstance(v, six.text_type):
v = v.decode("utf8")
v = escape_item(v, conversions, encoders)
params.append(v)
return (comp.string % tuple(params))
示例2: test_mysql_variants
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_mysql_variants(self):
self.assertEqual(
"LONGTEXT",
str(
types.JsonEncodedDict(mysql_as_long=True).compile(
dialect=mysql.dialect())
)
)
self.assertEqual(
"MEDIUMTEXT",
str(
types.JsonEncodedDict(mysql_as_medium=True).compile(
dialect=mysql.dialect())
)
)
self.assertRaises(
TypeError,
lambda: types.JsonEncodedDict(
mysql_as_long=True,
mysql_as_medium=True)
)
示例3: test_is_disconnect
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_is_disconnect(
self, arg0, message, exc_cls_name, dialect_name, is_disconnect
):
class Error(Exception):
pass
dbapi = mock.Mock()
dbapi.Error = Error
dbapi.ProgrammingError = type("ProgrammingError", (Error,), {})
dbapi.OperationalError = type("OperationalError", (Error,), {})
dbapi.InterfaceError = type("InterfaceError", (Error,), {})
dbapi.InternalError = type("InternalError", (Error,), {})
dialect = getattr(mysql, dialect_name).dialect(dbapi=dbapi)
error = getattr(dbapi, exc_cls_name)(arg0, message)
eq_(dialect.is_disconnect(error, None, None), is_disconnect)
示例4: _test_ssl_arguments
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def _test_ssl_arguments(self, dialect):
kwarg = dialect.create_connect_args(
make_url(
"mysql://scott:tiger@localhost:3306/test"
"?ssl_ca=/ca.pem&ssl_cert=/cert.pem&ssl_key=/key.pem"
)
)[1]
# args that differ among mysqldb and oursql
for k in ("use_unicode", "found_rows", "client_flag"):
kwarg.pop(k, None)
eq_(
kwarg,
{
"passwd": "tiger",
"db": "test",
"ssl": {
"ca": "/ca.pem",
"cert": "/cert.pem",
"key": "/key.pem",
},
"host": "localhost",
"user": "scott",
"port": 3306,
},
)
示例5: test_mysqlconnector_raise_on_warnings_arg
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_mysqlconnector_raise_on_warnings_arg(self):
from sqlalchemy.dialects.mysql import mysqlconnector
dialect = mysqlconnector.dialect()
kw = dialect.create_connect_args(
make_url(
"mysql+mysqlconnector://u:p@host/db?raise_on_warnings=true"
)
)[1]
eq_(kw["raise_on_warnings"], True)
kw = dialect.create_connect_args(
make_url(
"mysql+mysqlconnector://u:p@host/db?raise_on_warnings=false"
)
)[1]
eq_(kw["raise_on_warnings"], False)
kw = dialect.create_connect_args(
make_url("mysql+mysqlconnector://u:p@host/db")
)[1]
assert "raise_on_warnings" not in kw
示例6: test_no_default_isolation_level
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_no_default_isolation_level(self):
from sqlalchemy.testing import mock
engine = engines.testing_engine()
real_isolation_level = testing.db.dialect.get_isolation_level
def fake_isolation_level(connection):
connection = mock.Mock(
cursor=mock.Mock(
return_value=mock.Mock(
fetchone=mock.Mock(return_value=None)
)
)
)
return real_isolation_level(connection)
with mock.patch.object(
engine.dialect, "get_isolation_level", fake_isolation_level
):
with expect_warnings(
"Could not retrieve transaction isolation level for MySQL "
"connection."
):
engine.connect()
示例7: test_prefix_with
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_prefix_with(self):
table1 = self.tables.mytable
stmt = (
table1.update()
.prefix_with("A", "B", dialect="mysql")
.prefix_with("C", "D")
)
self.assert_compile(
stmt,
"UPDATE C D mytable SET myid=:myid, name=:name, "
"description=:description",
)
self.assert_compile(
stmt,
"UPDATE A B C D mytable SET myid=%s, name=%s, description=%s",
dialect=mysql.dialect(),
)
示例8: test_update_from_join_mysql
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_update_from_join_mysql(self):
users, addresses = self.tables.users, self.tables.addresses
j = users.join(addresses)
self.assert_compile(
update(j)
.values(name="newname")
.where(addresses.c.email_address == "e1"),
""
"UPDATE users "
"INNER JOIN addresses ON users.id = addresses.user_id "
"SET users.name=%s "
"WHERE "
"addresses.email_address = %s",
checkparams={"email_address_1": "e1", "name": "newname"},
dialect=mysql.dialect(),
)
示例9: test_autocorrelate_error
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_autocorrelate_error(self):
users, addresses = self.tables.users, self.tables.addresses
stmt = (
users.update()
.values(name="newname")
.where(users.c.id == addresses.c.user_id)
.where(
~exists()
.where(addresses.c.user_id == users.c.id)
.where(addresses.c.email_address == "foo")
)
)
assert_raises_message(
exc.InvalidRequestError,
".*returned no FROM clauses due to auto-correlation.*",
stmt.compile,
dialect=default.StrCompileDialect(),
)
示例10: test_prefix_with
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_prefix_with(self):
table1 = self.tables.mytable
stmt = (
table1.insert()
.prefix_with("A", "B", dialect="mysql")
.prefix_with("C", "D")
)
self.assert_compile(
stmt,
"INSERT C D INTO mytable (myid, name, description) "
"VALUES (:myid, :name, :description)",
)
self.assert_compile(
stmt,
"INSERT A B C D INTO mytable (myid, name, description) "
"VALUES (%s, %s, %s)",
dialect=mysql.dialect(),
)
示例11: test_insert_from_select_returning
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_insert_from_select_returning(self):
table1 = self.tables.mytable
sel = select([table1.c.myid, table1.c.name]).where(
table1.c.name == "foo"
)
ins = (
self.tables.myothertable.insert()
.from_select(("otherid", "othername"), sel)
.returning(self.tables.myothertable.c.otherid)
)
self.assert_compile(
ins,
"INSERT INTO myothertable (otherid, othername) "
"SELECT mytable.myid, mytable.name FROM mytable "
"WHERE mytable.name = %(name_1)s RETURNING myothertable.otherid",
checkparams={"name_1": "foo"},
dialect="postgresql",
)
示例12: test_insert_from_select_seq
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_insert_from_select_seq(self):
m = MetaData()
t1 = Table(
"t",
m,
Column("id", Integer, Sequence("id_seq"), primary_key=True),
Column("data", String),
)
stmt = t1.insert().from_select(("data",), select([t1.c.data]))
self.assert_compile(
stmt,
"INSERT INTO t (data, id) SELECT t.data, "
"nextval('id_seq') AS next_value_1 FROM t",
dialect=postgresql.dialect(),
)
示例13: test_insert_from_select_cte_follows_insert_two
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_insert_from_select_cte_follows_insert_two(self):
dialect = default.DefaultDialect()
dialect.cte_follows_insert = True
table1 = self.tables.mytable
cte = table1.select().cte("c")
stmt = cte.select()
ins = table1.insert().from_select(table1.c, stmt)
self.assert_compile(
ins,
"INSERT INTO mytable (myid, name, description) "
"WITH c AS (SELECT mytable.myid AS myid, mytable.name AS name, "
"mytable.description AS description FROM mytable) "
"SELECT c.myid, c.name, c.description FROM c",
dialect=dialect,
)
示例14: test_anticipate_no_pk_composite_pk_implicit_returning
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_anticipate_no_pk_composite_pk_implicit_returning(self):
t = Table(
"t",
MetaData(),
Column("x", Integer, primary_key=True),
Column("y", Integer, primary_key=True),
)
d = postgresql.dialect()
d.implicit_returning = True
with expect_warnings(
"Column 't.y' is marked as a member.*"
"Note that as of SQLAlchemy 1.1,"
):
self.assert_compile(
t.insert(),
"INSERT INTO t (x) VALUES (%(x)s)",
params={"x": 5},
dialect=d,
)
示例15: test_anticipate_no_pk_non_composite_pk_implicit_returning
# 需要导入模块: from sqlalchemy.dialects import mysql [as 别名]
# 或者: from sqlalchemy.dialects.mysql import dialect [as 别名]
def test_anticipate_no_pk_non_composite_pk_implicit_returning(self):
t = Table(
"t",
MetaData(),
Column("x", Integer, primary_key=True, autoincrement=False),
Column("q", Integer),
)
d = postgresql.dialect()
d.implicit_returning = True
with expect_warnings(
"Column 't.x' is marked as a member.*" "may not store NULL.$"
):
self.assert_compile(
t.insert(),
"INSERT INTO t (q) VALUES (%(q)s)",
params={"q": 5},
dialect=d,
)