1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
30*cdf0e10cSrcweir #ifndef DBAUI_TABLEROW_HXX
31*cdf0e10cSrcweir #include "TableRow.hxx"
32*cdf0e10cSrcweir #endif
33*cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
37*cdf0e10cSrcweir #include "FieldDescriptions.hxx"
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #include <algorithm>
40*cdf0e10cSrcweir #include <comphelper/types.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace dbaui;
43*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
44*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //========================================================================
48*cdf0e10cSrcweir // class OTableRow
49*cdf0e10cSrcweir //========================================================================
50*cdf0e10cSrcweir DBG_NAME(OTableRow)
51*cdf0e10cSrcweir //------------------------------------------------------------------------------
52*cdf0e10cSrcweir OTableRow::OTableRow()
53*cdf0e10cSrcweir 	:m_pActFieldDescr( NULL )
54*cdf0e10cSrcweir 	,m_nPos( -1 )
55*cdf0e10cSrcweir 	,m_bReadOnly( false )
56*cdf0e10cSrcweir 	,m_bOwnsDescriptions(false)
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 	DBG_CTOR(OTableRow,NULL);
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir //------------------------------------------------------------------------------
61*cdf0e10cSrcweir OTableRow::OTableRow(const Reference< XPropertySet >& xAffectedCol)
62*cdf0e10cSrcweir 	:m_pActFieldDescr( NULL )
63*cdf0e10cSrcweir 	,m_nPos( -1 )
64*cdf0e10cSrcweir 	,m_bReadOnly( false )
65*cdf0e10cSrcweir 	,m_bOwnsDescriptions(true)
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	DBG_CTOR(OTableRow,NULL);
68*cdf0e10cSrcweir 	m_pActFieldDescr = new OFieldDescription(xAffectedCol);
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir //------------------------------------------------------------------------------
71*cdf0e10cSrcweir OTableRow::OTableRow( const OTableRow& rRow, long nPosition )
72*cdf0e10cSrcweir     :m_pActFieldDescr(NULL)
73*cdf0e10cSrcweir 	,m_nPos( nPosition )
74*cdf0e10cSrcweir 	,m_bReadOnly(rRow.IsReadOnly())
75*cdf0e10cSrcweir 	,m_bOwnsDescriptions(false)
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir 	DBG_CTOR(OTableRow,NULL);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	OFieldDescription* pSrcField = rRow.GetActFieldDescr();
80*cdf0e10cSrcweir 	if(pSrcField)
81*cdf0e10cSrcweir 	{
82*cdf0e10cSrcweir 		m_pActFieldDescr = new OFieldDescription(*pSrcField);
83*cdf0e10cSrcweir 		m_bOwnsDescriptions = true;
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir //------------------------------------------------------------------------------
88*cdf0e10cSrcweir OTableRow::~OTableRow()
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	DBG_DTOR(OTableRow,NULL);
91*cdf0e10cSrcweir 	if(m_bOwnsDescriptions)
92*cdf0e10cSrcweir 		delete m_pActFieldDescr;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir //------------------------------------------------------------------------------
96*cdf0e10cSrcweir void OTableRow::SetPrimaryKey( sal_Bool bSet )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	DBG_CHKTHIS(OTableRow,NULL);
99*cdf0e10cSrcweir 	if(m_pActFieldDescr)
100*cdf0e10cSrcweir 		m_pActFieldDescr->SetPrimaryKey(bSet);
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir // -----------------------------------------------------------------------------
103*cdf0e10cSrcweir sal_Bool OTableRow::IsPrimaryKey() const
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir 	DBG_CHKTHIS(OTableRow,NULL);
106*cdf0e10cSrcweir 	return m_pActFieldDescr && m_pActFieldDescr->IsPrimaryKey();
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir // -----------------------------------------------------------------------------
109*cdf0e10cSrcweir void OTableRow::SetFieldType( const TOTypeInfoSP& _pType, sal_Bool _bForce )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	DBG_CHKTHIS(OTableRow,NULL);
112*cdf0e10cSrcweir 	if ( _pType.get() )
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		if( !m_pActFieldDescr )
115*cdf0e10cSrcweir 		{
116*cdf0e10cSrcweir 			m_pActFieldDescr = new OFieldDescription();
117*cdf0e10cSrcweir 			m_bOwnsDescriptions = true;
118*cdf0e10cSrcweir 		}
119*cdf0e10cSrcweir 		m_pActFieldDescr->FillFromTypeInfo(_pType,_bForce,sal_True);
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 	else
122*cdf0e10cSrcweir 	{
123*cdf0e10cSrcweir 		delete m_pActFieldDescr;
124*cdf0e10cSrcweir 		m_pActFieldDescr = NULL;
125*cdf0e10cSrcweir 	}
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir // -----------------------------------------------------------------------------
128*cdf0e10cSrcweir namespace dbaui
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
131*cdf0e10cSrcweir 	SvStream& operator<<( SvStream& _rStr, const OTableRow& _rRow )
132*cdf0e10cSrcweir 	{
133*cdf0e10cSrcweir 		_rStr << _rRow.m_nPos;
134*cdf0e10cSrcweir 		OFieldDescription* pFieldDesc = _rRow.GetActFieldDescr();
135*cdf0e10cSrcweir 		if(pFieldDesc)
136*cdf0e10cSrcweir 		{
137*cdf0e10cSrcweir 			_rStr << (sal_Int32)1;
138*cdf0e10cSrcweir 			_rStr.WriteByteString(pFieldDesc->GetName());
139*cdf0e10cSrcweir 			_rStr.WriteByteString(pFieldDesc->GetDescription());
140*cdf0e10cSrcweir             _rStr.WriteByteString(pFieldDesc->GetHelpText());
141*cdf0e10cSrcweir 			double nValue = 0.0;
142*cdf0e10cSrcweir 			Any aValue = pFieldDesc->GetControlDefault();
143*cdf0e10cSrcweir 			if ( aValue >>= nValue )
144*cdf0e10cSrcweir 			{
145*cdf0e10cSrcweir 				_rStr << sal_Int32(1);
146*cdf0e10cSrcweir 				_rStr << nValue;
147*cdf0e10cSrcweir 			}
148*cdf0e10cSrcweir 			else
149*cdf0e10cSrcweir 			{
150*cdf0e10cSrcweir 				_rStr << sal_Int32(2);
151*cdf0e10cSrcweir 				_rStr.WriteByteString(::comphelper::getString(aValue));
152*cdf0e10cSrcweir 			}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 			_rStr << pFieldDesc->GetType();
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 			_rStr << pFieldDesc->GetPrecision();
157*cdf0e10cSrcweir 			_rStr << pFieldDesc->GetScale();
158*cdf0e10cSrcweir 			_rStr << pFieldDesc->GetIsNullable();
159*cdf0e10cSrcweir 			_rStr << pFieldDesc->GetFormatKey();
160*cdf0e10cSrcweir 			_rStr << (sal_Int32)pFieldDesc->GetHorJustify();
161*cdf0e10cSrcweir 			_rStr << sal_Int32(pFieldDesc->IsAutoIncrement() ? 1 : 0);
162*cdf0e10cSrcweir 			_rStr << sal_Int32(pFieldDesc->IsPrimaryKey() ? 1 : 0);
163*cdf0e10cSrcweir 			_rStr << sal_Int32(pFieldDesc->IsCurrency() ? 1 : 0);
164*cdf0e10cSrcweir 		} // if(pFieldDesc)
165*cdf0e10cSrcweir 		else
166*cdf0e10cSrcweir 			_rStr << (sal_Int32)0;
167*cdf0e10cSrcweir 		return _rStr;
168*cdf0e10cSrcweir 	}
169*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
170*cdf0e10cSrcweir 	SvStream& operator>>( SvStream& _rStr, OTableRow& _rRow )
171*cdf0e10cSrcweir 	{
172*cdf0e10cSrcweir 		_rStr >> _rRow.m_nPos;
173*cdf0e10cSrcweir 		sal_Int32 nValue = 0;
174*cdf0e10cSrcweir 		_rStr >> nValue;
175*cdf0e10cSrcweir 		if ( nValue )
176*cdf0e10cSrcweir 		{
177*cdf0e10cSrcweir 			OFieldDescription* pFieldDesc = new OFieldDescription();
178*cdf0e10cSrcweir 			_rRow.m_pActFieldDescr = pFieldDesc;
179*cdf0e10cSrcweir 			String sValue;
180*cdf0e10cSrcweir 			_rStr.ReadByteString(sValue);
181*cdf0e10cSrcweir 			pFieldDesc->SetName(sValue);
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 			_rStr.ReadByteString(sValue);
184*cdf0e10cSrcweir 			pFieldDesc->SetDescription(sValue);
185*cdf0e10cSrcweir             _rStr.ReadByteString(sValue);
186*cdf0e10cSrcweir 			pFieldDesc->SetHelpText(sValue);
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 			_rStr >> nValue;
189*cdf0e10cSrcweir 			Any aControlDefault;
190*cdf0e10cSrcweir 			switch ( nValue )
191*cdf0e10cSrcweir 			{
192*cdf0e10cSrcweir 				case 1:
193*cdf0e10cSrcweir 				{
194*cdf0e10cSrcweir 					double nControlDefault;
195*cdf0e10cSrcweir 					_rStr >> nControlDefault;
196*cdf0e10cSrcweir 					aControlDefault <<= nControlDefault;
197*cdf0e10cSrcweir 					break;
198*cdf0e10cSrcweir 				}
199*cdf0e10cSrcweir 				case 2:
200*cdf0e10cSrcweir 					_rStr.ReadByteString(sValue);
201*cdf0e10cSrcweir 					aControlDefault <<= ::rtl::OUString(sValue);
202*cdf0e10cSrcweir 					break;
203*cdf0e10cSrcweir 			}
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 			pFieldDesc->SetControlDefault(aControlDefault);
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 			_rStr >> nValue;
209*cdf0e10cSrcweir 			pFieldDesc->SetTypeValue(nValue);
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 			_rStr >> nValue;
212*cdf0e10cSrcweir 			pFieldDesc->SetPrecision(nValue);
213*cdf0e10cSrcweir 			_rStr >> nValue;
214*cdf0e10cSrcweir 			pFieldDesc->SetScale(nValue);
215*cdf0e10cSrcweir 			_rStr >> nValue;
216*cdf0e10cSrcweir 			pFieldDesc->SetIsNullable(nValue);
217*cdf0e10cSrcweir 			_rStr >> nValue;
218*cdf0e10cSrcweir 			pFieldDesc->SetFormatKey(nValue);
219*cdf0e10cSrcweir 			_rStr >> nValue;
220*cdf0e10cSrcweir 			pFieldDesc->SetHorJustify((SvxCellHorJustify)nValue);
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 			_rStr >> nValue;
223*cdf0e10cSrcweir 			pFieldDesc->SetAutoIncrement(nValue != 0);
224*cdf0e10cSrcweir 			_rStr >> nValue;
225*cdf0e10cSrcweir 			pFieldDesc->SetPrimaryKey(nValue != 0);
226*cdf0e10cSrcweir 			_rStr >> nValue;
227*cdf0e10cSrcweir 			pFieldDesc->SetCurrency(nValue != 0);
228*cdf0e10cSrcweir 		}
229*cdf0e10cSrcweir 		return _rStr;
230*cdf0e10cSrcweir 	}
231*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 
236