CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RelationalOperation.cc
Go to the documentation of this file.
2 #include "RelationalOperation.h"
3 // externals
4 #include "CoralBase/Attribute.h"
5 #include "CoralBase/Blob.h"
6 #include "RelationalAccess/ISchema.h"
7 #include "RelationalAccess/ITable.h"
8 #include "RelationalAccess/ITableDataEditor.h"
9 #include "RelationalAccess/IBulkOperation.h"
10 #include "RelationalAccess/IQuery.h"
11 #include "RelationalAccess/ICursor.h"
12 
13 namespace ora {
14 
15  int existAttribute(const std::string& attributeName,
16  const coral::AttributeList& data){
17  int ret = -1;
18  int index = 0;
19  for(coral::AttributeList::const_iterator iAttr = data.begin();
20  iAttr!=data.end() && (ret==-1); ++iAttr){
21  if( iAttr->specification().name() == attributeName ) ret = index;
22  index++;
23  }
24  return ret;
25  }
26 
27  char* conditionOfType( ConditionType condType ){
28  static char* cond[ 5 ] = { (char*)"=",(char*)">",(char*)"<",(char*)">=",(char*)"<=" };
29  return cond[ condType ];
30  }
31 
32 }
33 
35  m_data(),
36  m_setClause(""),
37  m_whereClause(""){
38 }
39 
41 }
42 
43 int ora::InputRelationalData::addId(const std::string& columnName){
44  int index = existAttribute( columnName, m_data );
45  if(index == -1){
46  m_data.extend<int>( columnName );
47  index = m_data.size()-1;
48  if(!m_setClause.empty()) m_setClause += ", ";
49  m_setClause += ( columnName +"= :"+columnName );
50  }
51  return index;
52 }
53 
54 int ora::InputRelationalData::addData(const std::string& columnName,
55  const std::type_info& columnType ){
56  int index = existAttribute( columnName, m_data );
57  if(index == -1){
58  m_data.extend( columnName, columnType );
59  index = m_data.size()-1;
60  if(!m_setClause.empty()) m_setClause += ", ";
61  m_setClause += ( columnName +"= :"+columnName );
62  }
63  return index;
64 }
65 
66 int ora::InputRelationalData::addBlobData(const std::string& columnName){
67  int index = existAttribute( columnName, m_data );
68  if(index == -1){
69  m_data.extend<coral::Blob>( columnName );
70  index = m_data.size()-1;
71  if(!m_setClause.empty()) m_setClause += ", ";
72  m_setClause += ( columnName +"= :"+columnName );
73  }
74  return index;
75 }
76 
77 int ora::InputRelationalData::addWhereId( const std::string& columnName ){
78  int index = existAttribute( columnName, m_data );
79  if(index == -1){
80  m_data.extend<int>( columnName );
81  index = m_data.size()-1;
82  }
83  if(!m_whereClause.empty()) m_whereClause += " AND ";
84  m_whereClause += ( columnName +"= :"+columnName );
85  return index;
86 }
87 
88 int ora::InputRelationalData::addWhereId( const std::string& columnName, ConditionType condType ){
89  int index = existAttribute( columnName, m_data );
90  if( index == -1 ){
91  m_data.extend<int>( columnName );
92  index = m_data.size()-1;
93  if(!m_whereClause.empty()) m_whereClause += " AND ";
94  m_whereClause += ( columnName +conditionOfType(condType)+" :"+columnName );
95  }
96  return index;
97 }
98 
99 coral::AttributeList& ora::InputRelationalData::data(){
100  return m_data;
101 }
102 
103 coral::AttributeList& ora::InputRelationalData::whereData(){
104  // data and where data are hosted in the same buffer (as for update operation)
105  return m_data;
106 }
107 
109  return m_setClause;
110 }
111 
113  return m_whereClause;
114 }
115 
116 ora::InsertOperation::InsertOperation( const std::string& tableName,
117  coral::ISchema& schema ):
119  m_tableName( tableName ),
120  m_schema( schema ){
121 }
122 
124 }
125 
126 bool
128  return false;
129 }
130 
132  coral::ITable& table = m_schema.tableHandle( m_tableName );
133  table.dataEditor().insertRow( data() );
134  return true;
135 }
136 
138 }
139 
140 ora::BulkInsertOperation::BulkInsertOperation( const std::string& tableName,
141  coral::ISchema& schema ):
143  m_tableName( tableName ),
144  m_schema( schema ),
145  m_bulkOperations(){
146 }
147 
149  for( std::vector<coral::IBulkOperation*>::iterator iB = m_bulkOperations.begin();
150  iB != m_bulkOperations.end(); ++iB ){
151  delete *iB;
152  }
153 }
154 
155 coral::IBulkOperation& ora::BulkInsertOperation::setUp( int rowCacheSize ){
156  coral::ITable& table = m_schema.tableHandle( m_tableName );
157 
158  m_bulkOperations.push_back( table.dataEditor().bulkInsert( data(), rowCacheSize ) );
159  return *m_bulkOperations.back();
160 }
161 
162 bool
164  return false;
165 }
166 
168  for( std::vector<coral::IBulkOperation*>::iterator iB = m_bulkOperations.begin();
169  iB != m_bulkOperations.end(); ++iB ){
170  (*iB)->flush();
171  delete *iB;
172  }
173  m_bulkOperations.clear();
174  return true;
175 }
176 
178  for( std::vector<coral::IBulkOperation*>::iterator iB = m_bulkOperations.begin();
179  iB != m_bulkOperations.end(); ++iB ){
180  delete *iB;
181  }
182  m_bulkOperations.clear();
183 }
184 
185 ora::UpdateOperation::UpdateOperation( const std::string& tableName,
186  coral::ISchema& schema ):
188  m_tableName( tableName ),
189  m_schema( schema ){
190 }
191 
193 }
194 
195 bool
197  return true;
198 }
199 
201  bool ret = false;
202  if( updateClause().size() && whereClause().size() ){
203  coral::ITable& table = m_schema.tableHandle( m_tableName );
204  long nr = table.dataEditor().updateRows( updateClause(), whereClause(), data() );
205  ret = nr > 0;
206  }
207  return ret;
208 }
209 
211 }
212 
213 ora::DeleteOperation::DeleteOperation( const std::string& tableName,
214  coral::ISchema& schema ):
216  m_tableName( tableName ),
217  m_schema( schema ){
218 }
219 
221 }
222 
223 bool
225  return false;
226 }
227 
229  bool ret = false;
230  if( whereClause().size() ){
231  coral::ITable& table = m_schema.tableHandle( m_tableName );
232  long nr = table.dataEditor().deleteRows( whereClause(), whereData() );
233  ret = nr > 0;
234  }
235  return ret;
236 }
237 
239 }
240 
241 ora::SelectOperation::SelectOperation( const std::string& tableName,
242  coral::ISchema& schema ):
243  m_spec( new coral::AttributeListSpecification ),
244  m_whereData(),
245  m_whereClause(""),
246  m_orderByCols(),
247  m_query(),
248  m_cursor( 0 ),
249  m_tableName( tableName ),
250  m_schema( schema ){
251 }
252 
254  m_spec->release();
255 }
256 
257 void ora::SelectOperation::addOrderId(const std::string& columnName){
258  m_orderByCols.push_back( columnName );
259 }
260 
262  bool ret = false;
263  if( m_query.get() ){
264  ret = m_cursor->next();
265  if(!ret) clear();
266  }
267  return ret;
268 }
269 
271  m_query.reset();
272  m_cursor = 0;
273 }
274 
275 int ora::SelectOperation::addId(const std::string& columnName){
276  int idx = m_spec->index( columnName );
277  if( idx == -1){
278  m_spec->extend< int >( columnName );
279  idx = m_spec->size()-1;
280  }
281  return idx;
282 }
283 
284 int ora::SelectOperation::addData(const std::string& columnName,
285  const std::type_info& columnType ){
286  int idx = m_spec->index( columnName );
287  if( idx == -1){
288  m_spec->extend( columnName, columnType );
289  idx = m_spec->size()-1;
290  }
291  return idx;
292 }
293 
294 int ora::SelectOperation::addBlobData(const std::string& columnName ){
295  int idx = m_spec->index( columnName );
296  if( idx == -1){
297  m_spec->extend<coral::Blob>( columnName );
298  idx = m_spec->size()-1;
299  }
300  return idx;
301 }
302 
303 int ora::SelectOperation::addWhereId( const std::string& columnName ){
304  int index = existAttribute( columnName, m_whereData );
305  if( index == -1){
306  m_whereData.extend<int>( columnName );
307  index = m_whereData.size()-1;
308  if(!m_whereClause.empty()) m_whereClause += " AND ";
309  m_whereClause += ( columnName +"= :"+columnName );
310  }
311  return index;
312 }
313 
314 coral::AttributeList& ora::SelectOperation::data(){
315  if(!m_cursor) throwException( "Query on table "+m_tableName+" has not been executed.",
316  "ora::ReadOperation::data" );
317  return const_cast<coral::AttributeList&>(m_cursor->currentRow());
318 }
319 
320 coral::AttributeList& ora::SelectOperation::whereData(){
321  return m_whereData;
322 }
323 
325  return m_whereClause;
326 }
327 
329  m_cursor = 0;
330  coral::ITable& table = m_schema.tableHandle( m_tableName );
331  m_query.reset( table.newQuery() );
332  for ( coral::AttributeListSpecification::const_iterator iSpec = m_spec->begin();
333  iSpec != m_spec->end(); ++iSpec ) {
334  m_query->addToOutputList( iSpec->name() );
335  m_query->defineOutputType( iSpec->name(),iSpec->typeName());
336  }
337  for(std::vector<std::string>::iterator iCol = m_orderByCols.begin();
338  iCol != m_orderByCols.end(); iCol++ ){
339  m_query->addToOrderList( *iCol );
340  }
341  m_query->setCondition( m_whereClause, m_whereData );
342  m_query->setRowCacheSize( 100 ); // We should better define this value !!!
343  m_cursor = &m_query->execute();
344 }
345 
346 coral::AttributeListSpecification& ora::SelectOperation::attributeListSpecification(){
347  return *m_spec;
348 }
349 
list table
Definition: asciidump.py:386
int addWhereId(const std::string &columnName)
char * conditionOfType(ConditionType condType)
int addData(const std::string &columnName, const std::type_info &columnType)
coral::AttributeListSpecification & attributeListSpecification()
int addData(const std::string &columnName, const std::type_info &columnType)
coral::AttributeList & whereData()
UpdateOperation(const std::string &tableName, coral::ISchema &schema)
BulkInsertOperation(const std::string &tableName, coral::ISchema &schema)
SelectOperation(const std::string &tableName, coral::ISchema &schema)
InsertOperation(const std::string &tableName, coral::ISchema &schema)
int addWhereId(const std::string &columnName)
coral::IBulkOperation & setUp(int rowCacheSize)
DeleteOperation(const std::string &tableName, coral::ISchema &schema)
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:168
coral::AttributeList & data()
int addId(const std::string &columnName)
int addBlobData(const std::string &columnName)
void addOrderId(const std::string &columnName)
coral::AttributeList & data()
coral::AttributeList & whereData()
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
int addId(const std::string &columnName)
int existAttribute(const std::string &attributeName, const coral::AttributeList &data)
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
int addBlobData(const std::string &columnName)
tuple size
Write out results.