Line data Source code
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/zforlist.hxx>
24 :
25 : #include "dbdocutl.hxx"
26 : #include "document.hxx"
27 : #include "formula/errorcodes.hxx"
28 : #include "stringutil.hxx"
29 : #include "globalnames.hxx"
30 :
31 : using namespace ::com::sun::star;
32 :
33 : // ----------------------------------------------------------------------------
34 :
35 0 : ScDatabaseDocUtil::StrData::StrData() :
36 0 : mbSimpleText(true), mnStrLength(0)
37 : {
38 0 : }
39 :
40 : // ----------------------------------------------------------------------------
41 :
42 680 : void ScDatabaseDocUtil::PutData( ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab,
43 : const uno::Reference<sdbc::XRow>& xRow, long nRowPos,
44 : long nType, sal_Bool bCurrency, StrData* pStrData )
45 : {
46 680 : String aString;
47 680 : double nVal = 0.0;
48 680 : sal_Bool bValue = false;
49 680 : sal_Bool bEmptyFlag = false;
50 680 : sal_Bool bError = false;
51 680 : sal_uLong nFormatIndex = 0;
52 :
53 : //! wasNull calls only if null value was found?
54 :
55 : try
56 : {
57 680 : switch ( nType )
58 : {
59 : case sdbc::DataType::BIT:
60 : case sdbc::DataType::BOOLEAN:
61 : //! use language from doc (here, date/time and currency)?
62 : nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
63 0 : NUMBERFORMAT_LOGICAL, ScGlobal::eLnge );
64 0 : nVal = (xRow->getBoolean(nRowPos) ? 1 : 0);
65 0 : bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
66 0 : bValue = sal_True;
67 0 : break;
68 :
69 : case sdbc::DataType::TINYINT:
70 : case sdbc::DataType::SMALLINT:
71 : case sdbc::DataType::INTEGER:
72 : case sdbc::DataType::BIGINT:
73 : case sdbc::DataType::FLOAT:
74 : case sdbc::DataType::REAL:
75 : case sdbc::DataType::DOUBLE:
76 : case sdbc::DataType::NUMERIC:
77 : case sdbc::DataType::DECIMAL:
78 : //! do the conversion here?
79 0 : nVal = xRow->getDouble(nRowPos);
80 0 : bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
81 0 : bValue = sal_True;
82 0 : break;
83 :
84 : case sdbc::DataType::CHAR:
85 : case sdbc::DataType::VARCHAR:
86 : case sdbc::DataType::LONGVARCHAR:
87 680 : aString = xRow->getString(nRowPos);
88 680 : bEmptyFlag = ( aString.Len() == 0 ) && xRow->wasNull();
89 680 : break;
90 :
91 : case sdbc::DataType::DATE:
92 : {
93 0 : SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
94 : nFormatIndex = pFormTable->GetStandardFormat(
95 0 : NUMBERFORMAT_DATE, ScGlobal::eLnge );
96 :
97 0 : util::Date aDate = xRow->getDate(nRowPos);
98 0 : nVal = Date( aDate.Day, aDate.Month, aDate.Year ) -
99 0 : *pFormTable->GetNullDate();
100 0 : bEmptyFlag = xRow->wasNull();
101 0 : bValue = sal_True;
102 : }
103 0 : break;
104 :
105 : case sdbc::DataType::TIME:
106 : {
107 0 : SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
108 : nFormatIndex = pFormTable->GetStandardFormat(
109 0 : NUMBERFORMAT_TIME, ScGlobal::eLnge );
110 :
111 0 : util::Time aTime = xRow->getTime(nRowPos);
112 0 : nVal = aTime.Hours / static_cast<double>(::Time::hourPerDay) +
113 0 : aTime.Minutes / static_cast<double>(::Time::minutePerDay) +
114 0 : aTime.Seconds / static_cast<double>(::Time::secondPerDay) +
115 0 : aTime.NanoSeconds / static_cast<double>(::Time::nanoSecPerDay);
116 0 : bEmptyFlag = xRow->wasNull();
117 0 : bValue = sal_True;
118 : }
119 0 : break;
120 :
121 : case sdbc::DataType::TIMESTAMP:
122 : {
123 0 : SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
124 : nFormatIndex = pFormTable->GetStandardFormat(
125 0 : NUMBERFORMAT_DATETIME, ScGlobal::eLnge );
126 :
127 0 : util::DateTime aStamp = xRow->getTimestamp(nRowPos);
128 0 : nVal = ( Date( aStamp.Day, aStamp.Month, aStamp.Year ) -
129 0 : *pFormTable->GetNullDate() ) +
130 0 : aStamp.Hours / static_cast<double>(::Time::hourPerDay) +
131 0 : aStamp.Minutes / static_cast<double>(::Time::minutePerDay) +
132 0 : aStamp.Seconds / static_cast<double>(::Time::secondPerDay) +
133 0 : aStamp.NanoSeconds / static_cast<double>(::Time::nanoSecPerDay);
134 0 : bEmptyFlag = xRow->wasNull();
135 0 : bValue = sal_True;
136 : }
137 0 : break;
138 :
139 : case sdbc::DataType::SQLNULL:
140 0 : bEmptyFlag = sal_True;
141 0 : break;
142 :
143 : case sdbc::DataType::BINARY:
144 : case sdbc::DataType::VARBINARY:
145 : case sdbc::DataType::LONGVARBINARY:
146 : default:
147 0 : bError = sal_True; // unknown type
148 : }
149 : }
150 0 : catch ( uno::Exception& )
151 : {
152 0 : bError = sal_True;
153 : }
154 :
155 680 : if ( bValue && bCurrency )
156 : nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
157 0 : NUMBERFORMAT_CURRENCY, ScGlobal::eLnge );
158 :
159 680 : ScAddress aPos(nCol, nRow, nTab);
160 680 : if (bEmptyFlag)
161 437 : pDoc->SetEmptyCell(aPos);
162 243 : else if (bError)
163 : {
164 0 : pDoc->SetError( nCol, nRow, nTab, NOTAVAILABLE );
165 : }
166 243 : else if (bValue)
167 : {
168 0 : pDoc->SetValue(aPos, nVal);
169 0 : if (nFormatIndex)
170 0 : pDoc->SetNumberFormat(aPos, nFormatIndex);
171 : }
172 : else
173 : {
174 243 : if (aString.Len())
175 : {
176 230 : if (ScStringUtil::isMultiline(aString))
177 : {
178 0 : pDoc->SetEditText(aPos, aString);
179 0 : if (pStrData)
180 0 : pStrData->mbSimpleText = false;
181 : }
182 : else
183 : {
184 230 : ScSetStringParam aParam;
185 230 : aParam.setTextInput();
186 230 : pDoc->SetString(aPos, aString, &aParam);
187 230 : if (pStrData)
188 0 : pStrData->mbSimpleText = true;
189 : }
190 :
191 230 : if (pStrData)
192 0 : pStrData->mnStrLength = aString.Len();
193 : }
194 : else
195 13 : pDoc->SetEmptyCell(aPos);
196 680 : }
197 680 : }
198 :
199 :
200 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|