1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*9b5730f6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*9b5730f6SAndrew Rist * distributed with this work for additional information
6*9b5730f6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*9b5730f6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*9b5730f6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*9b5730f6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist * KIND, either express or implied. See the License for the
17*9b5730f6SAndrew Rist * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*9b5730f6SAndrew Rist *************************************************************/
21*9b5730f6SAndrew Rist
22*9b5730f6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include "connectivity/TKey.hxx"
29cdf0e10cSrcweir #include "connectivity/TKeyColumns.hxx"
30cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
32cdf0e10cSrcweir #include "TConnection.hxx"
33cdf0e10cSrcweir #include "connectivity/TTableHelper.hxx"
34cdf0e10cSrcweir
35cdf0e10cSrcweir using namespace connectivity;
36cdf0e10cSrcweir using namespace ::com::sun::star::uno;
37cdf0e10cSrcweir using namespace ::com::sun::star::beans;
38cdf0e10cSrcweir // using namespace ::com::sun::star::sdbcx;
39cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
40cdf0e10cSrcweir using namespace ::com::sun::star::container;
41cdf0e10cSrcweir using namespace ::com::sun::star::lang;
42cdf0e10cSrcweir // -------------------------------------------------------------------------
OTableKeyHelper(OTableHelper * _pTable)43cdf0e10cSrcweir OTableKeyHelper::OTableKeyHelper(OTableHelper* _pTable) : connectivity::sdbcx::OKey(sal_True)
44cdf0e10cSrcweir ,m_pTable(_pTable)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir construct();
47cdf0e10cSrcweir }
48cdf0e10cSrcweir // -------------------------------------------------------------------------
OTableKeyHelper(OTableHelper * _pTable,const::rtl::OUString & _Name,const sdbcx::TKeyProperties & _rProps)49cdf0e10cSrcweir OTableKeyHelper::OTableKeyHelper( OTableHelper* _pTable
50cdf0e10cSrcweir ,const ::rtl::OUString& _Name
51cdf0e10cSrcweir ,const sdbcx::TKeyProperties& _rProps
52cdf0e10cSrcweir ) : connectivity::sdbcx::OKey(_Name,_rProps,sal_True)
53cdf0e10cSrcweir ,m_pTable(_pTable)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir construct();
56cdf0e10cSrcweir refreshColumns();
57cdf0e10cSrcweir }
58cdf0e10cSrcweir // -------------------------------------------------------------------------
refreshColumns()59cdf0e10cSrcweir void OTableKeyHelper::refreshColumns()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir if ( !m_pTable )
62cdf0e10cSrcweir return;
63cdf0e10cSrcweir
64cdf0e10cSrcweir ::std::vector< ::rtl::OUString> aVector;
65cdf0e10cSrcweir if ( !isNew() )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir aVector = m_aProps->m_aKeyColumnNames;
68cdf0e10cSrcweir if ( aVector.empty() )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
71cdf0e10cSrcweir ::rtl::OUString aSchema,aTable;
72cdf0e10cSrcweir m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
73cdf0e10cSrcweir m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
74cdf0e10cSrcweir
75cdf0e10cSrcweir if ( m_Name.getLength() ) // foreign key
76cdf0e10cSrcweir {
77cdf0e10cSrcweir
78cdf0e10cSrcweir Reference< XResultSet > xResult = m_pTable->getMetaData()->getImportedKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
79cdf0e10cSrcweir aSchema,aTable);
80cdf0e10cSrcweir
81cdf0e10cSrcweir if ( xResult.is() )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY);
84cdf0e10cSrcweir while( xResult->next() )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir ::rtl::OUString aForeignKeyColumn = xRow->getString(8);
87cdf0e10cSrcweir if(xRow->getString(12) == m_Name)
88cdf0e10cSrcweir aVector.push_back(aForeignKeyColumn);
89cdf0e10cSrcweir }
90cdf0e10cSrcweir }
91cdf0e10cSrcweir }
92cdf0e10cSrcweir
93cdf0e10cSrcweir if ( aVector.empty() )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir const Reference< XResultSet > xResult = m_pTable->getMetaData()->getPrimaryKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
96cdf0e10cSrcweir aSchema,aTable);
97cdf0e10cSrcweir
98cdf0e10cSrcweir if ( xResult.is() )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir const Reference< XRow > xRow(xResult,UNO_QUERY);
101cdf0e10cSrcweir while( xResult->next() )
102cdf0e10cSrcweir aVector.push_back(xRow->getString(4));
103cdf0e10cSrcweir } // if ( xResult.is() )
104cdf0e10cSrcweir }
105cdf0e10cSrcweir }
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
108cdf0e10cSrcweir
109cdf0e10cSrcweir if ( m_pColumns )
110cdf0e10cSrcweir m_pColumns ->reFill(aVector);
111cdf0e10cSrcweir else
112cdf0e10cSrcweir m_pColumns = new OKeyColumnsHelper(this,m_aMutex,aVector);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir // -----------------------------------------------------------------------------
115cdf0e10cSrcweir
116