Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / data / dbdocutl.cxx
blob2db7b5ff361b1eeb9a76981e26db8be249105aaf
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/zforlist.hxx>
25 #include "dbdocutl.hxx"
26 #include "document.hxx"
27 #include "formula/errorcodes.hxx"
28 #include "stringutil.hxx"
29 #include "globalnames.hxx"
31 using namespace ::com::sun::star;
33 // ----------------------------------------------------------------------------
35 ScDatabaseDocUtil::StrData::StrData() :
36 mbSimpleText(true), mnStrLength(0)
40 // ----------------------------------------------------------------------------
42 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 )
46 OUString aString;
47 double nVal = 0.0;
48 sal_Bool bValue = false;
49 sal_Bool bEmptyFlag = false;
50 sal_Bool bError = false;
51 sal_uLong nFormatIndex = 0;
53 //! wasNull calls only if null value was found?
55 try
57 switch ( nType )
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 NUMBERFORMAT_LOGICAL, ScGlobal::eLnge );
64 nVal = (xRow->getBoolean(nRowPos) ? 1 : 0);
65 bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
66 bValue = sal_True;
67 break;
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 nVal = xRow->getDouble(nRowPos);
80 bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
81 bValue = sal_True;
82 break;
84 case sdbc::DataType::CHAR:
85 case sdbc::DataType::VARCHAR:
86 case sdbc::DataType::LONGVARCHAR:
87 aString = xRow->getString(nRowPos);
88 bEmptyFlag = ( aString.isEmpty() ) && xRow->wasNull();
89 break;
91 case sdbc::DataType::DATE:
93 SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
94 nFormatIndex = pFormTable->GetStandardFormat(
95 NUMBERFORMAT_DATE, ScGlobal::eLnge );
97 util::Date aDate = xRow->getDate(nRowPos);
98 nVal = Date( aDate.Day, aDate.Month, aDate.Year ) -
99 *pFormTable->GetNullDate();
100 bEmptyFlag = xRow->wasNull();
101 bValue = sal_True;
103 break;
105 case sdbc::DataType::TIME:
107 SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
108 nFormatIndex = pFormTable->GetStandardFormat(
109 NUMBERFORMAT_TIME, ScGlobal::eLnge );
111 util::Time aTime = xRow->getTime(nRowPos);
112 nVal = aTime.Hours / static_cast<double>(::Time::hourPerDay) +
113 aTime.Minutes / static_cast<double>(::Time::minutePerDay) +
114 aTime.Seconds / static_cast<double>(::Time::secondPerDay) +
115 aTime.NanoSeconds / static_cast<double>(::Time::nanoSecPerDay);
116 bEmptyFlag = xRow->wasNull();
117 bValue = sal_True;
119 break;
121 case sdbc::DataType::TIMESTAMP:
123 SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
124 nFormatIndex = pFormTable->GetStandardFormat(
125 NUMBERFORMAT_DATETIME, ScGlobal::eLnge );
127 util::DateTime aStamp = xRow->getTimestamp(nRowPos);
128 nVal = ( Date( aStamp.Day, aStamp.Month, aStamp.Year ) -
129 *pFormTable->GetNullDate() ) +
130 aStamp.Hours / static_cast<double>(::Time::hourPerDay) +
131 aStamp.Minutes / static_cast<double>(::Time::minutePerDay) +
132 aStamp.Seconds / static_cast<double>(::Time::secondPerDay) +
133 aStamp.NanoSeconds / static_cast<double>(::Time::nanoSecPerDay);
134 bEmptyFlag = xRow->wasNull();
135 bValue = sal_True;
137 break;
139 case sdbc::DataType::SQLNULL:
140 bEmptyFlag = sal_True;
141 break;
143 case sdbc::DataType::BINARY:
144 case sdbc::DataType::VARBINARY:
145 case sdbc::DataType::LONGVARBINARY:
146 default:
147 bError = sal_True; // unknown type
150 catch ( uno::Exception& )
152 bError = sal_True;
155 if ( bValue && bCurrency )
156 nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
157 NUMBERFORMAT_CURRENCY, ScGlobal::eLnge );
159 ScAddress aPos(nCol, nRow, nTab);
160 if (bEmptyFlag)
161 pDoc->SetEmptyCell(aPos);
162 else if (bError)
164 pDoc->SetError( nCol, nRow, nTab, NOTAVAILABLE );
166 else if (bValue)
168 pDoc->SetValue(aPos, nVal);
169 if (nFormatIndex)
170 pDoc->SetNumberFormat(aPos, nFormatIndex);
172 else
174 if (!aString.isEmpty())
176 if (ScStringUtil::isMultiline(aString))
178 pDoc->SetEditText(aPos, aString);
179 if (pStrData)
180 pStrData->mbSimpleText = false;
182 else
184 ScSetStringParam aParam;
185 aParam.setTextInput();
186 pDoc->SetString(aPos, aString, &aParam);
187 if (pStrData)
188 pStrData->mbSimpleText = true;
191 if (pStrData)
192 pStrData->mnStrLength = aString.getLength();
194 else
195 pDoc->SetEmptyCell(aPos);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */