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 "ado/ADatabaseMetaDataResultSetMetaData.hxx"
31 #include "ado/Awrapado.hxx"
32 #include "connectivity/dbexception.hxx"
33 
34 
35 using namespace connectivity;
36 using namespace connectivity::ado;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::beans;
40 using namespace com::sun::star::sdbc;
41 
42 // -------------------------------------------------------------------------
43 ODatabaseMetaDataResultSetMetaData::~ODatabaseMetaDataResultSetMetaData()
44 {
45 	if(m_pRecordSet)
46 		m_pRecordSet->Release();
47 }
48 // -------------------------------------------------------------------------
49 sal_Int32 SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnDisplaySize( sal_Int32 column ) throw(SQLException, RuntimeException)
50 {
51 	sal_Int32 nSize = 0;
52 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
53 		nSize = (*m_mColumnsIter).second.getColumnDisplaySize();
54 	else if(m_pRecordSet)
55 	{
56 		WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
57 		if(aField.IsValid())
58 			nSize = aField.GetActualSize();
59 	}
60 	return nSize;
61 }
62 // -------------------------------------------------------------------------
63 
64 sal_Int32 SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnType( sal_Int32 column ) throw(SQLException, RuntimeException)
65 {
66 	sal_Int32  nType = 0;
67 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
68 		nType = (*m_mColumnsIter).second.getColumnType();
69 	else if(m_pRecordSet)
70 	{
71 		WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
72 		nType = ADOS::MapADOType2Jdbc(aField.GetADOType());
73 	}
74 	return nType;
75 }
76 // -------------------------------------------------------------------------
77 
78 sal_Int32 SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnCount(  ) throw(SQLException, RuntimeException)
79 {
80 	if(!m_pRecordSet)
81 		return 0;
82 	if(m_nColCount != -1)
83 		return m_nColCount;
84 
85 	if(m_vMapping.size())
86 		return m_mColumns.size();
87 
88 	ADOFields* pFields	= NULL;
89 	m_pRecordSet->get_Fields(&pFields);
90 	WpOLEAppendCollection<ADOFields, ADOField, WpADOField>	aFields(pFields);
91 	m_nColCount = aFields.GetItemCount();
92 	return m_nColCount;
93 }
94 // -------------------------------------------------------------------------
95 
96 ::rtl::OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnName( sal_Int32 column ) throw(SQLException, RuntimeException)
97 {
98 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
99 		return (*m_mColumnsIter).second.getColumnName();
100 	if(!m_pRecordSet)
101 		return ::rtl::OUString();
102 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
103 	if(aField.IsValid())
104 		return aField.GetName();
105 
106 	return ::rtl::OUString();
107 }
108 // -------------------------------------------------------------------------
109 ::rtl::OUString SAL_CALL ODatabaseMetaDataResultSetMetaData::getColumnLabel( sal_Int32 column ) throw(SQLException, RuntimeException)
110 {
111 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
112 		return (*m_mColumnsIter).second.getColumnLabel();
113 	return getColumnName(column);
114 }
115 // -------------------------------------------------------------------------
116 
117 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isCurrency( sal_Int32 column ) throw(SQLException, RuntimeException)
118 {
119 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
120 		return (*m_mColumnsIter).second.isCurrency();
121 	if(!m_pRecordSet)
122 		return 0;
123 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
124 	if(aField.IsValid())
125 	{
126 		return (aField.GetAttributes() & adFldFixed) == adFldFixed;
127 	}
128 	return sal_False;
129 }
130 // -------------------------------------------------------------------------
131 
132 
133 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isSigned( sal_Int32 column ) throw(SQLException, RuntimeException)
134 {
135 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
136 		return (*m_mColumnsIter).second.isSigned();
137 	if(!m_pRecordSet)
138 		return 0;
139 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
140 	if(aField.IsValid())
141 	{
142 		return (aField.GetAttributes() & adFldNegativeScale) == adFldNegativeScale;
143 	}
144 	return sal_False;
145 }
146 // -------------------------------------------------------------------------
147 sal_Int32 SAL_CALL ODatabaseMetaDataResultSetMetaData::getPrecision( sal_Int32 column ) throw(SQLException, RuntimeException)
148 {
149 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
150 		return (*m_mColumnsIter).second.getPrecision();
151 	if(!m_pRecordSet)
152 		return 0;
153 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
154 	if(aField.IsValid())
155 		return aField.GetPrecision();
156 	return 0;
157 }
158 // -------------------------------------------------------------------------
159 sal_Int32 SAL_CALL ODatabaseMetaDataResultSetMetaData::getScale( sal_Int32 column ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
160 {
161 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
162 		return (*m_mColumnsIter).second.getScale();
163 
164 	if(!m_pRecordSet)
165 		return 0;
166 
167 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
168 	if(aField.IsValid())
169 		return aField.GetNumericScale();
170 	return 0;
171 }
172 // -------------------------------------------------------------------------
173 
174 sal_Int32 SAL_CALL ODatabaseMetaDataResultSetMetaData::isNullable( sal_Int32 column ) throw(SQLException, RuntimeException)
175 {
176 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
177 		return (*m_mColumnsIter).second.isNullable();
178 
179 	if(!m_pRecordSet)
180 		return 0;
181 
182 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
183 	if(aField.IsValid())
184 	{
185 		return (aField.GetAttributes() & adFldIsNullable) == adFldIsNullable;
186 	}
187 	return sal_False;
188 }
189 // -------------------------------------------------------------------------
190 
191 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isReadOnly( sal_Int32 column ) throw(SQLException, RuntimeException)
192 {
193 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
194 		return (*m_mColumnsIter).second.isReadOnly();
195 
196 	if(!m_pRecordSet)
197 		return 0;
198 
199 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
200 	if(aField.IsValid())
201 	{
202 		//	return (aField.GetStatus() & adFieldReadOnly) == adFieldReadOnly;
203 	}
204 	return sal_False;
205 }
206 // -------------------------------------------------------------------------
207 
208 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isDefinitelyWritable( sal_Int32 column ) throw(SQLException, RuntimeException)
209 {
210 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
211 		return (*m_mColumnsIter).second.isDefinitelyWritable();
212 
213 	if(!m_pRecordSet)
214 		return 0;
215 
216 	WpADOField aField = ADOS::getField(m_pRecordSet,m_vMapping[column]);
217 	if(aField.IsValid())
218 	{
219 		return (aField.GetAttributes() & adFldUpdatable) == adFldUpdatable;
220 	}
221 	return sal_False;
222 ;
223 }
224 // -------------------------------------------------------------------------
225 sal_Bool SAL_CALL ODatabaseMetaDataResultSetMetaData::isWritable( sal_Int32 column ) throw(SQLException, RuntimeException)
226 {
227 	if(m_mColumns.size() && (m_mColumnsIter = m_mColumns.find(column)) != m_mColumns.end())
228 		return (*m_mColumnsIter).second.isWritable();
229 	return isDefinitelyWritable(column);
230 }
231 // -----------------------------------------------------------------------------
232 
233 
234