Skip to content

Commit dbe774c

Browse files
committed
Fix issue with sqlite not storing literals properly.
1 parent ba59458 commit dbe774c

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

rdflib_sqlalchemy/SQLAlchemy.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,13 @@ def py2executeSQL(self, cursor, qStr, params=None, paramList=False):
326326
elif paramList:
327327
raise Exception("Not supported!")
328328
else:
329-
params = tuple([
330-
not isinstance(item, int) and u"'%s'" % item or item
331-
for item in params])
329+
def py_to_sql(param):
330+
if param is None:
331+
return 'NULL'
332+
if isinstance(param, int):
333+
return param
334+
return u"'%s'" % param
335+
params = tuple(map(py_to_sql, params))
332336
querystr = qStr.replace('"', "'")
333337
cursor.execute(querystr % params)
334338

@@ -461,8 +465,8 @@ def buildLiteralTripleSQLCommand(
461465
self.normalizeTerm(obj),
462466
self.normalizeTerm(context.identifier),
463467
triplePattern,
464-
isinstance(obj, Literal) and obj.language or 'NULL',
465-
isinstance(obj, Literal) and obj.datatype or 'NULL']
468+
isinstance(obj, Literal) and obj.language or None,
469+
isinstance(obj, Literal) and obj.datatype or None]
466470

467471
def buildTripleSQLCommand(
468472
self, subject, predicate, obj, context, storeId, quoted):
@@ -484,8 +488,8 @@ def buildTripleSQLCommand(
484488
self.normalizeTerm(obj),
485489
self.normalizeTerm(context.identifier),
486490
triplePattern,
487-
isinstance(obj, Literal) and obj.language or 'NULL',
488-
isinstance(obj, Literal) and obj.datatype or 'NULL']
491+
isinstance(obj, Literal) and obj.language or None,
492+
isinstance(obj, Literal) and obj.datatype or None]
489493
else:
490494
command = "INSERT INTO %s " % stmt_table + \
491495
"(subject,predicate,object,context,termComb) " + \

test/graph_case.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
13
import unittest
24
from rdflib import Graph
35
from rdflib import RDF
46
from rdflib import URIRef
7+
from rdflib import Literal
58
from rdflib import plugin
69
from rdflib.store import Store
710

@@ -268,6 +271,29 @@ def testGraphIntersection(self):
268271

269272
self.assertEquals((michel, likes, cheese) in g1, True)
270273

274+
def testStoreLiterals(self):
275+
bob = self.bob
276+
says = URIRef(u'says')
277+
hello = Literal(u'hello', lang='en')
278+
konichiwa = Literal(u'こんにちは', lang='ja')
279+
something = Literal(u'something')
280+
281+
self.graph.add((bob, says, hello))
282+
self.graph.add((bob, says, konichiwa))
283+
self.graph.add((bob, says, something))
284+
self.graph.commit()
285+
286+
objs = list(self.graph.objects(subject=bob, predicate=says))
287+
for o in objs:
288+
if o.value == u'hello':
289+
self.assertEquals(o.language, 'en')
290+
elif o.value == u'こんにちは':
291+
self.assertEquals(o.language, 'ja')
292+
elif o.value == u'something':
293+
self.assertIsNone(o.language)
294+
else:
295+
self.fail()
296+
self.assertEquals(len(list(objs)), 3)
271297

272298
xmltestdoc = """<?xml version="1.0" encoding="UTF-8"?>
273299
<rdf:RDF

test/test_sqlalchemy_sqlite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import graph_case
1010
from rdflib import Literal
1111

12-
sqlalchemy_url = Literal("sqlite://")
12+
sqlalchemy_url = Literal(os.environ.get('DBURI',"sqlite://"))
1313

1414

1515
class SQLASQLiteGraphTestCase(graph_case.GraphTestCase):

0 commit comments

Comments
 (0)