Debian Codegraph @

rdflib_sqlalchemy/base.py

raw symbols SCIP
1 """Base classes for the store."""
2 from rdflib import Literal
3 from rdflib.graph import Graph, QuotedGraph
4 from rdflib.plugins.stores.regexmatching import REGEXTerm
5 from sqlalchemy.sql import expression
6
7 from rdflib_sqlalchemy.termutils import (
8 type_to_term_combination,
9 statement_to_term_combination,
10 )
11
12
13 class SQLGeneratorMixin(object):
14 """SQL statement generator mixin for the SQLAlchemy store."""
15
16 def _build_type_sql_command(self, member, klass, context):
17 """Build an insert command for a type table."""
18 # columns: member,klass,context
19 rt = self.tables["type_statements"].insert()
20 return rt, {
21 "member": member,
22 "klass": klass,
23 "context": context.identifier,
24 "termComb": int(type_to_term_combination(member, klass, context))
25 }
26
27 def _build_literal_triple_sql_command(self, subject, predicate, obj, context):
28 """
29 Build an insert command for literal triples.
30
31 These triples correspond to RDF statements where the object is a Literal,
32 e.g. `rdflib.Literal`.
33
34 """
35 triple_pattern = int(
36 statement_to_term_combination(subject, predicate, obj, context)
37 )
38
39 command = self.tables["literal_statements"].insert()
40 values = {
41 "subject": subject,
42 "predicate": predicate,
43 "object": obj,
44 "context": context.identifier,
45 "termComb": triple_pattern,
46 "objLanguage": isinstance(obj, Literal) and obj.language or None,
47 "objDatatype": isinstance(obj, Literal) and obj.datatype or None,
48 }
49 return command, values
50
51 def _build_triple_sql_command(self, subject, predicate, obj, context, quoted):
52 """
53 Build an insert command for regular triple table.
54
55 """
56 stmt_table = (
57 self.tables["quoted_statements"]
58 if quoted
59 else self.tables["asserted_statements"]
60 )
61
62 triple_pattern = statement_to_term_combination(
63 subject,
64 predicate,
65 obj,
66 context,
67 )
68 command = stmt_table.insert()
69
70 if quoted:
71 params = {
72 "subject": subject,
73 "predicate": predicate,
74 "object": obj,
75 "context": context.identifier,
76 "termComb": triple_pattern,
77 "objLanguage": isinstance(obj, Literal) and obj.language or None,
78 "objDatatype": isinstance(obj, Literal) and obj.datatype or None
79 }
80 else:
81 params = {
82 "subject": subject,
83 "predicate": predicate,
84 "object": obj,
85 "context": context.identifier,
86 "termComb": triple_pattern,
87 }
88 return command, params
89
90 def build_clause(self, table, subject, predicate, obj, context=None, typeTable=False):
91 """Build WHERE clauses for the supplied terms and, context."""
92 if typeTable:
93 clauseList = [
94 self.build_type_member_clause(subject, table),
95 self.build_type_class_clause(obj, table),
96 self.build_context_clause(context, table)
97 ]
98 else:
99 clauseList = [
100 self.build_subject_clause(subject, table),
101 self.build_predicate_clause(predicate, table),
102 self.build_object_clause(obj, table),
103 self.build_context_clause(context, table),
104 self.build_literal_datatype_clause(obj, table),
105 self.build_literal_language_clause(obj, table)
106 ]
107
108 clauseList = [clause for clause in clauseList if clause is not None]
109 if clauseList:
110 return expression.and_(*clauseList)
111 else:
112 return None
113
114 def build_literal_datatype_clause(self, obj, table):
115 """Build Literal and datatype clause."""
116 if isinstance(obj, Literal) and obj.datatype is not None:
117 return table.c.objDatatype == obj.datatype
118 else:
119 return None
120
121 def build_literal_language_clause(self, obj, table):
122 """Build Literal and language clause."""
123 if isinstance(obj, Literal) and obj.language is not None:
124 return table.c.objLanguage == obj.language
125 else:
126 return None
127
128 # Where Clause utility Functions
129 # The predicate and object clause builders are modified in order
130 # to optimize subjects and objects utility functions which can
131 # take lists as their last argument (object, predicate - respectively)
132
133 def build_subject_clause(self, subject, table):
134 """Build Subject clause."""
135 if isinstance(subject, REGEXTerm):
136 # TODO: this work only in mysql. Must adapt for postgres and sqlite
137 return table.c.subject.op("REGEXP")(subject)
138 elif isinstance(subject, list):
139 # clauseStrings = [] --- unused
140 return expression.or_(
141 *[self.build_subject_clause(s, table) for s in subject if s])
142 elif isinstance(subject, (QuotedGraph, Graph)):
143 return table.c.subject == subject.identifier
144 elif subject is not None:
145 return table.c.subject == subject
146 else:
147 return None
148
149 def build_predicate_clause(self, predicate, table):
150 """
151 Build Predicate clause.
152
153 Capable of taking a list of predicates as well, in which case
154 subclauses are joined with 'OR'.
155
156 """
157 if isinstance(predicate, REGEXTerm):
158 # TODO: this work only in mysql. Must adapt for postgres and sqlite
159 return table.c.predicate.op("REGEXP")(predicate)
160 elif isinstance(predicate, list):
161 return expression.or_(
162 *[self.build_predicate_clause(p, table) for p in predicate if p])
163 elif predicate is not None:
164 return table.c.predicate == predicate
165 else:
166 return None
167
168 def build_object_clause(self, obj, table):
169 """
170 Build Object clause.
171
172 Capable of taking a list of objects as well, in which case subclauses
173 are joined with 'OR'.
174
175 """
176 if isinstance(obj, REGEXTerm):
177 # TODO: this work only in mysql. Must adapt for postgres and sqlite
178 return table.c.object.op("REGEXP")(obj)
179 elif isinstance(obj, list):
180 return expression.or_(
181 *[self.build_object_clause(o, table) for o in obj if o])
182 elif isinstance(obj, (QuotedGraph, Graph)):
183 return table.c.object == obj.identifier
184 elif obj is not None:
185 return table.c.object == obj
186 else:
187 return None
188
189 def build_context_clause(self, context, table):
190 """Build Context clause."""
191 if isinstance(context, REGEXTerm):
192 # TODO: this work only in mysql. Must adapt for postgres and sqlite
193 return table.c.context.op("regexp")(context.identifier)
194 elif context is not None and context.identifier is not None:
195 return table.c.context == context.identifier
196 else:
197 return None
198
199 def build_type_member_clause(self, subject, table):
200 """Build Type Member clause."""
201 if isinstance(subject, REGEXTerm):
202 # TODO: this work only in mysql. Must adapt for postgres and sqlite
203 return table.c.member.op("regexp")(subject)
204 elif isinstance(subject, list):
205 return expression.or_(
206 *[self.build_type_member_clause(s, table) for s in subject if s])
207 elif subject is not None:
208 return table.c.member == subject
209 else:
210 return None
211
212 def build_type_class_clause(self, obj, table):
213 """Build Type Class clause."""
214 if isinstance(obj, REGEXTerm):
215 # TODO: this work only in mysql. Must adapt for postgres and sqlite
216 return table.c.klass.op("regexp")(obj)
217 elif isinstance(obj, list):
218 return expression.or_(
219 *[self.build_type_class_clause(o, table) for o in obj if o])
220 elif obj is not None:
221 return obj and table.c.klass == obj
222 else:
223 return None