[Cubicweb] Using only Yams and RQL with SQLite

Sylvain Thénault sylvain.thenault at logilab.fr
Fri Feb 26 14:52:29 CET 2010

Hello Yann,

On 26 février 14:15, Yann Cointepas wrote:
> I wonder if it is possible for a Python script (without server, user
> authentication, etc.) to define a schema with Yams, create a database with
> SQLite and use RQL for querying. My goal is to find a suitable file format
> to store graphs, do queries on them and eventually put/pull them in/from
> real databases. I was thinking to use directly SQLite but I would really
> appreciate to have the power of RQL queries. And it would even make more
> sense if our databases are based on CubicWeb.

huumm, that looks like a very nice use case. We're very interested in
such usage of yams/rql outside the cubicweb framework (well, the heavy
part of it at list). Unfortunatly we've currently no time to do so but
if you've some, we'll definitly help you to acheive this!
> I looked at CubicWeb sources and test codes and I had been able to build an
> incomplete schema (for instance it has no "is" relation) and to get SQL code
> to generate some tables within a SQLite database but I did not find a way to
> use RQL queries on it.

Here are some guide lines:

* build the schema from yams objects directly is too much pain. You should
  be able to write it as usual with python files. If you don't want a whole
  cubicweb schema, something like:

    from yams.reader import SchemaLoader
    schema = SchemaLoader().load([<directory where schema.py may be found>])

  should load your schema.

* to handle rql, something like:

    from rql import RQLHelper
    from cubicweb.server.sources.rql2sql import SQLGenerator
    rqlh = RQLHelper(schema)
    sqlgen = SQLGenerator(schema, dbms_helper) # get dbms_helper for sqlite from logilab.common.adbh

    def rql2sql(rql, args):
      """return sql query and args to give to sql cursor:
      cu.execute(*rql2sql('Any X,Y WHERE X relation Y'))
      rqlst = rqlh.parse(rql)
      rqlh.compute_solutions(rqlst, kwargs=args)
      return sqlgen.generate(rqlst, args)

this is untested pseudo-code but it should put you on the right way. You'll 
probably encounter some problems due to missing cw specific stuff in the
schema, but I don't think anything non tweakable. Don't hesitate to ask
more question, and let us tuned about your progress!

Sylvain Thénault                               LOGILAB, Paris (France)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
CubicWeb, the semantic web framework:    http://www.cubicweb.org

More information about the Cubicweb mailing list