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