1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
45 bool bEmptyFlag
= false;
47 sal_uInt32 nFormatIndex
= 0;
49 // wasNull calls only if null value was found?
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();
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();
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();
87 case sdbc::DataType::DATE
:
89 util::Date aDate
= xRow
->getDate(nRowPos
);
90 bEmptyFlag
= xRow
->wasNull();
95 SvNumberFormatter
* pFormTable
= rDoc
.GetFormatTable();
96 nFormatIndex
= pFormTable
->GetStandardFormat(
97 SvNumFormatType::DATE
, ScGlobal::eLnge
);
98 nVal
= Date( aDate
) - pFormTable
->GetNullDate();
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();
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();
141 case sdbc::DataType::SQLNULL
:
145 case sdbc::DataType::BINARY
:
146 case sdbc::DataType::VARBINARY
:
147 case sdbc::DataType::LONGVARBINARY
:
149 bError
= true; // unknown type
152 catch ( uno::Exception
& )
157 if ( bValue
&& bCurrency
)
158 nFormatIndex
= rDoc
.GetFormatTable()->GetStandardFormat(
159 SvNumFormatType::CURRENCY
, ScGlobal::eLnge
);
161 ScAddress
aPos(nCol
, nRow
, nTab
);
163 rDoc
.SetEmptyCell(aPos
);
166 rDoc
.SetError( nCol
, nRow
, nTab
, FormulaError::NotAvailable
);
170 rDoc
.SetValue(aPos
, nVal
);
172 rDoc
.SetNumberFormat(aPos
, nFormatIndex
);
176 if (!aString
.isEmpty())
178 if (ScStringUtil::isMultiline(aString
))
180 rDoc
.SetEditText(aPos
, aString
);
182 pStrData
->mbSimpleText
= false;
186 ScSetStringParam aParam
;
187 aParam
.setTextInput();
188 rDoc
.SetString(aPos
, aString
, &aParam
);
190 pStrData
->mbSimpleText
= true;
194 pStrData
->mnStrLength
= aString
.getLength();
197 rDoc
.SetEmptyCell(aPos
);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */