3 #include "RelationalAccess/ISchema.h"
4 #include "RelationalAccess/TableDescription.h"
5 #include "RelationalAccess/ITable.h"
6 #include "RelationalAccess/ITableDataEditor.h"
7 #include "RelationalAccess/ITablePrivilegeManager.h"
8 #include "RelationalAccess/IQuery.h"
9 #include "RelationalAccess/ICursor.h"
10 #include "CoralBase/Attribute.h"
11 #include "CoralBase/AttributeList.h"
12 #include "CoralBase/AttributeSpecification.h"
13 #include "RelationalAccess/SchemaException.h"
18 m_sequenceTableName( sequenceTableName ),
20 m_sequenceTableExists(
false ),
21 m_whereClause( std::
string(
"REFTABLE_NAME")+
" =:"+std::
string(
"REFTABLE_NAME")),
23 m_setClause( std::
string(
"IDVALUE")+
" = "+std::
string(
"IDVALUE")+
" + 1"),
32 m_sequenceTableExists=m_coraldb.nominalSchema().existsTable(m_sequenceTableName) ;
41 std::map< std::string, unsigned long long >::iterator iSequence = m_tableToId.find( tableName );
42 coral::ISchema&
schema=m_coraldb.nominalSchema();
43 if ( iSequence == m_tableToId.end() ) {
45 if ( ! m_sequenceTableExists ) {
49 unsigned long long lastIdUsed = 0;
50 if ( ! ( this->lockEntry( schema, tableName, lastIdUsed ) ) ) {
52 coral::AttributeList rowData;
54 rowData.extend<
unsigned long long>(
"IDVALUE");
55 coral::AttributeList::iterator iAttribute = rowData.begin();
58 unsigned long long startingIdValue = lastIdUsed;
59 iAttribute->data<
unsigned long long >() = startingIdValue;
61 schema.tableHandle( m_sequenceTableName ).dataEditor().insertRow( rowData );
62 m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
63 return startingIdValue;
64 }
catch(
const coral::DataEditorException& er){
65 this->lockEntry( schema, tableName, lastIdUsed );
67 iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
68 m_whereData->begin()->data<
std::string>() = tableName;
69 schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
79 iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
82 unsigned long long& oid = iSequence->second;
83 this->lockEntry( schema, tableName, oid );
87 m_whereData->begin()->data<
std::string>() = tableName;
88 schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
139 return m_sequenceTableExists;
144 coral::ISchema&
schema= m_coraldb.nominalSchema();
146 description.setName(m_sequenceTableName);
147 description.insertColumn(
std::string(
"REFTABLE_NAME"),
148 coral::AttributeSpecification::typeNameForType<std::string>() );
149 description.setNotNullConstraint(
std::string(
"REFTABLE_NAME"));
151 coral::AttributeSpecification::typeNameForType<unsigned long long>() );
152 description.setNotNullConstraint(
std::string(
"IDVALUE"));
153 description.setPrimaryKey( std::vector< std::string >( 1,
std::string(
"REFTABLE_NAME")));
154 schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
155 m_sequenceTableExists=
true;
161 unsigned long long& lastId ){
162 std::auto_ptr< coral::IQuery >
query( schema.tableHandle(m_sequenceTableName).newQuery());
163 query->limitReturnedRows( 1, 0 );
165 query->defineOutputType(
std::string(
"IDVALUE"), coral::AttributeSpecification::typeNameForType<unsigned long long>() );
166 query->setForUpdate();
167 m_whereData->begin()->data<
std::string >() = sequenceName;
168 query->setCondition( m_whereClause, *m_whereData );
169 coral::ICursor& cursor =
query->execute();
170 if ( cursor.next() ) {
171 lastId = cursor.currentRow().begin()->data<
unsigned long long >();
~SequenceManager()
Destructor.
bool existSequencesTable()
Whether sequence table exists.
void createSequencesTable()
Creates the table holding the sequences.
bool lockEntry(coral::ISchema &schema, const std::string &reftableName, unsigned long long &lastId)
Locks the id entry in the ref table and returns the lastId value.
SequenceManager(cond::DbSession &coraldb, const std::string &sequenceTableName)
Constructor.
void clear()
Clears the internal state.
volatile std::atomic< bool > shutdown_flag false
coral::AttributeList * m_whereData
The data for the where clause.
unsigned long long incrementId(const std::string &reftableName)
Increments and returns a new valid oid for a table.