Skip to content

Commit 8b9175d

Browse files
committed
Merge pull request #5 from editorsnotes/master
SQL backends not storing literals properly.
2 parents ba59458 + dbf1bc3 commit 8b9175d

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

rdflib_sqlalchemy/SQLAlchemy.py

Lines changed: 20 additions & 11 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 "'%s'" % param
335+
params = tuple(map(py_to_sql, params))
332336
querystr = qStr.replace('"', "'")
333337
cursor.execute(querystr % params)
334338

@@ -348,11 +352,16 @@ def pycompat_executeSQL(self, cursor, qStr, params=None, paramList=False):
348352
except:
349353
pass
350354

351-
def locproc(item):
355+
def py_to_sql(param):
356+
if param is None:
357+
return 'NULL'
358+
if isinstance(param, int):
359+
return param
352360
try:
353-
return "'%s'" % item.decode()
361+
return "'%s'" % param.decode()
354362
except:
355-
return item
363+
return param
364+
356365
# _logger.debug("SQLGenerator %s - %s" % (qStr,params))
357366
if not params:
358367
querystr = qStr.replace('"', "'")
@@ -365,7 +374,7 @@ def locproc(item):
365374
elif paramList:
366375
raise Exception("Not supported!")
367376
else:
368-
params = tuple([locproc(item) for item in params])
377+
params = tuple(map(py_to_sql, params))
369378
querystr = qStr.replace('"', "'")
370379
querystr = querystr % params
371380
# if isinstance(qStr, bytes): qStr = qStr.decode()
@@ -461,8 +470,8 @@ def buildLiteralTripleSQLCommand(
461470
self.normalizeTerm(obj),
462471
self.normalizeTerm(context.identifier),
463472
triplePattern,
464-
isinstance(obj, Literal) and obj.language or 'NULL',
465-
isinstance(obj, Literal) and obj.datatype or 'NULL']
473+
isinstance(obj, Literal) and obj.language or None,
474+
isinstance(obj, Literal) and obj.datatype or None]
466475

467476
def buildTripleSQLCommand(
468477
self, subject, predicate, obj, context, storeId, quoted):
@@ -484,8 +493,8 @@ def buildTripleSQLCommand(
484493
self.normalizeTerm(obj),
485494
self.normalizeTerm(context.identifier),
486495
triplePattern,
487-
isinstance(obj, Literal) and obj.language or 'NULL',
488-
isinstance(obj, Literal) and obj.datatype or 'NULL']
496+
isinstance(obj, Literal) and obj.language or None,
497+
isinstance(obj, Literal) and obj.datatype or None]
489498
else:
490499
command = "INSERT INTO %s " % stmt_table + \
491500
"(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.assertEquals(o.language, None)
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)