tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / core / data / dbdocutl.cxx
blobdf373d991d17e79f684835c844e8036f9eb42905
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 <com/sun/star/sdbc/DataType.hpp>
21 #include <com/sun/star/sdbc/XRow.hpp>
23 #include <svl/numformat.hxx>
24 #include <svl/zforlist.hxx>
26 #include <dbdocutl.hxx>
27 #include <document.hxx>
28 #include <formula/errorcodes.hxx>
29 #include <stringutil.hxx>
31 using namespace ::com::sun::star;
33 ScDatabaseDocUtil::StrData::StrData() :
34 mbSimpleText(true), mnStrLength(0)
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)
42 OUString aString;
43 double nVal = 0.0;
44 bool bValue = false;
45 bool bEmptyFlag = false;
46 bool bError = false;
47 sal_uInt32 nFormatIndex = 0;
49 // wasNull calls only if null value was found?
51 try
53 switch ( nType )
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;
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;
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;
87 case sdbc::DataType::DATE:
89 util::Date aDate = xRow->getDate(nRowPos);
90 bEmptyFlag = xRow->wasNull();
91 if (bEmptyFlag)
92 nVal = 0.0;
93 else
95 SvNumberFormatter* pFormTable = rDoc.GetFormatTable();
96 nFormatIndex = pFormTable->GetStandardFormat(
97 SvNumFormatType::DATE, ScGlobal::eLnge );
98 nVal = Date( aDate ) - pFormTable->GetNullDate();
100 bValue = true;
102 break;
104 case sdbc::DataType::TIME:
106 SvNumberFormatter* pFormTable = rDoc.GetFormatTable();
107 nFormatIndex = pFormTable->GetStandardFormat(
108 SvNumFormatType::TIME, ScGlobal::eLnge );
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;
118 break;
120 case sdbc::DataType::TIMESTAMP:
122 SvNumberFormatter* pFormTable = rDoc.GetFormatTable();
123 nFormatIndex = pFormTable->GetStandardFormat(
124 SvNumFormatType::DATETIME, ScGlobal::eLnge );
126 util::DateTime aStamp = xRow->getTimestamp(nRowPos);
127 if (aStamp.Year != 0)
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;
139 break;
141 case sdbc::DataType::SQLNULL:
142 bEmptyFlag = true;
143 break;
145 case sdbc::DataType::BINARY:
146 case sdbc::DataType::VARBINARY:
147 case sdbc::DataType::LONGVARBINARY:
148 default:
149 bError = true; // unknown type
152 catch ( uno::Exception& )
154 bError = true;
157 if ( bValue && bCurrency )
158 nFormatIndex = rDoc.GetFormatTable()->GetStandardFormat(
159 SvNumFormatType::CURRENCY, ScGlobal::eLnge );
161 ScAddress aPos(nCol, nRow, nTab);
162 if (bEmptyFlag)
163 rDoc.SetEmptyCell(aPos);
164 else if (bError)
166 rDoc.SetError( nCol, nRow, nTab, FormulaError::NotAvailable );
168 else if (bValue)
170 rDoc.SetValue(aPos, nVal);
171 if (nFormatIndex)
172 rDoc.SetNumberFormat(aPos, nFormatIndex);
174 else
176 if (!aString.isEmpty())
178 if (ScStringUtil::isMultiline(aString))
180 rDoc.SetEditText(aPos, aString);
181 if (pStrData)
182 pStrData->mbSimpleText = false;
184 else
186 ScSetStringParam aParam;
187 aParam.setTextInput();
188 rDoc.SetString(aPos, aString, &aParam);
189 if (pStrData)
190 pStrData->mbSimpleText = true;
193 if (pStrData)
194 pStrData->mnStrLength = aString.getLength();
196 else
197 rDoc.SetEmptyCell(aPos);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */