12 #include "RelationalAccess/ISchema.h"
19 std::map<std::string, std::set<std::string> >::iterator iTab = tableList.find( tableName );
20 if( iTab ==tableList.end() ){
21 std::set<std::string> dependencies;
22 tableList.insert(std::make_pair( tableName, dependencies ) );
25 iElem != source.
end(); iElem++ ){
26 std::map<std::string, std::set<std::string> >::iterator iT = tableList.find( tableName );
27 const std::string& innerTable = iElem->second.tableName();
28 if( innerTable != tableName ){
29 iT->second.insert( innerTable );
37 std::vector<std::string>& orderedList ){
38 orderedList.push_back( tableName );
39 std::map<std::string, std::set<std::string> >::const_iterator iDeps = tableList.find( tableName );
40 if(iDeps != tableList.end() ){
41 for( std::set<std::string>::const_iterator iDt = iDeps->second.begin();
42 iDt != iDeps->second.end(); iDt++ ){
54 m_containerId( containerId ),
55 m_containerName( containerName ),
56 m_className( containerType.Name( Reflex::SCOPED ) ),
57 m_classDict( containerType ),
60 m_containerSchemaSequences( session.
schema() ),
62 m_dependentMappings(){
69 m_containerId( containerId ),
70 m_containerName( containerName ),
71 m_className( className ),
75 m_containerSchemaSequences( session.
schema() ),
77 m_dependentMappings(){
81 for( std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.begin();
82 iDep != m_dependentMappings.end(); ++iDep ){
89 if( !m_classDict )
throwException(
"Container class \""+m_className+
"\" has not been found in the dictionary.",
90 "ContainerSchema::initClassDict");
97 m_session.schema().containerHeaderTable().addContainer( m_containerId, m_containerName, m_className );
100 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
103 m_mapping.setVersion( newMappingVersion );
104 m_session.mappingDatabase().storeMapping( m_mapping );
105 m_session.mappingDatabase().insertClassVersion( m_classDict, 0, m_containerId, newMappingVersion,
true );
109 for( std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.begin();
110 iDep != m_dependentMappings.end(); ++iDep ){
115 mapping2Schema.
create( m_mapping );
121 std::map< std::string, std::set<std::string> > tableHierarchy;
122 std::set<std::string> topLevelTables;
123 for( std::set<std::string>::const_iterator iV = containerMappingVersions.begin();
124 iV!= containerMappingVersions.end(); ++iV ){
126 if( m_session.mappingDatabase().getMappingByVersion( *iV, mapping ) ){
131 for(std::set<std::string>::const_iterator iMainT = topLevelTables.begin();
132 iMainT != topLevelTables.end(); ++iMainT ){
139 std::set<std::string> containerMappingVersions;
140 m_session.mappingDatabase().getMappingVersionsForContainer( m_containerId, containerMappingVersions );
141 std::vector<std::string> orderedTableList;
142 getTableHierarchy( containerMappingVersions, orderedTableList );
145 std::set<std::string> depClasses;
146 m_session.mappingDatabase().getDependentClassesForContainer( m_containerId, depClasses );
149 for( std::set<std::string>::const_iterator iM = containerMappingVersions.begin();
150 iM != containerMappingVersions.end(); ++iM ){
151 m_session.mappingDatabase().removeMapping( *iM );
155 for(std::set<std::string>::const_iterator iDepCl = depClasses.begin();
156 iDepCl != depClasses.end(); iDepCl++){
161 m_session.schema().containerHeaderTable().removeContainer( m_containerId );
164 for(std::vector<std::string>::reverse_iterator iTable = orderedTableList.rbegin();
165 iTable != orderedTableList.rend(); iTable++ ){
166 m_session.schema().storageSchema().dropIfExistsTable( *iTable );
175 if( !m_session.mappingDatabase().getBaseMappingForContainer( m_classDict.Name(Reflex::SCOPED), m_containerId, baseMapping )){
176 throwException(
"Base mapping has not been found in the database.",
177 "ContainerSchema::evolve");
179 mapGen.createNewMapping( m_containerName, m_classDict, baseMapping, m_mapping );
180 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
181 m_mapping.setVersion( newMappingVersion );
182 m_session.mappingDatabase().storeMapping( m_mapping );
183 m_session.mappingDatabase().insertClassVersion( m_classDict, 0, m_containerId, newMappingVersion );
185 mapping2Schema.
alter( m_mapping );
192 std::map<std::string,MappingTree*>::iterator iDep =
193 m_dependentMappings.insert( std::make_pair( className,
new MappingTree ) ).first;
194 if( baseMapping.
className() != dependentClass.Name(Reflex::SCOPED) ){
195 throwException(
"Provided base mapping does not map class \""+dependentClass.Name(Reflex::SCOPED)+
"\".",
196 "ContainerSchema::evolve");
198 mapGen.createNewDependentMapping( dependentClass, m_mapping, baseMapping, *iDep->second );
199 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
200 iDep->second->setVersion( newMappingVersion );
201 m_session.mappingDatabase().storeMapping( *iDep->second );
202 m_session.mappingDatabase().insertClassVersion( dependentClass, 1, m_containerId, newMappingVersion,
false );
207 std::set<std::string> containerMappingVersions;
208 m_session.mappingDatabase().getMappingVersionsForContainer( m_containerId, containerMappingVersions );
209 std::vector<std::string> orderedTableList;
210 getTableHierarchy( containerMappingVersions, orderedTableList );
211 for( std::vector<std::string>::const_iterator iT = orderedTableList.begin();
212 iT != orderedTableList.end(); iT++ ){
226 if( !m_session.mappingDatabase().getMappingForContainer( m_className, classVersion, m_containerId, m_mapping ) ){
231 std::string msg(
"No mapping available for the class=\""+m_className+
"\" version=\""+classVersion+
"\"." );
233 "ContainerSchema::mapping");
240 if( m_mapping.topElement().find( m_className )==m_mapping.topElement().end() ){
241 throwException(
"Mapping for container class \""+m_className+
"\" could not be loaded.",
242 "ContainerSchema::mapping");
248 if( !dependentClassDict )
throwException(
"The dependent class has not been found in the dictionary.",
249 "ContainerSchema::loadMappingForDependentClass");
251 std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.find( className );
252 if( iDep == m_dependentMappings.end() ){
254 iDep = m_dependentMappings.insert( std::make_pair( className,
new MappingTree ) ).first;
255 if( ! m_session.mappingDatabase().getMappingForContainer( className,
259 m_dependentMappings.erase( className );
268 std::map<std::string,MappingTree*>::iterator iDep =
269 m_dependentMappings.insert( std::make_pair( className,
new MappingTree ) ).first;
272 mapGen.createNewDependentMapping( dependentClassDict, m_mapping, *iDep->second );
273 mapping2Schema.
create( *iDep->second );
274 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
275 iDep->second->setVersion( newMappingVersion );
276 m_session.mappingDatabase().storeMapping( *iDep->second );
277 m_session.mappingDatabase().insertClassVersion( dependentClassDict, 1, m_containerId, newMappingVersion,
true );
284 if( !m_session.mappingDatabase().getBaseMappingForContainer( className,
285 m_containerId, baseMapping ) ){
286 create( dependentClassDict );
288 evolve( dependentClassDict, baseMapping );
294 if( ! loadMappingForDependentClass( dependentClassDict ) ){
295 extend( dependentClassDict );
304 if( ! loadMappingForDependentClass( dependentClassDict ) ){
308 if( !m_session.mappingDatabase().getBaseMappingForContainer( className,
309 m_containerId, baseMapping ) ){
313 create( dependentClassDict );
318 evolve( dependentClassDict, baseMapping );
323 std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.find( className );
324 if( iDep == m_dependentMappings.end() ){
325 throwException(
"Mapping for class \""+ className +
"\" is not available in the database.",
326 "ContainerSchema::mappingForDependentClass");
328 return iDep->second->topElement();
332 return m_session.mappingDatabase().getDependentMappingsForContainer( m_containerId, destination );
336 return m_containerSchemaSequences;
340 return m_session.configuration().blobStreamingService();
344 return m_session.configuration().referenceHandler();
348 return m_mapping.version();
352 return m_containerId;
356 return m_containerName;
364 return m_session.schema().storageSchema();
Sequences & containerSequences()
void addFromTableHierarchy(const std::string &tableName, std::map< std::string, std::set< std::string > > &tableList, std::vector< std::string > &orderedList)
const std::string & className() const
IReferenceHandler * referenceHandler()
void setAccessPermission(const std::string &principal, bool forWrite)
static std::string automaticContainerCreation()
void getTableHierarchyFromMappingElement(const MappingElement &source, std::map< std::string, std::set< std::string > > &tableList)
bool extendIfRequired(const Reflex::Type &dependentClass)
void setTableAccessPermission(coral::ITable &table, const std::string &principal, bool forWrite)
static std::string automaticDatabaseCreation()
void getTableHierarchy(const std::set< std::string > &containerMappingVersions, std::vector< std::string > &destination)
const Reflex::Type & type()
iterator begin()
Returns an iterator in the beginning of the sequence.
MappingElement & mappingForDependentClass(const Reflex::Type &dependentClass, bool writeEnabled=false)
bool mappingForDependentClasses(std::vector< MappingElement > &destination)
const std::string & className()
const MappingElement & topElement() const
void createNewMapping(const std::string &containerName, const Reflex::Type &classDictionary, MappingTree &destination)
static std::string versionOfClass(const Reflex::Type &dictionary)
bool loadMappingForDependentClass(const Reflex::Type &dependentClass)
DatabaseSession & dbSession()
Interface for a Streaming Service.
void alter(const MappingTree &mapping)
const std::string & containerName()
void create(const MappingTree &mapping)
coral::ISchema & storageSchema()
std::map< std::string, MappingElement >::const_iterator const_iterator
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
iterator end()
Returns an iterator in the end of the sequence.
MappingTree & mapping(bool writeEnabled=false)
static std::string sequenceNameForContainer(const std::string &containerName)
const std::string & tableName() const
static std::string automaticSchemaEvolution()
static std::string sequenceNameForDependentClass(const std::string &containerName, const std::string &className)
volatile std::atomic< bool > shutdown_flag false
ContainerSchema(int containerId, const std::string &containerName, const Reflex::Type &containerType, DatabaseSession &session)
Reflex::Type lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
static std::string const source
const std::string & mappingVersion()
IBlobStreamingService * blobStreamingService()
std::string className(const T &t)
void extend(const Reflex::Type &dependentClass)