23#include <com/sun/star/sdb/tools/XKeyAlteration.hpp>
24#include <com/sun/star/sdbc/XRow.hpp>
25#include <com/sun/star/sdbc/XResultSet.hpp>
26#include <com/sun/star/sdbcx/KeyType.hpp>
27#include <com/sun/star/sdbc/KeyRule.hpp>
28#include <com/sun/star/sdbc/SQLException.hpp>
47 ::osl::Mutex& _rMutex,
48 const ::std::vector< OUString>& _rVector
49 ) :
OKeys_BASE(*_pTable,true,_rMutex,_rVector,true)
85 const char* pKeyRule =
nullptr;
88 case KeyRule::CASCADE:
89 pKeyRule = _bUpdate ?
" ON UPDATE CASCADE " :
" ON DELETE CASCADE ";
91 case KeyRule::RESTRICT:
92 pKeyRule = _bUpdate ?
" ON UPDATE RESTRICT " :
" ON DELETE RESTRICT ";
94 case KeyRule::SET_NULL:
95 pKeyRule = _bUpdate ?
" ON UPDATE SET NULL " :
" ON DELETE SET NULL ";
97 case KeyRule::SET_DEFAULT:
98 pKeyRule = _bUpdate ?
" ON UPDATE SET DEFAULT " :
" ON DELETE SET DEFAULT ";
105 sRet = OUString::createFromAscii(pKeyRule);
111 Reference< XColumnsSupplier > xColSupp( _rSourceDescriptor, UNO_QUERY_THROW );
112 Reference< XIndexAccess > xSourceCols( xColSupp->getColumns(), UNO_QUERY_THROW );
114 xColSupp.set( _rDestDescriptor, UNO_QUERY_THROW );
115 Reference< XAppend > xDestAppend( xColSupp->getColumns(), UNO_QUERY_THROW );
117 sal_Int32
nCount = xSourceCols->getCount();
120 Reference< XPropertySet > xColProp( xSourceCols->getByIndex(
i), UNO_QUERY );
121 xDestAppend->appendByDescriptor( xColProp );
129 if ( !xConnection.is() )
133 Reference< XPropertySet > xNewDescriptor(
cloneDescriptor( descriptor ) );
135 return xNewDescriptor;
140 sal_Int32 nUpdateRule = 0, nDeleteRule = 0;
141 OUString sReferencedName;
143 if ( nKeyType == KeyType::FOREIGN )
158 OUStringBuffer aSql(
"ALTER TABLE ");
164 if ( nKeyType == KeyType::PRIMARY )
166 aSql.append(
" PRIMARY KEY (");
168 else if ( nKeyType == KeyType::FOREIGN )
170 aSql.append(
" FOREIGN KEY (");
173 throw SQLException();
175 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
176 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
177 Reference< XPropertySet > xColProp;
178 for(sal_Int32
i = 0 ;
i < xColumns->getCount() ; ++
i)
182 xColProp.set(xColumns->getByIndex(
i), css::uno::UNO_QUERY);
188 if ( nKeyType == KeyType::FOREIGN )
190 aSql.append(
" REFERENCES "
194 for(sal_Int32
i=0;
i<xColumns->getCount();++
i)
198 xColumns->getByIndex(
i) >>= xColProp;
208 xStmt->execute(aSql.makeStringAndClear());
211 OUString sNewName( _rForName );
214 OUString aSchema,aTable;
217 Reference< XResultSet > xResult;
218 sal_Int32 nColumn = 12;
219 if ( nKeyType == KeyType::FOREIGN )
232 Reference< XRow > xRow(xResult,UNO_QUERY);
233 while( xResult->next() )
235 OUString
sName = xRow->getString(nColumn);
243 ::comphelper::disposeComponent(xResult);
246 catch(
const SQLException&)
250 m_pTable->
addKey(sNewName,std::make_shared<sdbcx::KeyProperties>(sReferencedName,nKeyType,nUpdateRule,nDeleteRule));
257 return " DROP CONSTRAINT ";
278 sal_Int32 nKeyType = KeyType::PRIMARY;
284 if ( KeyType::PRIMARY == nKeyType )
286 aSql.append(
" DROP PRIMARY KEY");
298 xStmt->execute(aSql.makeStringAndClear());
299 ::comphelper::disposeComponent(xStmt);
OKeysHelper(OTableHelper *_pTable, ::osl::Mutex &_rMutex, const ::std::vector< OUString > &_rVector)
virtual sdbcx::ObjectType createObject(const OUString &_rName) override
virtual OUString getDropForeignKey() const
virtual void impl_refresh() override
static void cloneDescriptorColumns(const sdbcx::ObjectType &_rSourceDescriptor, const sdbcx::ObjectType &_rDestDescriptor)
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
virtual sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
appends an object described by a descriptor, under a given name
css::uno::Reference< css::sdbc::XConnection > const & getConnection() const
void addKey(const OUString &_sName, const std::shared_ptr< sdbcx::KeyProperties > &_aKeyProperties)
virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > getMetaData() const override
std::shared_ptr< sdbcx::KeyProperties > getKeyProperties(const OUString &_sName) const
css::uno::Reference< css::sdb::tools::XKeyAlteration > const & getKeyService() const
virtual void refreshKeys() override
ObjectType getObject(sal_Int32 _nIndex)
return the object, if not existent it creates it.
::std::unique_ptr< IObjectCollection > m_pElements
ObjectType cloneDescriptor(const ObjectType &_descriptor)
clones the given descriptor
sal_Int32 getINT32(const Any &_rAny)
OUString getString(const Any &_rAny)
css::uno::Reference< css::beans::XPropertySet > ObjectType
static OUString getKeyRuleString(bool _bUpdate, sal_Int32 _nKeyRule)
returns the keyrule string for the primary key
#define PROPERTY_ID_REFERENCEDTABLE
#define PROPERTY_ID_CATALOGNAME
#define PROPERTY_ID_RELATEDCOLUMN
#define PROPERTY_ID_SCHEMANAME
#define PROPERTY_ID_DELETERULE
#define PROPERTY_ID_UPDATERULE