Skip to content

Commit afce0c9

Browse files
committed
Corrections for mysql. Still not work on python3
1 parent df520e8 commit afce0c9

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

rdflib_sqlalchemy/SQLAlchemy.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
RDF,
1212
URIRef
1313
)
14+
from rdflib.term import Node
1415
from rdflib.graph import Graph
1516
from rdflib.graph import QuotedGraph
1617
from rdflib.store import Store # , NodePickler
@@ -48,6 +49,8 @@
4849

4950
INTERNED_PREFIX = 'kb_'
5051

52+
MYSQL_MAX_INDEX_LENGTH = 200
53+
5154
Any = None
5255

5356
# Stolen from Will Waites' py4s
@@ -294,6 +297,8 @@ class TermType(types.TypeDecorator):
294297
def process_bind_param(self, value, dialect):
295298
if isinstance(value, (QuotedGraph, Graph)):
296299
return value.identifier
300+
elif isinstance(value, Node):
301+
return str(value) if PY3 else unicode(value)
297302
else:
298303
return value
299304

@@ -578,13 +583,8 @@ def open(self, configuration, create=True):
578583
name, opts = _parse_rfc1738_args(configuration)
579584
self.engine = sqlalchemy.create_engine(configuration)
580585
with self.engine.connect() as connection:
581-
trans = connection.begin()
582586
if create:
583-
try:
584-
self.metadata.create_all(self.engine)
585-
trans.commit()
586-
except Exception:
587-
trans.rollback()
587+
self.metadata.create_all(self.engine)
588588
#self._db.create_function("regexp", 2, regexp)
589589
if configuration:
590590
from sqlalchemy.engine import reflection
@@ -1211,10 +1211,10 @@ def __create_table_definitions(self):
12111211
nullable=False, key="termComb"),
12121212
Index("%s_A_termComb_index" % self._internedId,
12131213
'termComb'),
1214-
Index("%s_A_s_index" % self._internedId, 'subject'),
1215-
Index("%s_A_p_index" % self._internedId, 'predicate'),
1216-
Index("%s_A_o_index" % self._internedId, 'object'),
1217-
Index("%s_A_c_index" % self._internedId, 'context')),
1214+
Index("%s_A_s_index" % self._internedId, 'subject', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1215+
Index("%s_A_p_index" % self._internedId, 'predicate', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1216+
Index("%s_A_o_index" % self._internedId, 'object', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1217+
Index("%s_A_c_index" % self._internedId, 'context', mysql_length=MYSQL_MAX_INDEX_LENGTH)),
12181218
'type_statements':
12191219
Table('%s_type_statements' % self._internedId, self.metadata,
12201220
Column('member', TermType, nullable=False),
@@ -1224,9 +1224,9 @@ def __create_table_definitions(self):
12241224
key="termComb"),
12251225
Index("%s_T_termComb_index" % self._internedId,
12261226
'termComb'),
1227-
Index("%s_member_index" % self._internedId, 'member'),
1228-
Index("%s_klass_index" % self._internedId, 'klass'),
1229-
Index("%s_c_index" % self._internedId, 'context')),
1227+
Index("%s_member_index" % self._internedId, 'member', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1228+
Index("%s_klass_index" % self._internedId, 'klass', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1229+
Index("%s_c_index" % self._internedId, 'context', mysql_length=MYSQL_MAX_INDEX_LENGTH)),
12301230
'literal_statements':
12311231
Table(
12321232
'%s_literal_statements' % self._internedId, self.metadata,
@@ -1242,9 +1242,9 @@ def __create_table_definitions(self):
12421242
key="objDatatype"),
12431243
Index("%s_L_termComb_index" % self._internedId,
12441244
'termComb'),
1245-
Index("%s_L_s_index" % self._internedId, 'subject'),
1246-
Index("%s_L_p_index" % self._internedId, 'predicate'),
1247-
Index("%s_L_c_index" % self._internedId, 'context')),
1245+
Index("%s_L_s_index" % self._internedId, 'subject', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1246+
Index("%s_L_p_index" % self._internedId, 'predicate', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1247+
Index("%s_L_c_index" % self._internedId, 'context', mysql_length=MYSQL_MAX_INDEX_LENGTH)),
12481248
'quoted_statements':
12491249
Table(
12501250
"%s_quoted_statements" % self._internedId, self.metadata,
@@ -1260,17 +1260,17 @@ def __create_table_definitions(self):
12601260
key="objDatatype"),
12611261
Index("%s_Q_termComb_index" % self._internedId,
12621262
'termComb'),
1263-
Index("%s_Q_s_index" % self._internedId, 'subject'),
1264-
Index("%s_Q_p_index" % self._internedId, 'predicate'),
1265-
Index("%s_Q_o_index" % self._internedId, 'object'),
1266-
Index("%s_Q_c_index" % self._internedId, 'context')),
1263+
Index("%s_Q_s_index" % self._internedId, 'subject', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1264+
Index("%s_Q_p_index" % self._internedId, 'predicate', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1265+
Index("%s_Q_o_index" % self._internedId, 'object', mysql_length=MYSQL_MAX_INDEX_LENGTH),
1266+
Index("%s_Q_c_index" % self._internedId, 'context', mysql_length=MYSQL_MAX_INDEX_LENGTH)),
12671267
'namespace_binds':
12681268
Table(
12691269
"%s_namespace_binds" % self._internedId, self.metadata,
12701270
Column('prefix', types.String(20), unique=True,
12711271
nullable=False, primary_key=True),
12721272
Column('uri', types.Text),
1273-
Index("%s_uri_index" % self._internedId, 'uri'))
1273+
Index("%s_uri_index" % self._internedId, 'uri', mysql_length=MYSQL_MAX_INDEX_LENGTH))
12741274
}
12751275

12761276

test/test_sqlalchemy_mysql.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
raise SkipTest("MySQLdb not found, skipping MySQL tests")
1010
import logging
1111
_logger = logging.getLogger(__name__)
12-
import context_case
13-
import graph_case
12+
from . import context_case
13+
from . import graph_case
1414
from rdflib import Literal
1515

1616
# Specific to Travis-ci continuous integration and testing ...
1717
sqlalchemy_url = Literal(os.environ.get(
1818
'DBURI',
19-
"mysql://[email protected]:3306/rdflibsqla_test"))
19+
"mysql://[email protected]:3306/rdflibsqla_test?charset=utf8"))
2020
# Generally ...
2121
# sqlalchemy_url = Literal(
2222
# "mysql+mysqldb://user:password@hostname:port/database?charset=utf8")
@@ -26,31 +26,33 @@ class SQLAMySQLGraphTestCase(graph_case.GraphTestCase):
2626
storetest = True
2727
storename = "SQLAlchemy"
2828
uri = sqlalchemy_url
29+
create = True
2930

3031
def setUp(self):
31-
graph_case.GraphTestCase.setUp(
32-
self, uri=self.uri, storename=self.storename)
32+
super(SQLAMySQLGraphTestCase,self).setUp(
33+
uri=self.uri, storename=self.storename)
3334

3435
def tearDown(self):
35-
graph_case.GraphTestCase.tearDown(self, uri=self.uri)
36+
super(SQLAMySQLGraphTestCase, self).tearDown(uri=self.uri)
3637

3738

3839
class SQLAMySQLContextTestCase(context_case.ContextTestCase):
3940
storetest = True
4041
storename = "SQLAlchemy"
4142
uri = sqlalchemy_url
43+
create = True
4244

4345
def setUp(self):
44-
context_case.ContextTestCase.setUp(
45-
self, uri=self.uri, storename=self.storename)
46+
super(SQLAMySQLContextTestCase, self).setUp(
47+
uri=self.uri, storename=self.storename)
4648

4749
def tearDown(self):
48-
context_case.ContextTestCase.tearDown(self, uri=self.uri)
50+
super(SQLAMySQLContextTestCase, self).tearDown(uri=self.uri)
4951

5052
def testLenInMultipleContexts(self):
5153
raise SkipTest("Known issue.")
5254

53-
55+
5456
class SQLAMySQLIssueTestCase(unittest.TestCase):
5557
storetest = True
5658
storename = "SQLAlchemy"

test/test_sqlalchemy_postgresql.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class SQLAPgSQLGraphTestCase(graph_case.GraphTestCase):
2424
create = True
2525

2626
def setUp(self):
27-
graph_case.GraphTestCase.setUp(
28-
self, uri=self.uri, storename=self.storename)
27+
super(SQLAPgSQLGraphTestCase, self).setUp(
28+
uri=self.uri, storename=self.storename)
2929

3030
def tearDown(self):
31-
graph_case.GraphTestCase.tearDown(self, uri=self.uri)
31+
super(SQLAPgSQLGraphTestCase, self).tearDown(uri=self.uri)
3232

3333

3434
class SQLAPgSQLContextTestCase(context_case.ContextTestCase):
@@ -38,11 +38,11 @@ class SQLAPgSQLContextTestCase(context_case.ContextTestCase):
3838
create = True
3939

4040
def setUp(self):
41-
context_case.ContextTestCase.setUp(
42-
self, uri=self.uri, storename=self.storename)
41+
super(SQLAPgSQLContextTestCase, self).setUp(
42+
uri=self.uri, storename=self.storename)
4343

4444
def tearDown(self):
45-
context_case.ContextTestCase.tearDown(self, uri=self.uri)
45+
super(SQLAPgSQLContextTestCase, self).tearDown(uri=self.uri)
4646

4747
def testLenInMultipleContexts(self):
4848
raise SkipTest("Known issue.")

test/test_sqlalchemy_sqlite.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class SQLASQLiteGraphTestCase(graph_case.GraphTestCase):
1818
uri = sqlalchemy_url
1919

2020
def setUp(self):
21-
graph_case.GraphTestCase.setUp(
22-
self, uri=self.uri, storename=self.storename)
21+
super(SQLASQLiteGraphTestCase, self).setUp(
22+
uri=self.uri, storename=self.storename)
2323

2424
def tearDown(self):
25-
graph_case.GraphTestCase.tearDown(self, uri=self.uri)
25+
super(SQLASQLiteGraphTestCase, self).tearDown(uri=self.uri)
2626

2727

2828
class SQLASQLiteContextTestCase(context_case.ContextTestCase):
@@ -31,11 +31,11 @@ class SQLASQLiteContextTestCase(context_case.ContextTestCase):
3131
uri = sqlalchemy_url
3232

3333
def setUp(self):
34-
context_case.ContextTestCase.setUp(
35-
self, uri=self.uri, storename=self.storename)
34+
super(SQLASQLiteContextTestCase, self).setUp(
35+
uri=self.uri, storename=self.storename)
3636

3737
def tearDown(self):
38-
context_case.ContextTestCase.tearDown(self, uri=self.uri)
38+
super(SQLASQLiteContextTestCase, self).tearDown(uri=self.uri)
3939

4040
def testLenInMultipleContexts(self):
4141
raise SkipTest("Known issue.")

0 commit comments

Comments
 (0)