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_dbaccess.hxx"
30 #ifndef DBAUI_TABLEROW_HXX
31 #include "TableRow.hxx"
32 #endif
33 #ifndef _TOOLS_DEBUG_HXX
34 #include <tools/debug.hxx>
35 #endif
36 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
37 #include "FieldDescriptions.hxx"
38 #endif
39 #include <algorithm>
40 #include <comphelper/types.hxx>
41 
42 using namespace dbaui;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::beans;
46 
47 //========================================================================
48 // class OTableRow
49 //========================================================================
50 DBG_NAME(OTableRow)
51 //------------------------------------------------------------------------------
52 OTableRow::OTableRow()
53 	:m_pActFieldDescr( NULL )
54 	,m_nPos( -1 )
55 	,m_bReadOnly( false )
56 	,m_bOwnsDescriptions(false)
57 {
58 	DBG_CTOR(OTableRow,NULL);
59 }
60 //------------------------------------------------------------------------------
61 OTableRow::OTableRow(const Reference< XPropertySet >& xAffectedCol)
62 	:m_pActFieldDescr( NULL )
63 	,m_nPos( -1 )
64 	,m_bReadOnly( false )
65 	,m_bOwnsDescriptions(true)
66 {
67 	DBG_CTOR(OTableRow,NULL);
68 	m_pActFieldDescr = new OFieldDescription(xAffectedCol);
69 }
70 //------------------------------------------------------------------------------
71 OTableRow::OTableRow( const OTableRow& rRow, long nPosition )
72     :m_pActFieldDescr(NULL)
73 	,m_nPos( nPosition )
74 	,m_bReadOnly(rRow.IsReadOnly())
75 	,m_bOwnsDescriptions(false)
76 {
77 	DBG_CTOR(OTableRow,NULL);
78 
79 	OFieldDescription* pSrcField = rRow.GetActFieldDescr();
80 	if(pSrcField)
81 	{
82 		m_pActFieldDescr = new OFieldDescription(*pSrcField);
83 		m_bOwnsDescriptions = true;
84 	}
85 }
86 
87 //------------------------------------------------------------------------------
88 OTableRow::~OTableRow()
89 {
90 	DBG_DTOR(OTableRow,NULL);
91 	if(m_bOwnsDescriptions)
92 		delete m_pActFieldDescr;
93 }
94 
95 //------------------------------------------------------------------------------
96 void OTableRow::SetPrimaryKey( sal_Bool bSet )
97 {
98 	DBG_CHKTHIS(OTableRow,NULL);
99 	if(m_pActFieldDescr)
100 		m_pActFieldDescr->SetPrimaryKey(bSet);
101 }
102 // -----------------------------------------------------------------------------
103 sal_Bool OTableRow::IsPrimaryKey() const
104 {
105 	DBG_CHKTHIS(OTableRow,NULL);
106 	return m_pActFieldDescr && m_pActFieldDescr->IsPrimaryKey();
107 }
108 // -----------------------------------------------------------------------------
109 void OTableRow::SetFieldType( const TOTypeInfoSP& _pType, sal_Bool _bForce )
110 {
111 	DBG_CHKTHIS(OTableRow,NULL);
112 	if ( _pType.get() )
113 	{
114 		if( !m_pActFieldDescr )
115 		{
116 			m_pActFieldDescr = new OFieldDescription();
117 			m_bOwnsDescriptions = true;
118 		}
119 		m_pActFieldDescr->FillFromTypeInfo(_pType,_bForce,sal_True);
120 	}
121 	else
122 	{
123 		delete m_pActFieldDescr;
124 		m_pActFieldDescr = NULL;
125 	}
126 }
127 // -----------------------------------------------------------------------------
128 namespace dbaui
129 {
130 	// -----------------------------------------------------------------------------
131 	SvStream& operator<<( SvStream& _rStr, const OTableRow& _rRow )
132 	{
133 		_rStr << _rRow.m_nPos;
134 		OFieldDescription* pFieldDesc = _rRow.GetActFieldDescr();
135 		if(pFieldDesc)
136 		{
137 			_rStr << (sal_Int32)1;
138 			_rStr.WriteByteString(pFieldDesc->GetName());
139 			_rStr.WriteByteString(pFieldDesc->GetDescription());
140             _rStr.WriteByteString(pFieldDesc->GetHelpText());
141 			double nValue = 0.0;
142 			Any aValue = pFieldDesc->GetControlDefault();
143 			if ( aValue >>= nValue )
144 			{
145 				_rStr << sal_Int32(1);
146 				_rStr << nValue;
147 			}
148 			else
149 			{
150 				_rStr << sal_Int32(2);
151 				_rStr.WriteByteString(::comphelper::getString(aValue));
152 			}
153 
154 			_rStr << pFieldDesc->GetType();
155 
156 			_rStr << pFieldDesc->GetPrecision();
157 			_rStr << pFieldDesc->GetScale();
158 			_rStr << pFieldDesc->GetIsNullable();
159 			_rStr << pFieldDesc->GetFormatKey();
160 			_rStr << (sal_Int32)pFieldDesc->GetHorJustify();
161 			_rStr << sal_Int32(pFieldDesc->IsAutoIncrement() ? 1 : 0);
162 			_rStr << sal_Int32(pFieldDesc->IsPrimaryKey() ? 1 : 0);
163 			_rStr << sal_Int32(pFieldDesc->IsCurrency() ? 1 : 0);
164 		} // if(pFieldDesc)
165 		else
166 			_rStr << (sal_Int32)0;
167 		return _rStr;
168 	}
169 	// -----------------------------------------------------------------------------
170 	SvStream& operator>>( SvStream& _rStr, OTableRow& _rRow )
171 	{
172 		_rStr >> _rRow.m_nPos;
173 		sal_Int32 nValue = 0;
174 		_rStr >> nValue;
175 		if ( nValue )
176 		{
177 			OFieldDescription* pFieldDesc = new OFieldDescription();
178 			_rRow.m_pActFieldDescr = pFieldDesc;
179 			String sValue;
180 			_rStr.ReadByteString(sValue);
181 			pFieldDesc->SetName(sValue);
182 
183 			_rStr.ReadByteString(sValue);
184 			pFieldDesc->SetDescription(sValue);
185             _rStr.ReadByteString(sValue);
186 			pFieldDesc->SetHelpText(sValue);
187 
188 			_rStr >> nValue;
189 			Any aControlDefault;
190 			switch ( nValue )
191 			{
192 				case 1:
193 				{
194 					double nControlDefault;
195 					_rStr >> nControlDefault;
196 					aControlDefault <<= nControlDefault;
197 					break;
198 				}
199 				case 2:
200 					_rStr.ReadByteString(sValue);
201 					aControlDefault <<= ::rtl::OUString(sValue);
202 					break;
203 			}
204 
205 			pFieldDesc->SetControlDefault(aControlDefault);
206 
207 
208 			_rStr >> nValue;
209 			pFieldDesc->SetTypeValue(nValue);
210 
211 			_rStr >> nValue;
212 			pFieldDesc->SetPrecision(nValue);
213 			_rStr >> nValue;
214 			pFieldDesc->SetScale(nValue);
215 			_rStr >> nValue;
216 			pFieldDesc->SetIsNullable(nValue);
217 			_rStr >> nValue;
218 			pFieldDesc->SetFormatKey(nValue);
219 			_rStr >> nValue;
220 			pFieldDesc->SetHorJustify((SvxCellHorJustify)nValue);
221 
222 			_rStr >> nValue;
223 			pFieldDesc->SetAutoIncrement(nValue != 0);
224 			_rStr >> nValue;
225 			pFieldDesc->SetPrimaryKey(nValue != 0);
226 			_rStr >> nValue;
227 			pFieldDesc->SetCurrency(nValue != 0);
228 		}
229 		return _rStr;
230 	}
231 	// -----------------------------------------------------------------------------
232 }
233 
234 
235 
236