LibreOffice Module connectivity (master) 1
firebird/Clob.cxx
Go to the documentation of this file.
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.
4 *
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 */
9
10#include <sal/config.h>
11
12#include "Clob.hxx"
13#include "Blob.hxx"
14
17
18using namespace ::connectivity::firebird;
19
20using namespace ::osl;
21
22using namespace ::com::sun::star;
23using namespace ::com::sun::star::io;
24using namespace ::com::sun::star::sdbc;
25using namespace ::com::sun::star::uno;
26
27Clob::Clob(isc_db_handle* pDatabaseHandle,
28 isc_tr_handle* pTransactionHandle,
29 ISC_QUAD const & aBlobID):
31 m_aBlob(new connectivity::firebird::Blob(pDatabaseHandle, pTransactionHandle, aBlobID)),
32 m_nCharCount(-1)
33{
34}
35
36void SAL_CALL Clob::disposing()
37{
38 m_aBlob->dispose();
39 m_aBlob.clear();
40 Clob_BASE::disposing();
41}
42
43sal_Int64 SAL_CALL Clob::length()
44{
45 MutexGuard aGuard(m_aMutex);
46 checkDisposed(Clob_BASE::rBHelper.bDisposed);
47
48 if( m_nCharCount >= 0 )
49 return m_nCharCount;
50 m_nCharCount = 0;
51
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;
56 do
57 {
58 bLastSegmRead = m_aBlob->readOneSegment( aSegmentBytes );
59 OUString sSegment(aSegmentBytes.data(), aSegmentBytes.size(), RTL_TEXTENCODING_UTF8);
60
61 if( !bLastSegmRead)
62 m_nCharCount += sSegment.getLength();
63 }while( !bLastSegmRead );
64
65 m_aBlob->closeInput(); // reset position
66 return m_nCharCount;
67}
68
69OUString SAL_CALL Clob::getSubString(sal_Int64 nPosition,
70 sal_Int32 nLength)
71{
72 if (nPosition < 1) // XClob is indexed from 1
73 throw lang::IllegalArgumentException("nPosition < 1", *this, 0);
74 --nPosition; // make 0-based
75
76 if (nLength < 0)
77 throw lang::IllegalArgumentException("nLength < 0", *this, 0);
78
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
83
84 OUStringBuffer sSegmentBuffer;
85 std::vector<char> aSegmentBytes;
86
87 for (;;)
88 {
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);
92
93 // skip irrelevant parts
94 if (sSegment.getLength() < nPosition)
95 {
96 if (bLastRead)
97 throw lang::IllegalArgumentException("nPosition out of range", *this, 0);
98 nPosition -= sSegment.getLength();
99 continue;
100 }
101
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();
109
110 assert(sSegmentBuffer.getLength() < nLength);
111
112 if (bLastRead)
113 throw lang::IllegalArgumentException("out of range", *this, 0);
114
115 nPosition = 0; // No offset after first append
116 }
117}
118
119uno::Reference< XInputStream > SAL_CALL Clob::getCharacterStream()
120{
121 MutexGuard aGuard(m_aMutex);
122 checkDisposed(Clob_BASE::rBHelper.bDisposed);
123
124 return m_aBlob->getBinaryStream();
125}
126
127sal_Int64 SAL_CALL Clob::position(const OUString& /*rPattern*/,
128 sal_Int32 /*nStart*/)
129{
131 return 0;
132}
133
134sal_Int64 SAL_CALL Clob::positionOfClob(const Reference <XClob >& /*rPattern*/,
135 sal_Int64 /*aStart*/)
136{
137 ::dbtools::throwFeatureNotImplementedSQLException("Clob::positionOfClob", *this);
138 return 0;
139}
140
141/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
virtual OUString SAL_CALL getSubString(sal_Int64 aPosition, sal_Int32 aLength) override
virtual sal_Int64 SAL_CALL positionOfClob(const ::css::uno::Reference< ::css::sdbc::XClob > &rPattern, sal_Int64 aStart) override
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getCharacterStream() override
virtual sal_Int64 SAL_CALL length() override
virtual void SAL_CALL disposing() override
rtl::Reference< connectivity::firebird::Blob > m_aBlob
virtual sal_Int64 SAL_CALL position(const OUString &rPattern, sal_Int32 aStart) override
std::mutex m_aMutex
void checkDisposed(bool _bThrow)
Definition: Driver.cxx:208
::cppu::WeakComponentImplHelper< css::sdbc::XClob > Clob_BASE
void throwFeatureNotImplementedSQLException(const OUString &_rFeatureName, const Reference< XInterface > &_rxContext, const Any &_rNextException)
sal_Int32 nLength