Changes between Version 3 and Version 4 of JSON1Extension


Ignore:
Timestamp:
Aug 14, 2020, 2:29:04 AM (4 years ago)
Author:
Sage Abdullah
Comment:

Add workaround by dynamically loading the extension

Legend:

Unmodified
Added
Removed
Modified
  • JSON1Extension

    v3 v4  
    88on Python's `sqlite3`__ library. If the extension is not enabled on your installation, a system error
    99(``fields.E180``) will be raised. To check if the extension is enabled on your installation, you can
    10 do a query with one of the functions included in the extension, e.g. ``JSON()``. For example::
     10do a query with one of the functions included in the extension, e.g. ``JSON()``. For example:
     11
     12.. code-block:: python
    1113
    1214    >>> import sqlite3
     
    2729packages and enabled by default. If that's not the case on your installation, then do the following:
    2830
    29 - Download the `SQLite amalgamation`__, with or without the configuration script.
     31- Download the `SQLite amalgamation`_, with or without the configuration script.
    3032- Extract the source code archive and enter the directory of the result.
    3133- Compile the source code using the ``-DSQLITE_ENABLE_JSON1`` flag to enable the JSON1
     
    4749- Now, the JSON1 extension should be ready to be used in Python and Django.
    4850
    49 .. __: https://www.sqlite.org/download.html
     51.. _SQLite amalgamation: https://www.sqlite.org/download.html
    5052.. __: https://www.sqlite.org/howtocompile.html
    5153
     
    8385.. __: https://www.sqlite.org/download.html
    8486
     87Other workarounds
     88=================
     89
     90Load the JSON1 extension dynamically
     91------------------------------------
     92
     93The following workaround works by compiling the JSON1 extension as a loadable extension
     94and loading it when the database connection is created by utilizing Django's |connection_created|__
     95signal. It hasn't been tested, but it should work on Linux, macOS, and Windows.
     96
     97.. |connection_created| replace:: ``connection_created``
     98.. __: https://docs.djangoproject.com/en/3.1/ref/signals/#connection-created
     99
     100- Download the `SQLite amalgamation`_, with or without the configuration script.
     101- Extract the source code archive and enter the directory of the result.
     102- Follow the instructions to `compile a loadable extension`__ according to your operating system,
     103  but replace the ``YourCode.c`` placeholder to point to ``ext/misc/json1.c``.
     104
     105  For example, on Linux it would be::
     106
     107    gcc -g -fPIC -shared ext/misc/json1.c -o json1.so
     108
     109  On macOS, it would be::
     110
     111    gcc -g -fPIC -dynamiclib ext/misc/json1.c -o json1.dylib
     112
     113  On Windows with MSVC, it would be::
     114
     115    cl ext/misc/json1.c -link -dll -out:json1.dll
     116
     117  On Windows with MinGW, it would be::
     118
     119    gcc -g -shared ext/misc/json1.c -o json1.dll
     120
     121.. __: https://www.sqlite.org/loadext.html#compiling_a_loadable_extension
     122
     123- Place the compiled JSON1 extension somewhere desirable.
     124- Create a signal handler for the ``connection_created`` signal. For example:
     125
     126  .. code-block:: python
     127
     128    def load_json1(connection, **kwargs):
     129        if connection.vendor != 'sqlite':
     130            return
     131        connection.connection.enable_load_extension(True)
     132        connection.connection.load_extension('./json1')
     133
     134  You should replace ``'./json1'`` if your compiled extension is stored under a different directory.
     135
     136- Connect the receiver function in one of your apps' ``ready()`` function.
     137
     138  .. code-block:: python
     139
     140    class MyAppConfig(AppConfig):
     141        # ...
     142
     143        def ready(self):
     144            connection_created.connect(load_json1)
     145
     146  You can also connect the function using the ``@receiver`` decorator. For more information,
     147  read the docs on how to `connect receiver functions`__.
     148
     149.. __: https://docs.djangoproject.com/en/3.1/topics/signals/#connecting-receiver-functions
    85150}}}
Back to Top