Skip to content

Commit 061b01d

Browse files
committed
Ensure db param is quoted. Correct self.bind query string composition. Finesse various infelicities.
1 parent 31278ad commit 061b01d

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

rdflib_sqlalchemy/SQLAlchemy.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import logging
44
import sqlalchemy
55
import hashlib
6-
from rdflib import BNode
7-
from rdflib import Literal
8-
from rdflib import RDF
9-
from rdflib import URIRef
6+
from rdflib import (
7+
BNode,
8+
Literal,
9+
RDF,
10+
URIRef
11+
)
1012
from rdflib.graph import Graph
1113
from rdflib.graph import QuotedGraph
1214
from rdflib.store import Store # , NodePickler
@@ -224,7 +226,7 @@ def createTerm(
224226
rt = Literal(termString, objLanguage)
225227
store.literalCache[((termString, objLanguage))] = rt
226228
elif objDatatype and not objLanguage:
227-
rt = Literal(termString, objDatatype)
229+
rt = Literal(termString, datatype=objDatatype)
228230
store.literalCache[((termString, objDatatype))] = rt
229231
elif not objLanguage and not objDatatype:
230232
rt = Literal(termString)
@@ -319,7 +321,7 @@ def py2executeSQL(self, cursor, qStr, params=None, paramList=False):
319321
(here) is to fill the parameters in-place surrounding each param
320322
with quote characters
321323
"""
322-
# print("SQLGenerator", qStr,params)
324+
# print("py2executeSQL", qStr, params)
323325
if not params:
324326
querystr = qStr.replace('"', "'")
325327
cursor.execute(unicode(querystr))
@@ -345,22 +347,23 @@ def pycompat_executeSQL(self, cursor, qStr, params=None, paramList=False):
345347
with quote characters
346348
"""
347349
# if isinstance(qStr, bytes): qStr = qStr.decode()
350+
# print("pycompat_executeSQL", qStr, params)
348351
try:
349352
if qStr is None:
350353
raise ValueError("Query must be a string, it cannot be None")
351354
qStr = qStr.decode()
352355
except:
353356
pass
354357

355-
def py_to_sql(param):
358+
def py3_to_sql(param):
356359
if param is None:
357360
return 'NULL'
358361
if isinstance(param, int):
359362
return param
360363
try:
361364
return "'%s'" % param.decode()
362365
except:
363-
return param
366+
return "'%s'" % param
364367

365368
# _logger.debug("SQLGenerator %s - %s" % (qStr,params))
366369
if not params:
@@ -374,7 +377,7 @@ def py_to_sql(param):
374377
elif paramList:
375378
raise Exception("Not supported!")
376379
else:
377-
params = tuple(map(py_to_sql, params))
380+
params = tuple(map(py3_to_sql, params))
378381
querystr = qStr.replace('"', "'")
379382
querystr = querystr % params
380383
# if isinstance(qStr, bytes): qStr = qStr.decode()
@@ -1331,10 +1334,10 @@ def __repr__(self):
13311334
"contexts, %s classification assertions, " +
13321335
"%s quoted statements, %s property/value " +
13331336
"assertions, and %s other assertions>" % (
1334-
len([c for c in self.contexts()]),
1337+
len([ctx for ctx in self.contexts()]),
13351338
typeLen, quotedLen, literalLen, assertedLen))
13361339
except Exception:
1337-
return "<Partitioned MySQL N3 Store>"
1340+
return "<Partitioned SQL N3 Store>"
13381341

13391342
def __len__(self, context=None):
13401343
""" Number of statements in the store. """
@@ -1599,8 +1602,8 @@ def bind(self, prefix, namespace):
15991602
trans = self.connection.begin()
16001603
try:
16011604
c.execute(
1602-
"INSERT INTO %s_namespace_binds " +
1603-
"(prefix,uri) VALUES ('%s', '%s')" % (
1605+
("INSERT INTO %s_namespace_binds " +
1606+
"(prefix,uri) VALUES ('%s', '%s')") % (
16041607
self._internedId,
16051608
prefix,
16061609
namespace))

0 commit comments

Comments
 (0)