Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "connectivity/TKeys.hxx"
21 : : #include "connectivity/TKey.hxx"
22 : : #include <com/sun/star/sdbc/XRow.hpp>
23 : : #include <com/sun/star/sdbc/XResultSet.hpp>
24 : : #include <com/sun/star/sdbcx/KeyType.hpp>
25 : : #include <com/sun/star/sdbc/KeyRule.hpp>
26 : : #include "connectivity/dbtools.hxx"
27 : : #include <comphelper/extract.hxx>
28 : : #include <comphelper/types.hxx>
29 : : #include <comphelper/property.hxx>
30 : : #include "TConnection.hxx"
31 : :
32 : : namespace connectivity
33 : : {
34 : : using namespace comphelper;
35 : : using namespace connectivity::sdbcx;
36 : : using namespace dbtools;
37 : : using namespace ::com::sun::star::uno;
38 : : using namespace ::com::sun::star::beans;
39 : : using namespace ::com::sun::star::sdbcx;
40 : : using namespace ::com::sun::star::sdbc;
41 : : using namespace ::com::sun::star::container;
42 : : using namespace ::com::sun::star::lang;
43 : :
44 : :
45 : :
46 : 0 : OKeysHelper::OKeysHelper( OTableHelper* _pTable,
47 : : ::osl::Mutex& _rMutex,
48 : : const TStringVector& _rVector
49 : : ) : OKeys_BASE(*_pTable,sal_True,_rMutex,_rVector,sal_True)
50 : 0 : ,m_pTable(_pTable)
51 : : {
52 : 0 : }
53 : : // -------------------------------------------------------------------------
54 : 0 : sdbcx::ObjectType OKeysHelper::createObject(const ::rtl::OUString& _rName)
55 : : {
56 : 0 : sdbcx::ObjectType xRet = NULL;
57 : :
58 [ # # ]: 0 : if(!_rName.isEmpty())
59 : : {
60 [ # # ][ # # ]: 0 : OTableKeyHelper* pRet = new OTableKeyHelper(m_pTable,_rName,m_pTable->getKeyProperties(_rName));
[ # # ]
61 [ # # ][ # # ]: 0 : xRet = pRet;
62 : : }
63 : :
64 [ # # ]: 0 : if(!xRet.is()) // we have a primary key with a system name
65 : : {
66 [ # # ][ # # ]: 0 : OTableKeyHelper* pRet = new OTableKeyHelper(m_pTable,_rName,m_pTable->getKeyProperties(_rName));
[ # # ]
67 [ # # ][ # # ]: 0 : xRet = pRet;
68 : : }
69 : :
70 : 0 : return xRet;
71 : : }
72 : : // -------------------------------------------------------------------------
73 : 0 : void OKeysHelper::impl_refresh() throw(RuntimeException)
74 : : {
75 : 0 : m_pTable->refreshKeys();
76 : 0 : }
77 : : // -------------------------------------------------------------------------
78 : 0 : Reference< XPropertySet > OKeysHelper::createDescriptor()
79 : : {
80 [ # # ][ # # ]: 0 : return new OTableKeyHelper(m_pTable);
81 : : }
82 : : // -----------------------------------------------------------------------------
83 : : /** returns the keyrule string for the primary key
84 : : */
85 : 0 : ::rtl::OUString getKeyRuleString(sal_Bool _bUpdate,sal_Int32 _nKeyRule)
86 : : {
87 : 0 : const char* pKeyRule = NULL;
88 [ # # # # : 0 : switch ( _nKeyRule )
# ]
89 : : {
90 : : case KeyRule::CASCADE:
91 [ # # ]: 0 : pKeyRule = _bUpdate ? " ON UPDATE CASCADE " : " ON DELETE CASCADE ";
92 : 0 : break;
93 : : case KeyRule::RESTRICT:
94 [ # # ]: 0 : pKeyRule = _bUpdate ? " ON UPDATE RESTRICT " : " ON DELETE RESTRICT ";
95 : 0 : break;
96 : : case KeyRule::SET_NULL:
97 [ # # ]: 0 : pKeyRule = _bUpdate ? " ON UPDATE SET NULL " : " ON DELETE SET NULL ";
98 : 0 : break;
99 : : case KeyRule::SET_DEFAULT:
100 [ # # ]: 0 : pKeyRule = _bUpdate ? " ON UPDATE SET DEFAULT " : " ON DELETE SET DEFAULT ";
101 : 0 : break;
102 : : default:
103 : : ;
104 : : }
105 : 0 : ::rtl::OUString sRet;
106 [ # # ]: 0 : if ( pKeyRule )
107 : 0 : sRet = ::rtl::OUString::createFromAscii(pKeyRule);
108 : 0 : return sRet;
109 : : }
110 : : // -------------------------------------------------------------------------
111 : 0 : void OKeysHelper::cloneDescriptorColumns( const sdbcx::ObjectType& _rSourceDescriptor, const sdbcx::ObjectType& _rDestDescriptor )
112 : : {
113 [ # # ]: 0 : Reference< XColumnsSupplier > xColSupp( _rSourceDescriptor, UNO_QUERY_THROW );
114 [ # # ][ # # ]: 0 : Reference< XIndexAccess > xSourceCols( xColSupp->getColumns(), UNO_QUERY_THROW );
[ # # ]
115 : :
116 [ # # ]: 0 : xColSupp.set( _rDestDescriptor, UNO_QUERY_THROW );
117 [ # # ][ # # ]: 0 : Reference< XAppend > xDestAppend( xColSupp->getColumns(), UNO_QUERY_THROW );
[ # # ]
118 : :
119 [ # # ][ # # ]: 0 : sal_Int32 nCount = xSourceCols->getCount();
120 [ # # ]: 0 : for ( sal_Int32 i=0; i< nCount; ++i )
121 : : {
122 [ # # ][ # # ]: 0 : Reference< XPropertySet > xColProp( xSourceCols->getByIndex(i), UNO_QUERY );
[ # # ]
123 [ # # ][ # # ]: 0 : xDestAppend->appendByDescriptor( xColProp );
124 : 0 : }
125 : 0 : }
126 : : // -------------------------------------------------------------------------
127 : : // XAppend
128 : 0 : sdbcx::ObjectType OKeysHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
129 : : {
130 [ # # ]: 0 : Reference< XConnection> xConnection = m_pTable->getConnection();
131 [ # # ]: 0 : if ( !xConnection.is() )
132 [ # # ]: 0 : return NULL;
133 [ # # ]: 0 : if ( m_pTable->isNew() )
134 : : {
135 [ # # ]: 0 : Reference< XPropertySet > xNewDescriptor( cloneDescriptor( descriptor ) );
136 [ # # ]: 0 : cloneDescriptorColumns( descriptor, xNewDescriptor );
137 : 0 : return xNewDescriptor;
138 : : }
139 : :
140 [ # # ]: 0 : const ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
141 [ # # ][ # # ]: 0 : sal_Int32 nKeyType = getINT32(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)));
[ # # ][ # # ]
142 : 0 : sal_Int32 nUpdateRule = 0, nDeleteRule = 0;
143 : 0 : ::rtl::OUString sReferencedName;
144 : :
145 [ # # ]: 0 : if ( nKeyType == KeyType::FOREIGN )
146 : : {
147 [ # # ][ # # ]: 0 : descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)) >>= sReferencedName;
[ # # ]
148 [ # # ][ # # ]: 0 : descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_UPDATERULE)) >>= nUpdateRule;
[ # # ]
149 [ # # ][ # # ]: 0 : descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DELETERULE)) >>= nDeleteRule;
[ # # ]
150 : : }
151 : :
152 [ # # ][ # # ]: 0 : if ( m_pTable->getKeyService().is() )
153 : : {
154 [ # # ][ # # ]: 0 : m_pTable->getKeyService()->addKey(m_pTable,descriptor);
[ # # ][ # # ]
[ # # ]
155 : : }
156 : : else
157 : : {
158 : : // if we're here, we belong to a table which is not new, i.e. already exists in the database.
159 : : // In this case, really append the new index.
160 : 0 : ::rtl::OUStringBuffer aSql;
161 [ # # ]: 0 : aSql.appendAscii("ALTER TABLE ");
162 [ # # ][ # # ]: 0 : ::rtl::OUString aQuote = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString( );
[ # # ][ # # ]
[ # # ]
163 : :
164 [ # # ][ # # ]: 0 : aSql.append(composeTableName( m_pTable->getConnection()->getMetaData(), m_pTable, ::dbtools::eInTableDefinitions, false, false, true ));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
165 [ # # ]: 0 : aSql.appendAscii(" ADD ");
166 : :
167 [ # # ]: 0 : if ( nKeyType == KeyType::PRIMARY )
168 : : {
169 [ # # ]: 0 : aSql.appendAscii(" PRIMARY KEY (");
170 : : }
171 [ # # ]: 0 : else if ( nKeyType == KeyType::FOREIGN )
172 : : {
173 [ # # ]: 0 : aSql.appendAscii(" FOREIGN KEY (");
174 : : }
175 : : else
176 [ # # ]: 0 : throw SQLException();
177 : :
178 [ # # ]: 0 : Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
179 [ # # ][ # # ]: 0 : Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
[ # # ]
180 : 0 : Reference< XPropertySet > xColProp;
181 [ # # ][ # # ]: 0 : for(sal_Int32 i = 0 ; i < xColumns->getCount() ; ++i)
[ # # ]
182 : : {
183 [ # # ]: 0 : if ( i > 0 )
184 [ # # ]: 0 : aSql.appendAscii(",");
185 [ # # ][ # # ]: 0 : ::cppu::extractInterface(xColProp,xColumns->getByIndex(i));
[ # # ]
186 [ # # ][ # # ]: 0 : aSql.append( ::dbtools::quoteName( aQuote,getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))) );
[ # # ][ # # ]
[ # # ][ # # ]
187 : :
188 : : }
189 [ # # ]: 0 : aSql.appendAscii(")");
190 : :
191 [ # # ]: 0 : if ( nKeyType == KeyType::FOREIGN )
192 : : {
193 [ # # ]: 0 : aSql.appendAscii(" REFERENCES ");
194 [ # # ][ # # ]: 0 : aSql.append(::dbtools::quoteTableName(m_pTable->getConnection()->getMetaData(),sReferencedName,::dbtools::eInTableDefinitions));
[ # # ][ # # ]
[ # # ]
195 [ # # ]: 0 : aSql.appendAscii(" (");
196 : :
197 [ # # ][ # # ]: 0 : for(sal_Int32 i=0;i<xColumns->getCount();++i)
[ # # ]
198 : : {
199 [ # # ]: 0 : if ( i > 0 )
200 [ # # ]: 0 : aSql.appendAscii(",");
201 [ # # ][ # # ]: 0 : xColumns->getByIndex(i) >>= xColProp;
[ # # ]
202 [ # # ][ # # ]: 0 : aSql.append(::dbtools::quoteName( aQuote,getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_RELATEDCOLUMN)))));
[ # # ][ # # ]
[ # # ][ # # ]
203 : :
204 : : }
205 [ # # ]: 0 : aSql.appendAscii(")");
206 [ # # ]: 0 : aSql.append(getKeyRuleString(sal_True ,nUpdateRule));
207 [ # # ]: 0 : aSql.append(getKeyRuleString(sal_False ,nDeleteRule));
208 : : }
209 : :
210 [ # # ][ # # ]: 0 : Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
[ # # ]
211 [ # # ][ # # ]: 0 : xStmt->execute(aSql.makeStringAndClear());
[ # # ]
212 : : }
213 : : // find the name which the database gave the new key
214 : 0 : ::rtl::OUString sNewName( _rForName );
215 : : try
216 : : {
217 : 0 : ::rtl::OUString aSchema,aTable;
218 [ # # ][ # # ]: 0 : m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
219 [ # # ][ # # ]: 0 : m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
220 : 0 : Reference< XResultSet > xResult;
221 : 0 : sal_Int32 nColumn = 12;
222 [ # # ]: 0 : if ( nKeyType == KeyType::FOREIGN )
223 [ # # ][ # # ]: 0 : xResult = m_pTable->getMetaData()->getImportedKeys( m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME))
224 : : ,aSchema
225 [ # # ][ # # ]: 0 : ,aTable);
[ # # ][ # # ]
226 : : else
227 : : {
228 [ # # ][ # # ]: 0 : xResult = m_pTable->getMetaData()->getPrimaryKeys( m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME))
229 : : ,aSchema
230 [ # # ][ # # ]: 0 : ,aTable);
[ # # ][ # # ]
231 : 0 : nColumn = 6;
232 : : }
233 [ # # ]: 0 : if ( xResult.is() )
234 : : {
235 [ # # ]: 0 : Reference< XRow > xRow(xResult,UNO_QUERY);
236 [ # # ][ # # ]: 0 : while( xResult->next() )
[ # # ]
237 : : {
238 [ # # ][ # # ]: 0 : ::rtl::OUString sName = xRow->getString(nColumn);
239 [ # # ][ # # ]: 0 : if ( !m_pElements->exists(sName) ) // this name wasn't inserted yet so it must be te new one
240 : : {
241 [ # # ][ # # ]: 0 : descriptor->setPropertyValue( rPropMap.getNameByIndex( PROPERTY_ID_NAME ), makeAny( sName ) );
[ # # ][ # # ]
242 : 0 : sNewName = sName;
243 : : break;
244 : : }
245 [ # # ]: 0 : }
246 [ # # ]: 0 : ::comphelper::disposeComponent(xResult);
247 [ # # ]: 0 : }
248 : : }
249 [ # # ]: 0 : catch(const SQLException&)
250 : : {
251 : : }
252 : :
253 [ # # ][ # # ]: 0 : m_pTable->addKey(sNewName,sdbcx::TKeyProperties(new sdbcx::KeyProperties(sReferencedName,nKeyType,nUpdateRule,nDeleteRule)));
[ # # ][ # # ]
[ # # ]
254 : :
255 [ # # ]: 0 : return createObject( sNewName );
256 : : }
257 : : // -----------------------------------------------------------------------------
258 : 0 : ::rtl::OUString OKeysHelper::getDropForeignKey() const
259 : : {
260 : 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP CONSTRAINT "));
261 : : }
262 : : // -------------------------------------------------------------------------
263 : : // XDrop
264 : 0 : void OKeysHelper::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
265 : : {
266 [ # # ]: 0 : Reference< XConnection> xConnection = m_pTable->getConnection();
267 [ # # ][ # # ]: 0 : if ( xConnection.is() && !m_pTable->isNew() )
[ # # ]
268 : : {
269 [ # # ][ # # ]: 0 : Reference<XPropertySet> xKey(getObject(_nPos),UNO_QUERY);
270 [ # # ][ # # ]: 0 : if ( m_pTable->getKeyService().is() )
271 : : {
272 [ # # ][ # # ]: 0 : m_pTable->getKeyService()->dropKey(m_pTable,xKey);
[ # # ][ # # ]
[ # # ]
273 : : }
274 : : else
275 : : {
276 : 0 : ::rtl::OUStringBuffer aSql;
277 [ # # ]: 0 : aSql.appendAscii("ALTER TABLE ");
278 : :
279 [ # # ][ # # ]: 0 : aSql.append( composeTableName( m_pTable->getConnection()->getMetaData(), m_pTable,::dbtools::eInTableDefinitions, false, false, true ));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
280 : :
281 : 0 : sal_Int32 nKeyType = KeyType::PRIMARY;
282 [ # # ]: 0 : if ( xKey.is() )
283 : : {
284 [ # # ]: 0 : ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
285 [ # # ][ # # ]: 0 : xKey->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)) >>= nKeyType;
[ # # ]
286 : : }
287 [ # # ]: 0 : if ( KeyType::PRIMARY == nKeyType )
288 : : {
289 [ # # ]: 0 : aSql.appendAscii(" DROP PRIMARY KEY");
290 : : }
291 : : else
292 : : {
293 [ # # ][ # # ]: 0 : aSql.append(getDropForeignKey());
294 [ # # ][ # # ]: 0 : const ::rtl::OUString aQuote = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString();
[ # # ][ # # ]
[ # # ]
295 [ # # ][ # # ]: 0 : aSql.append( ::dbtools::quoteName( aQuote,_sElementName) );
296 : : }
297 : :
298 [ # # ][ # # ]: 0 : Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
[ # # ]
299 [ # # ]: 0 : if ( xStmt.is() )
300 : : {
301 [ # # ][ # # ]: 0 : xStmt->execute(aSql.makeStringAndClear());
[ # # ]
302 [ # # ]: 0 : ::comphelper::disposeComponent(xStmt);
303 : 0 : }
304 : 0 : }
305 : 0 : }
306 : 0 : }
307 : : // -----------------------------------------------------------------------------
308 : : } // namespace connectivity
309 : : // -----------------------------------------------------------------------------
310 : :
311 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|