1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dbdocutl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 // INCLUDE ---------------------------------------------------------------
38 #include <com/sun/star/sdbc/DataType.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <svtools/zforlist.hxx>
43 #include "dbdocutl.hxx"
44 #include "document.hxx"
46 #include "formula/errorcodes.hxx"
48 using namespace ::com::sun::star
;
50 #define D_TIMEFACTOR 86400.0
52 // -----------------------------------------------------------------------
55 void ScDatabaseDocUtil::PutData( ScDocument
* pDoc
, SCCOL nCol
, SCROW nRow
, SCTAB nTab
,
56 const uno::Reference
<sdbc::XRow
>& xRow
, long nRowPos
,
57 long nType
, BOOL bCurrency
, BOOL
* pSimpleFlag
)
62 BOOL bEmptyFlag
= FALSE
;
64 ULONG nFormatIndex
= 0;
66 //! wasNull calls only if null value was found?
72 case sdbc::DataType::BIT
:
73 case sdbc::DataType::BOOLEAN
:
74 //! use language from doc (here, date/time and currency)?
75 nFormatIndex
= pDoc
->GetFormatTable()->GetStandardFormat(
76 NUMBERFORMAT_LOGICAL
, ScGlobal::eLnge
);
77 nVal
= (xRow
->getBoolean(nRowPos
) ? 1 : 0);
78 bEmptyFlag
= ( nVal
== 0.0 ) && xRow
->wasNull();
82 case sdbc::DataType::TINYINT
:
83 case sdbc::DataType::SMALLINT
:
84 case sdbc::DataType::INTEGER
:
85 case sdbc::DataType::BIGINT
:
86 case sdbc::DataType::FLOAT
:
87 case sdbc::DataType::REAL
:
88 case sdbc::DataType::DOUBLE
:
89 case sdbc::DataType::NUMERIC
:
90 case sdbc::DataType::DECIMAL
:
91 //! do the conversion here?
92 nVal
= xRow
->getDouble(nRowPos
);
93 bEmptyFlag
= ( nVal
== 0.0 ) && xRow
->wasNull();
97 case sdbc::DataType::CHAR
:
98 case sdbc::DataType::VARCHAR
:
99 case sdbc::DataType::LONGVARCHAR
:
100 aString
= xRow
->getString(nRowPos
);
101 bEmptyFlag
= ( aString
.Len() == 0 ) && xRow
->wasNull();
104 case sdbc::DataType::DATE
:
106 SvNumberFormatter
* pFormTable
= pDoc
->GetFormatTable();
107 nFormatIndex
= pFormTable
->GetStandardFormat(
108 NUMBERFORMAT_DATE
, ScGlobal::eLnge
);
110 util::Date aDate
= xRow
->getDate(nRowPos
);
111 nVal
= Date( aDate
.Day
, aDate
.Month
, aDate
.Year
) -
112 *pFormTable
->GetNullDate();
113 bEmptyFlag
= xRow
->wasNull();
118 case sdbc::DataType::TIME
:
120 SvNumberFormatter
* pFormTable
= pDoc
->GetFormatTable();
121 nFormatIndex
= pFormTable
->GetStandardFormat(
122 NUMBERFORMAT_TIME
, ScGlobal::eLnge
);
124 util::Time aTime
= xRow
->getTime(nRowPos
);
125 nVal
= ( aTime
.Hours
* 3600 + aTime
.Minutes
* 60 +
126 aTime
.Seconds
+ aTime
.HundredthSeconds
/ 100.0 ) / D_TIMEFACTOR
;
127 bEmptyFlag
= xRow
->wasNull();
132 case sdbc::DataType::TIMESTAMP
:
134 SvNumberFormatter
* pFormTable
= pDoc
->GetFormatTable();
135 nFormatIndex
= pFormTable
->GetStandardFormat(
136 NUMBERFORMAT_DATETIME
, ScGlobal::eLnge
);
138 util::DateTime aStamp
= xRow
->getTimestamp(nRowPos
);
139 nVal
= ( Date( aStamp
.Day
, aStamp
.Month
, aStamp
.Year
) -
140 *pFormTable
->GetNullDate() ) +
141 ( aStamp
.Hours
* 3600 + aStamp
.Minutes
* 60 +
142 aStamp
.Seconds
+ aStamp
.HundredthSeconds
/ 100.0 ) / D_TIMEFACTOR
;
143 bEmptyFlag
= xRow
->wasNull();
148 case sdbc::DataType::SQLNULL
:
152 case sdbc::DataType::BINARY
:
153 case sdbc::DataType::VARBINARY
:
154 case sdbc::DataType::LONGVARBINARY
:
156 bError
= TRUE
; // unknown type
159 catch ( uno::Exception
& )
164 if ( bValue
&& bCurrency
)
165 nFormatIndex
= pDoc
->GetFormatTable()->GetStandardFormat(
166 NUMBERFORMAT_CURRENCY
, ScGlobal::eLnge
);
172 pDoc
->PutCell( nCol
, nRow
, nTab
, pCell
);
176 pDoc
->SetError( nCol
, nRow
, nTab
, NOTAVAILABLE
);
180 pCell
= new ScValueCell( nVal
);
181 if (nFormatIndex
== 0)
182 pDoc
->PutCell( nCol
, nRow
, nTab
, pCell
);
184 pDoc
->PutCell( nCol
, nRow
, nTab
, pCell
, nFormatIndex
);
190 pCell
= ScBaseCell::CreateTextCell( aString
, pDoc
);
191 if ( pSimpleFlag
&& pCell
->GetCellType() == CELLTYPE_EDIT
)
192 *pSimpleFlag
= FALSE
;
196 pDoc
->PutCell( nCol
, nRow
, nTab
, pCell
);