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