Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / file / filrow.cxx
blob54ba6140f040f35f92a37a2e5f1c6ba58e03b36b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "filrow.hxx"
21 #include "filtask.hxx"
22 #include <com/sun/star/script/CannotConvertException.hpp>
23 #include <com/sun/star/script/Converter.hpp>
24 #include <com/sun/star/sdbc/SQLException.hpp>
26 using namespace fileaccess;
27 using namespace com::sun::star;
28 using namespace css::uno;
30 #if OSL_DEBUG_LEVEL > 0
31 #define THROW_WHERE SAL_WHERE
32 #else
33 #define THROW_WHERE ""
34 #endif
36 // Function for TypeConverting
38 template< class _type_ >
39 static bool convert( TaskManager const * pShell,
40 uno::Reference< script::XTypeConverter >& xConverter,
41 uno::Any& rValue,
42 _type_& aReturn )
44 // Try first without converting
45 bool no_success = ! ( rValue >>= aReturn );
47 if ( no_success )
49 if( ! xConverter.is() )
51 xConverter = script::Converter::create(pShell->m_xContext);
54 try
56 if( rValue.hasValue() )
58 uno::Any aConvertedValue
59 = xConverter->convertTo( rValue,cppu::UnoType<_type_>::get() );
60 no_success = ! ( aConvertedValue >>= aReturn );
62 else
63 no_success = true;
65 catch (const lang::IllegalArgumentException&)
67 no_success = true;
69 catch (const script::CannotConvertException&)
71 no_success = true;
74 return no_success;
78 XRow_impl::XRow_impl( TaskManager* pMyShell,const uno::Sequence< uno::Any >& seq )
79 : m_aValueMap( seq ),
80 m_nWasNull(false),
81 m_pMyShell( pMyShell )
85 XRow_impl::~XRow_impl()
90 sal_Bool SAL_CALL
91 XRow_impl::wasNull()
93 return m_nWasNull;
97 OUString SAL_CALL
98 XRow_impl::getString(
99 sal_Int32 columnIndex )
101 if( isIndexOutOfBounds( columnIndex ) )
102 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
103 return getValue<OUString>(columnIndex);
106 sal_Bool SAL_CALL
107 XRow_impl::getBoolean(
108 sal_Int32 columnIndex )
110 if( isIndexOutOfBounds( columnIndex ) )
111 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
112 return getValue<bool>(columnIndex);
116 sal_Int8 SAL_CALL
117 XRow_impl::getByte(
118 sal_Int32 columnIndex )
120 if( isIndexOutOfBounds( columnIndex ) )
121 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
122 return getValue<sal_Int8>(columnIndex);
125 sal_Int16 SAL_CALL
126 XRow_impl::getShort(
127 sal_Int32 columnIndex )
129 if( isIndexOutOfBounds( columnIndex ) )
130 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
131 return getValue<sal_Int16>(columnIndex);
135 sal_Int32 SAL_CALL
136 XRow_impl::getInt(
137 sal_Int32 columnIndex )
139 if( isIndexOutOfBounds( columnIndex ) )
140 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
141 return getValue<sal_Int32>(columnIndex);
144 sal_Int64 SAL_CALL
145 XRow_impl::getLong(
146 sal_Int32 columnIndex )
148 if( isIndexOutOfBounds( columnIndex ) )
149 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
150 return getValue<sal_Int64>(columnIndex);
153 float SAL_CALL
154 XRow_impl::getFloat(
155 sal_Int32 columnIndex )
157 if( isIndexOutOfBounds( columnIndex ) )
158 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
159 return getValue<float>(columnIndex);
162 double SAL_CALL
163 XRow_impl::getDouble(
164 sal_Int32 columnIndex )
166 if( isIndexOutOfBounds( columnIndex ) )
167 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
168 return getValue<double>(columnIndex);
171 uno::Sequence< sal_Int8 > SAL_CALL
172 XRow_impl::getBytes(
173 sal_Int32 columnIndex )
175 if( isIndexOutOfBounds( columnIndex ) )
176 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
177 return getValue<uno::Sequence< sal_Int8 >>(columnIndex);
180 util::Date SAL_CALL
181 XRow_impl::getDate(
182 sal_Int32 columnIndex )
184 if( isIndexOutOfBounds( columnIndex ) )
185 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
186 return getValue<util::Date>(columnIndex);
189 util::Time SAL_CALL
190 XRow_impl::getTime(
191 sal_Int32 columnIndex )
193 if( isIndexOutOfBounds( columnIndex ) )
194 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
195 return getValue<util::Time>(columnIndex);
198 util::DateTime SAL_CALL
199 XRow_impl::getTimestamp(
200 sal_Int32 columnIndex )
202 if( isIndexOutOfBounds( columnIndex ) )
203 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
204 return getValue<util::DateTime>(columnIndex);
208 uno::Reference< io::XInputStream > SAL_CALL
209 XRow_impl::getBinaryStream(
210 sal_Int32 columnIndex )
212 if( isIndexOutOfBounds( columnIndex ) )
213 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
214 return getValue<uno::Reference< io::XInputStream >>(columnIndex);
218 uno::Reference< io::XInputStream > SAL_CALL
219 XRow_impl::getCharacterStream(
220 sal_Int32 columnIndex )
222 if( isIndexOutOfBounds( columnIndex ) )
223 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
224 return getValue<uno::Reference< io::XInputStream >>(columnIndex);
228 uno::Any SAL_CALL
229 XRow_impl::getObject(
230 sal_Int32 columnIndex,
231 const uno::Reference< container::XNameAccess >& )
233 if( isIndexOutOfBounds( columnIndex ) )
234 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
235 osl::MutexGuard aGuard( m_aMutex );
236 uno::Any Value = m_aValueMap[columnIndex - 1];
237 m_nWasNull = !Value.hasValue();
238 return Value;
241 uno::Reference< sdbc::XRef > SAL_CALL
242 XRow_impl::getRef(
243 sal_Int32 columnIndex )
245 if( isIndexOutOfBounds( columnIndex ) )
246 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
247 return getValue<uno::Reference< sdbc::XRef >>(columnIndex);
250 uno::Reference< sdbc::XBlob > SAL_CALL
251 XRow_impl::getBlob(
252 sal_Int32 columnIndex )
254 if( isIndexOutOfBounds( columnIndex ) )
255 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
256 return getValue<uno::Reference< sdbc::XBlob >>(columnIndex);
259 uno::Reference< sdbc::XClob > SAL_CALL
260 XRow_impl::getClob(
261 sal_Int32 columnIndex )
263 if( isIndexOutOfBounds( columnIndex ) )
264 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
265 return getValue<uno::Reference< sdbc::XClob >>(columnIndex);
269 uno::Reference< sdbc::XArray > SAL_CALL
270 XRow_impl::getArray(
271 sal_Int32 columnIndex )
273 if( isIndexOutOfBounds( columnIndex ) )
274 throw sdbc::SQLException( THROW_WHERE, uno::Reference< uno::XInterface >(), OUString(), 0, uno::Any() );
275 return getValue<uno::Reference< sdbc::XArray >>(columnIndex);
278 bool
279 XRow_impl::isIndexOutOfBounds(sal_Int32 nIndex)
281 return nIndex < 1 || m_aValueMap.getLength() < nIndex;
284 template<typename T>
285 T XRow_impl::getValue(sal_Int32 columnIndex)
287 T aValue{};
288 osl::MutexGuard aGuard( m_aMutex );
289 m_nWasNull = ::convert<T>( m_pMyShell, m_xTypeConverter, m_aValueMap[ --columnIndex ], aValue );
290 return aValue;
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */