xref: /aoo41x/main/sc/source/core/data/dbdocutl.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 
32 
33 // INCLUDE ---------------------------------------------------------------
34 
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/XRow.hpp>
37 
38 #include <svl/zforlist.hxx>
39 
40 #include "dbdocutl.hxx"
41 #include "document.hxx"
42 #include "cell.hxx"
43 #include "formula/errorcodes.hxx"
44 
45 using namespace ::com::sun::star;
46 
47 #define D_TIMEFACTOR              86400.0
48 
49 // -----------------------------------------------------------------------
50 
51 // static
52 void ScDatabaseDocUtil::PutData( ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab,
53 								const uno::Reference<sdbc::XRow>& xRow, long nRowPos,
54 								long nType, sal_Bool bCurrency, sal_Bool* pSimpleFlag )
55 {
56 	String aString;
57 	double nVal = 0.0;
58 	sal_Bool bValue = sal_False;
59 	sal_Bool bEmptyFlag = sal_False;
60 	sal_Bool bError = sal_False;
61 	sal_uLong nFormatIndex = 0;
62 
63 	//!	wasNull calls only if null value was found?
64 
65 	try
66 	{
67 		switch ( nType )
68 		{
69 			case sdbc::DataType::BIT:
70 			case sdbc::DataType::BOOLEAN:
71 				//!	use language from doc (here, date/time and currency)?
72 				nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
73 									NUMBERFORMAT_LOGICAL, ScGlobal::eLnge );
74 				nVal = (xRow->getBoolean(nRowPos) ? 1 : 0);
75 				bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
76 				bValue = sal_True;
77 				break;
78 
79 			case sdbc::DataType::TINYINT:
80 			case sdbc::DataType::SMALLINT:
81 			case sdbc::DataType::INTEGER:
82 			case sdbc::DataType::BIGINT:
83 			case sdbc::DataType::FLOAT:
84 			case sdbc::DataType::REAL:
85 			case sdbc::DataType::DOUBLE:
86 			case sdbc::DataType::NUMERIC:
87 			case sdbc::DataType::DECIMAL:
88 				//!	do the conversion here?
89 				nVal = xRow->getDouble(nRowPos);
90 				bEmptyFlag = ( nVal == 0.0 ) && xRow->wasNull();
91 				bValue = sal_True;
92 				break;
93 
94 			case sdbc::DataType::CHAR:
95 			case sdbc::DataType::VARCHAR:
96 			case sdbc::DataType::LONGVARCHAR:
97 				aString = xRow->getString(nRowPos);
98 				bEmptyFlag = ( aString.Len() == 0 ) && xRow->wasNull();
99 				break;
100 
101 			case sdbc::DataType::DATE:
102 				{
103 					SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
104 					nFormatIndex = pFormTable->GetStandardFormat(
105 										NUMBERFORMAT_DATE, ScGlobal::eLnge );
106 
107 					util::Date aDate = xRow->getDate(nRowPos);
108 					nVal = Date( aDate.Day, aDate.Month, aDate.Year ) -
109 												*pFormTable->GetNullDate();
110 					bEmptyFlag = xRow->wasNull();
111 					bValue = sal_True;
112 				}
113 				break;
114 
115 			case sdbc::DataType::TIME:
116 				{
117 					SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
118 					nFormatIndex = pFormTable->GetStandardFormat(
119 										NUMBERFORMAT_TIME, ScGlobal::eLnge );
120 
121 					util::Time aTime = xRow->getTime(nRowPos);
122 					nVal = ( aTime.Hours * 3600 + aTime.Minutes * 60 +
123 							 aTime.Seconds + aTime.HundredthSeconds / 100.0 ) / D_TIMEFACTOR;
124 					bEmptyFlag = xRow->wasNull();
125 					bValue = sal_True;
126 				}
127 				break;
128 
129 			case sdbc::DataType::TIMESTAMP:
130 				{
131 					SvNumberFormatter* pFormTable = pDoc->GetFormatTable();
132 					nFormatIndex = pFormTable->GetStandardFormat(
133 										NUMBERFORMAT_DATETIME, ScGlobal::eLnge );
134 
135 					util::DateTime aStamp = xRow->getTimestamp(nRowPos);
136 					nVal = ( Date( aStamp.Day, aStamp.Month, aStamp.Year ) -
137 												*pFormTable->GetNullDate() ) +
138 						   ( aStamp.Hours * 3600 + aStamp.Minutes * 60 +
139 							 aStamp.Seconds + aStamp.HundredthSeconds / 100.0 ) / D_TIMEFACTOR;
140 					bEmptyFlag = xRow->wasNull();
141 					bValue = sal_True;
142 				}
143 				break;
144 
145 			case sdbc::DataType::SQLNULL:
146 				bEmptyFlag = sal_True;
147 				break;
148 
149 			case sdbc::DataType::BINARY:
150 			case sdbc::DataType::VARBINARY:
151 			case sdbc::DataType::LONGVARBINARY:
152 			default:
153 				bError = sal_True;		// unknown type
154 		}
155 	}
156 	catch ( uno::Exception& )
157 	{
158 		bError = sal_True;
159 	}
160 
161 	if ( bValue && bCurrency )
162 		nFormatIndex = pDoc->GetFormatTable()->GetStandardFormat(
163 							NUMBERFORMAT_CURRENCY, ScGlobal::eLnge );
164 
165 	ScBaseCell* pCell;
166 	if (bEmptyFlag)
167 	{
168 		pCell = NULL;
169 		pDoc->PutCell( nCol, nRow, nTab, pCell );
170 	}
171 	else if (bError)
172 	{
173 		pDoc->SetError( nCol, nRow, nTab, NOTAVAILABLE );
174 	}
175 	else if (bValue)
176 	{
177 		pCell = new ScValueCell( nVal );
178 		if (nFormatIndex == 0)
179 			pDoc->PutCell( nCol, nRow, nTab, pCell );
180 		else
181 			pDoc->PutCell( nCol, nRow, nTab, pCell, nFormatIndex );
182 	}
183 	else
184 	{
185 		if (aString.Len())
186 		{
187 			pCell = ScBaseCell::CreateTextCell( aString, pDoc );
188 			if ( pSimpleFlag && pCell->GetCellType() == CELLTYPE_EDIT )
189 				*pSimpleFlag = sal_False;
190 		}
191 		else
192 			pCell = NULL;
193 		pDoc->PutCell( nCol, nRow, nTab, pCell );
194 	}
195 }
196 
197 
198