1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 
32 #include "connectivity/TKey.hxx"
33 #include "connectivity/TKeyColumns.hxx"
34 #include <com/sun/star/sdbc/XRow.hpp>
35 #include <com/sun/star/sdbc/XResultSet.hpp>
36 #include "TConnection.hxx"
37 #include "connectivity/TTableHelper.hxx"
38 
39 using namespace connectivity;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 //	using namespace ::com::sun::star::sdbcx;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::lang;
46 // -------------------------------------------------------------------------
47 OTableKeyHelper::OTableKeyHelper(OTableHelper* _pTable) : connectivity::sdbcx::OKey(sal_True)
48 	,m_pTable(_pTable)
49 {
50 	construct();
51 }
52 // -------------------------------------------------------------------------
53 OTableKeyHelper::OTableKeyHelper(	OTableHelper* _pTable
54             ,const ::rtl::OUString& _Name
55             ,const sdbcx::TKeyProperties& _rProps
56 			) : connectivity::sdbcx::OKey(_Name,_rProps,sal_True)
57 				,m_pTable(_pTable)
58 {
59 	construct();
60 	refreshColumns();
61 }
62 // -------------------------------------------------------------------------
63 void OTableKeyHelper::refreshColumns()
64 {
65 	if ( !m_pTable )
66 		return;
67 
68 	::std::vector< ::rtl::OUString> aVector;
69 	if ( !isNew() )
70 	{
71         aVector = m_aProps->m_aKeyColumnNames;
72         if ( aVector.empty() )
73 		{
74 		    ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
75 		    ::rtl::OUString aSchema,aTable;
76 		    m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME))	>>= aSchema;
77 		    m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))		>>= aTable;
78 
79 		    if ( m_Name.getLength() ) // foreign key
80 		    {
81 
82 			    Reference< XResultSet > xResult = m_pTable->getMetaData()->getImportedKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
83 				    aSchema,aTable);
84 
85 			    if ( xResult.is() )
86 			    {
87 				    Reference< XRow > xRow(xResult,UNO_QUERY);
88 				    while( xResult->next() )
89 				    {
90 					    ::rtl::OUString aForeignKeyColumn = xRow->getString(8);
91 					    if(xRow->getString(12) == m_Name)
92 						    aVector.push_back(aForeignKeyColumn);
93 				    }
94 			    }
95 		    }
96 
97             if ( aVector.empty() )
98 		    {
99 			    const Reference< XResultSet > xResult = m_pTable->getMetaData()->getPrimaryKeys(m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),
100 				    aSchema,aTable);
101 
102 			    if ( xResult.is() )
103 			    {
104 				    const Reference< XRow > xRow(xResult,UNO_QUERY);
105 				    while( xResult->next() )
106 					    aVector.push_back(xRow->getString(4));
107 			    } // if ( xResult.is() )
108             }
109 		}
110 	}
111 
112 
113 	if ( m_pColumns )
114 		m_pColumns ->reFill(aVector);
115 	else
116 		m_pColumns	= new OKeyColumnsHelper(this,m_aMutex,aVector);
117 }
118 // -----------------------------------------------------------------------------
119 
120