cid#1636677 Uninitialized scalar field
[LibreOffice.git] / connectivity / source / drivers / firebird / Clob.cxx
blob95d14c11d08d9d1c007d951d26f20da978614e3c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #include <sal/config.h>
12 #include "Clob.hxx"
13 #include "Blob.hxx"
15 #include <connectivity/CommonTools.hxx>
16 #include <connectivity/dbexception.hxx>
18 using namespace ::connectivity::firebird;
20 using namespace ::osl;
22 using namespace ::com::sun::star;
23 using namespace ::com::sun::star::io;
24 using namespace ::com::sun::star::sdbc;
25 using namespace ::com::sun::star::uno;
27 Clob::Clob(isc_db_handle* pDatabaseHandle,
28 isc_tr_handle* pTransactionHandle,
29 ISC_QUAD const & aBlobID):
30 Clob_BASE(m_aMutex),
31 m_aBlob(new connectivity::firebird::Blob(pDatabaseHandle, pTransactionHandle, aBlobID)),
32 m_nCharCount(-1)
36 void SAL_CALL Clob::disposing()
38 m_aBlob->dispose();
39 m_aBlob.clear();
40 Clob_BASE::disposing();
43 sal_Int64 SAL_CALL Clob::length()
45 MutexGuard aGuard(m_aMutex);
46 checkDisposed(Clob_BASE::rBHelper.bDisposed);
48 if( m_nCharCount >= 0 )
49 return m_nCharCount;
50 m_nCharCount = 0;
52 // Read each segment, and calculate it's size by interpreting it as a
53 // character stream. Assume that no characters are split by the segments.
54 bool bLastSegmRead = false;
55 std::vector<char> aSegmentBytes;
58 bLastSegmRead = m_aBlob->readOneSegment( aSegmentBytes );
59 OUString sSegment(aSegmentBytes.data(), aSegmentBytes.size(), RTL_TEXTENCODING_UTF8);
61 if( !bLastSegmRead)
62 m_nCharCount += sSegment.getLength();
63 }while( !bLastSegmRead );
65 m_aBlob->closeInput(); // reset position
66 return m_nCharCount;
69 OUString SAL_CALL Clob::getSubString(sal_Int64 nPosition,
70 sal_Int32 nLength)
72 if (nPosition < 1) // XClob is indexed from 1
73 throw lang::IllegalArgumentException(u"nPosition < 1"_ustr, *this, 0);
74 --nPosition; // make 0-based
76 if (nLength < 0)
77 throw lang::IllegalArgumentException(u"nLength < 0"_ustr, *this, 0);
79 MutexGuard aGuard(m_aMutex);
80 checkDisposed(Clob_BASE::rBHelper.bDisposed);
81 // TODO do not reset position if it is not necessary
82 m_aBlob->closeInput(); // reset position
84 OUStringBuffer sSegmentBuffer;
85 std::vector<char> aSegmentBytes;
87 for (;;)
89 bool bLastRead = m_aBlob->readOneSegment( aSegmentBytes );
90 // TODO: handle possible case of split UTF-8 character
91 OUString sSegment(aSegmentBytes.data(), aSegmentBytes.size(), RTL_TEXTENCODING_UTF8);
93 // skip irrelevant parts
94 if (sSegment.getLength() < nPosition)
96 if (bLastRead)
97 throw lang::IllegalArgumentException(u"nPosition out of range"_ustr, *this, 0);
98 nPosition -= sSegment.getLength();
99 continue;
102 // Getting here for the first time, nPosition may be > 0, meaning copy start offset.
103 // This also handles sSegment.getLength() == nPosition case, including nLength == 0.
104 const sal_Int32 nCharsToCopy = std::min<sal_Int32>(sSegment.getLength() - nPosition,
105 nLength - sSegmentBuffer.getLength());
106 sSegmentBuffer.append(sSegment.subView(nPosition, nCharsToCopy));
107 if (sSegmentBuffer.getLength() == nLength)
108 return sSegmentBuffer.makeStringAndClear();
110 assert(sSegmentBuffer.getLength() < nLength);
112 if (bLastRead)
113 throw lang::IllegalArgumentException(u"out of range"_ustr, *this, 0);
115 nPosition = 0; // No offset after first append
119 uno::Reference< XInputStream > SAL_CALL Clob::getCharacterStream()
121 MutexGuard aGuard(m_aMutex);
122 checkDisposed(Clob_BASE::rBHelper.bDisposed);
124 return m_aBlob->getBinaryStream();
127 sal_Int64 SAL_CALL Clob::position(const OUString& /*rPattern*/,
128 sal_Int32 /*nStart*/)
130 ::dbtools::throwFeatureNotImplementedSQLException(u"Clob::position"_ustr, *this);
133 sal_Int64 SAL_CALL Clob::positionOfClob(const Reference <XClob >& /*rPattern*/,
134 sal_Int64 /*aStart*/)
136 ::dbtools::throwFeatureNotImplementedSQLException(u"Clob::positionOfClob"_ustr, *this);
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */