Wiki page
[mod_spatialite] by
sandro
2014-06-25 17:03:02.
D 2014-06-25T17:03:02.597
L mod_spatialite
U sandro
W 5308
back to <a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=4.2.0-doc">4.2.0-doc</a>
<h1>Dynamically loading SpatiaLite as an <u><i>extension module</i></u></h1>
<h3>Theoretical foundations</h3>
Speacking in rigorous technical terms, SpatiaLite simply is an extension to SQLite.<br>
Consequently we are strictly required to deploy some appropriate mechanism allowing to load both binary libraries (<i>libsqlite3</i> and <i>libspatialite</i>) and properly registering within SQLite3 the many <b><i>extended Spatial SQL functions</i></b> implemented by SpatiaLite.<br>
A further compication should be taken in proper debt; <i>libspatialite</i> isn't a stand-alone self-contained library, and depends on several further libraries (<i>libgeos</i>, <i>libproj</i>, <i>libxml2</i> and so on).<br><br>
There are several possible alternative paths leading to this final goal:
<ul>
<li>using <b><i>static linkage</i></b>: a straighforward solution avoiding many subsequent headhaches related to installation and run-time configuration.<br>
Following this approach a single monolithic <b><i>fat</i></b> executable internally containing all required binary code will be created at build time; this monolithic executable will have no external dependencies at all, thus radically simplifying any installation task.<br>
Such strategy nicely works in the case of stand-alone self-contained apps (e.g. both <b>spatialite.exe</b> and <b>spatialite_gui.exe</b> Windows binaries are usually built this way).<br>
Unhappily this elementary simple solution doesn't fit well in Linux and Unix-like environments; and even worst often directly conflicts with the internal architecture supported by many language connectors.</li>
<li>using <b><i>shared libraries<i></b> (aka <b><i>DLLs</i></b> on Windows).<br>
This second approach will carefully avoid to link alltogether a monolithic executable; each single code component will be shipped as an autonomous individual member, and only at run-time all component will be finally bound together.<br>
The good new is that this way each library will be (hopefully) installed only once on your system, and that every executable depending on that library will load exactly the same binary code, thus granting a robust consistency.<br>
The bad new is in that if a single required component couldn't be loaded at run-time (for any possible reason) the executable as a whole will fail to run.<br>
To make things even worst, this schema works nicely on Linux, Unix and Mac OS X (because all these o.s. adopts strong rules intended to enforce overall system robustness); but on Windows it's not at all difficult to create the infamous <b><i>DLL hell</i></b>, simply because this o.s. substantially hasn't strong rules to be respected, thus vanishing any possible system consistency.</li>
<li>using real <b><i>dynamic linkage</i></b> (which isn't the same of simply using shared libraries).<br>
Following this third approach an extension module could be selectively loaded or not; if for any reason the main executable fails to load an eventual extension module, it will still continue to work (obviously missing to support the failing extension).<br>
Happily enough SQLite3 fully supports this third more sophisticated mechanism; and this option is widely supported by many language connectors.</li>
</li>
</ul><br>
<h3>How it works</h3>
<i>libsqlite3</i> has the capability to dynamically load extension modules. This feature is available as a C-language API, but is avaibles as a <i>special</i> SQL function as well:
<verbatim>
SELECT load_extension('mod_spatialite');
</verbatim>
This SQL function make elementary simple (and language independent) the problem of loading any possible extension; you are simply expected to execute a rather trivial SQL statement in order to load an external extension.<br><br>
Loadable extension anathomy:
<ul>
<li>any SQLite extension module is expected to declare a conventional interface.<br>
Except for this, any SQLite's own extension simply is an ordinary shared library or DLL as any other.</li>
<li>if such interface isn't correctly declared by the shared library being loaded, then SQLite3 will simply assume that it's not a valid extension module.<br>
And consequently will refuse to load the module without any other consequence (except may be printing some appropriate error message).</li>
<li>a more obvious failure cause could be the one to attempt loading a not existing module (or possibly, a module whom actual address couldn't be resolved following the standard seach rules).</li>
<li>finally, the module could effectively exists and could correctly declare a valid conventional interface. Anyway, if the corresponding shared library has further dependecies some broken link could possibly forbid a succesful loading.</li>
<li>the last hill-fated combination: SQLite3 is a strongly configurable component. If for any highly opinable choice your system packages had decided to disable at all the extension module mechamism you'll then never be able to load any extension. Full stop. (anyway you'll probably be able to switch to some more reasoble / less paranoid distribution).
</ul>
<hr><br>
back to <a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=4.2.0-doc">4.2.0-doc</a>
Z 8b2e823048b8ecede253987fe6050ada