xref: /trunk/main/sc/source/core/data/dbdocutl.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <svl/zforlist.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "dbdocutl.hxx"
37cdf0e10cSrcweir #include "document.hxx"
38cdf0e10cSrcweir #include "cell.hxx"
39cdf0e10cSrcweir #include "formula/errorcodes.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace ::com::sun::star;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #define D_TIMEFACTOR              86400.0
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // -----------------------------------------------------------------------
46cdf0e10cSrcweir 
47cdf0e10cSrcweir // static
PutData(ScDocument * pDoc,SCCOL nCol,SCROW nRow,SCTAB nTab,const uno::Reference<sdbc::XRow> & xRow,long nRowPos,long nType,sal_Bool bCurrency,sal_Bool * pSimpleFlag)48cdf0e10cSrcweir void ScDatabaseDocUtil::PutData( ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab,
49cdf0e10cSrcweir                                 const uno::Reference<sdbc::XRow>& xRow, long nRowPos,
50cdf0e10cSrcweir                                 long nType, sal_Bool bCurrency, sal_Bool* pSimpleFlag )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     String aString;
53cdf0e10cSrcweir     double nVal = 0.0;
54cdf0e10cSrcweir     sal_Bool bValue = sal_False;
55cdf0e10cSrcweir     sal_Bool bEmptyFlag = sal_False;
56cdf0e10cSrcweir     sal_Bool bError = sal_False;
57cdf0e10cSrcweir     sal_uLong nFormatIndex = 0;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     //! wasNull calls only if null value was found?
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     try
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         switch ( nType )
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             case sdbc::DataType::BIT:
66cdf0e10cSrcweir             case sdbc::DataType::BOOLEAN:
67cdf0e10cSrcweir                 //! use language from doc (here, date/time and currency)?
68cdf0e10cSrcweir                 nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
69cdf0e10cSrcweir                                     NUMBERFORMAT_LOGICAL, ScGlobal::eLnge );
70cdf0e10cSrcweir                 nVal = (xRow->getBoolean(nRowPos) ? 1 : 0);
71cdf0e10cSrcweir                 bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
72cdf0e10cSrcweir                 bValue = sal_True;
73cdf0e10cSrcweir                 break;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             case sdbc::DataType::TINYINT:
76cdf0e10cSrcweir             case sdbc::DataType::SMALLINT:
77cdf0e10cSrcweir             case sdbc::DataType::INTEGER:
78cdf0e10cSrcweir             case sdbc::DataType::BIGINT:
79cdf0e10cSrcweir             case sdbc::DataType::FLOAT:
80cdf0e10cSrcweir             case sdbc::DataType::REAL:
81cdf0e10cSrcweir             case sdbc::DataType::DOUBLE:
82cdf0e10cSrcweir             case sdbc::DataType::NUMERIC:
83cdf0e10cSrcweir             case sdbc::DataType::DECIMAL:
84cdf0e10cSrcweir                 //! do the conversion here?
85cdf0e10cSrcweir                 nVal = xRow->getDouble(nRowPos);
86cdf0e10cSrcweir                 bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
87cdf0e10cSrcweir                 bValue = sal_True;
88cdf0e10cSrcweir                 break;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             case sdbc::DataType::CHAR:
91cdf0e10cSrcweir             case sdbc::DataType::VARCHAR:
92cdf0e10cSrcweir             case sdbc::DataType::LONGVARCHAR:
93cdf0e10cSrcweir                 aString = xRow->getString(nRowPos);
94cdf0e10cSrcweir                 bEmptyFlag = ( aString.Len() == 0 ) && xRow->wasNull();
95cdf0e10cSrcweir                 break;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             case sdbc::DataType::DATE:
98cdf0e10cSrcweir                 {
99cdf0e10cSrcweir                     SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
100cdf0e10cSrcweir                     nFormatIndex = pFormTable->GetStandardFormat(
101cdf0e10cSrcweir                                         NUMBERFORMAT_DATE, ScGlobal::eLnge );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir                     util::Date aDate = xRow->getDate(nRowPos);
104cdf0e10cSrcweir                     nVal = Date( aDate.Day, aDate.Month, aDate.Year ) -
105cdf0e10cSrcweir                                                 *pFormTable->GetNullDate();
106cdf0e10cSrcweir                     bEmptyFlag = xRow->wasNull();
107cdf0e10cSrcweir                     bValue = sal_True;
108cdf0e10cSrcweir                 }
109cdf0e10cSrcweir                 break;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             case sdbc::DataType::TIME:
112cdf0e10cSrcweir                 {
113cdf0e10cSrcweir                     SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
114cdf0e10cSrcweir                     nFormatIndex = pFormTable->GetStandardFormat(
115cdf0e10cSrcweir                                         NUMBERFORMAT_TIME, ScGlobal::eLnge );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir                     util::Time aTime = xRow->getTime(nRowPos);
118cdf0e10cSrcweir                     nVal = ( aTime.Hours * 3600 + aTime.Minutes * 60 +
119cdf0e10cSrcweir                              aTime.Seconds + aTime.HundredthSeconds / 100.0 ) / D_TIMEFACTOR;
120cdf0e10cSrcweir                     bEmptyFlag = xRow->wasNull();
121cdf0e10cSrcweir                     bValue = sal_True;
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir                 break;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             case sdbc::DataType::TIMESTAMP:
126cdf0e10cSrcweir                 {
127cdf0e10cSrcweir                     SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
128cdf0e10cSrcweir                     nFormatIndex = pFormTable->GetStandardFormat(
129cdf0e10cSrcweir                                         NUMBERFORMAT_DATETIME, ScGlobal::eLnge );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir                     util::DateTime aStamp = xRow->getTimestamp(nRowPos);
132cdf0e10cSrcweir                     nVal = ( Date( aStamp.Day, aStamp.Month, aStamp.Year ) -
133cdf0e10cSrcweir                                                 *pFormTable->GetNullDate() ) +
134cdf0e10cSrcweir                            ( aStamp.Hours * 3600 + aStamp.Minutes * 60 +
135cdf0e10cSrcweir                              aStamp.Seconds + aStamp.HundredthSeconds / 100.0 ) / D_TIMEFACTOR;
136cdf0e10cSrcweir                     bEmptyFlag = xRow->wasNull();
137cdf0e10cSrcweir                     bValue = sal_True;
138cdf0e10cSrcweir                 }
139cdf0e10cSrcweir                 break;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             case sdbc::DataType::SQLNULL:
142cdf0e10cSrcweir                 bEmptyFlag = sal_True;
143cdf0e10cSrcweir                 break;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             case sdbc::DataType::BINARY:
146cdf0e10cSrcweir             case sdbc::DataType::VARBINARY:
147cdf0e10cSrcweir             case sdbc::DataType::LONGVARBINARY:
148cdf0e10cSrcweir             default:
149cdf0e10cSrcweir                 bError = sal_True;      // unknown type
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir     catch ( uno::Exception& )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         bError = sal_True;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     if ( bValue && bCurrency )
158cdf0e10cSrcweir         nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
159cdf0e10cSrcweir                             NUMBERFORMAT_CURRENCY, ScGlobal::eLnge );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     ScBaseCell* pCell;
162cdf0e10cSrcweir     if (bEmptyFlag)
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         pCell = NULL;
165cdf0e10cSrcweir         pDoc->PutCell( nCol, nRow, nTab, pCell );
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     else if (bError)
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         pDoc->SetError( nCol, nRow, nTab, NOTAVAILABLE );
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir     else if (bValue)
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         pCell = new ScValueCell( nVal );
174cdf0e10cSrcweir         if (nFormatIndex == 0)
175cdf0e10cSrcweir             pDoc->PutCell( nCol, nRow, nTab, pCell );
176cdf0e10cSrcweir         else
177cdf0e10cSrcweir             pDoc->PutCell( nCol, nRow, nTab, pCell, nFormatIndex );
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir     else
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         if (aString.Len())
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             pCell = ScBaseCell::CreateTextCell( aString, pDoc );
184cdf0e10cSrcweir             if ( pSimpleFlag && pCell->GetCellType() == CELLTYPE_EDIT )
185cdf0e10cSrcweir                 *pSimpleFlag = sal_False;
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir         else
188cdf0e10cSrcweir             pCell = NULL;
189cdf0e10cSrcweir         pDoc->PutCell( nCol, nRow, nTab, pCell );
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir }
192