You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
im not sure I have the resources to support a SQLIte JSON type directly right now as I generally do not support SQLite extensions. However, SQLAlchemy's type system is fully extensible so I will put up a recipe soon.
Here's a recipe. A real version of this feature would be sent as a PR that looks a lot like the MySQL JSON feature, as the syntax here seems to work mostly the same way, except I'm not sure MySQL's json_extract() reutrns non-JSON values the way this one does (maybe it does).
from sqlalchemy import TypeDecorator, String, func
from sqlalchemy.types import NullType
import json
class SQLiteJson(TypeDecorator):
impl = String
class Comparator(String.Comparator):
def __getitem__(self, index):
if isinstance(index, tuple):
index = "$%s" % (
"".join([
"[%s]" % elem if isinstance(elem, int)
else '."%s"' % elem for elem in index
])
)
elif isinstance(index, int):
index = "$[%s]" % index
else:
index = '$."%s"' % index
# json_extract does not appear to return JSON sub-elements
# which is weird.
return func.json_extract(self.expr, index, type_=NullType)
comparator_factory = Comparator
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
from sqlalchemy import create_engine, Table, MetaData, Column, Integer, select
e = create_engine("sqlite://", echo='debug')
m = MetaData()
t = Table(
't', m, Column('id', Integer, primary_key=True),
Column('data', SQLiteJson)
)
m.create_all(e)
with e.connect() as conn:
conn.execute(
t.insert(), data={"x": {"y": "two", "z": [1, 2, 3]}}
)
value = conn.scalar(
select([t.c.data[('x', 'y')]]).where(t.c.data[('x', 'y')] == 'two')
)
print value
I don't have any plans to work on this alone so someone would need to work on adopting the above recipe into a patch that looks a lot like the MySQL feature.
With that in place a very simple copy of the MySQL implementation passes the current JSON tests, though it incorrectly produces JSON null for missing keys.
Turns out this is documented in "3.3 VALUE arguments" (just managed to skip that part entirely first):
However, if a value argument come directly from the result of another json1 function, then the argument is understood to be actual JSON and the complete JSON is inserted rather than a quoted string.
Added support for SQLite's json functionality via the new
SQLite implementation for :class:.sqltypes.JSON, :class:.sqlite.JSON.
The name used for the type is JSON, following an example found at
SQLite's own documentation. Pull request courtesy Ilja Everilä.
Activity
sqlalchemy-bot commentedon Nov 11, 2016
Michael Bayer (@zzzeek) wrote:
im not sure I have the resources to support a SQLIte JSON type directly right now as I generally do not support SQLite extensions. However, SQLAlchemy's type system is fully extensible so I will put up a recipe soon.
sqlalchemy-bot commentedon Nov 11, 2016
Michael Bayer (@zzzeek) wrote:
hm, it seems those extensions come pre-installed, OK. it's just SQL functions so there's not much here.
sqlalchemy-bot commentedon Nov 11, 2016
Michael Bayer (@zzzeek) wrote:
Here's a recipe. A real version of this feature would be sent as a PR that looks a lot like the MySQL JSON feature, as the syntax here seems to work mostly the same way, except I'm not sure MySQL's json_extract() reutrns non-JSON values the way this one does (maybe it does).
sqlalchemy-bot commentedon Nov 11, 2016
Changes by Michael Bayer (@zzzeek):
sqlalchemy-bot commentedon Nov 11, 2016
Changes by Michael Bayer (@zzzeek):
sqlalchemy-bot commentedon Jun 4, 2017
Timothée Jeannin (@TimotheeJeannin) wrote:
Would be very nice to have support for the JSON type in SQLite databases !
sqlalchemy-bot commentedon Nov 4, 2017
maria (@ciacicode) wrote:
Has there been any progress on this? I can't find any mention of json1 support in the docs: http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
sqlalchemy-bot commentedon Nov 6, 2017
Michael Bayer (@zzzeek) wrote:
I don't have any plans to work on this alone so someone would need to work on adopting the above recipe into a patch that looks a lot like the MySQL feature.
sqlalchemy-bot commentedon Mar 12, 2018
Ilja Everilä (@everilae) wrote:
For reasons perfectly unclear to me
would seem to produce somewhat the same behaviour as MySQL's
JSON_EXTRACThas. No idea why, since a simpledoes produce a JSON string containing escaped JSON, but for extracted text this does not happen:
With that in place a very simple copy of the MySQL implementation passes the current JSON tests, though it incorrectly produces JSON null for missing keys.
sqlalchemy-bot commentedon Mar 12, 2018
Ilja Everilä (@everilae) wrote:
Turns out this is documented in "3.3 VALUE arguments" (just managed to skip that part entirely first):
and as such dependable.
sqlalchemy-bot commentedon Mar 15, 2018
Michael Bayer (@zzzeek) wrote:
this is in gerrit now at https://gerrit.sqlalchemy.org/#/c/zzzeek/sqlalchemy/+/706/
sqlalchemy-bot commentedon Mar 15, 2018
Changes by Michael Bayer (@zzzeek):
sqlalchemy-bot commentedon Mar 15, 2018
Changes by Michael Bayer (@zzzeek):
sqlalchemy-bot commentedon Mar 16, 2018
Peter Senna Tschudin (@petersenna) wrote:
I'm testing
https://gerrit.sqlalchemy.org/#/c/zzzeek/sqlalchemy/+/706/
Let me know if I can do any specific test
sqlalchemy-bot commentedon Mar 16, 2018
Michael Bayer (@zzzeek) wrote:
well I just kicked the build follower which had crashed so lets see how the base test suite runs.
sqlalchemy-bot commentedon Mar 27, 2018
Michael Bayer (@zzzeek) wrote:
Issue #4226 was marked as a duplicate of this issue.
sqlalchemy-bot commentedon Jul 10, 2018
Michael Bayer (@zzzeek) wrote:
Sqlite json
Added support for SQLite's json functionality via the new
SQLite implementation for :class:
.sqltypes.JSON, :class:.sqlite.JSON.The name used for the type is
JSON, following an example found atSQLite's own documentation. Pull request courtesy Ilja Everilä.
Fixes: #3850
Change-Id: I3d2714fb8655343a99d13dc751b16b93d05d7dda
Pull-request: zzzeek/sqlalchemy#434
→ 62d5908
sqlalchemy-bot commentedon Jul 10, 2018
Changes by Michael Bayer (@zzzeek):