<s> '''<STR_LIT>''' <EOL> import time , datetime <EOL> from arelle . ModelDocument import Type <EOL> from arelle . ModelDtsObject import ModelConcept , ModelResource <EOL> from arelle . ModelValue import qname <EOL> from arelle . ValidateXbrlCalcs import roundValue <EOL> from arelle . XmlUtil import elementFragmentIdentifier <EOL> from arelle import XbrlConst <EOL> from . SqlDb import XPDBException , isSqlConnection , SqlDbConnection <EOL> def insertIntoDB ( modelXbrl , <EOL> user = None , password = None , host = None , port = None , database = None , timeout = None , <EOL> product = "<STR_LIT>" , rssItem = None , ** kwargs ) : <EOL> xpgdb = None <EOL> try : <EOL> xpgdb = XbrlPostgresDatabaseConnection ( modelXbrl , user , password , host , port , database , timeout , product ) <EOL> xpgdb . verifyTables ( ) <EOL> xpgdb . insertXbrl ( rssItem = rssItem ) <EOL> xpgdb . close ( ) <EOL> except Exception as ex : <EOL> if xpgdb is not None : <EOL> try : <EOL> xpgdb . close ( rollback = True ) <EOL> except Exception as ex2 : <EOL> pass <EOL> raise <EOL> def isDBPort ( host , port , timeout = <NUM_LIT:10> ) : <EOL> return isSqlConnection ( host , port , timeout , product = "<STR_LIT>" ) <EOL> XBRLDBTABLES = { <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> } <EOL> class XbrlPostgresDatabaseConnection ( SqlDbConnection ) : <EOL> def verifyTables ( self ) : <EOL> missingTables = XBRLDBTABLES - self . tablesInDB ( ) <EOL> if missingTables == XBRLDBTABLES : <EOL> self . create ( "<STR_LIT>" ) <EOL> self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT:description>' , ) , ( '<STR_LIT:description>' , ) , <EOL> ( ( '<STR_LIT>' , ) , ( '<STR_LIT>' , ) , ( '<STR_LIT:none>' , ) ) ) <EOL> self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT:description>' , ) , ( '<STR_LIT:description>' , ) , <EOL> ( ( '<STR_LIT>' , ) , ( '<STR_LIT>' , ) ) ) <EOL> self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT:description>' , ) , ( '<STR_LIT:description>' , ) , <EOL> ( ( '<STR_LIT>' , ) , ( '<STR_LIT>' , ) , ( '<STR_LIT>' , ) ) ) <EOL> missingTables = XBRLDBTABLES - self . tablesInDB ( ) <EOL> if missingTables : <EOL> raise XPDBException ( "<STR_LIT>" , <EOL> _ ( "<STR_LIT>" ) , <EOL> missingTableNames = '<STR_LIT:U+002CU+0020>' . join ( t for t in sorted ( missingTables ) ) ) <EOL> def insertXbrl ( self , rssItem ) : <EOL> try : <EOL> from arelle import ValidateXbrlDimensions <EOL> ValidateXbrlDimensions . loadDimensionDefaults ( self . modelXbrl ) <EOL> self . identifyPreexistingDocuments ( ) <EOL> self . identifyConceptsUsed ( ) <EOL> startedAt = time . time ( ) <EOL> self . insertAccession ( rssItem ) <EOL> self . insertUris ( ) <EOL> self . insertQnames ( ) <EOL> self . insertNamespaces ( ) <EOL> self . insertDocuments ( ) <EOL> self . insertCustomArcroles ( ) <EOL> self . insertCustomRoles ( ) <EOL> self . insertElements ( ) <EOL> self . insertResources ( ) <EOL> self . insertNetworks ( ) <EOL> self . modelXbrl . profileStat ( _ ( "<STR_LIT>" ) , time . time ( ) - startedAt ) <EOL> startedAt = time . time ( ) <EOL> self . insertFacts ( ) <EOL> self . modelXbrl . profileStat ( _ ( "<STR_LIT>" ) , time . time ( ) - startedAt ) <EOL> startedAt = time . time ( ) <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> self . commit ( ) <EOL> self . modelXbrl . profileStat ( _ ( "<STR_LIT>" ) , time . time ( ) - startedAt ) <EOL> self . showStatus ( "<STR_LIT>" , clearAfter = <NUM_LIT> ) <EOL> except Exception as ex : <EOL> self . showStatus ( "<STR_LIT>" , clearAfter = <NUM_LIT> ) <EOL> raise <EOL> def insertAccession ( self , rssItem ) : <EOL> self . accessionId = "<STR_LIT>" <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> if rssItem is None : <EOL> _time = time . time ( ) <EOL> now = datetime . datetime . fromtimestamp ( _time ) <EOL> today = datetime . date ( now . year , now . month , now . day ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( ( today , <EOL> <NUM_LIT:0> , <EOL> self . modelXbrl . modelDocument . creationSoftwareComment , <EOL> self . modelXbrl . uri , <EOL> str ( int ( _time ) ) <EOL> ) , ) , <EOL> checkIfExisting = True , <EOL> returnExistenceStatus = True ) <EOL> else : <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( ( rssItem . acceptanceDatetime , <EOL> True , <EOL> rssItem . filingDate or datetime . datetime . min , <EOL> rssItem . cikNumber or <NUM_LIT:0> , <EOL> rssItem . companyName , <EOL> self . modelXbrl . modelDocument . creationSoftwareComment , <EOL> rssItem . assignedSic or - <NUM_LIT:1> , <EOL> rssItem . htmlUrl , <EOL> rssItem . url , <EOL> rssItem . accessionNumber or '<STR_LIT>' <EOL> ) , ) , <EOL> checkIfExisting = True , <EOL> returnExistenceStatus = True ) <EOL> for id , filing_accession_number , existenceStatus in table : <EOL> self . accessionId = id <EOL> self . accessionPreviouslyInDB = existenceStatus <EOL> break <EOL> def insertUris ( self ) : <EOL> uris = ( _DICT_SET ( self . modelXbrl . namespaceDocs . keys ( ) ) | <EOL> _DICT_SET ( self . modelXbrl . arcroleTypes . keys ( ) ) | <EOL> _DICT_SET ( XbrlConst . standardArcroleCyclesAllowed . keys ( ) ) | <EOL> _DICT_SET ( self . modelXbrl . roleTypes . keys ( ) ) | <EOL> XbrlConst . standardRoles ) <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> tuple ( ( uri , ) <EOL> for uri in uris ) , <EOL> checkIfExisting = True ) <EOL> self . uriId = dict ( ( uri , id ) <EOL> for id , uri in table ) <EOL> def insertQnames ( self ) : <EOL> qnames = ( _DICT_SET ( self . modelXbrl . qnameConcepts . keys ( ) ) | <EOL> _DICT_SET ( self . modelXbrl . qnameAttributes . keys ( ) ) | <EOL> _DICT_SET ( self . modelXbrl . qnameTypes . keys ( ) ) | <EOL> set ( measure <EOL> for unit in self . modelXbrl . units . values ( ) <EOL> for measures in unit . measures <EOL> for measure in measures ) ) <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( qn . namespaceURI , qn . localName ) <EOL> for qn in qnames ) , <EOL> checkIfExisting = True ) <EOL> self . qnameId = dict ( ( qname ( ns , ln ) , id ) <EOL> for id , ns , ln in table ) <EOL> def insertNamespaces ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> if self . disclosureSystem . baseTaxonomyNamespaces : <EOL> namespaceUris = self . disclosureSystem . baseTaxonomyNamespaces <EOL> else : <EOL> namespaceUris = self . modelXbrl . namespaceDocs . keys ( ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> tuple ( ( uri , True , <NUM_LIT:0> , self . disclosureSystem . standardPrefixes . get ( uri , None ) ) <EOL> for uri in namespaceUris ) , <EOL> checkIfExisting = True ) <EOL> self . namespaceId = dict ( ( uri , id ) <EOL> for id , uri in table ) <EOL> def identifyPreexistingDocuments ( self ) : <EOL> self . existingDocumentIds = { } <EOL> docUris = set ( ) <EOL> for modelDocument in self . modelXbrl . urlDocs . values ( ) : <EOL> if modelDocument . type == Type . SCHEMA : <EOL> docUris . add ( self . dbStr ( modelDocument . uri ) ) <EOL> if docUris : <EOL> results = self . execute ( "<STR_LIT>" + <EOL> '<STR_LIT:U+002CU+0020>' . join ( docUris ) + "<STR_LIT>" ) <EOL> self . existingDocumentIds = dict ( ( docUri , docId ) for docId , docUri in results ) <EOL> def identifyConceptsUsed ( self ) : <EOL> self . relationshipSets = [ ( arcrole , ELR , linkqname , arcqname ) <EOL> for arcrole , ELR , linkqname , arcqname in self . modelXbrl . baseSets . keys ( ) <EOL> if ELR and ( arcrole . startswith ( "<STR_LIT>" ) or ( linkqname and arcqname ) ) ] <EOL> conceptsUsed = set ( f . qname for f in self . modelXbrl . factsInInstance ) <EOL> for cntx in self . modelXbrl . contexts . values ( ) : <EOL> for dim in cntx . qnameDims . values ( ) : <EOL> conceptsUsed . add ( dim . dimensionQname ) <EOL> if dim . isExplicit : <EOL> conceptsUsed . add ( dim . memberQname ) <EOL> else : <EOL> conceptsUsed . add ( dim . typedMember . qname ) <EOL> for defaultDim , defaultDimMember in self . modelXbrl . qnameDimensionDefaults . items ( ) : <EOL> conceptsUsed . add ( defaultDim ) <EOL> conceptsUsed . add ( defaultDimMember ) <EOL> for relationshipSetKey in self . relationshipSets : <EOL> relationshipSet = self . modelXbrl . relationshipSet ( * relationshipSetKey ) <EOL> for rel in relationshipSet . modelRelationships : <EOL> if isinstance ( rel . fromModelObject , ModelConcept ) : <EOL> conceptsUsed . add ( rel . fromModelObject ) <EOL> if isinstance ( rel . toModelObject , ModelConcept ) : <EOL> conceptsUsed . add ( rel . toModelObject ) <EOL> for qn in ( XbrlConst . qnXbrliIdentifier , XbrlConst . qnXbrliPeriod , XbrlConst . qnXbrliUnit ) : <EOL> conceptsUsed . add ( self . modelXbrl . qnameConcepts [ qn ] ) <EOL> conceptsUsed -= { None } <EOL> self . conceptsUsed = conceptsUsed <EOL> def insertDocuments ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> set ( ( docUri , ) <EOL> for docUri in self . modelXbrl . urlDocs . keys ( ) <EOL> if docUri not in self . existingDocumentIds ) , <EOL> checkIfExisting = True ) <EOL> self . documentIds = dict ( ( uri , id ) <EOL> for id , uri in table ) <EOL> self . documentIds . update ( self . existingDocumentIds ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> tuple ( ( self . accessionId , docId ) <EOL> for docId in self . documentIds . values ( ) ) , <EOL> checkIfExisting = True ) <EOL> def insertCustomArcroles ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> arcroleTypesByIds = dict ( ( ( self . documentIds [ arcroleType . modelDocument . uri ] , <EOL> self . uriId [ arcroleType . arcroleURI ] ) , <EOL> arcroleType ) <EOL> for arcroleTypes in self . modelXbrl . arcroleTypes . values ( ) <EOL> for arcroleType in arcroleTypes <EOL> if arcroleType . modelDocument . uri not in self . existingDocumentIds ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( arcroleTypeIDs [ <NUM_LIT:0> ] , <EOL> arcroleTypeIDs [ <NUM_LIT:1> ] , <EOL> arcroleType . definition , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:none>' : <NUM_LIT:3> } [ arcroleType . cyclesAllowed ] ) <EOL> for arcroleTypeIDs , arcroleType in arcroleTypesByIds . items ( ) ) ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( id , self . qnameId [ usedOn ] ) <EOL> for id , docid , uriid in table <EOL> for usedOn in arcroleTypesByIds [ ( docid , uriid ) ] . usedOns ) ) <EOL> def insertCustomRoles ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> roleTypesByIds = dict ( ( ( self . documentIds [ roleType . modelDocument . uri ] , <EOL> self . uriId [ roleType . roleURI ] ) , <EOL> roleType ) <EOL> for roleTypes in self . modelXbrl . roleTypes . values ( ) <EOL> for roleType in roleTypes <EOL> if roleType . modelDocument . uri not in self . existingDocumentIds ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( roleTypeIDs [ <NUM_LIT:0> ] , <EOL> roleTypeIDs [ <NUM_LIT:1> ] , <EOL> roleType . definition ) <EOL> for roleTypeIDs , roleType in roleTypesByIds . items ( ) ) ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( id , self . qnameId [ usedOn ] ) <EOL> for id , docid , uriid in table <EOL> for usedOn in roleTypesByIds [ ( docid , uriid ) ] . usedOns ) ) <EOL> def insertElements ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> filingDocumentConcepts = set ( ) <EOL> existingDocumentUsedConcepts = set ( ) <EOL> for concept in self . modelXbrl . qnameConcepts . values ( ) : <EOL> if concept . modelDocument . uri not in self . existingDocumentIds : <EOL> filingDocumentConcepts . add ( concept ) <EOL> elif concept in self . conceptsUsed : <EOL> existingDocumentUsedConcepts . add ( concept ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> tuple ( ( self . qnameId [ concept . qname ] , <EOL> self . qnameId . get ( concept . typeQname ) , <EOL> self . qnameId . get ( concept . baseXbrliTypeQname <EOL> if not isinstance ( concept . baseXbrliTypeQname , list ) <EOL> else concept . baseXbrliTypeQname [ <NUM_LIT:0> ] <EOL> ) , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , None : None } [ concept . balance ] , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:3> , None : <NUM_LIT:0> } [ concept . periodType ] , <EOL> self . qnameId . get ( concept . substitutionGroupQname ) , <EOL> concept . isAbstract , <EOL> concept . isNillable , <EOL> self . documentIds [ concept . modelDocument . uri ] , <EOL> concept . isNumeric , <EOL> concept . isMonetary ) <EOL> for concept in filingDocumentConcepts ) <EOL> ) <EOL> self . elementId = dict ( ( qnameId , elementId ) <EOL> for elementId , qnameId in table ) <EOL> filingDocumentConcepts . clear ( ) <EOL> if existingDocumentUsedConcepts : <EOL> conceptQnameIds = [ ] <EOL> for concept in existingDocumentUsedConcepts : <EOL> conceptQnameIds . append ( str ( self . qnameId [ concept . qname ] ) ) <EOL> results = self . execute ( "<STR_LIT>" + <EOL> '<STR_LIT:U+002CU+0020>' . join ( conceptQnameIds ) + "<STR_LIT>" ) <EOL> for elementId , qnameId in results : <EOL> self . elementId [ qnameId ] = elementId <EOL> existingDocumentUsedConcepts . clear ( ) <EOL> def conceptElementId ( self , concept ) : <EOL> if isinstance ( concept , ModelConcept ) : <EOL> return self . elementId . get ( self . qnameId . get ( concept . qname ) ) <EOL> else : <EOL> return None <EOL> def insertResources ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> uniqueResources = dict ( ( ( self . documentIds [ resource . modelDocument . uri ] , <EOL> resource . sourceline , <EOL> resource . objectIndex ) , resource ) <EOL> for arcrole in ( XbrlConst . conceptLabel , XbrlConst . conceptReference , <EOL> XbrlConst . elementLabel , XbrlConst . elementReference ) <EOL> for rel in self . modelXbrl . relationshipSet ( arcrole ) . modelRelationships <EOL> if rel . fromModelObject is not None and rel . toModelObject is not None <EOL> for resource in ( rel . fromModelObject , rel . toModelObject ) <EOL> if isinstance ( resource , ModelResource ) ) <EOL> resourceData = tuple ( ( self . uriId [ resource . role ] , <EOL> self . qnameId [ resource . qname ] , <EOL> self . documentIds [ resource . modelDocument . uri ] , <EOL> resource . sourceline , <EOL> resource . objectIndex ) <EOL> for resource in uniqueResources . values ( ) ) <EOL> uniqueResources . clear ( ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> resourceData , <EOL> checkIfExisting = True ) <EOL> self . resourceId = dict ( ( ( docId , line , offset ) , id ) <EOL> for id , docId , line , offset in table ) <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> uniqueResources = dict ( ( ( self . resourceId [ self . documentIds [ resource . modelDocument . uri ] , <EOL> resource . sourceline , <EOL> resource . objectIndex ] ) , resource ) <EOL> for arcrole in ( XbrlConst . conceptLabel , XbrlConst . elementLabel ) <EOL> for rel in self . modelXbrl . relationshipSet ( arcrole ) . modelRelationships <EOL> if rel . fromModelObject is not None and rel . toModelObject is not None <EOL> for resource in ( rel . fromModelObject , rel . toModelObject ) <EOL> if isinstance ( resource , ModelResource ) ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ) , <EOL> tuple ( ( resourceId , <EOL> resource . textValue , <EOL> resource . xmlLang ) <EOL> for resourceId , resource in uniqueResources . items ( ) ) , <EOL> checkIfExisting = True ) <EOL> uniqueResources . clear ( ) <EOL> def insertNetworks ( self ) : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( self . accessionId , <EOL> self . qnameId [ linkqname ] , <EOL> self . uriId [ ELR ] , <EOL> self . qnameId [ arcqname ] , <EOL> self . uriId [ arcrole ] , <EOL> None if ELR in XbrlConst . standardRoles else <EOL> self . modelXbrl . roleTypes [ ELR ] [ <NUM_LIT:0> ] . definition ) <EOL> for arcrole , ELR , linkqname , arcqname in self . modelXbrl . baseSets . keys ( ) <EOL> if ELR and linkqname and arcqname and not arcrole . startswith ( "<STR_LIT>" ) ) ) <EOL> self . networkId = dict ( ( ( accId , linkQnId , linkRoleId , arcQnId , arcRoleId ) , id ) <EOL> for id , accId , linkQnId , linkRoleId , arcQnId , arcRoleId in table ) <EOL> dbRels = [ ] <EOL> def walkTree ( rels , seq , depth , relationshipSet , visited , dbRels , networkId ) : <EOL> for rel in rels : <EOL> if rel not in visited and rel . toModelObject is not None : <EOL> visited . add ( rel ) <EOL> dbRels . append ( ( rel , seq , depth , networkId ) ) <EOL> seq += <NUM_LIT:1> <EOL> seq = walkTree ( relationshipSet . fromModelObject ( rel . toModelObject ) , seq , depth + <NUM_LIT:1> , relationshipSet , visited , dbRels , networkId ) <EOL> visited . remove ( rel ) <EOL> return seq <EOL> for arcrole , ELR , linkqname , arcqname in self . modelXbrl . baseSets . keys ( ) : <EOL> if ELR and linkqname and arcqname and not arcrole . startswith ( "<STR_LIT>" ) : <EOL> networkId = self . networkId [ ( self . accessionId , <EOL> self . qnameId [ linkqname ] , <EOL> self . uriId [ ELR ] , <EOL> self . qnameId [ arcqname ] , <EOL> self . uriId [ arcrole ] ) ] <EOL> relationshipSet = self . modelXbrl . relationshipSet ( arcrole , ELR , linkqname , arcqname ) <EOL> seq = <NUM_LIT:1> <EOL> for rootConcept in relationshipSet . rootConcepts : <EOL> seq = walkTree ( relationshipSet . fromModelObject ( rootConcept ) , seq , <NUM_LIT:1> , relationshipSet , set ( ) , dbRels , networkId ) <EOL> def resourceResourceId ( resource ) : <EOL> if isinstance ( resource , ModelResource ) : <EOL> return self . resourceId . get ( ( self . documentIds [ resource . modelDocument . uri ] , <EOL> resource . sourceline , <EOL> resource . objectIndex ) ) <EOL> else : <EOL> return None <EOL> relsData = tuple ( ( networkId , <EOL> self . conceptElementId ( rel . fromModelObject ) , <EOL> self . conceptElementId ( rel . toModelObject ) , <EOL> self . dbNum ( rel . order ) , <EOL> resourceResourceId ( rel . fromModelObject ) , <EOL> resourceResourceId ( rel . toModelObject ) , <EOL> self . dbNum ( rel . weight ) , <EOL> sequence , <EOL> depth , <EOL> self . qnameId . get ( rel . preferredLabel ) if rel . preferredLabel else None ) <EOL> for rel , sequence , depth , networkId in dbRels <EOL> if rel . fromModelObject is not None and rel . toModelObject is not None ) <EOL> del dbRels [ : ] <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> relsData ) <EOL> def insertFacts ( self ) : <EOL> accsId = self . accessionId <EOL> if self . accessionPreviouslyInDB : <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> self . execute ( "<STR_LIT>" . format ( accsId ) , <EOL> close = False , fetch = False ) <EOL> self . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( accsId ) , <EOL> close = False , fetch = False ) <EOL> self . execute ( "<STR_LIT>" . format ( accsId ) , <EOL> close = False , fetch = False ) <EOL> self . execute ( "<STR_LIT>" . format ( accsId ) , <EOL> close = False , fetch = False ) <EOL> self . showStatus ( "<STR_LIT>" ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( accsId , <EOL> unitId ) <EOL> for unitId in self . modelXbrl . units . keys ( ) ) ) <EOL> self . unitId = dict ( ( ( _accsId , xmlId ) , id ) <EOL> for id , _accsId , xmlId in table ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( self . unitId [ ( accsId , unit . id ) ] , <EOL> self . qnameId [ measure ] , <EOL> <NUM_LIT:1> if ( not unit . measures [ <NUM_LIT:1> ] ) else ( i + <NUM_LIT:1> ) ) <EOL> for unit in self . modelXbrl . units . values ( ) <EOL> for i in range ( <NUM_LIT:2> ) <EOL> for measure in unit . measures [ i ] ) ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( accsId , <EOL> cntx . startDatetime if cntx . isStartEndPeriod else None , <EOL> cntx . endDatetime if cntx . isStartEndPeriod else None , <EOL> cntx . instantDatetime if cntx . isInstantPeriod else None , <EOL> bool ( cntx . qnameDims ) , <EOL> cntx . id , <EOL> cntx . entityIdentifier [ <NUM_LIT:0> ] , <EOL> cntx . entityIdentifier [ <NUM_LIT:1> ] ) <EOL> for cntx in self . modelXbrl . contexts . values ( ) ) ) <EOL> self . cntxId = dict ( ( ( _accsId , xmlId ) , id ) <EOL> for id , _accsId , xmlId in table ) <EOL> values = [ ] <EOL> for cntx in self . modelXbrl . contexts . values ( ) : <EOL> for dim in cntx . qnameDims . values ( ) : <EOL> values . append ( ( self . cntxId [ ( accsId , cntx . id ) ] , <EOL> self . qnameId [ dim . dimensionQname ] , <EOL> self . qnameId . get ( dim . memberQname ) , <EOL> self . qnameId . get ( dim . typedMember . qname ) if dim . isTyped else None , <EOL> False , <EOL> dim . contextElement == "<STR_LIT>" , <EOL> dim . typedMember . stringValue if dim . isTyped else None ) ) <EOL> for dimQname , memQname in self . modelXbrl . qnameDimensionDefaults . items ( ) : <EOL> if dimQname not in cntx . qnameDims : <EOL> values . append ( ( self . cntxId [ ( accsId , cntx . id ) ] , <EOL> self . qnameId [ dimQname ] , <EOL> self . qnameId [ memQname ] , <EOL> None , <EOL> True , <EOL> True , <EOL> None ) ) <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> values ) <EOL> def insertFactSet ( modelFacts , tupleFactId ) : <EOL> table = self . getTable ( '<STR_LIT>' , '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tuple ( ( accsId , <EOL> tupleFactId , <EOL> self . cntxId . get ( ( accsId , fact . contextID ) ) , <EOL> self . unitId . get ( ( accsId , fact . unitID ) ) , <EOL> self . conceptElementId ( fact . concept ) , <EOL> roundValue ( fact . value , fact . precision , fact . decimals ) if fact . isNumeric and not fact . isNil else None , <EOL> fact . value , <EOL> elementFragmentIdentifier ( fact ) , <EOL> fact . xAttributes [ '<STR_LIT>' ] . xValue if ( '<STR_LIT>' in fact . xAttributes and isinstance ( fact . xAttributes [ '<STR_LIT>' ] . xValue , int ) ) else None , <EOL> fact . xAttributes [ '<STR_LIT>' ] . xValue if ( '<STR_LIT>' in fact . xAttributes and isinstance ( fact . xAttributes [ '<STR_LIT>' ] . xValue , int ) ) else None , <EOL> '<STR_LIT>' in fact . xAttributes and fact . xAttributes [ '<STR_LIT>' ] . xValue == '<STR_LIT>' , <EOL> '<STR_LIT>' in fact . xAttributes and fact . xAttributes [ '<STR_LIT>' ] . xValue == '<STR_LIT>' , <EOL> ) <EOL> for fact in modelFacts ) ) <EOL> factId = dict ( ( xmlId , id ) <EOL> for id , _accsId , xmlId in table ) <EOL> for fact in modelFacts : <EOL> if fact . isTuple : <EOL> insertFactSet ( fact . modelTupleFacts , <EOL> factId [ elementFragmentIdentifier ( fact ) ] ) <EOL> insertFactSet ( self . modelXbrl . facts , None ) </s>
