本文整理汇总了Python中sqlalchemy.types.JSON属性的典型用法代码示例。如果您正苦于以下问题:Python types.JSON属性的具体用法?Python types.JSON怎么用?Python types.JSON使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类sqlalchemy.types
的用法示例。
在下文中一共展示了types.JSON属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_dialect_impl
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
return dialect.type_descriptor(JSONB())
elif dialect.name == 'mysql':
return dialect.type_descriptor(types.JSON())
elif dialect.name == 'oracle':
return dialect.type_descriptor(CLOB())
else:
return dialect.type_descriptor(String())
示例2: _json_deserializer
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def _json_deserializer(self, row):
"""JSON deserializer for RECORD types.
The DB-API layer already deserializes JSON to a dictionary, so this
just returns the input.
"""
return row
示例3: set_custom_properties
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def set_custom_properties(self, table):
model = table.__tablename__
for property, values in properties["custom"].get(model, {}).items():
if values.get("private", False):
kwargs = {}
else:
kwargs = {
"default": values["default"],
"info": {"log_change": values.get("log_change", True)},
}
column = self.Column(
{
"boolean": Boolean,
"dict": self.Dict,
"float": Float,
"integer": Integer,
"json": JSON,
"string": self.LargeString,
"select": self.SmallString,
"multiselect": self.List,
}[values.get("type", "string")],
**kwargs,
)
if not values.get("serialize", True):
self.dont_serialize[model].append(property)
if not values.get("migrate", True):
self.dont_migrate[model].append(property)
setattr(table, property, column)
return table
示例4: string_json_attribute_person_model
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def string_json_attribute_person_model(base):
"""
This approach to faking JSON support for testing with sqlite is borrowed from:
https://avacariu.me/articles/2016/compiling-json-as-text-for-sqlite-with-sqlalchemy
"""
import sqlalchemy.types as types
import json
class StringyJSON(types.TypeDecorator):
"""Stores and retrieves JSON as TEXT."""
impl = types.TEXT
def process_bind_param(self, value, dialect):
if value is not None:
value = json.dumps(value)
return value
def process_result_value(self, value, dialect):
if value is not None:
value = json.loads(value)
return value
# TypeEngine.with_variant says "use StringyJSON instead when
# connecting to 'sqlite'"
MagicJSON = types.JSON().with_variant(StringyJSON, 'sqlite')
class StringJsonAttributePerson(base):
__tablename__ = 'string_json_attribute_person'
person_id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
birth_date = Column(DateTime)
# This model uses a String type for "json_tags" to avoid dependency on a nonstandard SQL type in testing, \
# while still demonstrating support
address = Column(MagicJSON)
yield StringJsonAttributePerson
示例5: test_json
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_json(self):
for t in ['json', 'jsonb']:
self._test(t, sqltypes.JSON)
示例6: test_reflection
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_reflection(self):
Table("json_test", self.metadata, Column("foo", sqlite.JSON))
self.metadata.create_all()
reflected = Table("json_test", MetaData(), autoload_with=testing.db)
is_(reflected.c.foo.type._type_affinity, sqltypes.JSON)
assert isinstance(reflected.c.foo.type, sqlite.JSON)
示例7: test_rudimentary_roundtrip
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_rudimentary_roundtrip(self):
sqlite_json = Table(
"json_test", self.metadata, Column("foo", sqlite.JSON)
)
self.metadata.create_all()
value = {"json": {"foo": "bar"}, "recs": ["one", "two"]}
with testing.db.connect() as conn:
conn.execute(sqlite_json.insert(), foo=value)
eq_(conn.scalar(select([sqlite_json.c.foo])), value)
示例8: test_extract_subobject
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_extract_subobject(self):
sqlite_json = Table(
"json_test", self.metadata, Column("foo", sqlite.JSON)
)
self.metadata.create_all()
value = {"json": {"foo": "bar"}}
with testing.db.connect() as conn:
conn.execute(sqlite_json.insert(), foo=value)
eq_(
conn.scalar(select([sqlite_json.c.foo["json"]])), value["json"]
)
示例9: test_deprecated_serializer_args
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_deprecated_serializer_args(self):
sqlite_json = Table(
"json_test", self.metadata, Column("foo", sqlite.JSON)
)
data_element = {"foo": "bar"}
js = mock.Mock(side_effect=json.dumps)
jd = mock.Mock(side_effect=json.loads)
with testing.expect_deprecated(
"The _json_deserializer argument to the SQLite "
"dialect has been renamed",
"The _json_serializer argument to the SQLite "
"dialect has been renamed",
):
engine = engines.testing_engine(
options=dict(_json_serializer=js, _json_deserializer=jd)
)
self.metadata.create_all(engine)
with engine.begin() as conn:
conn.execute(sqlite_json.insert(), {"foo": data_element})
row = conn.execute(select([sqlite_json.c.foo])).first()
eq_(row, (data_element,))
eq_(js.mock_calls, [mock.call(data_element)])
eq_(jd.mock_calls, [mock.call(json.dumps(data_element))])
示例10: setup
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def setup(self):
metadata = MetaData()
self.test_table = Table(
"test_table",
metadata,
Column("id", Integer, primary_key=True),
Column("test_column", JSON),
)
self.jsoncol = self.test_table.c.test_column
示例11: test_custom_astext_type
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_custom_astext_type(self):
class MyType(types.UserDefinedType):
pass
col = column("x", JSON(astext_type=MyType))
is_(col["q"].astext.type.__class__, MyType)
is_(col[("q", "p")].astext.type.__class__, MyType)
is_(col["q"]["p"].astext.type.__class__, MyType)
示例12: _test_insert_nulljson_into_none_as_null
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def _test_insert_nulljson_into_none_as_null(self, engine):
with engine.connect() as conn:
conn.execute(
self.tables.data_table.insert(),
{"name": "r1", "nulldata": JSON.NULL},
)
self._assert_column_is_JSON_NULL(conn, column="nulldata")
示例13: test_reflection
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_reflection(self):
Table("mysql_json", self.metadata, Column("foo", mysql.JSON))
self.metadata.create_all()
reflected = Table("mysql_json", MetaData(), autoload_with=testing.db)
is_(reflected.c.foo.type._type_affinity, sqltypes.JSON)
assert isinstance(reflected.c.foo.type, mysql.JSON)
示例14: test_rudimental_round_trip
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def test_rudimental_round_trip(self):
# note that test_suite has many more JSON round trip tests
# using the backend-agnostic JSON type
mysql_json = Table(
"mysql_json", self.metadata, Column("foo", mysql.JSON)
)
self.metadata.create_all()
value = {"json": {"foo": "bar"}, "recs": ["one", "two"]}
with testing.db.connect() as conn:
conn.execute(mysql_json.insert(), foo=value)
eq_(conn.scalar(select([mysql_json.c.foo])), value)
示例15: mutable_json_type
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import JSON [as 别名]
def mutable_json_type(dbtype=JSON, nested=False):
"""Type creator for (optionally nested) mutable JSON column types.
The default backend data type is sqlalchemy.types.JSON, but can be set to
any other type by providing the `dbtype` parameter.
"""
mutable_type = NestedMutable if nested else MutableDict
return mutable_type.as_mutable(dbtype)
# Base mutable JSON types