1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "mysqlc_propertyids.hxx"
21 #include "mysqlc_general.hxx"
22 #include "mysqlc_resultset.hxx"
23 #include "mysqlc_resultsetmetadata.hxx"
24 
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
27 #include <com/sun/star/sdbc/ResultSetType.hpp>
28 #include <com/sun/star/sdbc/FetchDirection.hpp>
29 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <comphelper/seqstream.hxx>
33 
34 using namespace rtl;
35 
36 using namespace connectivity::mysqlc;
37 using namespace cppu;
38 using namespace com::sun::star;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::beans;
41 using namespace com::sun::star::sdbc;
42 using namespace com::sun::star::sdbcx;
43 using namespace com::sun::star::container;
44 using namespace com::sun::star::io;
45 using namespace com::sun::star::uno;
46 using namespace com::sun::star::util;
47 using namespace ::comphelper;
48 using ::osl::MutexGuard;
49 
50 namespace
51 {
52 // copied from string misc, it should be replaced when library is not an
53 // extension anymore
lcl_split(const OString & rStr,char cSeparator)54 std::vector<OString> lcl_split(const OString& rStr, char cSeparator)
55 {
56     std::vector<OString> vec;
57     sal_Int32 idx = 0;
58     do
59     {
60         OString kw = rStr.getToken(0, cSeparator, idx);
61         kw = kw.trim();
62         if (!kw.isEmpty())
63         {
64             vec.push_back(kw);
65         }
66     } while (idx >= 0);
67     return vec;
68 }
69 }
70 
checkRowIndex()71 void OResultSet::checkRowIndex()
72 {
73     if (m_nRowPosition < 0 || m_nRowPosition >= m_nRowCount)
74     {
75         throw SQLException("Cursor position out of range", *this, OUString(), 1, Any());
76     }
77 }
78 
checkNull(sal_Int32 column)79 bool OResultSet::checkNull(sal_Int32 column)
80 {
81     if (m_aRows[m_nRowPosition][column - 1].isEmpty())
82     {
83         m_bWasNull = true;
84         return true;
85     }
86     m_bWasNull = false;
87     return false;
88 }
89 
getImplementationName()90 OUString SAL_CALL OResultSet::getImplementationName()
91 {
92     return "com.sun.star.sdbcx.mysqlc.ResultSet";
93 }
94 
getSupportedServiceNames()95 uno::Sequence<OUString> SAL_CALL OResultSet::getSupportedServiceNames()
96 {
97     return { "com.sun.star.sdbc.ResultSet", "com.sun.star.sdbcx.ResultSet" };
98 }
99 
supportsService(const OUString & _rServiceName)100 sal_Bool SAL_CALL OResultSet::supportsService(const OUString& _rServiceName)
101 {
102     return cppu::supportsService(this, _rServiceName);
103 }
104 
OResultSet(OConnection & rConn,OCommonStatement * pStmt,MYSQL_RES * pResult,rtl_TextEncoding _encoding)105 OResultSet::OResultSet(OConnection& rConn, OCommonStatement* pStmt, MYSQL_RES* pResult,
106                        rtl_TextEncoding _encoding)
107     : OResultSet_BASE(m_aMutex)
108     , OPropertySetHelper(OResultSet_BASE::rBHelper)
109     , m_pMysql(rConn.getMysqlConnection())
110     , m_aStatement(static_cast<OWeakObject*>(pStmt))
111     , m_pResult(pResult)
112     , m_encoding(_encoding)
113 {
114     assert(m_pResult);
115     m_xMetaData = new OResultSetMetaData(rConn, m_pResult);
116 }
117 
ensureResultFetched()118 void OResultSet::ensureResultFetched()
119 {
120     if (m_pResult)
121     {
122         fetchResult();
123     }
124 }
125 
ensureFieldInfoFetched()126 void OResultSet::ensureFieldInfoFetched()
127 {
128     if (m_pResult == nullptr)
129         return; // already fetched
130 
131     // it works only if result set is produced via mysql_store_result
132     // TODO ensure that
133     m_nRowCount = mysql_num_rows(m_pResult);
134 
135     if (!m_aFields.empty())
136         return;
137     unsigned nFieldCount = mysql_num_fields(m_pResult);
138     MYSQL_FIELD* pFields = mysql_fetch_fields(m_pResult);
139     m_aFields.reserve(nFieldCount);
140     for (unsigned i = 0; i < nFieldCount; ++i)
141         m_aFields.push_back(OUString{
142             pFields[i].name, static_cast<sal_Int32>(strlen(pFields[i].name)), m_encoding });
143 }
144 
fetchResult()145 void OResultSet::fetchResult()
146 {
147     // Mysql C API does not allow simultaneously opened result sets, but sdbc does.
148     // Because of that we need to fetch all of the data ASAP
149     ensureFieldInfoFetched();
150 
151     // fetch all the data
152     m_aRows.reserve(m_nRowCount);
153 
154     for (sal_Int32 row = 0; row < m_nRowCount; ++row)
155     {
156         MYSQL_ROW data = mysql_fetch_row(m_pResult);
157         unsigned long* lengths = mysql_fetch_lengths(m_pResult);
158         m_aRows.push_back(DataFields{});
159         // MYSQL_ROW is char**, array of strings
160         for (std::size_t col = 0; col < m_aFields.size(); ++col)
161         {
162             m_aRows.back().push_back(OString{ data[col], static_cast<sal_Int32>(lengths[col]) });
163         }
164     }
165     unsigned errorNum = mysql_errno(m_pMysql);
166     if (errorNum)
167         mysqlc_sdbc_driver::throwSQLExceptionWithMsg(
168             mysql_error(m_pMysql), mysql_sqlstate(m_pMysql), errorNum, *this, m_encoding);
169     mysql_free_result(m_pResult);
170     m_pResult = nullptr;
171 }
172 
disposing()173 void OResultSet::disposing()
174 {
175     OPropertySetHelper::disposing();
176 
177     MutexGuard aGuard(m_aMutex);
178 
179     if (m_pResult != nullptr)
180     {
181         mysql_free_result(m_pResult);
182         m_pResult = nullptr;
183     }
184     m_aStatement = nullptr;
185     m_xMetaData = nullptr;
186 }
187 
queryInterface(const Type & rType)188 Any SAL_CALL OResultSet::queryInterface(const Type& rType)
189 {
190     Any aRet = OPropertySetHelper::queryInterface(rType);
191     if (!aRet.hasValue())
192     {
193         aRet = OResultSet_BASE::queryInterface(rType);
194     }
195     return aRet;
196 }
197 
getTypes()198 uno::Sequence<Type> SAL_CALL OResultSet::getTypes()
199 {
200     OTypeCollection aTypes(cppu::UnoType<XMultiPropertySet>::get(),
201                            cppu::UnoType<XFastPropertySet>::get(),
202                            cppu::UnoType<XPropertySet>::get());
203 
204     return concatSequences(aTypes.getTypes(), OResultSet_BASE::getTypes());
205 }
206 
findColumn(const OUString & columnName)207 sal_Int32 SAL_CALL OResultSet::findColumn(const OUString& columnName)
208 {
209     MutexGuard aGuard(m_aMutex);
210     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
211     ensureFieldInfoFetched();
212 
213     for (std::size_t i = 0; i < m_aFields.size(); ++i)
214     {
215         if (columnName.equalsIgnoreAsciiCase(m_aFields[i]))
216             return static_cast<sal_Int32>(i) + 1; // sdbc indexes from 1
217     }
218 
219     throw SQLException("The column name '" + columnName + "' is not valid.", *this, "42S22", 0,
220                        Any());
221 }
222 
getBinaryStream(sal_Int32 column)223 uno::Reference<XInputStream> SAL_CALL OResultSet::getBinaryStream(sal_Int32 column)
224 {
225     MutexGuard aGuard(m_aMutex);
226     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
227     checkBordersAndEnsureFetched(column);
228     if (checkNull(column))
229         return nullptr;
230 
231     OString sVal = m_aRows[m_nRowPosition][column - 1];
232     return new SequenceInputStream{ uno::Sequence<sal_Int8>(
233         reinterpret_cast<sal_Int8 const*>(sVal.getStr()), getDataLength(column)) };
234 }
235 
getCharacterStream(sal_Int32 column)236 uno::Reference<XInputStream> SAL_CALL OResultSet::getCharacterStream(sal_Int32 column)
237 {
238     MutexGuard aGuard(m_aMutex);
239     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
240     checkBordersAndEnsureFetched(column);
241     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getCharacterStream",
242                                                             *this);
243     return nullptr;
244 }
245 
getBoolean(sal_Int32 column)246 sal_Bool SAL_CALL OResultSet::getBoolean(sal_Int32 column)
247 {
248     MutexGuard aGuard(m_aMutex);
249     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
250     checkBordersAndEnsureFetched(column);
251     if (checkNull(column))
252         return false;
253 
254     OString sVal = m_aRows[m_nRowPosition][column - 1];
255     return sVal.toInt32() != 0;
256 }
257 
getByte(sal_Int32 column)258 sal_Int8 SAL_CALL OResultSet::getByte(sal_Int32 column)
259 {
260     MutexGuard aGuard(m_aMutex);
261     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
262     checkBordersAndEnsureFetched(column);
263     if (checkNull(column))
264         return 0;
265 
266     OString sVal = m_aRows[m_nRowPosition][column - 1];
267 
268     return static_cast<sal_Int8>(sVal.toInt32());
269 }
270 
getBytes(sal_Int32 column)271 uno::Sequence<sal_Int8> SAL_CALL OResultSet::getBytes(sal_Int32 column)
272 {
273     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
274     MutexGuard aGuard(m_aMutex);
275     checkBordersAndEnsureFetched(column);
276     OString sVal = m_aRows[m_nRowPosition][column - 1];
277     if (checkNull(column))
278         return uno::Sequence<sal_Int8>();
279 
280     return uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const*>(sVal.getStr()),
281                                    getDataLength(column));
282 }
283 
getDate(sal_Int32 column)284 Date SAL_CALL OResultSet::getDate(sal_Int32 column)
285 {
286     MutexGuard aGuard(m_aMutex);
287     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
288     checkBordersAndEnsureFetched(column);
289 
290     Date d;
291 
292     if (checkNull(column))
293         return d;
294 
295     OString dateStr = m_aRows[m_nRowPosition][column - 1];
296 
297     OString dateString(dateStr);
298     OString token;
299     sal_Int32 nIndex = 0, i = 0;
300     do
301     {
302         token = dateString.getToken(0, '-', nIndex);
303         switch (i)
304         {
305             case 0:
306                 d.Year = static_cast<sal_uInt16>(token.toUInt32());
307                 break;
308             case 1:
309                 d.Month = static_cast<sal_uInt16>(token.toUInt32());
310                 break;
311             case 2:
312                 d.Day = static_cast<sal_uInt16>(token.toUInt32());
313                 break;
314             default:;
315         }
316         i++;
317     } while (nIndex >= 0);
318     return d;
319 }
320 
getDouble(sal_Int32 column)321 double SAL_CALL OResultSet::getDouble(sal_Int32 column)
322 {
323     MutexGuard aGuard(m_aMutex);
324     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
325     checkBordersAndEnsureFetched(column);
326 
327     if (checkNull(column))
328         return 0.0;
329 
330     OString sVal = m_aRows[m_nRowPosition][column - 1];
331     return sVal.toDouble();
332 }
333 
getFloat(sal_Int32 column)334 float SAL_CALL OResultSet::getFloat(sal_Int32 column)
335 {
336     MutexGuard aGuard(m_aMutex);
337     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
338     checkBordersAndEnsureFetched(column);
339     if (checkNull(column))
340         return 0.0f;
341 
342     OString sVal = m_aRows[m_nRowPosition][column - 1];
343     return sVal.toFloat();
344 }
345 
getInt(sal_Int32 column)346 sal_Int32 SAL_CALL OResultSet::getInt(sal_Int32 column)
347 {
348     MutexGuard aGuard(m_aMutex);
349     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
350     checkBordersAndEnsureFetched(column);
351     if (checkNull(column))
352         return 0;
353 
354     OString sVal = m_aRows[m_nRowPosition][column - 1];
355     return sVal.toInt32();
356 }
357 
getRow()358 sal_Int32 SAL_CALL OResultSet::getRow()
359 {
360     MutexGuard aGuard(m_aMutex);
361     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
362 
363     return m_nRowPosition + 1; // indexed from 1
364 }
365 
getLong(sal_Int32 column)366 sal_Int64 SAL_CALL OResultSet::getLong(sal_Int32 column)
367 {
368     MutexGuard aGuard(m_aMutex);
369     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
370     checkBordersAndEnsureFetched(column);
371     if (checkNull(column))
372         return 0LL;
373 
374     OString sVal = m_aRows[m_nRowPosition][column - 1];
375     return sVal.toInt64();
376 }
377 
getMetaData()378 uno::Reference<XResultSetMetaData> SAL_CALL OResultSet::getMetaData()
379 {
380     MutexGuard aGuard(m_aMutex);
381     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
382     return m_xMetaData;
383 }
384 
getArray(sal_Int32 column)385 uno::Reference<XArray> SAL_CALL OResultSet::getArray(sal_Int32 column)
386 {
387     MutexGuard aGuard(m_aMutex);
388     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
389     checkBordersAndEnsureFetched(column);
390 
391     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getArray", *this);
392     return nullptr;
393 }
394 
getClob(sal_Int32 column)395 uno::Reference<XClob> SAL_CALL OResultSet::getClob(sal_Int32 column)
396 {
397     MutexGuard aGuard(m_aMutex);
398     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
399     checkBordersAndEnsureFetched(column);
400 
401     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getClob", *this);
402     return nullptr;
403 }
404 
getBlob(sal_Int32 column)405 uno::Reference<XBlob> SAL_CALL OResultSet::getBlob(sal_Int32 column)
406 {
407     MutexGuard aGuard(m_aMutex);
408     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
409     checkBordersAndEnsureFetched(column);
410 
411     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getBlob", *this);
412     return nullptr;
413 }
414 
getRef(sal_Int32 column)415 uno::Reference<XRef> SAL_CALL OResultSet::getRef(sal_Int32 column)
416 {
417     MutexGuard aGuard(m_aMutex);
418     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
419     checkBordersAndEnsureFetched(column);
420 
421     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getRef", *this);
422     return nullptr;
423 }
424 
getObject(sal_Int32 column,const uno::Reference<XNameAccess> &)425 Any SAL_CALL OResultSet::getObject(sal_Int32 column,
426                                    const uno::Reference<XNameAccess>& /* typeMap */)
427 {
428     MutexGuard aGuard(m_aMutex);
429     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
430     checkBordersAndEnsureFetched(column);
431 
432     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getObject", *this);
433     return Any();
434 }
435 
getShort(sal_Int32 column)436 sal_Int16 SAL_CALL OResultSet::getShort(sal_Int32 column)
437 {
438     MutexGuard aGuard(m_aMutex);
439     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
440     checkBordersAndEnsureFetched(column);
441     if (checkNull(column))
442         return 0;
443 
444     OString sVal = m_aRows[m_nRowPosition][column - 1];
445     return sVal.toInt32();
446 }
447 
getString(sal_Int32 column)448 OUString SAL_CALL OResultSet::getString(sal_Int32 column)
449 {
450     MutexGuard aGuard(m_aMutex);
451     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
452     checkBordersAndEnsureFetched(column);
453     if (checkNull(column))
454         return rtl::OUString{};
455 
456     OString sVal = m_aRows[m_nRowPosition][column - 1];
457     return OStringToOUString(sVal, m_encoding);
458 }
459 
getTime(sal_Int32 column)460 Time SAL_CALL OResultSet::getTime(sal_Int32 column)
461 {
462     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
463     MutexGuard aGuard(m_aMutex);
464     checkBordersAndEnsureFetched(column);
465 
466     Time t;
467     if (checkNull(column))
468         return t;
469 
470     OString sVal = m_aRows[m_nRowPosition][column - 1];
471     OString timeString{ sVal.getStr(), getDataLength(column) };
472     OString token;
473     sal_Int32 nIndex, i = 0;
474 
475     nIndex = timeString.indexOf(' ') + 1;
476     do
477     {
478         token = timeString.getToken(0, ':', nIndex);
479         switch (i)
480         {
481             case 0:
482                 t.Hours = static_cast<sal_uInt16>(token.toUInt32());
483                 break;
484             case 1:
485                 t.Minutes = static_cast<sal_uInt16>(token.toUInt32());
486                 break;
487             case 2:
488                 t.Seconds = static_cast<sal_uInt16>(token.toUInt32());
489                 break;
490         }
491         i++;
492     } while (nIndex >= 0);
493 
494     return t;
495 }
496 
getTimestamp(sal_Int32 column)497 DateTime SAL_CALL OResultSet::getTimestamp(sal_Int32 column)
498 {
499     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
500     MutexGuard aGuard(m_aMutex);
501     checkBordersAndEnsureFetched(column);
502 
503     if (checkNull(column))
504         return DateTime{};
505 
506     OString sVal = m_aRows[m_nRowPosition][column - 1];
507 
508     // YY-MM-DD HH:MM:SS
509     std::vector<OString> dateAndTime
510         = lcl_split(OString{ sVal.getStr(), getDataLength(column) }, ' ');
511 
512     auto dateParts = lcl_split(dateAndTime.at(0), '-');
513     auto timeParts = lcl_split(dateAndTime.at(1), ':');
514 
515     if (dateParts.size() < 2 || timeParts.size() < 2)
516         throw SQLException("Timestamp has a wrong format", *this, OUString(), 1, Any());
517 
518     DateTime dt;
519 
520     dt.Year = dateParts.at(0).toUInt32();
521     dt.Month = dateParts.at(1).toUInt32();
522     dt.Day = dateParts.at(2).toUInt32();
523     dt.Hours = timeParts.at(0).toUInt32();
524     dt.Minutes = timeParts.at(1).toUInt32();
525     dt.Seconds = timeParts.at(2).toUInt32();
526     return dt;
527 }
528 
isBeforeFirst()529 sal_Bool SAL_CALL OResultSet::isBeforeFirst()
530 {
531     MutexGuard aGuard(m_aMutex);
532     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
533 
534     return m_nRowPosition < 0;
535 }
536 
isAfterLast()537 sal_Bool SAL_CALL OResultSet::isAfterLast()
538 {
539     MutexGuard aGuard(m_aMutex);
540     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
541     ensureFieldInfoFetched();
542 
543     return m_nRowPosition >= m_nRowCount;
544 }
545 
isFirst()546 sal_Bool SAL_CALL OResultSet::isFirst()
547 {
548     MutexGuard aGuard(m_aMutex);
549     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
550     ensureFieldInfoFetched();
551 
552     return m_nRowPosition == 0 && !isAfterLast();
553 }
554 
isLast()555 sal_Bool SAL_CALL OResultSet::isLast()
556 {
557     MutexGuard aGuard(m_aMutex);
558     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
559     ensureFieldInfoFetched();
560 
561     return m_nRowPosition == m_nRowCount - 1;
562 }
563 
beforeFirst()564 void SAL_CALL OResultSet::beforeFirst()
565 {
566     MutexGuard aGuard(m_aMutex);
567     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
568     m_nRowPosition = -1;
569 }
570 
afterLast()571 void SAL_CALL OResultSet::afterLast()
572 {
573     MutexGuard aGuard(m_aMutex);
574     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
575     ensureFieldInfoFetched();
576     m_nRowPosition = m_nRowCount;
577 }
578 
close()579 void SAL_CALL OResultSet::close()
580 {
581     MutexGuard aGuard(m_aMutex);
582     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
583 
584     if (m_pResult != nullptr)
585     {
586         mysql_free_result(m_pResult);
587         m_pResult = nullptr;
588     }
589     dispose();
590 }
591 
first()592 sal_Bool SAL_CALL OResultSet::first()
593 {
594     MutexGuard aGuard(m_aMutex);
595     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
596     m_nRowPosition = 0;
597 
598     return true;
599 }
600 
last()601 sal_Bool SAL_CALL OResultSet::last()
602 {
603     MutexGuard aGuard(m_aMutex);
604     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
605     ensureFieldInfoFetched();
606     m_nRowPosition = m_nRowCount - 1;
607 
608     return true;
609 }
610 
absolute(sal_Int32 row)611 sal_Bool SAL_CALL OResultSet::absolute(sal_Int32 row)
612 {
613     MutexGuard aGuard(m_aMutex);
614     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
615     ensureFieldInfoFetched();
616 
617     sal_Int32 nToGo = row < 0 ? (m_nRowCount - 1) - row : row - 1;
618 
619     if (nToGo >= m_nRowCount)
620         nToGo = m_nRowCount - 1;
621     if (nToGo < 0)
622         nToGo = 0;
623 
624     m_nRowPosition = nToGo;
625 
626     return true;
627 }
628 
relative(sal_Int32 row)629 sal_Bool SAL_CALL OResultSet::relative(sal_Int32 row)
630 {
631     MutexGuard aGuard(m_aMutex);
632     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
633     ensureFieldInfoFetched();
634 
635     if (row == 0)
636         return true;
637 
638     sal_Int32 nToGo = m_nRowPosition + row;
639     if (nToGo >= m_nRowCount)
640         nToGo = m_nRowCount - 1;
641     if (nToGo < 0)
642         nToGo = 0;
643 
644     m_nRowPosition = nToGo;
645 
646     return true;
647 }
648 
previous()649 sal_Bool SAL_CALL OResultSet::previous()
650 {
651     MutexGuard aGuard(m_aMutex);
652     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
653 
654     if (m_nRowPosition == 0)
655     {
656         m_nRowPosition--;
657         return false;
658     }
659     else if (m_nRowPosition < 0)
660     {
661         return false;
662     }
663 
664     m_nRowPosition--;
665     return true;
666 }
667 
getStatement()668 uno::Reference<uno::XInterface> SAL_CALL OResultSet::getStatement()
669 {
670     MutexGuard aGuard(m_aMutex);
671     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
672 
673     return m_aStatement.get();
674 }
675 
rowDeleted()676 sal_Bool SAL_CALL OResultSet::rowDeleted()
677 {
678     MutexGuard aGuard(m_aMutex);
679     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
680 
681     return false;
682 }
683 
rowInserted()684 sal_Bool SAL_CALL OResultSet::rowInserted()
685 {
686     MutexGuard aGuard(m_aMutex);
687     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
688 
689     return false;
690 }
691 
rowUpdated()692 sal_Bool SAL_CALL OResultSet::rowUpdated()
693 {
694     MutexGuard aGuard(m_aMutex);
695     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
696 
697     return false;
698 }
699 
next()700 sal_Bool SAL_CALL OResultSet::next()
701 {
702     MutexGuard aGuard(m_aMutex);
703     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
704     ensureFieldInfoFetched();
705     if (m_nRowPosition + 1 > m_nRowCount) // afterlast
706         return false;
707     if (m_nRowPosition + 1 == m_nRowCount) // last
708     {
709         // return false but take it to afterlast anyway
710         ++m_nRowPosition;
711         return false;
712     }
713     ++m_nRowPosition;
714     return true;
715 }
716 
wasNull()717 sal_Bool SAL_CALL OResultSet::wasNull()
718 {
719     MutexGuard aGuard(m_aMutex);
720     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
721 
722     return m_bWasNull;
723 }
724 
cancel()725 void SAL_CALL OResultSet::cancel()
726 {
727     MutexGuard aGuard(m_aMutex);
728     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
729 }
730 
clearWarnings()731 void SAL_CALL OResultSet::clearWarnings() {}
732 
getWarnings()733 Any SAL_CALL OResultSet::getWarnings() { return Any(); }
734 
insertRow()735 void SAL_CALL OResultSet::insertRow()
736 {
737     MutexGuard aGuard(m_aMutex);
738     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
739     // you only have to implement this if you want to insert new rows
740     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::insertRow", *this);
741 }
742 
updateRow()743 void SAL_CALL OResultSet::updateRow()
744 {
745     MutexGuard aGuard(m_aMutex);
746     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
747 
748     // only when you allow updates
749     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateRow", *this);
750 }
751 
deleteRow()752 void SAL_CALL OResultSet::deleteRow()
753 {
754     MutexGuard aGuard(m_aMutex);
755     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
756     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::deleteRow", *this);
757 }
758 
cancelRowUpdates()759 void SAL_CALL OResultSet::cancelRowUpdates()
760 {
761     MutexGuard aGuard(m_aMutex);
762     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
763     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::cancelRowUpdates", *this);
764 }
765 
moveToInsertRow()766 void SAL_CALL OResultSet::moveToInsertRow()
767 {
768     MutexGuard aGuard(m_aMutex);
769     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
770 
771     // only when you allow insert's
772     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::moveToInsertRow", *this);
773 }
774 
moveToCurrentRow()775 void SAL_CALL OResultSet::moveToCurrentRow()
776 {
777     MutexGuard aGuard(m_aMutex);
778     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
779 }
780 
updateNull(sal_Int32 column)781 void SAL_CALL OResultSet::updateNull(sal_Int32 column)
782 {
783     MutexGuard aGuard(m_aMutex);
784     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
785     checkColumnIndex(column);
786     checkRowIndex();
787     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateNull", *this);
788 }
789 
updateBoolean(sal_Int32 column,sal_Bool)790 void SAL_CALL OResultSet::updateBoolean(sal_Int32 column, sal_Bool /* x */)
791 {
792     MutexGuard aGuard(m_aMutex);
793     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
794     checkColumnIndex(column);
795     checkRowIndex();
796     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateBoolean", *this);
797 }
798 
updateByte(sal_Int32 column,sal_Int8)799 void SAL_CALL OResultSet::updateByte(sal_Int32 column, sal_Int8 /* x */)
800 {
801     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
802     MutexGuard aGuard(m_aMutex);
803     checkColumnIndex(column);
804     checkRowIndex();
805     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateByte", *this);
806 }
807 
updateShort(sal_Int32 column,sal_Int16)808 void SAL_CALL OResultSet::updateShort(sal_Int32 column, sal_Int16 /* x */)
809 {
810     MutexGuard aGuard(m_aMutex);
811     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
812     checkColumnIndex(column);
813     checkRowIndex();
814     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateShort", *this);
815 }
816 
updateInt(sal_Int32 column,sal_Int32)817 void SAL_CALL OResultSet::updateInt(sal_Int32 column, sal_Int32 /* x */)
818 {
819     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
820     MutexGuard aGuard(m_aMutex);
821     checkColumnIndex(column);
822     checkRowIndex();
823     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateInt", *this);
824 }
825 
updateLong(sal_Int32 column,sal_Int64)826 void SAL_CALL OResultSet::updateLong(sal_Int32 column, sal_Int64 /* x */)
827 {
828     MutexGuard aGuard(m_aMutex);
829     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
830     checkColumnIndex(column);
831     checkRowIndex();
832     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateLong", *this);
833 }
834 
updateFloat(sal_Int32 column,float)835 void SAL_CALL OResultSet::updateFloat(sal_Int32 column, float /* x */)
836 {
837     MutexGuard aGuard(m_aMutex);
838     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
839     checkColumnIndex(column);
840     checkRowIndex();
841     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateFloat", *this);
842 }
843 
updateDouble(sal_Int32 column,double)844 void SAL_CALL OResultSet::updateDouble(sal_Int32 column, double /* x */)
845 {
846     MutexGuard aGuard(m_aMutex);
847     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
848     checkColumnIndex(column);
849     checkRowIndex();
850     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateDouble", *this);
851 }
852 
updateString(sal_Int32 column,const OUString &)853 void SAL_CALL OResultSet::updateString(sal_Int32 column, const OUString& /* x */)
854 {
855     MutexGuard aGuard(m_aMutex);
856     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
857     checkColumnIndex(column);
858     checkRowIndex();
859     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateString", *this);
860 }
861 
updateBytes(sal_Int32 column,const uno::Sequence<sal_Int8> &)862 void SAL_CALL OResultSet::updateBytes(sal_Int32 column, const uno::Sequence<sal_Int8>& /* x */)
863 {
864     MutexGuard aGuard(m_aMutex);
865     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
866     checkColumnIndex(column);
867     checkRowIndex();
868     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateBytes", *this);
869 }
870 
updateDate(sal_Int32 column,const Date &)871 void SAL_CALL OResultSet::updateDate(sal_Int32 column, const Date& /* x */)
872 {
873     MutexGuard aGuard(m_aMutex);
874     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
875     checkColumnIndex(column);
876     checkRowIndex();
877     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateDate", *this);
878 }
879 
updateTime(sal_Int32 column,const Time &)880 void SAL_CALL OResultSet::updateTime(sal_Int32 column, const Time& /* x */)
881 {
882     MutexGuard aGuard(m_aMutex);
883     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
884     checkColumnIndex(column);
885     checkRowIndex();
886     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateTime", *this);
887 }
888 
updateTimestamp(sal_Int32 column,const DateTime &)889 void SAL_CALL OResultSet::updateTimestamp(sal_Int32 column, const DateTime& /* x */)
890 {
891     MutexGuard aGuard(m_aMutex);
892     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
893     checkColumnIndex(column);
894     checkRowIndex();
895     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateTimestamp", *this);
896 }
897 
updateBinaryStream(sal_Int32 column,const uno::Reference<XInputStream> &,sal_Int32)898 void SAL_CALL OResultSet::updateBinaryStream(sal_Int32 column,
899                                              const uno::Reference<XInputStream>& /* x */,
900                                              sal_Int32 /* length */)
901 {
902     MutexGuard aGuard(m_aMutex);
903     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
904     checkColumnIndex(column);
905     checkRowIndex();
906     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateBinaryStream",
907                                                             *this);
908 }
909 
updateCharacterStream(sal_Int32 column,const uno::Reference<XInputStream> &,sal_Int32)910 void SAL_CALL OResultSet::updateCharacterStream(sal_Int32 column,
911                                                 const uno::Reference<XInputStream>& /* x */,
912                                                 sal_Int32 /* length */)
913 {
914     MutexGuard aGuard(m_aMutex);
915     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
916     checkColumnIndex(column);
917     checkRowIndex();
918     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateCharacterStream",
919                                                             *this);
920 }
921 
refreshRow()922 void SAL_CALL OResultSet::refreshRow()
923 {
924     MutexGuard aGuard(m_aMutex);
925     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
926     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::refreshRow", *this);
927 }
928 
updateObject(sal_Int32 column,const Any &)929 void SAL_CALL OResultSet::updateObject(sal_Int32 column, const Any& /* x */)
930 {
931     MutexGuard aGuard(m_aMutex);
932     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
933     checkColumnIndex(column);
934     checkRowIndex();
935     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateObject", *this);
936 }
937 
updateNumericObject(sal_Int32 column,const Any &,sal_Int32)938 void SAL_CALL OResultSet::updateNumericObject(sal_Int32 column, const Any& /* x */,
939                                               sal_Int32 /* scale */)
940 {
941     MutexGuard aGuard(m_aMutex);
942     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
943     checkColumnIndex(column);
944     checkRowIndex();
945     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::updateNumericObject",
946                                                             *this);
947 }
948 
949 // XRowLocate
getBookmark()950 Any SAL_CALL OResultSet::getBookmark()
951 {
952     MutexGuard aGuard(m_aMutex);
953     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
954 
955     // if you don't want to support bookmark you must remove the XRowLocate interface
956     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getBookmark", *this);
957 
958     return Any();
959 }
960 
moveToBookmark(const Any &)961 sal_Bool SAL_CALL OResultSet::moveToBookmark(const Any& /* bookmark */)
962 {
963     MutexGuard aGuard(m_aMutex);
964     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
965 
966     return false;
967 }
968 
moveRelativeToBookmark(const Any &,sal_Int32)969 sal_Bool SAL_CALL OResultSet::moveRelativeToBookmark(const Any& /* bookmark */,
970                                                      sal_Int32 /* rows */)
971 {
972     MutexGuard aGuard(m_aMutex);
973     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
974 
975     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::moveRelativeToBookmark",
976                                                             *this);
977     return false;
978 }
979 
compareBookmarks(const Any &,const Any &)980 sal_Int32 SAL_CALL OResultSet::compareBookmarks(const Any& /* n1 */, const Any& /* n2 */)
981 {
982     MutexGuard aGuard(m_aMutex);
983     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
984 
985     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::compareBookmarks", *this);
986 
987     return CompareBookmark::NOT_EQUAL;
988 }
989 
hasOrderedBookmarks()990 sal_Bool SAL_CALL OResultSet::hasOrderedBookmarks() { return false; }
991 
hashBookmark(const Any &)992 sal_Int32 SAL_CALL OResultSet::hashBookmark(const Any& /* bookmark */)
993 {
994     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::hashBookmark", *this);
995     return 0;
996 }
997 
998 // XDeleteRows
deleteRows(const uno::Sequence<Any> &)999 uno::Sequence<sal_Int32> SAL_CALL OResultSet::deleteRows(const uno::Sequence<Any>& /* rows */)
1000 {
1001     MutexGuard aGuard(m_aMutex);
1002     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
1003 
1004     mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::deleteRows", *this);
1005     return uno::Sequence<sal_Int32>();
1006 }
1007 
createArrayHelper() const1008 IPropertyArrayHelper* OResultSet::createArrayHelper() const
1009 {
1010     uno::Sequence<Property> aProps(5);
1011     Property* pProperties = aProps.getArray();
1012     sal_Int32 nPos = 0;
1013     pProperties[nPos++] = Property("FetchDirection", PROPERTY_ID_FETCHDIRECTION,
1014                                    cppu::UnoType<sal_Int32>::get(), 0);
1015     pProperties[nPos++]
1016         = Property("FetchSize", PROPERTY_ID_FETCHSIZE, cppu::UnoType<sal_Int32>::get(), 0);
1017     pProperties[nPos++] = Property("IsBookmarkable", PROPERTY_ID_ISBOOKMARKABLE,
1018                                    cppu::UnoType<bool>::get(), PropertyAttribute::READONLY);
1019     pProperties[nPos++] = Property("ResultSetConcurrency", PROPERTY_ID_RESULTSETCONCURRENCY,
1020                                    cppu::UnoType<sal_Int32>::get(), PropertyAttribute::READONLY);
1021     pProperties[nPos++] = Property("ResultSetType", PROPERTY_ID_RESULTSETTYPE,
1022                                    cppu::UnoType<sal_Int32>::get(), PropertyAttribute::READONLY);
1023 
1024     return new OPropertyArrayHelper(aProps);
1025 }
1026 
getInfoHelper()1027 IPropertyArrayHelper& OResultSet::getInfoHelper() { return *getArrayHelper(); }
1028 
convertFastPropertyValue(Any &,Any &,sal_Int32 nHandle,const Any &)1029 sal_Bool OResultSet::convertFastPropertyValue(Any& /* rConvertedValue */, Any& /* rOldValue */,
1030                                               sal_Int32 nHandle, const Any& /* rValue */)
1031 {
1032     switch (nHandle)
1033     {
1034         case PROPERTY_ID_ISBOOKMARKABLE:
1035         case PROPERTY_ID_CURSORNAME:
1036         case PROPERTY_ID_RESULTSETCONCURRENCY:
1037         case PROPERTY_ID_RESULTSETTYPE:
1038             throw css::lang::IllegalArgumentException();
1039         case PROPERTY_ID_FETCHDIRECTION:
1040         case PROPERTY_ID_FETCHSIZE:
1041         default:;
1042     }
1043     return false;
1044 }
1045 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any &)1046 void OResultSet::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& /* rValue */)
1047 {
1048     switch (nHandle)
1049     {
1050         case PROPERTY_ID_ISBOOKMARKABLE:
1051         case PROPERTY_ID_CURSORNAME:
1052         case PROPERTY_ID_RESULTSETCONCURRENCY:
1053         case PROPERTY_ID_RESULTSETTYPE:
1054             throw uno::Exception("cannot set prop " + OUString::number(nHandle), nullptr);
1055         case PROPERTY_ID_FETCHDIRECTION:
1056             break;
1057         case PROPERTY_ID_FETCHSIZE:
1058             break;
1059         default:;
1060     }
1061 }
1062 
getFastPropertyValue(Any & _rValue,sal_Int32 nHandle) const1063 void OResultSet::getFastPropertyValue(Any& _rValue, sal_Int32 nHandle) const
1064 {
1065     switch (nHandle)
1066     {
1067         case PROPERTY_ID_ISBOOKMARKABLE:
1068             _rValue <<= false;
1069             break;
1070         case PROPERTY_ID_CURSORNAME:
1071             break;
1072         case PROPERTY_ID_RESULTSETCONCURRENCY:
1073             _rValue <<= ResultSetConcurrency::READ_ONLY;
1074             break;
1075         case PROPERTY_ID_RESULTSETTYPE:
1076             _rValue <<= ResultSetType::SCROLL_INSENSITIVE;
1077             break;
1078         case PROPERTY_ID_FETCHDIRECTION:
1079             _rValue <<= FetchDirection::FORWARD;
1080             break;
1081         case PROPERTY_ID_FETCHSIZE:
1082             _rValue <<= sal_Int32(50);
1083             break;
1084             ;
1085         default:;
1086     }
1087 }
1088 
acquire()1089 void SAL_CALL OResultSet::acquire() noexcept { OResultSet_BASE::acquire(); }
1090 
release()1091 void SAL_CALL OResultSet::release() noexcept { OResultSet_BASE::release(); }
1092 
getPropertySetInfo()1093 css::uno::Reference<css::beans::XPropertySetInfo> SAL_CALL OResultSet::getPropertySetInfo()
1094 {
1095     return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
1096 }
1097 
checkColumnIndex(sal_Int32 index)1098 void OResultSet::checkColumnIndex(sal_Int32 index)
1099 {
1100     if (index < 1 || index > static_cast<int>(m_aFields.size()))
1101     {
1102         /* static object for efficiency or thread safety is a problem ? */
1103         throw SQLException("index out of range", *this, OUString(), 1, Any());
1104     }
1105 }
1106 
checkBordersAndEnsureFetched(sal_Int32 index)1107 void OResultSet::checkBordersAndEnsureFetched(sal_Int32 index)
1108 {
1109     ensureResultFetched();
1110     checkColumnIndex(index);
1111     checkRowIndex();
1112 }
1113 
1114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1115