CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DatabaseUtilitySession.cc
Go to the documentation of this file.
3 #include "DatabaseSession.h"
4 #include "DatabaseContainer.h"
5 #include "MappingRules.h"
6 #include "MappingDatabase.h"
7 #include "MappingToSchema.h"
8 #include "IDatabaseSchema.h"
9 #include "MappingTree.h"
10 // externals
11 #include "Reflex/Type.h"
12 
14  m_session( dbSession ){
15 }
16 
18 }
19 
20 std::set<std::string> ora::DatabaseUtilitySession::listMappingVersions( int containerId ){
21  std::set<std::string> mappingList;
22  m_session.mappingDatabase().getMappingVersionsForContainer( containerId, mappingList );
23  return mappingList;
24 }
25 
26 std::map<std::string,std::string> ora::DatabaseUtilitySession::listMappings( int containerId ){
27  std::map<std::string,std::string> versionMap;
28  m_session.mappingDatabase().getClassVersionListForContainer( containerId, versionMap );
29  return versionMap;
30 }
31 
33  std::ostream& outputStream ){
35  if(m_session.mappingDatabase().getMappingByVersion( mappingVersion, dest )){
36  dest.printXML( outputStream );
37  return true;
38  }
39  return false;
40 }
41 
43  DatabaseSession& sourceSession){
44  if(!m_session.exists()){
45  if( m_session.configuration().properties().getFlag( Configuration::automaticDatabaseCreation() )){
46  m_session.create();
47  m_session.open();
48  } else {
49  throwException( "ORA Database not found in \""+m_session.connectionString()+"\".",
50  "DatabaseUtilitySession::importContainerSchema");
51  }
52  } else {
53  m_session.open();
54  if( existsContainer( containerName ) ){
55  throwException( "A Container named \""+containerName+"\" already exists in the database.",
56  "DatabaseUtilitySession::importContainerSchema" );
57  }
58  }
59  sourceSession.open();
60  Sequences containerSchemaSequences( m_session.schema() );
61  Handle<ora::DatabaseContainer> cont = sourceSession.containerHandle( containerName );
62  // first create the container locally:
63  Handle<ora::DatabaseContainer> newCont = m_session.addContainer( containerName, cont->className() );
64  cont->className();
65  MappingToSchema mapping2Schema( m_session.schema().storageSchema() );
66  std::set<std::string> existingVersions = m_session.mappingDatabase().versions();
67  std::set<std::string> baseVersions;
68  // first create the cont base schema
69  MappingTree baseMapping;
70  if(!sourceSession.mappingDatabase().getBaseMappingForContainer( cont->className(), cont->id(), baseMapping )){
71  throwException( "Base mapping for class \""+cont->className()+"\" has not been found in the database.",
72  "DatabaseUtilitySession::importContainerSchema");
73  }
74  std::set<std::string> classVersions;
75  if(!sourceSession.mappingDatabase().getClassVersionListForMappingVersion( baseMapping.version(), classVersions )){
76  throwException( "No class versions found for mapping \""+baseMapping.version()+"\".",
77  "DatabaseUtilitySession::importContainerSchema");
78  }
79  if( existingVersions.find( baseMapping.version() )!= existingVersions.end() ){
80  throwException("Mapping version \""+baseMapping.version()+"\" for base mapping of class \""+cont->className()+"\" already exists in the database.","DatabaseUtilitySession::importContainerSchema");
81  }
82  if( !mapping2Schema.check( baseMapping ) ){
83  throwException("Schema base for class \""+baseMapping.className()+"\" cannot be replicated, because some schema objects have been found with the same name.","DatabaseUtilitySession::importContainerSchema");
84  }
85  baseVersions.insert( baseMapping.version() );
86  existingVersions.insert( baseMapping.version() );
87  m_session.mappingDatabase().storeMapping( baseMapping );
88  bool first = true;
89  for( std::set<std::string>::const_iterator iCv = classVersions.begin(); iCv != classVersions.end(); ++iCv ){
90  m_session.mappingDatabase().insertClassVersion( cont->className(), *iCv , 0, newCont->id(), baseMapping.version(), first );
91  first = false;
92  }
93  // ...the main container sequence...
94  containerSchemaSequences.create( MappingRules::sequenceNameForContainer( containerName ));
95  // ...the main tables
96  mapping2Schema.create( baseMapping );
97  // second create the base dependencies if any
98  std::set<std::string> dependentClasses;
99  sourceSession.mappingDatabase().getDependentClassesForContainer( cont->id(), dependentClasses );
100  for( std::set<std::string>::const_iterator iCl = dependentClasses.begin(); iCl != dependentClasses.end(); ++iCl ){
101  MappingTree baseDepMapping;
102  if(!sourceSession.mappingDatabase().getBaseMappingForContainer( *iCl, cont->id(), baseDepMapping )){
103  throwException( "Base mapping for class \""+*iCl+"\" has not been found in the database.",
104  "DatabaseUtilitySession::importContainerSchema");
105  }
106  std::set<std::string> depClassVersions;
107  if(!sourceSession.mappingDatabase().getClassVersionListForMappingVersion( baseDepMapping.version(), depClassVersions )){
108  throwException( "No class versions found for mapping \""+baseDepMapping.version()+"\".",
109  "DatabaseUtilitySession::importContainerSchema");
110  }
111  if( existingVersions.find( baseDepMapping.version() )!= existingVersions.end() ){
112  throwException("Mapping version \""+baseDepMapping.version()+"\" for base mapping of class \""+*iCl+"\" already exists in the database.","DatabaseUtilitySession::importContainerSchema");
113  }
114  if( !mapping2Schema.check( baseDepMapping ) ){
115  throwException("Schema base for class \""+baseDepMapping.className()+"\" cannot be replicated, because some schema objects have been found with the same name.","DatabaseUtilitySession::importContainerSchema");
116  }
117  baseVersions.insert( baseDepMapping.version() );
118  existingVersions.insert( baseDepMapping.version() );
119  m_session.mappingDatabase().storeMapping( baseDepMapping );
120  first = true;
121  for( std::set<std::string>::const_iterator iCv = depClassVersions.begin(); iCv != depClassVersions.end(); ++iCv ){
122  m_session.mappingDatabase().insertClassVersion( *iCl, *iCv , 1, newCont->id(), baseDepMapping.version(), first );
123  first = false;
124  }
125  // create the dep classes sequences.
126  containerSchemaSequences.create( MappingRules::sequenceNameForDependentClass( containerName, *iCl ));
127  // create the dep tables
128  mapping2Schema.create( baseDepMapping );
129  }
131  std::set<std::string> allVersions;
132  if(!sourceSession.mappingDatabase().getMappingVersionsForContainer( cont->id(), allVersions )){
133  std::stringstream mess;
134  mess << "No mapping versions found for container id="<<cont->id();
135  throwException( mess.str(), "DatabaseUtilitySession::importContainerSchema");
136  }
137  for( std::set<std::string>::const_iterator iVer = allVersions.begin(); iVer != allVersions.end(); ++iVer ){
138  // skip the bases
139  if( baseVersions.find( *iVer )== baseVersions.end() ){
140  MappingTree evMapping;
141  if(!sourceSession.mappingDatabase().getMappingByVersion( *iVer, evMapping) ){
142  throwException("Mapping version \""+*iVer+"\" has not been found in the database.",
143  "DatabaseUtilitySession::importContainerSchema");
144  }
145  std::set<std::string> cvs;
146  if(!sourceSession.mappingDatabase().getClassVersionListForMappingVersion( evMapping.version(), cvs )){
147  throwException( "No class versions found for mapping \""+evMapping.version()+"\".",
148  "DatabaseUtilitySession::importContainerSchema");
149  }
150  if( existingVersions.find( *iVer )!= existingVersions.end() ){
151  throwException("Mapping version \""+*iVer+"\" for mapping of class \""+evMapping.className()+"\" already exists in the database.","DatabaseUtilitySession::importContainerSchema");
152  }
153  if( !mapping2Schema.check( evMapping ) ){
154  throwException("Evolved schema for class \""+evMapping.className()+"\" cannot be replicated, because some schema objects have been found with the same name.","DatabaseUtilitySession::importContainerSchema");
155  }
156  m_session.mappingDatabase().storeMapping( evMapping );
157  existingVersions.insert( evMapping.version() );
158  int depIndex = 0;
159  std::string className = evMapping.className();
160  if( evMapping.className() != baseMapping.className() ){
161  // dependencies
162  depIndex = 1;
163  }
164  for( std::set<std::string>::const_iterator iCv = cvs.begin(); iCv != cvs.end(); ++iCv ){
165  m_session.mappingDatabase().insertClassVersion( evMapping.className(), *iCv , depIndex, newCont->id(), evMapping.version() );
166  }
167  // then evolve the schema
168  mapping2Schema.alter( evMapping );
169  }
170  }
171  return newCont;
172 }
173 
175  const std::string& containerName ){
176  DatabaseSession sourceSession( m_session.connectionPool() );
177  sourceSession.connect(sourceConnectionString, true );
178  sourceSession.startTransaction( true );
179  importContainerSchema(containerName, sourceSession );
180  sourceSession.commitTransaction();
181 }
182 
183 
185  bool found = false;
186  for( std::map<int, Handle<DatabaseContainer> >::const_iterator iC = m_session.containers().begin();
187  iC != m_session.containers().end(); ++iC ){
188  if( iC->second->name() == containerName ) {
189  found = true;
190  break;
191  }
192  }
193  return found;
194 }
195 
196 void ora::DatabaseUtilitySession::importContainer( const std::string& sourceConnectionString,
197  const std::string& containerName ){
198  DatabaseSession sourceSession( m_session.connectionPool() );
199  sourceSession.connect(sourceConnectionString, true );
200  sourceSession.startTransaction( true );
201  Handle<ora::DatabaseContainer> newCont = importContainerSchema(containerName, sourceSession );
202  Handle<ora::DatabaseContainer> cont = sourceSession.containerHandle( containerName );
203  Handle<IteratorBuffer> iterator = cont->iteratorBuffer();
204  std::vector<void*> objects;
205  const Reflex::Type& contType = cont->type();
206  while( iterator->next() ){
207  void* data = iterator->getItem();
208  objects.push_back( data );
209  newCont->insertItem( data, contType );
210  }
211  newCont->flush();
212  for( std::vector<void*>::const_iterator iO = objects.begin(); iO != objects.end(); iO++ ){
213  contType.Destruct( *iO );
214  }
215  sourceSession.commitTransaction();
216 }
217 
219  if( !m_session.exists() ){
220  throwException( "ORA Database not found in \""+m_session.connectionString()+"\".",
221  "DatabaseUtilitySession::eraseMapping");
222 
223  }
224  m_session.mappingDatabase().removeMapping( mappingVersion );
225 }
226 
228  if( !m_session.exists() ){
229  throwException( "ORA Database not found in \""+m_session.connectionString()+"\".",
230  "DatabaseUtilitySession::containerHandle");
231 
232  }
233  m_session.open();
234  return m_session.containerHandle( name );
235 }
236 
Handle< DatabaseContainer > containerHandle(const std::string &name)
void eraseMapping(const std::string &mappingVersion)
const Reflex::Type & type()
bool getBaseMappingForContainer(const std::string &className, int containerId, MappingTree &destination)
const std::string & className() const
Definition: MappingTree.h:138
bool getClassVersionListForMappingVersion(const std::string &mappingVersion, std::set< std::string > &destination)
const std::string & version() const
Definition: MappingTree.h:128
bool dumpMapping(const std::string &mappingVersion, std::ostream &outputStream)
int insertItem(const void *data, const Reflex::Type &type)
static std::string automaticDatabaseCreation()
bool connect(const std::string &connectionString, bool readOnly)
void importContainerSchema(const std::string &sourceConnectionString, const std::string &containerName)
void importContainer(const std::string &sourceConnectionString, const std::string &containerName)
bool getMappingVersionsForContainer(int containerId, std::set< std::string > &versionList)
DatabaseUtilitySession(DatabaseSession &dbSession)
const std::string & className()
MappingDatabase & mappingDatabase()
bool first
Definition: L1TdeRCT.cc:79
void printXML(std::ostream &outputStream) const
Definition: MappingTree.cc:142
bool getDependentClassesForContainer(int containerId, std::set< std::string > &list)
bool getMappingByVersion(const std::string &version, MappingTree &destination)
int cont
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Handle< IteratorBuffer > iteratorBuffer()
std::map< std::string, std::string > listMappings(int containerId)
bool existsContainer(const std::string &containerName)
static std::string sequenceNameForContainer(const std::string &containerName)
Definition: MappingRules.cc:14
static std::string sequenceNameForDependentClass(const std::string &containerName, const std::string &className)
Definition: MappingRules.cc:20
Handle< DatabaseContainer > containerHandle(const std::string &name)
std::set< std::string > listMappingVersions(int containerId)
std::string className(const T &t)
Definition: ClassName.h:30