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 #include "connectivity/TKeyColumns.hxx"
31 #include "connectivity/sdbcx/VKeyColumn.hxx"
32 #include <com/sun/star/sdbc/XRow.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/sdbc/DataType.hpp>
35 #include <com/sun/star/sdbc/ColumnValue.hpp>
36 #include <comphelper/extract.hxx>
37 #include <comphelper/property.hxx>
38 #include "TConnection.hxx"
39 #include "connectivity/TTableHelper.hxx"
40 
41 using namespace connectivity;
42 using namespace connectivity::sdbcx;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::beans;
45 //	using namespace ::com::sun::star::sdbcx;
46 using namespace ::com::sun::star::sdbc;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::lang;
49 
50 // -------------------------------------------------------------------------
51 OKeyColumnsHelper::OKeyColumnsHelper(	OTableKeyHelper* _pKey,
52 				::osl::Mutex& _rMutex,
53 				const ::std::vector< ::rtl::OUString> &_rVector)
54 			: connectivity::sdbcx::OCollection(*_pKey,sal_True,_rMutex,_rVector)
55 			,m_pKey(_pKey)
56 {
57 }
58 // -------------------------------------------------------------------------
59 sdbcx::ObjectType OKeyColumnsHelper::createObject(const ::rtl::OUString& _rName)
60 {
61 	::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
62 	::rtl::OUString aSchema,aTable;
63 	m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME))	>>= aSchema;
64 	m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))			>>= aTable;
65 
66 	// frist get the related column to _rName
67 	Reference< XResultSet > xResult = m_pKey->getTable()->getMetaData()->getImportedKeys(
68 			m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),aSchema,aTable);
69 
70 	::rtl::OUString aRefColumnName;
71 	if ( xResult.is() )
72 	{
73 		Reference< XRow > xRow(xResult,UNO_QUERY);
74 		::rtl::OUString aTemp;
75 		while(xResult->next())
76 		{
77 			aTemp = xRow->getString(4);
78 			if(xRow->getString(8) == _rName && m_pKey->getName() == xRow->getString(12))
79 			{
80 				aRefColumnName = aTemp;
81 				break;
82 			}
83 		}
84 	}
85 
86 	sdbcx::ObjectType xRet;
87 
88 	// now describe the column _rName and set his related column
89 	xResult = m_pKey->getTable()->getMetaData()->getColumns(
90 				m_pKey->getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)),aSchema,aTable,_rName);
91 
92 	if ( xResult.is() )
93 	{
94 		Reference< XRow > xRow(xResult,UNO_QUERY);
95 		if ( xResult->next() )
96 		{
97 			if ( xRow->getString(4) == _rName )
98 			{
99 				sal_Int32 nDataType = xRow->getInt(5);
100 				::rtl::OUString aTypeName(xRow->getString(6));
101 				sal_Int32 nSize = xRow->getInt(7);
102 				sal_Int32 nDec	= xRow->getInt(9);
103 				sal_Int32 nNull	= xRow->getInt(11);
104 				::rtl::OUString sColumnDef;
105 				try
106 				{
107 					sColumnDef = xRow->getString(13);
108 				}
109 				catch(const SQLException&)
110 				{
111 					// somethimes we get an error when asking for this param
112 				}
113 
114 				OKeyColumn* pRet = new OKeyColumn(aRefColumnName,
115 													_rName,
116 													aTypeName,
117 													sColumnDef,
118 													nNull,
119 													nSize,
120 													nDec,
121 													nDataType,
122 													sal_False,
123 													sal_False,
124 													sal_False,
125 													isCaseSensitive());
126 				xRet = pRet;
127 			}
128 		}
129 	}
130 
131 	return xRet;
132 }
133 // -------------------------------------------------------------------------
134 Reference< XPropertySet > OKeyColumnsHelper::createDescriptor()
135 {
136 	return new OKeyColumn(isCaseSensitive());
137 }
138 // -------------------------------------------------------------------------
139 void OKeyColumnsHelper::impl_refresh() throw(::com::sun::star::uno::RuntimeException)
140 {
141 	m_pKey->refreshColumns();
142 }
143 // -----------------------------------------------------------------------------
144 
145 
146