1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DTABLE_HXX
21 #define INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DTABLE_HXX
22 
23 #include <file/FTable.hxx>
24 #include <connectivity/sdbcx/VColumn.hxx>
25 #include <connectivity/CommonTools.hxx>
26 #include <tools/urlobj.hxx>
27 
28 
29 namespace connectivity
30 {
31     namespace dbase
32     {
33         typedef file::OFileTable ODbaseTable_BASE;
34         class ODbaseConnection;
35 
36         class ODbaseTable : public ODbaseTable_BASE
37         {
38         // The first byte of a dBase file specifies its type
39         public:
40             enum DBFType  { dBaseIII         = 0x03,
41                             dBaseIV          = 0x04,
42                             dBaseV           = 0x05,
43                             VisualFoxPro     = 0x30,
44                             VisualFoxProAuto = 0x31, // Visual FoxPro with AutoIncrement field
45                             dBaseFS          = 0x43,
46                             dBaseFSMemo      = 0xB3,
47                             dBaseIIIMemo     = 0x83,
48                             dBaseIVMemo      = 0x8B,
49                             dBaseIVMemoSQL   = 0x8E,
50                             FoxProMemo       = 0xF5
51                           };
52             enum DBFMemoType {  MemodBaseIII = 0,
53                                 MemodBaseIV,
54                                 MemoFoxPro
55                             };
56 
57         private:
58             // sources: https://www.clicketyclick.dk/databases/xbase/format/dbf.html (dBASE III and 5)
59             // http://www.dbase.com/KnowledgeBase/int/db7_file_fmt.htm (dBASE 7) which is similar at least for this part
60             struct DBFHeader {                                                                   //   address/pos in trailer
61                                 DBFType     type;                      // dBASE/xBASE type, see DBFType 00h
62                                 sal_uInt8   dateElems[3];              // Date of last change (YYMMDD)  01h
63                                 sal_uInt32  nbRecords;                 // Number of records             04h
64                                 sal_uInt16  headerLength;              //                               08h
65                                 sal_uInt16  recordLength;              // Length of 1 record            10h
66                                 sal_uInt8   trailer[20];
67                                 // this last field contains these data:
68                                 // - reserved:2 bytes:should be filled with 0                           12h/0
69                                 // - incomplete transaction:1 byte:dBASE IV                             14h/2
70                                                  // 00h Transaction ended (or rolled back)
71                                                  // 01h Transaction started
72                                 // - encryptionFlag:1 byte: dBASE IV                                    15h/3
73                                                  // 00h not encrypted
74                                                  // 01h for encrypted
75                                 // - freeRecordThread:4 bytes:reserved for LAN only                     16h/4
76                                 // - multiUserdBASE:8 bytes:reserved for multi-user dBASE (dBASE III+)  20h/8
77                                 // - MDXFlag:1 byte:dBASE IV                                            28h/16
78                                                  // 0x01 if a production .MDX file exists for this table
79                                                  // 0x00 if no .MDX file exists
80                                 // - languageDriver:1 byte:codepage (from Foxpro)                       29h/17
81                                 // - reserved:2 bytes: should be filled with 0                          30h/18
82                             };
83             struct DBFColumn {                       /* Column descriptors */
84                                 sal_uInt8    db_fnm[11];                     /* Field name                  */
85                                 sal_uInt8    db_typ;                         /* Field type                  */
86                                 sal_uInt32   db_adr;                         /* Field address               */
87                                 sal_uInt8    db_flng;                        /* Field length                */
88                                 sal_uInt8    db_dez;                         /* Decimal places for N        */
89                                 sal_uInt8    db_free2[14];                   /* Reserved                    */
90                             };
91             struct DBFMemoHeader
92             {
93                 DBFMemoType db_typ;                         /* File type                    */
94                 sal_uInt32  db_next;                        /* Next free block              */
95                 sal_uInt16  db_size;                        /* Block size: dBase 3 fixed    */
DBFMemoHeaderconnectivity::dbase::ODbaseTable::DBFMemoHeader96                 DBFMemoHeader()
97                     : db_typ(MemodBaseIII)
98                     , db_next(0)
99                     , db_size(0)
100                 {
101                 }
102             };
103 
104             std::vector<sal_Int32> m_aTypes;      // holds all types for columns just to avoid to ask the propertyset
105             std::vector<sal_Int32> m_aPrecisions; // same as above
106             std::vector<sal_Int32> m_aScales;
107             std::vector<sal_Int32> m_aRealFieldLengths;
108             DBFHeader       m_aHeader = {};
109             DBFMemoHeader   m_aMemoHeader;
110             std::unique_ptr<SvStream> m_pMemoStream;
111             rtl_TextEncoding m_eEncoding;
112 
113             void alterColumn(sal_Int32 index,
114                              const css::uno::Reference< css::beans::XPropertySet>& descriptor ,
115                              const css::uno::Reference< css::sdbcx::XDataDescriptorFactory>& xOldColumn );
116             void readHeader();
117             void fillColumns();
118             OUString createTempFile();
119             void copyData(ODbaseTable* _pNewTable,sal_Int32 _nPos);
120             bool CreateFile(const INetURLObject& aFile, bool& bCreateMemo);
121             bool CreateMemoFile(const INetURLObject& aFile);
HasMemoFields() const122             bool HasMemoFields() const { return m_aHeader.type > dBaseIV;}
123             void ReadMemoHeader();
124             bool ReadMemo(std::size_t nBlockNo, ORowSetValue& aVariable);
125 
126             void WriteMemo(const ORowSetValue& aVariable, std::size_t& rBlockNr);
127             bool WriteBuffer();
128             bool UpdateBuffer(OValueRefVector& rRow, const OValueRefRow& pOrgRow, const css::uno::Reference< css::container::XIndexAccess>& _xCols, bool bForceAllFields);
129             css::uno::Reference< css::beans::XPropertySet> isUniqueByColumnName(sal_Int32 _nColumnPos);
130             bool AllocBuffer();
131 
132             void throwInvalidDbaseFormat();
133             /// @throws css::sdbc::SQLException
134             /// @throws css::container::ElementExistException
135             /// @throws css::uno::RuntimeException
136             void renameImpl( const OUString& newName );
137             void throwInvalidColumnType(const char* pErrorId, const OUString& _sColumnName);
138 
139         protected:
140             virtual void FileClose() override;
141 //          using ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper;
142 
143         public:
144             virtual void refreshColumns() override;
145             virtual void refreshIndexes() override;
146 
147         public:
148             ODbaseTable( sdbcx::OCollection* _pTables,ODbaseConnection* _pConnection);
149             ODbaseTable( sdbcx::OCollection* _pTables,ODbaseConnection* _pConnection,
150                     const OUString& Name,
151                     const OUString& Type,
152                     const OUString& Description = OUString(),
153                     const OUString& SchemaName = OUString(),
154                     const OUString& CatalogName = OUString()
155                 );
156 
157             void construct() override; // can throw any exception
158 
159             virtual sal_Int32 getCurrentLastPos() const override;
160             virtual bool seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos) override;
161             virtual bool fetchRow(OValueRefRow& _rRow,const OSQLColumns& _rCols, bool bRetrieveData) override;
162 
163             virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
164             //XTypeProvider
165             virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes(  ) override;
166             virtual void SAL_CALL disposing() override;
167 
168             // css::lang::XUnoTunnel
169             virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
170             static css::uno::Sequence< sal_Int8 > getUnoTunnelId();
171             // XAlterTable
172             virtual void SAL_CALL alterColumnByName( const OUString& colName, const css::uno::Reference< css::beans::XPropertySet >& descriptor ) override;
173             virtual void SAL_CALL alterColumnByIndex( sal_Int32 index, const css::uno::Reference< css::beans::XPropertySet >& descriptor ) override;
174             // XRename
175             virtual void SAL_CALL rename( const OUString& newName ) override;
176 
177             bool    DropImpl();
178             bool    CreateImpl();
179 
180 
181             virtual bool InsertRow(OValueRefVector& rRow, const css::uno::Reference< css::container::XIndexAccess>& _xCols) override;
182             virtual bool DeleteRow(const OSQLColumns& _rCols) override;
183             virtual bool UpdateRow(OValueRefVector& rRow, OValueRefRow& pOrgRow,const css::uno::Reference< css::container::XIndexAccess>& _xCols) override;
184 
185             virtual void addColumn(const css::uno::Reference< css::beans::XPropertySet>& descriptor) override;
186             virtual void dropColumn(sal_Int32 _nPos) override;
187 
188             static OUString   getEntry(file::OConnection const * _pConnection,const OUString& _sURL );
189             static bool     Drop_Static(const OUString& _sUrl, bool _bHasMemoFields, sdbcx::OCollection* _pIndexes );
190 
191             virtual void refreshHeader() override;
192 
193             virtual css::uno::Reference< css::sdbc::XDatabaseMetaData> getMetaData() const override;
194         };
195     }
196 }
197 #endif // INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DTABLE_HXX
198 
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
200