<s> from rdflib . term import BNode , URIRef , Literal <EOL> from rdflib . namespace import RDF <EOL> try : <EOL> from hashlib import sha1 <EOL> except ImportError : <EOL> from sha import sha as sha1 <EOL> from rdfextras . utils . termutils import REVERSE_TERM_COMBINATIONS <EOL> from rdfextras . utils . termutils import TERM_INSTANTIATION_DICT <EOL> from rdfextras . utils . termutils import constructGraph <EOL> from rdfextras . utils . termutils import type2TermCombination <EOL> from rdfextras . utils . termutils import statement2TermCombination <EOL> from rdfextras . utils . termutils import escape_quotes <EOL> from rdfextras . store . REGEXMatching import PYTHON_REGEX <EOL> from rdfextras . store . REGEXMatching import REGEXTerm <EOL> from rdflib . graph import Graph <EOL> from rdflib . graph import QuotedGraph <EOL> from rdflib . store import Store <EOL> from rdflib . py3compat import PY3 <EOL> def bb ( u ) : return u . encode ( '<STR_LIT:utf-8>' ) <EOL> Any = None <EOL> COUNT_SELECT = <NUM_LIT:0> <EOL> CONTEXT_SELECT = <NUM_LIT:1> <EOL> TRIPLE_SELECT = <NUM_LIT:2> <EOL> TRIPLE_SELECT_NO_ORDER = <NUM_LIT:3> <EOL> ASSERTED_NON_TYPE_PARTITION = <NUM_LIT:3> <EOL> ASSERTED_TYPE_PARTITION = <NUM_LIT:4> <EOL> QUOTED_PARTITION = <NUM_LIT:5> <EOL> ASSERTED_LITERAL_PARTITION = <NUM_LIT:6> <EOL> FULL_TRIPLE_PARTITIONS = [ QUOTED_PARTITION , ASSERTED_LITERAL_PARTITION ] <EOL> INTERNED_PREFIX = '<STR_LIT>' <EOL> def skolemise ( statement ) : <EOL> def _sk ( x ) : <EOL> if isinstance ( x , BNode ) : <EOL> return URIRef ( "<STR_LIT>" % x ) <EOL> return x <EOL> return tuple ( map ( _sk , statement ) ) <EOL> def deskolemise ( statement ) : <EOL> def _dst ( x ) : <EOL> if isinstance ( x , URIRef ) and x . startswith ( "<STR_LIT>" ) : <EOL> _unused , bnid = x . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> return BNode ( bnid ) <EOL> return x <EOL> return tuple ( map ( _dst , statement ) ) <EOL> def queryAnalysis ( query , store , cursor ) : <EOL> """<STR_LIT>""" <EOL> cursor . execute ( store . _normalizeSQLCmd ( '<STR_LIT>' + query ) ) <EOL> rt = cursor . fetchall ( ) [ <NUM_LIT:0> ] <EOL> table , joinType , posKeys , _key , key_len , comparedCol , rowsExamined , extra = rt <EOL> if not _key : <EOL> assert joinType == '<STR_LIT>' <EOL> if not hasattr ( store , '<STR_LIT>' ) : <EOL> store . queryOptMarks = { } <EOL> hits = store . queryOptMarks . get ( ( '<STR_LIT>' , table ) , <NUM_LIT:0> ) <EOL> store . queryOptMarks [ ( '<STR_LIT>' , table ) ] = hits + <NUM_LIT:1> <EOL> if not hasattr ( store , '<STR_LIT>' ) : <EOL> store . queryOptMarks = { } <EOL> hits = store . queryOptMarks . get ( ( _key , table ) , <NUM_LIT:0> ) <EOL> store . queryOptMarks [ ( _key , table ) ] = hits + <NUM_LIT:1> <EOL> def unionSELECT ( selectComponents , distinct = False , selectType = TRIPLE_SELECT ) : <EOL> """<STR_LIT>""" <EOL> selects = [ ] <EOL> for tableName , tableAlias , whereClause , tableType in selectComponents : <EOL> if selectType == COUNT_SELECT : <EOL> selectString = "<STR_LIT>" <EOL> tableSource = "<STR_LIT>" % tableName <EOL> elif selectType == CONTEXT_SELECT : <EOL> selectString = "<STR_LIT>" % tableAlias <EOL> tableSource = "<STR_LIT>" % ( tableName , tableAlias ) <EOL> elif tableType in FULL_TRIPLE_PARTITIONS : <EOL> selectString = "<STR_LIT>" <EOL> tableSource = "<STR_LIT>" % ( tableName , tableAlias ) <EOL> elif tableType == ASSERTED_TYPE_PARTITION : <EOL> selectString = """<STR_LIT>""" % ( tableAlias , RDF . type , tableAlias , tableAlias , tableAlias ) <EOL> tableSource = "<STR_LIT>" % ( tableName , tableAlias ) <EOL> elif tableType == ASSERTED_NON_TYPE_PARTITION : <EOL> selectString = """<STR_LIT>""" <EOL> tableSource = "<STR_LIT>" % ( tableName , tableAlias ) <EOL> selects . append ( selectString + tableSource + whereClause ) <EOL> orderStmt = '<STR_LIT>' <EOL> if selectType == TRIPLE_SELECT : <EOL> orderStmt = '<STR_LIT>' <EOL> if distinct : <EOL> return '<STR_LIT>' . join ( selects ) + orderStmt <EOL> else : <EOL> return '<STR_LIT>' . join ( selects ) + orderStmt <EOL> def extractTriple ( tupleRt , store , hardCodedContext = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> subject , predicate , obj , rtContext , termComb , objLanguage , objDatatype = tupleRt <EOL> termCombString = REVERSE_TERM_COMBINATIONS [ termComb ] <EOL> subjTerm , predTerm , objTerm , ctxTerm = termCombString <EOL> except ValueError : <EOL> subject , subjTerm , predicate , predTerm , obj , objTerm , rtContext , ctxTerm , objLanguage , objDatatype = tupleRt <EOL> context = rtContext is not None and rtContext or hardCodedContext . identifier <EOL> s = createTerm ( subject , subjTerm , store ) <EOL> p = createTerm ( predicate , predTerm , store ) <EOL> o = createTerm ( obj , objTerm , store , objLanguage , objDatatype ) <EOL> graphKlass , idKlass = constructGraph ( ctxTerm ) <EOL> return s , p , o , ( graphKlass , idKlass , context ) <EOL> def createTerm ( termString , termType , store , objLanguage = None , objDatatype = None ) : <EOL> """<STR_LIT>""" <EOL> if termType == '<STR_LIT:L>' : <EOL> cache = store . literalCache . get ( ( termString , objLanguage , objDatatype ) ) <EOL> if cache is not None : <EOL> return cache <EOL> else : <EOL> if objLanguage and not objDatatype : <EOL> rt = Literal ( termString , lang = objLanguage ) <EOL> store . literalCache [ ( ( termString , objLanguage ) ) ] = rt <EOL> elif objDatatype and not objLanguage : <EOL> rt = Literal ( termString , datatype = objDatatype ) <EOL> store . literalCache [ ( ( termString , objDatatype ) ) ] = rt <EOL> elif not objLanguage and not objDatatype : <EOL> rt = Literal ( termString ) <EOL> store . literalCache [ ( ( termString ) ) ] = rt <EOL> else : <EOL> rt = Literal ( termString , datatype = objDatatype ) <EOL> store . literalCache [ ( ( termString , objDatatype ) ) ] = rt <EOL> return rt <EOL> elif termType == '<STR_LIT:F>' : <EOL> cache = store . otherCache . get ( ( termType , termString ) ) <EOL> if cache is not None : <EOL> return cache <EOL> else : <EOL> rt = QuotedGraph ( store , URIRef ( termString ) ) <EOL> store . otherCache [ ( termType , termString ) ] = rt <EOL> return rt <EOL> elif termType == '<STR_LIT:B>' : <EOL> cache = store . bnodeCache . get ( ( termString ) ) <EOL> if cache is not None : <EOL> return cache <EOL> else : <EOL> rt = TERM_INSTANTIATION_DICT [ termType ] ( termString ) <EOL> store . bnodeCache [ ( termString ) ] = rt <EOL> return rt <EOL> elif termType == '<STR_LIT>' : <EOL> cache = store . uriCache . get ( ( termString ) ) <EOL> if cache is not None : <EOL> return cache <EOL> else : <EOL> rt = URIRef ( termString ) <EOL> store . uriCache [ ( termString ) ] = rt <EOL> return rt <EOL> else : <EOL> cache = store . otherCache . get ( ( termType , termString ) ) <EOL> if cache is not None : <EOL> return cache <EOL> else : <EOL> rt = TERM_INSTANTIATION_DICT [ termType ] ( termString ) <EOL> store . otherCache [ ( termType , termString ) ] = rt <EOL> return rt <EOL> class SQLGenerator : <EOL> def executeSQL ( self , cursor , qStr , params = None , paramList = False ) : <EOL> """<STR_LIT>""" <EOL> if not params : <EOL> cursor . execute ( unicode ( qStr ) ) <EOL> elif paramList : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> params = tuple ( [ not isinstance ( item , int ) and u"<STR_LIT>" % item or item <EOL> for item in params ] ) <EOL> querystr = unicode ( qStr ) . replace ( '<STR_LIT:">' , "<STR_LIT:'>" ) <EOL> cursor . execute ( querystr % params ) <EOL> def EscapeQuotes ( self , qstr ) : <EOL> return escape_quotes ( qstr ) <EOL> def _normalizeSQLCmd ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> if PY3 : <EOL> if not isinstance ( cmd , str ) : <EOL> cmd = str ( cmd , '<STR_LIT:ascii>' ) <EOL> return cmd <EOL> else : <EOL> import types <EOL> if not isinstance ( cmd , types . UnicodeType ) : <EOL> cmd = unicode ( cmd , '<STR_LIT:ascii>' ) <EOL> return cmd . encode ( '<STR_LIT:utf-8>' ) <EOL> def normalizeTerm ( self , term ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( term , ( QuotedGraph , Graph ) ) : <EOL> return term . identifier . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( term , Literal ) : <EOL> return self . EscapeQuotes ( term ) . encode ( '<STR_LIT:utf-8>' ) <EOL> elif term is None or isinstance ( term , ( tuple , list , REGEXTerm ) ) : <EOL> return term <EOL> else : <EOL> return term . encode ( '<STR_LIT:utf-8>' ) <EOL> def buildTypeSQLCommand ( self , member , klass , context , storeId ) : <EOL> """<STR_LIT>""" <EOL> rt = "<STR_LIT>" % storeId + "<STR_LIT>" <EOL> return rt , [ <EOL> self . normalizeTerm ( member ) , <EOL> self . normalizeTerm ( klass ) , <EOL> self . normalizeTerm ( context . identifier ) , <EOL> int ( type2TermCombination ( member , klass , context ) ) ] <EOL> def buildLiteralTripleSQLCommand ( self , subject , predicate , obj , context , storeId ) : <EOL> """<STR_LIT>""" <EOL> triplePattern = int ( statement2TermCombination ( subject , predicate , obj , context ) ) <EOL> literal_table = "<STR_LIT>" % storeId <EOL> command = "<STR_LIT>" % literal_table + "<STR_LIT>" <EOL> return command , [ <EOL> self . normalizeTerm ( subject ) , <EOL> self . normalizeTerm ( predicate ) , <EOL> self . normalizeTerm ( obj ) , <EOL> self . normalizeTerm ( context . identifier ) , <EOL> triplePattern , <EOL> isinstance ( obj , Literal ) and obj . language or '<STR_LIT>' , <EOL> isinstance ( obj , Literal ) and obj . datatype or '<STR_LIT>' ] <EOL> def buildTripleSQLCommand ( self , subject , predicate , obj , context , storeId , quoted ) : <EOL> """<STR_LIT>""" <EOL> stmt_table = quoted and "<STR_LIT>" % storeId or "<STR_LIT>" % storeId <EOL> triplePattern = statement2TermCombination ( subject , predicate , obj , context ) <EOL> if quoted : <EOL> command = "<STR_LIT>" % stmt_table + "<STR_LIT>" <EOL> params = [ <EOL> self . normalizeTerm ( subject ) , <EOL> self . normalizeTerm ( predicate ) , <EOL> self . normalizeTerm ( obj ) , <EOL> self . normalizeTerm ( context . identifier ) , <EOL> triplePattern , <EOL> isinstance ( obj , Literal ) and obj . language or '<STR_LIT>' , <EOL> isinstance ( obj , Literal ) and obj . datatype or '<STR_LIT>' ] <EOL> else : <EOL> command = "<STR_LIT>" % stmt_table + "<STR_LIT>" <EOL> params = [ <EOL> self . normalizeTerm ( subject ) , <EOL> self . normalizeTerm ( predicate ) , <EOL> self . normalizeTerm ( obj ) , <EOL> self . normalizeTerm ( context . identifier ) , <EOL> triplePattern ] <EOL> return command , params <EOL> def buildClause ( self , tableName , subject , predicate , obj , context = None , typeTable = False ) : <EOL> """<STR_LIT>""" <EOL> parameters = [ ] <EOL> if typeTable : <EOL> rdf_type_memberClause = rdf_type_contextClause = rdf_type_contextClause = None <EOL> clauseParts = self . buildTypeMemberClause ( self . normalizeTerm ( subject ) , tableName ) <EOL> if clauseParts is not None : <EOL> rdf_type_memberClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauseParts = self . buildTypeClassClause ( self . normalizeTerm ( obj ) , tableName ) <EOL> if clauseParts is not None : <EOL> rdf_type_klassClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( clauseParts [ - <NUM_LIT:1> ] ) <EOL> clauseParts = self . buildContextClause ( context , tableName ) <EOL> if clauseParts is not None : <EOL> rdf_type_contextClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> typeClauses = [ rdf_type_memberClause , rdf_type_klassClause , rdf_type_contextClause ] <EOL> clauseString = '<STR_LIT>' . join ( [ clause for clause in typeClauses if clause ] ) <EOL> clauseString = clauseString and '<STR_LIT>' + clauseString or '<STR_LIT>' <EOL> else : <EOL> subjClause = predClause = objClause = contextClause = litDTypeClause = litLanguageClause = None <EOL> clauseParts = self . buildSubjClause ( self . normalizeTerm ( subject ) , tableName ) <EOL> if clauseParts is not None : <EOL> subjClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauseParts = self . buildPredClause ( self . normalizeTerm ( predicate ) , tableName ) <EOL> if clauseParts is not None : <EOL> predClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauseParts = self . buildObjClause ( self . normalizeTerm ( obj ) , tableName ) <EOL> if clauseParts is not None : <EOL> objClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauseParts = self . buildContextClause ( context , tableName ) <EOL> if clauseParts is not None : <EOL> contextClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauseParts = self . buildLitDTypeClause ( obj , tableName ) <EOL> if clauseParts is not None : <EOL> litDTypeClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauseParts = self . buildLitLanguageClause ( obj , tableName ) <EOL> if clauseParts is not None : <EOL> litLanguageClause = clauseParts [ <NUM_LIT:0> ] <EOL> parameters . extend ( [ param for param in clauseParts [ - <NUM_LIT:1> ] if param is not None ] ) <EOL> clauses = [ subjClause , predClause , objClause , contextClause , litDTypeClause , litLanguageClause ] <EOL> clauseString = '<STR_LIT>' . join ( [ clause for clause in clauses if clause ] ) <EOL> clauseString = clauseString and '<STR_LIT>' + clauseString or '<STR_LIT>' <EOL> return clauseString , [ p for p in parameters if p is not None ] <EOL> def buildLitDTypeClause ( self , obj , tableName ) : <EOL> if isinstance ( obj , Literal ) : <EOL> return obj . datatype is not None and ( <EOL> "<STR_LIT>" % ( tableName ) + "<STR_LIT:%s>" , [ obj . datatype . encode ( '<STR_LIT:utf-8>' ) ] <EOL> ) or None <EOL> else : <EOL> return None <EOL> def buildLitLanguageClause ( self , obj , tableName ) : <EOL> if isinstance ( obj , Literal ) : <EOL> return obj . language is not None and ( <EOL> "<STR_LIT>" % ( tableName ) + "<STR_LIT:%s>" , [ obj . language . encode ( '<STR_LIT:utf-8>' ) ] <EOL> ) or None <EOL> else : <EOL> return None <EOL> def buildSubjClause ( self , subject , tableName ) : <EOL> pass <EOL> def buildPredClause ( self , predicate , tableName ) : <EOL> pass <EOL> def buildObjClause ( self , obj , tableName ) : <EOL> pass <EOL> def buildContextClause ( self , context , tableName ) : <EOL> pass <EOL> def buildTypeMemberClause ( self , subject , tableName ) : <EOL> pass <EOL> def buildTypeClassClause ( self , obj , tableName ) : <EOL> pass <EOL> class AbstractSQLStore ( Store , SQLGenerator ) : <EOL> """<STR_LIT>""" <EOL> context_aware = True <EOL> formula_aware = True <EOL> transaction_aware = True <EOL> regex_matching = PYTHON_REGEX <EOL> autocommit_default = True <EOL> def __init__ ( self , identifier = None , configuration = None ) : <EOL> """<STR_LIT>""" <EOL> self . identifier = identifier and identifier or '<STR_LIT>' <EOL> self . _internedId = INTERNED_PREFIX + sha1 ( self . identifier . encode ( '<STR_LIT:utf8>' ) ) . hexdigest ( ) [ : <NUM_LIT:10> ] <EOL> self . STRONGLY_TYPED_TERMS = False <EOL> if configuration is not None : <EOL> self . open ( configuration ) <EOL> self . cacheHits = <NUM_LIT:0> <EOL> self . cacheMisses = <NUM_LIT:0> <EOL> self . literalCache = { } <EOL> self . uriCache = { } <EOL> self . bnodeCache = { } <EOL> self . otherCache = { } <EOL> self . _db = None <EOL> self . __node_pickler = None <EOL> def close ( self , commit_pending_transaction = False ) : <EOL> """<STR_LIT>""" <EOL> if commit_pending_transaction : <EOL> self . _db . commit ( ) <EOL> try : <EOL> self . _db . close ( ) <EOL> except : <EOL> pass <EOL> def add ( self , ( subject , predicate , obj ) , context = None , quoted = False ) : <EOL> """<STR_LIT>""" <EOL> c = self . _db . cursor ( ) <EOL> if self . autocommit_default : <EOL> c . execute ( """<STR_LIT>""" ) <EOL> if quoted or predicate != RDF . type : <EOL> if isinstance ( obj , Literal ) : <EOL> addCmd , params = self . buildLiteralTripleSQLCommand ( <EOL> subject , predicate , obj , context , self . _internedId ) <EOL> else : <EOL> addCmd , params = self . buildTripleSQLCommand ( <EOL> subject , predicate , obj , context , self . _internedId , quoted ) <EOL> elif predicate == RDF . type : <EOL> addCmd , params = self . buildTypeSQLCommand ( <EOL> subject , obj , context , self . _internedId ) <EOL> self . executeSQL ( c , addCmd , params ) <EOL> c . close ( ) <EOL> def addN ( self , quads ) : <EOL> c = self . _db . cursor ( ) <EOL> if self . autocommit_default : <EOL> c . execute ( """<STR_LIT>""" ) <EOL> literalTriples = [ ] <EOL> typeTriples = [ ] <EOL> otherTriples = [ ] <EOL> literalTripleInsertCmd = None <EOL> typeTripleInsertCmd = None <EOL> otherTripleInsertCmd = None <EOL> for subject , predicate , obj , context in quads : <EOL> if isinstance ( context , QuotedGraph ) or predicate != RDF . type : <EOL> if isinstance ( obj , Literal ) : <EOL> cmd , params = self . buildLiteralTripleSQLCommand ( <EOL> subject , predicate , obj , context , self . _internedId ) <EOL> literalTripleInsertCmd = literalTripleInsertCmd is not None and literalTripleInsertCmd or cmd <EOL> literalTriples . append ( params ) <EOL> else : <EOL> cmd , params = self . buildTripleSQLCommand ( <EOL> subject , predicate , obj , context , self . _internedId , isinstance ( context , QuotedGraph ) ) <EOL> otherTripleInsertCmd = otherTripleInsertCmd is not None and otherTripleInsertCmd or cmd <EOL> otherTriples . append ( params ) <EOL> elif predicate == RDF . type : <EOL> cmd , params = self . buildTypeSQLCommand ( <EOL> subject , obj , context , self . _internedId ) <EOL> typeTripleInsertCmd = typeTripleInsertCmd is not None and typeTripleInsertCmd or cmd <EOL> typeTriples . append ( params ) <EOL> if literalTriples : <EOL> self . executeSQL ( <EOL> c , literalTripleInsertCmd , literalTriples , paramList = True ) <EOL> if typeTriples : <EOL> self . executeSQL ( <EOL> c , typeTripleInsertCmd , typeTriples , paramList = True ) <EOL> if otherTriples : <EOL> self . executeSQL ( <EOL> c , otherTripleInsertCmd , otherTriples , paramList = True ) <EOL> c . close ( ) <EOL> def remove ( self , ( subject , predicate , obj ) , context ) : <EOL> """<STR_LIT>""" <EOL> if context is not None : <EOL> if subject is None and predicate is None and object is None : <EOL> self . _remove_context ( context ) <EOL> return <EOL> c = self . _db . cursor ( ) <EOL> if self . autocommit_default : <EOL> c . execute ( """<STR_LIT>""" ) <EOL> quoted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_type_table = "<STR_LIT>" % self . _internedId <EOL> literal_table = "<STR_LIT>" % self . _internedId <EOL> if not predicate or predicate != RDF . type : <EOL> if not self . STRONGLY_TYPED_TERMS or isinstance ( obj , Literal ) : <EOL> clauseString , params = self . buildClause ( <EOL> literal_table , subject , predicate , obj , context ) <EOL> if clauseString : <EOL> cmd = "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( [ literal_table , clauseString ] ) <EOL> else : <EOL> cmd = "<STR_LIT>" + literal_table <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( cmd ) , params ) <EOL> for table in [ quoted_table , asserted_table ] : <EOL> if table == asserted_table and isinstance ( obj , Literal ) : <EOL> continue <EOL> else : <EOL> clauseString , params = self . buildClause ( <EOL> table , subject , predicate , obj , context ) <EOL> if clauseString : <EOL> cmd = "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( [ table , clauseString ] ) <EOL> else : <EOL> cmd = "<STR_LIT>" + table <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( cmd ) , params ) <EOL> if predicate == RDF . type or not predicate : <EOL> clauseString , params = self . buildClause ( <EOL> asserted_type_table , subject , RDF . type , obj , context , True ) <EOL> if clauseString : <EOL> cmd = "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( [ asserted_type_table , clauseString ] ) <EOL> else : <EOL> cmd = '<STR_LIT>' + asserted_type_table <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( cmd ) , params ) <EOL> clauseString , params = self . buildClause ( <EOL> quoted_table , subject , predicate , obj , context ) <EOL> if clauseString : <EOL> cmd = clauseString and "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( [ quoted_table , clauseString ] ) <EOL> else : <EOL> cmd = "<STR_LIT>" + quoted_table <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( cmd ) , params ) <EOL> c . close ( ) <EOL> def triples ( self , ( subject , predicate , obj ) , context = None ) : <EOL> """<STR_LIT>""" <EOL> quoted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_type_table = "<STR_LIT>" % self . _internedId <EOL> literal_table = "<STR_LIT>" % self . _internedId <EOL> c = self . _db . cursor ( ) <EOL> parameters = [ ] <EOL> if predicate == RDF . type : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , RDF . type , obj , context , True ) <EOL> parameters . extend ( params ) <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ] <EOL> elif isinstance ( predicate , REGEXTerm ) and predicate . compiledExpr . match ( RDF . type ) or not predicate : <EOL> selects = [ ] <EOL> if not self . STRONGLY_TYPED_TERMS or isinstance ( obj , Literal ) or not obj or ( self . STRONGLY_TYPED_TERMS and isinstance ( obj , REGEXTerm ) ) : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj , context ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) ) <EOL> if not isinstance ( obj , Literal ) and not ( isinstance ( obj , REGEXTerm ) and self . STRONGLY_TYPED_TERMS ) or not obj : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj , context ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) ) <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , RDF . type , obj , context , True ) <EOL> parameters . extend ( params ) <EOL> selects . append ( <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) <EOL> ) <EOL> elif predicate : <EOL> selects = [ ] <EOL> if not self . STRONGLY_TYPED_TERMS or isinstance ( obj , Literal ) or not obj or ( self . STRONGLY_TYPED_TERMS and isinstance ( obj , REGEXTerm ) ) : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj , context ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) ) <EOL> if not isinstance ( obj , Literal ) and not ( isinstance ( obj , REGEXTerm ) and self . STRONGLY_TYPED_TERMS ) or not obj : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj , context ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) ) <EOL> if context is not None : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj , context ) <EOL> parameters . extend ( params ) <EOL> selects . append ( <EOL> ( <EOL> quoted_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> QUOTED_PARTITION <EOL> ) <EOL> ) <EOL> q = self . _normalizeSQLCmd ( unionSELECT ( selects ) ) <EOL> self . executeSQL ( c , q , parameters ) <EOL> rt = c . fetchone ( ) <EOL> while rt : <EOL> s , p , o , ( graphKlass , idKlass , graphId ) = extractTriple ( rt , self , context ) <EOL> currentContext = graphKlass ( self , idKlass ( graphId ) ) <EOL> contexts = [ currentContext ] <EOL> rt = next = c . fetchone ( ) <EOL> sameTriple = next and extractTriple ( next , self , context ) [ : <NUM_LIT:3> ] == ( s , p , o ) <EOL> while sameTriple : <EOL> s2 , p2 , o2 , ( graphKlass , idKlass , graphId ) = extractTriple ( next , self , context ) <EOL> c2 = graphKlass ( self , idKlass ( graphId ) ) <EOL> contexts . append ( c2 ) <EOL> rt = next = c . fetchone ( ) <EOL> sameTriple = next and extractTriple ( next , self , context ) [ : <NUM_LIT:3> ] == ( s , p , o ) <EOL> yield ( s , p , o ) , ( c for c in contexts ) <EOL> def triples_choices ( self , ( subject , predicate , object_ ) , context = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( object_ , list ) : <EOL> assert not isinstance ( subject , list ) , "<STR_LIT>" <EOL> assert not isinstance ( predicate , list ) , "<STR_LIT>" <EOL> if not object_ : <EOL> object_ = None <EOL> for ( s1 , p1 , o1 ) , cg in self . triples ( <EOL> ( subject , predicate , object_ ) , context ) : <EOL> yield ( s1 , p1 , o1 ) , cg <EOL> elif isinstance ( subject , list ) : <EOL> assert not isinstance ( predicate , list ) , "<STR_LIT>" <EOL> if not subject : <EOL> subject = None <EOL> for ( s1 , p1 , o1 ) , cg in self . triples ( <EOL> ( subject , predicate , object_ ) , context ) : <EOL> yield ( s1 , p1 , o1 ) , cg <EOL> elif isinstance ( predicate , list ) : <EOL> assert not isinstance ( subject , list ) , "<STR_LIT>" <EOL> if not predicate : <EOL> predicate = None <EOL> for ( s1 , p1 , o1 ) , cg in self . triples ( <EOL> ( subject , predicate , object_ ) , context ) : <EOL> yield ( s1 , p1 , o1 ) , cg <EOL> def __repr__ ( self ) : <EOL> c = self . _db . cursor ( ) <EOL> quoted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_type_table = "<STR_LIT>" % self . _internedId <EOL> literal_table = "<STR_LIT>" % self . _internedId <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> quoted_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> QUOTED_PARTITION <EOL> ) , <EOL> ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) , <EOL> ] <EOL> q = unionSELECT ( selects , distinct = False , selectType = COUNT_SELECT ) <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( q ) ) <EOL> rt = c . fetchall ( ) <EOL> typeLen , quotedLen , assertedLen , literalLen = [ rtTuple [ <NUM_LIT:0> ] for rtTuple in rt ] <EOL> try : <EOL> return "<STR_LIT>" % ( <EOL> len ( [ c for c in self . contexts ( ) ] ) , typeLen , quotedLen , literalLen , assertedLen ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> def __len__ ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> c = self . _db . cursor ( ) <EOL> quoted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_type_table = "<STR_LIT>" % self . _internedId <EOL> literal_table = "<STR_LIT>" % self . _internedId <EOL> parameters = [ ] <EOL> quotedContext = assertedContext = typeContext = literalContext = None <EOL> clauseParts = self . buildContextClause ( context , quoted_table ) <EOL> if clauseParts : <EOL> quotedContext , params = clauseParts <EOL> parameters . extend ( [ p for p in params if p ] ) <EOL> clauseParts = self . buildContextClause ( context , asserted_table ) <EOL> if clauseParts : <EOL> assertedContext , params = clauseParts <EOL> parameters . extend ( [ p for p in params if p ] ) <EOL> clauseParts = self . buildContextClause ( context , asserted_type_table ) <EOL> if clauseParts : <EOL> typeContext , params = clauseParts <EOL> parameters . extend ( [ p for p in params if p ] ) <EOL> clauseParts = self . buildContextClause ( context , literal_table ) <EOL> if clauseParts : <EOL> literalContext , params = clauseParts <EOL> parameters . extend ( [ p for p in params if p ] ) <EOL> if context is not None : <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> typeContext and '<STR_LIT>' + typeContext or '<STR_LIT>' , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> quoted_table , <EOL> '<STR_LIT>' , <EOL> quotedContext and '<STR_LIT>' + quotedContext or '<STR_LIT>' , <EOL> QUOTED_PARTITION <EOL> ) , <EOL> ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> assertedContext and '<STR_LIT>' + assertedContext or '<STR_LIT>' , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> literalContext and '<STR_LIT>' + literalContext or '<STR_LIT>' , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) , <EOL> ] <EOL> q = unionSELECT ( selects , distinct = True , selectType = COUNT_SELECT ) <EOL> else : <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> typeContext and '<STR_LIT>' + typeContext or '<STR_LIT>' , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> assertedContext and '<STR_LIT>' + assertedContext or '<STR_LIT>' , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> literalContext and '<STR_LIT>' + literalContext or '<STR_LIT>' , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) , <EOL> ] <EOL> q = unionSELECT ( selects , distinct = False , selectType = COUNT_SELECT ) <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( q ) , parameters ) <EOL> rt = c . fetchall ( ) <EOL> c . close ( ) <EOL> return reduce ( lambda x , y : x + y , [ rtTuple [ <NUM_LIT:0> ] for rtTuple in rt ] ) <EOL> def contexts ( self , triple = None ) : <EOL> c = self . _db . cursor ( ) <EOL> quoted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_type_table = "<STR_LIT>" % self . _internedId <EOL> literal_table = "<STR_LIT>" % self . _internedId <EOL> parameters = [ ] <EOL> if triple is not None : <EOL> subject , predicate , obj = triple <EOL> if predicate == RDF . type : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , RDF . type , obj , Any , True ) <EOL> parameters . extend ( params ) <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ] <EOL> elif isinstance ( predicate , REGEXTerm ) and predicate . compiledExpr . match ( RDF . type ) or not predicate : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , RDF . type , obj , Any , True ) <EOL> parameters . extend ( params ) <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ] <EOL> if not self . STRONGLY_TYPED_TERMS or isinstance ( obj , Literal ) or not obj or ( self . STRONGLY_TYPED_TERMS and isinstance ( obj , REGEXTerm ) ) : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) ) <EOL> if not isinstance ( obj , Literal ) and not ( isinstance ( obj , REGEXTerm ) and self . STRONGLY_TYPED_TERMS ) or not obj : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) ) <EOL> elif predicate : <EOL> selects = [ ] <EOL> if not self . STRONGLY_TYPED_TERMS or isinstance ( obj , Literal ) or not obj or ( self . STRONGLY_TYPED_TERMS and isinstance ( obj , REGEXTerm ) ) : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) ) <EOL> if not isinstance ( obj , Literal ) and not ( isinstance ( obj , REGEXTerm ) and self . STRONGLY_TYPED_TERMS ) or not obj : <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj ) <EOL> parameters . extend ( params ) <EOL> selects . append ( ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) ) <EOL> clauseString , params = self . buildClause ( <EOL> '<STR_LIT>' , subject , predicate , obj ) <EOL> parameters . extend ( params ) <EOL> selects . append ( <EOL> ( <EOL> quoted_table , <EOL> '<STR_LIT>' , <EOL> clauseString , <EOL> QUOTED_PARTITION <EOL> ) <EOL> ) <EOL> q = unionSELECT ( selects , distinct = True , selectType = CONTEXT_SELECT ) <EOL> else : <EOL> selects = [ <EOL> ( <EOL> asserted_type_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ASSERTED_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> quoted_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> QUOTED_PARTITION <EOL> ) , <EOL> ( <EOL> asserted_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ASSERTED_NON_TYPE_PARTITION <EOL> ) , <EOL> ( <EOL> literal_table , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ASSERTED_LITERAL_PARTITION <EOL> ) , <EOL> ] <EOL> q = unionSELECT ( selects , distinct = True , selectType = CONTEXT_SELECT ) <EOL> self . executeSQL ( c , self . _normalizeSQLCmd ( q ) , parameters ) <EOL> rt = c . fetchall ( ) <EOL> for context in [ rtTuple [ <NUM_LIT:0> ] for rtTuple in rt ] : <EOL> yield context <EOL> c . close ( ) <EOL> def _remove_context ( self , identifier ) : <EOL> """<STR_LIT:U+0020>""" <EOL> assert identifier <EOL> c = self . _db . cursor ( ) <EOL> if self . autocommit_default : <EOL> c . execute ( """<STR_LIT>""" ) <EOL> quoted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_table = "<STR_LIT>" % self . _internedId <EOL> asserted_type_table = "<STR_LIT>" % self . _internedId <EOL> literal_table = "<STR_LIT>" % self . _internedId <EOL> for table in [ quoted_table , asserted_table , <EOL> asserted_type_table , literal_table ] : <EOL> clauseString , params = self . buildContextClause ( identifier , table ) <EOL> self . executeSQL ( <EOL> c , <EOL> self . _normalizeSQLCmd ( "<STR_LIT>" % ( table , clauseString ) ) , <EOL> [ p for p in params if p ] <EOL> ) <EOL> c . close ( ) <EOL> def subjects ( self , predicate = None , obj = None ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def objects ( self , subject = None , predicate = None ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def predicate_objects ( self , subject = None ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def subject_objects ( self , predicate = None ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def subject_predicates ( self , object = None ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def value ( self , subject , predicate = u'<STR_LIT>' , object = None , default = None , any = False ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def bind ( self , prefix , namespace ) : <EOL> """<STR_LIT:U+0020>""" <EOL> c = self . _db . cursor ( ) <EOL> try : <EOL> c . execute ( "<STR_LIT>" % ( <EOL> self . _internedId , <EOL> prefix , <EOL> namespace ) <EOL> ) <EOL> except : <EOL> pass <EOL> c . close ( ) <EOL> def prefix ( self , namespace ) : <EOL> """<STR_LIT:U+0020>""" <EOL> c = self . _db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" % ( <EOL> self . _internedId , <EOL> namespace ) <EOL> ) <EOL> rt = [ rtTuple [ <NUM_LIT:0> ] for rtTuple in c . fetchall ( ) ] <EOL> c . close ( ) <EOL> return rt and rt [ <NUM_LIT:0> ] or None <EOL> def namespace ( self , prefix ) : <EOL> """<STR_LIT:U+0020>""" <EOL> c = self . _db . cursor ( ) <EOL> try : <EOL> c . execute ( "<STR_LIT>" % ( <EOL> self . _internedId , <EOL> prefix ) <EOL> ) <EOL> except : <EOL> return None <EOL> rt = [ rtTuple [ <NUM_LIT:0> ] for rtTuple in c . fetchall ( ) ] <EOL> c . close ( ) <EOL> return rt and rt [ <NUM_LIT:0> ] or None <EOL> def namespaces ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> c = self . _db . cursor ( ) <EOL> c . execute ( "<STR_LIT>" % ( <EOL> self . _internedId <EOL> ) <EOL> ) <EOL> rt = c . fetchall ( ) <EOL> c . close ( ) <EOL> for prefix , uri in rt : <EOL> yield prefix , uri <EOL> def commit ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . _db . commit ( ) <EOL> def rollback ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . _db . rollback ( ) <EOL> table_name_prefixes = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] </s>
