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