1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _CONNECTIVITY_DBASE_TABLE_HXX_
25cdf0e10cSrcweir #define _CONNECTIVITY_DBASE_TABLE_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "file/FTable.hxx"
28cdf0e10cSrcweir #include "connectivity/sdbcx/VColumn.hxx"
29cdf0e10cSrcweir #include "connectivity/CommonTools.hxx"
30cdf0e10cSrcweir #include <tools/urlobj.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace connectivity
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	namespace dbase
36cdf0e10cSrcweir 	{
37cdf0e10cSrcweir 		typedef file::OFileTable ODbaseTable_BASE;
38cdf0e10cSrcweir 		class ODbaseConnection;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 		typedef ::std::map< ::rtl::OUString,
41cdf0e10cSrcweir 						::com::sun::star::uno::Reference< ::com::sun::star::container::XNamed>, comphelper::UStringMixLess > OContainer;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 		class ODbaseTable :	public ODbaseTable_BASE
44cdf0e10cSrcweir 		{
45cdf0e10cSrcweir 			// der Typ einer dBase datei wird mit dem ersten Byte bestimmt
46cdf0e10cSrcweir 		public:
47cdf0e10cSrcweir 			enum DBFType  {	dBaseIII         = 0x03,
48cdf0e10cSrcweir 							dBaseIV          = 0x04,
49cdf0e10cSrcweir 							dBaseV	         = 0x05,
50cdf0e10cSrcweir                             VisualFoxPro	 = 0x30,
51cdf0e10cSrcweir                             VisualFoxProAuto = 0x31, // Visual FoxPro w. AutoIncrement field
52cdf0e10cSrcweir 							dBaseFS          = 0x43,
53cdf0e10cSrcweir 							dBaseFSMemo      = 0xB3,
54cdf0e10cSrcweir 							dBaseIIIMemo     = 0x83,
55cdf0e10cSrcweir 							dBaseIVMemo      = 0x8B,
56cdf0e10cSrcweir 							dBaseIVMemoSQL   = 0x8E,
57cdf0e10cSrcweir 							FoxProMemo       = 0xF5
58cdf0e10cSrcweir 						  };
59cdf0e10cSrcweir 			enum DBFMemoType {	MemodBaseIII = 0,
60cdf0e10cSrcweir 								MemodBaseIV,
61cdf0e10cSrcweir 								MemoFoxPro
62cdf0e10cSrcweir 							};
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 		private:
65cdf0e10cSrcweir 			struct DBFHeader {                       /* Kopfsatz-Struktur            */
66cdf0e10cSrcweir 								DBFType	db_typ;		                    /* Dateityp						*/
67cdf0e10cSrcweir 								sal_uInt8    db_aedat[3];                    /* Datum der letzen Aenderung   */
68cdf0e10cSrcweir 																		/* JJ MM TT                     */
69cdf0e10cSrcweir 								sal_uInt32   db_anz;                         /* Anzahl der Saetze            */
70cdf0e10cSrcweir 								sal_uInt16  db_kopf;                        /* laenge Kopfsatz-Struktur     */
71cdf0e10cSrcweir 								sal_uInt16  db_slng;                        /* laenge der Daten-Saetze      */
72cdf0e10cSrcweir 								sal_uInt8    db_frei[20];                    /* reserviert                   */
73cdf0e10cSrcweir 							};
74cdf0e10cSrcweir 			struct DBFColumn {                       /* Feldbezeichner               */
75cdf0e10cSrcweir 								sal_uInt8    db_fnm[11];                     /* Feldname                     */
76cdf0e10cSrcweir 								sal_uInt8    db_typ;                         /* Feldtyp                      */
77cdf0e10cSrcweir 								sal_uInt32  db_adr;                         /* Feldadresse                  */
78cdf0e10cSrcweir 								sal_uInt8    db_flng;                        /* Feldlaenge                   */
79cdf0e10cSrcweir 								sal_uInt8    db_dez;                         /* Dezimalstellen fuer N        */
80cdf0e10cSrcweir 								sal_uInt8    db_frei2[14];                   /* reserviert                   */
81cdf0e10cSrcweir 							};
82cdf0e10cSrcweir 			struct DBFMemoHeader
83cdf0e10cSrcweir 							{
84cdf0e10cSrcweir 								DBFMemoType	db_typ;						/* Dateityp						*/
85cdf0e10cSrcweir                                 sal_uInt32  db_next;                        /* naechster freier Block       */
86cdf0e10cSrcweir                                 sal_uInt16  db_size;                        /* Blockgroesse: dBase 3 fest   */
87cdf0e10cSrcweir 							};
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 			::std::vector<sal_Int32> m_aTypes;		// holds all type for columns just to avoid to ask the propertyset
90cdf0e10cSrcweir 			::std::vector<sal_Int32> m_aPrecisions;	// same as aboth
91cdf0e10cSrcweir 			::std::vector<sal_Int32> m_aScales;
92cdf0e10cSrcweir             ::std::vector<sal_Int32> m_aRealFieldLengths;
93cdf0e10cSrcweir 			DBFHeader		m_aHeader;
94cdf0e10cSrcweir 			DBFMemoHeader	m_aMemoHeader;
95cdf0e10cSrcweir 			SvStream*		m_pMemoStream;
96cdf0e10cSrcweir             rtl_TextEncoding m_eEncoding;
97cdf0e10cSrcweir 			sal_Bool		m_bWriteableMemo;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 			void alterColumn(sal_Int32 index,
100cdf0e10cSrcweir 							 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& descriptor ,
101cdf0e10cSrcweir 							 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XDataDescriptorFactory>& xOldColumn );
102cdf0e10cSrcweir 			void readHeader();
103cdf0e10cSrcweir 			void fillColumns();
104cdf0e10cSrcweir 			String createTempFile();
105cdf0e10cSrcweir 			void copyData(ODbaseTable* _pNewTable,sal_Int32 _nPos);
106cdf0e10cSrcweir 			sal_Bool CreateFile(const INetURLObject& aFile, sal_Bool& bCreateMemo);
107cdf0e10cSrcweir 			sal_Bool CreateMemoFile(const INetURLObject& aFile);
HasMemoFields() const108cdf0e10cSrcweir 			sal_Bool HasMemoFields() const { return m_aHeader.db_typ > dBaseIV;}
109cdf0e10cSrcweir 			sal_Bool ReadMemoHeader();
110cdf0e10cSrcweir 			sal_Bool ReadMemo(sal_uIntPtr nBlockNo, ORowSetValue& aVariable);
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 			sal_Bool WriteMemo(ORowSetValue& aVariable, sal_uIntPtr& rBlockNr);
113cdf0e10cSrcweir 			sal_Bool WriteBuffer();
114cdf0e10cSrcweir 			sal_Bool UpdateBuffer(OValueRefVector& rRow, OValueRefRow pOrgRow,const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xCols);
115cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> isUniqueByColumnName(sal_Int32 _nColumnPos);
116cdf0e10cSrcweir 			void AllocBuffer();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 			void throwInvalidDbaseFormat();
119cdf0e10cSrcweir 			void SAL_CALL renameImpl( const ::rtl::OUString& newName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException);
120cdf0e10cSrcweir 			void throwInvalidColumnType(const sal_uInt16 _nErrorId,const ::rtl::OUString& _sColumnName);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 		protected:
123cdf0e10cSrcweir 			virtual void FileClose();
124cdf0e10cSrcweir //			using ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 		public:
127cdf0e10cSrcweir 			virtual void refreshColumns();
128cdf0e10cSrcweir 			virtual void refreshIndexes();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 		public:
131cdf0e10cSrcweir 			ODbaseTable( sdbcx::OCollection* _pTables,ODbaseConnection* _pConnection);
132cdf0e10cSrcweir 			ODbaseTable( sdbcx::OCollection* _pTables,ODbaseConnection* _pConnection,
133cdf0e10cSrcweir 					const ::rtl::OUString& _Name,
134cdf0e10cSrcweir 					const ::rtl::OUString& _Type,
135cdf0e10cSrcweir 					const ::rtl::OUString& _Description = ::rtl::OUString(),
136cdf0e10cSrcweir 					const ::rtl::OUString& _SchemaName = ::rtl::OUString(),
137cdf0e10cSrcweir 					const ::rtl::OUString& _CatalogName = ::rtl::OUString()
138cdf0e10cSrcweir 				);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 			void construct(); // can throw any exception
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			virtual sal_Int32 getCurrentLastPos() const;
143cdf0e10cSrcweir 			virtual sal_Bool seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos);
144cdf0e10cSrcweir 			virtual sal_Bool fetchRow(OValueRefRow& _rRow,const OSQLColumns& _rCols, sal_Bool _bUseTableDefs,sal_Bool bRetrieveData);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 			virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
147cdf0e10cSrcweir 			//XTypeProvider
148cdf0e10cSrcweir 			virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw(::com::sun::star::uno::RuntimeException);
149cdf0e10cSrcweir 			virtual void SAL_CALL disposing(void);
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 			// com::sun::star::lang::XUnoTunnel
152cdf0e10cSrcweir 			virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException);
153cdf0e10cSrcweir 			static ::com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
154cdf0e10cSrcweir 			// XAlterTable
155cdf0e10cSrcweir             virtual void SAL_CALL alterColumnByName( const ::rtl::OUString& colName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException);
156cdf0e10cSrcweir             virtual void SAL_CALL alterColumnByIndex( sal_Int32 index, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
157cdf0e10cSrcweir 			// XRename
158cdf0e10cSrcweir 			virtual void SAL_CALL rename( const ::rtl::OUString& newName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 			sal_Bool	DropImpl();
161cdf0e10cSrcweir 			sal_Bool	CreateImpl();
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 			virtual sal_Bool InsertRow(OValueRefVector& rRow, sal_Bool bFlush,const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xCols);
165cdf0e10cSrcweir 			virtual sal_Bool DeleteRow(const OSQLColumns& _rCols);
166cdf0e10cSrcweir 			virtual sal_Bool UpdateRow(OValueRefVector& rRow, OValueRefRow& pOrgRow,const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xCols);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 			virtual void addColumn(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& descriptor);
169cdf0e10cSrcweir 			virtual void dropColumn(sal_Int32 _nPos);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 			static String	getEntry(file::OConnection* _pConnection,const ::rtl::OUString& _sURL );
172cdf0e10cSrcweir 			static sal_Bool		Drop_Static(const ::rtl::OUString& _sUrl,sal_Bool _bHasMemoFields,sdbcx::OCollection* _pIndexes );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 			virtual void refreshHeader();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 			virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> getMetaData() const;
177cdf0e10cSrcweir 		};
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir }
180cdf0e10cSrcweir #endif // _CONNECTIVITY_DBASE_TABLE_HXX_
181cdf0e10cSrcweir 
182