00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef LIBLOOKDB_DBIMPORT_H
00019 #define LIBLOOKDB_DBIMPORT_H
00020
00021
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
00071 enum ColumnAllowance
00072 {
00073 EXACT = 0,
00074 ALLOW_EXTRA_IN_FILE,
00075 ALLOW_EXTRA_IN_DATABASE,
00076 ALLOW_ANY_EXTRA
00077 };
00078
00079
00081
00082 {
00083 FAIL_IMPORT = 0,
00084 FAIL_BUT_IGNORE_DUPLICATE,
00085 UPDATE,
00086 IGNORE_NEW
00087 };
00088
00089
00090
00092
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
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
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
00266
00267 LookStringList itsPrimaryColumns;
00268
00269 LookString itsToTable;
00270 LookString itsFromTable;
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
00292 long itsNRowsInserted;
00293 long itsNRowsUpdated;
00294 long itsNRowsWithErrors;
00295
00296
00297
00298
00299 LookString itsDestinationTable;
00300 DbDescribeTable::ColumnInfoList itsColumnDescriptions;
00301 LookStringList itsColumnNames;
00302
00303 LongValList itsPrimaryColumnIndices;
00304
00305 LongValList itsPrimaryColumnDatabaseIndices;
00306
00307
00308 LookStringList itsCommonColumnNames;
00309 LongValList itsCommonColumnFileIndices;
00310 LongValList itsCommonColumnTableIndices;
00311 BoolValList itsCommonColumnPrimaryFlags;
00312
00313
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
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 }
00373 #endif