[Cubicweb] Using only Yams and RQL with SQLite

Yann Cointepas yann at sapetnioc.org
Fri Feb 26 14:15:11 CET 2010


Dear CubicWeb experts,

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.

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 is the code I used for my tests:

from yams.schema import Schema
from yams.schema2sql import schema2sql
from yams.buildobjs import register_base_types, EntityType, RelationType,
RelationDefinition, String
from logilab.common.db import get_connection
from logilab.common.adbh import get_adv_func_helper
from cubicweb.server.sources.rql2sql import SQLGenerator
from rql import RQLHelper
from cubicweb.server.rqlannotation import SQLGenAnnotator
from cubicweb.schema import CubicWebSchema

# Schema definition
#class Node( EntityType ):
#  name = String()

# Create schema (still incomplete)
schema = CubicWebSchema( 'Graph Schema' )
#register_base_types( schema )
schema.add_entity_type( EntityType( 'Node' ) )
#schema.add_entity_type( Node() )
schema.add_relation_type( RelationType( 'name' ) )
schema.add_relation_def( RelationDefinition( 'Node', 'name','String' ) )

# Create connection to memory database
dbhelper = get_adv_func_helper( 'sqlite' )
cnx = get_connection( driver='sqlite', database=':memory:' )
print cnx

# Put schema in database (to be improved)
sql = schema2sql( dbhelper, schema )
for r in sql.split( '\n\n' ):
  for r2 in r.split( ';' ):
    r2 = r2.strip()
    if r2:
      print '-->', r2
      cnx.cursor().execute( r2 )
cnx.commit()

# Use rql (does not work at all)
rql = 'INSERT Node X: X name "toto"'
r = RQLHelper( schema )
a = SQLGenAnnotator( schema )
g = SQLGenerator( schema, dbhelper )

rqlst = r.parse( rql )
a.annotate( rqlst )
print g.select_sql( rqlst )


Any help would be very appreciated,

      Yann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubicweb.org/pipermail/cubicweb/attachments/20100226/6a36f09d/attachment-0001.html>


More information about the Cubicweb mailing list