Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

dbimport.h

00001 /* liblookdb: dbimport.h - Database import from flat file 
00002     Copyright (C) 1998-2001 LOOK Systems
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Lesser General Public
00006     License as published by the Free Software Foundation; either
00007     version 2.1 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this library; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 */
00018 #ifndef LIBLOOKDB_DBIMPORT_H
00019 #define LIBLOOKDB_DBIMPORT_H
00020 
00021 // $Id: dbimport.h,v 1.13 2001/09/07 12:41:04 clive Exp $
00022 
00023 #include "lookdbutils.h"
00024 #include "lkstring.h"
00025 #include "lookstringlist.h"
00026 #include "looklonglist.h"
00027 #include "lookboollist.h"
00028 #include "dbdescribetable.h"
00029 #include "lkdatetimereader.h"
00030 #include <vector>
00031 #include <deque>
00032 
00033 namespace look {
00034 
00035 typedef std::vector<std::string> StringVector;
00036 
00037 class DbConnection;
00038 class DbRow;
00039 
00041 
00062 class LOOKDBUTILITIES_DLL DbImport
00063 {
00064 public:
00065 
00066         // what are we allowed to do with columns?
00071         enum ColumnAllowance
00072         {
00073                 EXACT = 0,                     
00074                 ALLOW_EXTRA_IN_FILE,           
00075                 ALLOW_EXTRA_IN_DATABASE,       
00076                 ALLOW_ANY_EXTRA                
00077         };
00078 
00079         // import types - behaviour on finding match
00081         enum ImportMatchBehaviour
00082         {
00083                 FAIL_IMPORT = 0,              
00084                 FAIL_BUT_IGNORE_DUPLICATE,    
00085                 UPDATE,                       
00086                 IGNORE_NEW                    
00087         };
00088 
00089         // this is to cope with export files generated by Q&E (or in Q&E style)
00090         // normal assumes control chars are escaped
00092         enum TextConversionMode
00093         {
00094                 CONVERT_NORMAL = 1, 
00095                 CONVERT_TO_QE       
00096         };
00097 
00099         enum DateFormat
00100         {
00101                 DATE_FORMAT_NORMAL = 1, 
00102                 DATE_FORMAT_EXTENDED    
00103         };
00104 
00106         enum ErrorAction
00107         {
00108                 CONTINUE = 0,  
00109                 ABORT          
00110         };
00111 
00112         DbImport( DbConnection* theConnection = NULL );
00113 
00114         ~DbImport();
00115 
00116         bool DoImport( const LookString& theFileName );
00117 
00118         void SetPrimaryColumns( const LookString& theColumnNames );
00119         void SetFromTable( const LookString& theTableName );
00120         void SetToTable( const LookString& theTableName );
00121 
00123         void SetColumnAllowances( ColumnAllowance theAllowance )
00124         {
00125                 itsColumnAllowance = theAllowance;
00126         }
00127 
00129         void SetImportMatchBehaviour( ImportMatchBehaviour theImportMatchBehaviour )
00130         {
00131                 itsImportMatchBehaviour = theImportMatchBehaviour;
00132         }
00133 
00135         void SetNULLString( const LookString& theString )
00136         {
00137                 itsNULLString = theString;
00138         }
00139 
00141         void SetTextConversionMode( TextConversionMode theConversionMode )
00142         {
00143                 itsTextConversionMode = theConversionMode;
00144         }
00145 
00150         void SetDOSLineEnds( bool useCRLF )
00151         {
00152                 if( useCRLF )
00153                 {
00154                         itsLFString = "\r\n";
00155                 }
00156                 else
00157                 {
00158                         itsLFString = "\n";
00159                 }
00160         }
00161 
00163         long GetNRowsInserted( void )
00164         {
00165                 return itsNRowsInserted;
00166         }
00167 
00169         long GetNRowsUpdated( void )
00170         {
00171                 return itsNRowsUpdated;
00172         }
00173 
00175         long GetNRowsWithErrors( void )
00176         {
00177                 return itsNRowsWithErrors;
00178         }
00179 
00185         void SetAllowNullDates( bool allowNullDates = true )
00186         {
00187                 itAllowsNullDates = allowNullDates;
00188         }
00189 
00191         void SetErrorAction( ErrorAction theErrorAction )
00192         {
00193                 itsErrorAction = theErrorAction;
00194         }
00195 
00197         void SetDateFormat( DateFormat theDateFormat )
00198         {
00199                 itsDateFormat = theDateFormat;
00200         }
00201 
00202         void SetExDatePartOrder( LookDateTimeReader::DatePartOrder theExDatePartOrder )
00203         {
00204                 itsExDatePartOrder = theExDatePartOrder;
00205         }
00206 
00207         void SetExDateTimeOrder( LookDateTimeReader::DateTimeOrder theExDateTimeOrder )
00208         {
00209                 itsExDateTimeOrder = theExDateTimeOrder;
00210         }
00211 
00213 
00217         void SetNCommitRows( long theNRows )
00218         {
00219                 itsNCommitRows = theNRows;
00220         }
00221 
00222 protected:
00223 
00224         // forward declaration;
00225         struct SaveRow;
00226 
00227         LookString GetNextLine( std::istream& theStream, bool& finished );
00228         LookString GetColumn( const LookString& theLine, int theColumnIndex );
00229 
00230         bool CheckColumnNames( void );
00231 
00232         void MakeUpper( LookStringList& theStringList );
00233         void MakeUpper( DbDescribeTable::ColumnInfoList& theColumnList );
00234 
00235         void AddParameterToQuery( DbQuery* theQuery, const LookString& theString,
00236                 DbDescribeTable::SColumnInfo& theColumnDescription );
00237 
00238         bool InitialiseDuplicateCheckQuery( void );
00239 
00240         bool LoadRowForImport( const LookString& theRowString,
00241                 int theRowNumber );
00242 
00243         void TokeniseAndConvertRow( const LookString& theInString,
00244                 StringVector& theReturnStrings );
00245 
00246         bool CheckMatchingRows( SaveRow& theRow, int theRowNumber );
00247         bool CheckStoredRowPK( SaveRow& theNewRow,
00248                 SaveRow& theOldRow );
00249 
00250         bool RowsIdentical( SaveRow& theNewRow,
00251                 SaveRow& theOldRow );
00252 
00253         bool RowsIdentical( SaveRow& theNewRow,
00254                 DbRow* theOldRow );
00255 
00256         // saving methods
00257         DbQuery* InitialiseInsertQuery( DbConnection* theConnection );
00258         DbQuery* InitialiseUpdateQuery( DbConnection* theConnection );
00259 
00260         bool InsertRow( SaveRow& theRow, DbQuery* theQuery );
00261         bool UpdateRow( SaveRow& theRow, DbQuery* theQuery );
00262 
00263         bool SaveImport( void );
00264 
00265         // setup data
00266 
00267         LookStringList itsPrimaryColumns;
00268 
00269         LookString itsToTable;    // table we're saving the data in
00270         LookString itsFromTable;  // table it was originally saved as
00271 
00272         ColumnAllowance itsColumnAllowance;
00273         ImportMatchBehaviour itsImportMatchBehaviour;
00274         bool itAllowsNullDates;
00275 
00276         TextConversionMode itsTextConversionMode;
00277         DateFormat itsDateFormat;
00278         LookDateTimeReader::DatePartOrder itsExDatePartOrder;
00279         LookDateTimeReader::DateTimeOrder itsExDateTimeOrder;
00280 
00281         ErrorAction itsErrorAction;
00282         
00283         LookString itsNULLString;
00284         LookString itsLFString;
00285 
00286         DbConnection* itsConnection;
00287         bool itCommits;
00288 
00289         long itsNCommitRows;
00290 
00291         // result data
00292         long itsNRowsInserted;
00293         long itsNRowsUpdated;
00294         long itsNRowsWithErrors;
00295 
00296         // data processing data
00297 
00298         // this is not necessarily the to table - that could be empty
00299         LookString itsDestinationTable;
00300         DbDescribeTable::ColumnInfoList itsColumnDescriptions;
00301         LookStringList itsColumnNames;
00302 
00303         LongValList itsPrimaryColumnIndices;
00304         // used to get types of primary columns
00305         LongValList itsPrimaryColumnDatabaseIndices;
00306 
00307         // these are lists of the columns shared by both file and table
00308         LookStringList itsCommonColumnNames;
00309         LongValList itsCommonColumnFileIndices;
00310         LongValList itsCommonColumnTableIndices;
00311         BoolValList itsCommonColumnPrimaryFlags;
00312 
00313         // no update possible if all columns are primary
00314         bool itsUpdateIsAllowed;
00315 
00316         bool itIsLoadingData;
00317 
00318         DbQuery* itsDuplicateCheckQuery;
00319 
00320         struct SaveRow
00321         {
00322                 enum RowStatus
00323                 {
00324                         INSERT,
00325                         UPDATE,
00326                         IGNORE,
00327                         INVALID
00328                 };
00329 
00330                 StringVector itsColumns;
00331                 RowStatus itsStatus;
00332                 int itsRowNumber;
00333 
00334                 SaveRow( int theReserveColumns )
00335                         : itsStatus( INVALID ),
00336                         itsRowNumber( 0 )
00337                 {
00338                         itsColumns.reserve( theReserveColumns );
00339                 }
00340 
00341                 ~SaveRow()
00342                 {
00343                 }
00344 
00345                 SaveRow( const SaveRow& theOther )
00346                 {
00347                         *this = theOther;
00348                 }
00349 
00350                 SaveRow& operator=( const SaveRow& theOther )
00351                 {
00352                         itsStatus = theOther.itsStatus;
00353                         itsRowNumber = theOther.itsRowNumber;
00354                         itsColumns = theOther.itsColumns;
00355                         return *this;
00356                 }
00357 
00358                 // dummy for lists
00359                 bool operator==( const SaveRow& theOther )
00360                 {
00361                         return false;
00362                 }
00363         };
00364 
00365         typedef std::deque<SaveRow> SaveRowList;
00366 
00367         SaveRowList itsRowsToSave;
00368 
00369 };
00370 
00371 
00372 } // end of "look" namespace
00373 #endif

Generated at Thu Jan 17 12:53:06 2002 for liblookdb by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001