In Loki We Trust The many projects of Lokkju, Inc

rss
26Jul/102

Adding functionality to HTSQL v2

HTSQL is a very cool open source product that gives you a RESTful interface to multiple database backends. Because it uses it's own simple, but very powerful, syntax, you avoid most of the risks involved in passing in SQL. Currently it supports SQLite and PostgreSQL, but for my current project, I need to support Geometry columns.

For the first draft, I just wanted to add support for Spatialite, a spatially enabled version of SQLite. Since SQLite is already supported, this turned out to be relatively easy - though in hindsight, I may not even have implemented it in the easiest way possible - but I'll get to that in another post.

So, each database backend is in a namespace called htsql_[name], and they all subclass files in the main htsql namespace. I started off by cloning the htsql_sqlite tree into a new namespace called htsql_spatialite, and ripped out most of the code, leaving me with a basic structure. I then subclassed any SQLite classes I wanted to override - most importantly:

  1. Changed connect.py to import pyspatialite instead of pysqlite2.
  2. Added my own Column and Data types (Domains) in domain.py
  3. Modified introspect.py to handle my custom Domains, as well as to handle the blank Column type sometimes given for Geometry columns

I also, and here is where most of my functionality was added, overrode a few classes in tr/serializer.py:

  1. Class SpatialiteSerializeLeafReference(subclassing SerializeLeafReference) tests if I am selecting a Geometry column, and if I am, wraps it in the "AsText" function, to return WKT.
  2. a new Adaptor, FormatGeometry, which handled the representation of the WKT when returned to the client. Right now, only HTML is supported, but JSON, CSV, and the rest are easy to add in the same way.

The last thing you have to do is add a line in setup.py to point to your new database engine's entry point - it is in a list called ENTRY_POINTS.

Interestingly, I think I could better utilize the plugin architecture - but as I'm just discovering HTSQL, and there isn't all that many samples, nor much documentation, I'm pretty happy with what I accomplished.

You can see the full source of my additions at https://bitbucket.org/lokkju/htsql-appengine/src/3d7b7d8e8580/

   
home
about
trademarks