Esempio n. 1
0
 def check_good(self, q, sql):
     select = self.syntax.parse(q, {})
     qb = QueryBuilder(select)
     sql2 = qb.build()
     self.assertEqual(sql, sql2)
Esempio n. 2
0
 def sparql_query(self, q, mime_type, default_graph_uri, timeout):
     """Execute a SPARQL query
     @param query The query string
     @param mime_type The requested MIME type
     @param default_graph_uri The default graph URI
     @param start_response The response object
     @param timeout The timeout (in seconds) on the query
     """
     try:
         syntax = YuzuQLSyntax()
         try:
             select = syntax.parse(q, {
                 PREFIX1_QN: FullURI("<%s>" % PREFIX1_URI),
                 PREFIX2_QN: FullURI("<%s>" % PREFIX2_URI),
                 PREFIX3_QN: FullURI("<%s>" % PREFIX3_URI),
                 PREFIX4_QN: FullURI("<%s>" % PREFIX4_URI),
                 PREFIX5_QN: FullURI("<%s>" % PREFIX5_URI),
                 PREFIX6_QN: FullURI("<%s>" % PREFIX6_URI),
                 PREFIX7_QN: FullURI("<%s>" % PREFIX7_URI),
                 PREFIX8_QN: FullURI("<%s>" % PREFIX8_URI),
                 PREFIX9_QN: FullURI("<%s>" % PREFIX9_URI),
                 "rdf": FullURI("<http://www.w3.org/1999/02/22-"
                                "rdf-syntax-ns#>"),
                 "rdfs": FullURI("<http://www.w3.org/2000/01/rdf-schema#>"),
                 "owl": FullURI("<http://www.w3.org/2002/07/owl#>"),
                 "dc": FullURI("<http://purl.org/dc/elements/1.1/>"),
                 "dct": FullURI("<http://purl.org/dc/terms>"),
                 "xsd": FullURI("<http://www.w3.org/2001/XMLSchema#>")})
         except YuzuQLError as e:
             return False, 'error', e.value
         if select.limit < 0 or (select.limit >= YUZUQL_LIMIT and
                                 YUZUQL_LIMIT >= 0):
             return False, 'error', YZ_QUERY_LIMIT_EXCEEDED % YUZUQL_LIMIT
         qb = QueryBuilder(select)
         sql_query = qb.build()
         conn = sqlite3.connect(self.db)
         cursor = conn.cursor()
         cursor.execute(sql_query)
         vars = qb.vars()
         if mime_type == "sparql-json":
             results = sql_results_to_sparql_json(cursor.fetchall(), vars)
         else:
             results = sql_results_to_sparql_xml(cursor.fetchall(), vars)
         conn.close()
         return False, 'sparql', results
     except Exception as e:
         if SPARQL_ENDPOINT:
             graph = Graph('SPARQLStore')
             try:
                 graph.open(SPARQL_ENDPOINT)
                 try:
                     if default_graph_uri:
                         qres = graph.query(q, initNs=default_graph_uri)
                     else:
                         qres = graph.query(q)
                 except Exception as e:
                     traceback.print_exc()
                     print(e)
                     return False, 'error', YZ_BAD_REQUEST
                 if qres.type == "CONSTRUCT" or qres.type == "DESCRIBE":
                     if mime_type == "html" or mime_type == "json-ld":
                         mime_type == "pretty-xml"
                         return (False, mime_type,
                                 qres.serialize(format=mime_type))
                 elif (self.mime_type == 'sparql' or
                       self.mime_type == 'sparql-json' or
                       self.mime_type == 'html'):
                     return False, 'sparql', qres.serialize()
                 else:
                     return False, 'error', YZ_BAD_MIME
             finally:
                 graph.close()
         else:
             traceback.print_exc()
             print(e)
             return False, 'error', ""