LibreOffice Module sc (master)  1
dbdocutl.cxx
Go to the documentation of this file.
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 <com/sun/star/sdbc/DataType.hpp>
21 #include <com/sun/star/sdbc/XRow.hpp>
22 
23 #include <svl/numformat.hxx>
24 #include <svl/zforlist.hxx>
25 
26 #include <dbdocutl.hxx>
27 #include <document.hxx>
28 #include <formula/errorcodes.hxx>
29 #include <stringutil.hxx>
30 
31 using namespace ::com::sun::star;
32 
34  mbSimpleText(true), mnStrLength(0)
35 {
36 }
37 
38 void ScDatabaseDocUtil::PutData(ScDocument& rDoc, SCCOL nCol, SCROW nRow, SCTAB nTab,
39  const uno::Reference<sdbc::XRow>& xRow, sal_Int32 nRowPos,
40  tools::Long nType, bool bCurrency, StrData* pStrData)
41 {
42  OUString aString;
43  double nVal = 0.0;
44  bool bValue = false;
45  bool bEmptyFlag = false;
46  bool bError = false;
47  sal_uLong nFormatIndex = 0;
48 
49  // wasNull calls only if null value was found?
50 
51  try
52  {
53  switch ( nType )
54  {
55  case sdbc::DataType::BIT:
56  case sdbc::DataType::BOOLEAN:
57  //TODO: use language from doc (here, date/time and currency)?
58  nFormatIndex = rDoc.GetFormatTable()->GetStandardFormat(
59  SvNumFormatType::LOGICAL, ScGlobal::eLnge );
60  nVal = (xRow->getBoolean(nRowPos) ? 1 : 0);
61  bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
62  bValue = true;
63  break;
64 
65  case sdbc::DataType::TINYINT:
66  case sdbc::DataType::SMALLINT:
67  case sdbc::DataType::INTEGER:
68  case sdbc::DataType::BIGINT:
69  case sdbc::DataType::FLOAT:
70  case sdbc::DataType::REAL:
71  case sdbc::DataType::DOUBLE:
72  case sdbc::DataType::NUMERIC:
73  case sdbc::DataType::DECIMAL:
74  //TODO: do the conversion here?
75  nVal = xRow->getDouble(nRowPos);
76  bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
77  bValue = true;
78  break;
79 
80  case sdbc::DataType::CHAR:
81  case sdbc::DataType::VARCHAR:
82  case sdbc::DataType::LONGVARCHAR:
83  aString = xRow->getString(nRowPos);
84  bEmptyFlag = ( aString.isEmpty() ) && xRow->wasNull();
85  break;
86 
88  {
89  util::Date aDate = xRow->getDate(nRowPos);
90  bEmptyFlag = xRow->wasNull();
91  if (bEmptyFlag)
92  nVal = 0.0;
93  else
94  {
95  SvNumberFormatter* pFormTable = rDoc.GetFormatTable();
96  nFormatIndex = pFormTable->GetStandardFormat(
97  SvNumFormatType::DATE, ScGlobal::eLnge );
98  nVal = Date( aDate ) - pFormTable->GetNullDate();
99  }
100  bValue = true;
101  }
102  break;
103 
104  case sdbc::DataType::TIME:
105  {
106  SvNumberFormatter* pFormTable = rDoc.GetFormatTable();
107  nFormatIndex = pFormTable->GetStandardFormat(
108  SvNumFormatType::TIME, ScGlobal::eLnge );
109 
110  util::Time aTime = xRow->getTime(nRowPos);
111  nVal = aTime.Hours / static_cast<double>(::tools::Time::hourPerDay) +
112  aTime.Minutes / static_cast<double>(::tools::Time::minutePerDay) +
113  aTime.Seconds / static_cast<double>(::tools::Time::secondPerDay) +
114  aTime.NanoSeconds / static_cast<double>(::tools::Time::nanoSecPerDay);
115  bEmptyFlag = xRow->wasNull();
116  bValue = true;
117  }
118  break;
119 
120  case sdbc::DataType::TIMESTAMP:
121  {
122  SvNumberFormatter* pFormTable = rDoc.GetFormatTable();
123  nFormatIndex = pFormTable->GetStandardFormat(
124  SvNumFormatType::DATETIME, ScGlobal::eLnge );
125 
126  util::DateTime aStamp = xRow->getTimestamp(nRowPos);
127  if (aStamp.Year != 0)
128  {
129  nVal = ( Date( aStamp.Day, aStamp.Month, aStamp.Year ) -
130  pFormTable->GetNullDate() ) +
131  aStamp.Hours / static_cast<double>(::tools::Time::hourPerDay) +
132  aStamp.Minutes / static_cast<double>(::tools::Time::minutePerDay) +
133  aStamp.Seconds / static_cast<double>(::tools::Time::secondPerDay) +
134  aStamp.NanoSeconds / static_cast<double>(::tools::Time::nanoSecPerDay);
135  bEmptyFlag = xRow->wasNull();
136  bValue = true;
137  }
138  }
139  break;
140 
141  case sdbc::DataType::SQLNULL:
142  bEmptyFlag = true;
143  break;
144 
145  case sdbc::DataType::BINARY:
146  case sdbc::DataType::VARBINARY:
147  case sdbc::DataType::LONGVARBINARY:
148  default:
149  bError = true; // unknown type
150  }
151  }
152  catch ( uno::Exception& )
153  {
154  bError = true;
155  }
156 
157  if ( bValue && bCurrency )
158  nFormatIndex = rDoc.GetFormatTable()->GetStandardFormat(
159  SvNumFormatType::CURRENCY, ScGlobal::eLnge );
160 
161  ScAddress aPos(nCol, nRow, nTab);
162  if (bEmptyFlag)
163  rDoc.SetEmptyCell(aPos);
164  else if (bError)
165  {
166  rDoc.SetError( nCol, nRow, nTab, FormulaError::NotAvailable );
167  }
168  else if (bValue)
169  {
170  rDoc.SetValue(aPos, nVal);
171  if (nFormatIndex)
172  rDoc.SetNumberFormat(aPos, nFormatIndex);
173  }
174  else
175  {
176  if (!aString.isEmpty())
177  {
178  if (ScStringUtil::isMultiline(aString))
179  {
180  rDoc.SetEditText(aPos, aString);
181  if (pStrData)
182  pStrData->mbSimpleText = false;
183  }
184  else
185  {
186  ScSetStringParam aParam;
187  aParam.setTextInput();
188  rDoc.SetString(aPos, aString, &aParam);
189  if (pStrData)
190  pStrData->mbSimpleText = true;
191  }
192 
193  if (pStrData)
194  pStrData->mnStrLength = aString.getLength();
195  }
196  else
197  rDoc.SetEmptyCell(aPos);
198  }
199 }
200 
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const sal_Int64 hourPerDay
Store parameters used in the ScDocument::SetString() method.
Definition: stringutil.hxx:34
void setTextInput()
Call this whenever you need to unconditionally set input as text, no matter what the input is...
Definition: stringutil.cxx:38
sal_uIntPtr sal_uLong
long Long
static const sal_Int64 minutePerDay
SC_DLLPUBLIC void SetValue(SCCOL nCol, SCROW nRow, SCTAB nTab, const double &rVal)
Definition: document.cxx:3489
static bool SC_DLLPUBLIC isMultiline(const OUString &rStr)
Definition: stringutil.cxx:424
void SetError(SCCOL nCol, SCROW nRow, SCTAB nTab, const FormulaError nError)
Definition: documen2.cxx:1061
SC_DLLPUBLIC bool SetEditText(const ScAddress &rPos, std::unique_ptr< EditTextObject > pEditText)
This method manages the lifecycle of the passed edit text object.
Definition: document.cxx:3425
SC_DLLPUBLIC bool SetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString &rString, const ScSetStringParam *pParam=nullptr)
Definition: document.cxx:3394
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:441
sal_Int16 SCCOL
Definition: types.hxx:21
static SC_DLLPUBLIC LanguageType eLnge
Definition: global.hxx:538
void SetNumberFormat(const ScAddress &rPos, sal_uInt32 nNumberFormat)
Definition: document.cxx:3704
sal_Int32 SCROW
Definition: types.hxx:17
sal_uInt32 GetStandardFormat(SvNumFormatType eType, LanguageType eLnge=LANGUAGE_DONTKNOW)
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
void SetEmptyCell(const ScAddress &rPos)
Definition: document.cxx:3481
static const sal_Int64 nanoSecPerDay
static void PutData(ScDocument &rDoc, SCCOL nCol, SCROW nRow, SCTAB nTab, const css::uno::Reference< css::sdbc::XRow > &xRow, sal_Int32 nRowPos, tools::Long nType, bool bCurrency, StrData *pStrData=nullptr)
Definition: dbdocutl.cxx:38
const Date & GetNullDate() const
sal_Int16 SCTAB
Definition: types.hxx:22
Detailed information on single string value.
Definition: dbdocutl.hxx:36
static const sal_Int64 secondPerDay