-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableCompareStream.cpp
439 lines (368 loc) · 11.8 KB
/
TableCompareStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*----------------------------------------------------------------------
Copyright (c) Dan Petitt, http://www.coderanger.com
All Rights Reserved.
Please see the file "licence.txt" for licencing details.
File: TableCompareStream.cpp
Owner: [email protected]
Purpose: Derived table syncronisation class which loads up all data
into memory and analyses differences between the two databases
Advantages: It works
Disadvantages: It uses a lot of memory (hence only really use
this on a 64 bit machine in 64 bit mode)
----------------------------------------------------------------------*/
#include "stdafx.h"
#include "TableCompareStream.h"
CTableCompareStream::CTableCompareStream(void)
{
}
CTableCompareStream::~CTableCompareStream(void)
{
}
void CTableCompareStream::OnProcessTable( const XMySQL::CConnection::Table &tbl )
{
const size_t uNumFields = tbl.arrFields.size();
std::string strKeyFields, strAllFields;
for( size_t uField = 0; uField < uNumFields; uField++ )
{
const XMySQL::CConnection::FieldInfo &fieldInfo = tbl.arrFields.at( uField );
if( fieldInfo.bIsPrimaryKey )
{
if( strKeyFields.length() )
{
strKeyFields += _T(", ");
}
strKeyFields += _T("CAST( ") + fieldInfo.strName + _T(" AS CHAR )");
}
if( strAllFields.length() )
{
strAllFields += _T(",");
}
strAllFields += fieldInfo.strName;
}
std::stringstream dataRequestStream;
dataRequestStream << _T("SELECT MD5( CONCAT_WS( '-', ") << strKeyFields.c_str() << _T(" ) ) AS KeyHash, ") << strAllFields;
dataRequestStream << _T(" FROM `") << tbl.strName << _T("`");
dataRequestStream << _T(" ORDER BY KeyHash");
std::map< std::string, CTableCompareABC::Record > mapSourceRecords;
XMySQL::CResultSet rsSource( m_mySource, XMySQL::CResultSet::Client );
rsSource.Execute( dataRequestStream.str().c_str() );
while( !rsSource.IsEOF() )
{
const char * pszSourceKey = rsSource.GetString( 0 );
CTableCompareABC::Record rec;
rec.strHash = std::string( pszSourceKey );
// Store all our field data for each row for later comparison
for( UINT u = 0; u < rsSource.GetFieldCount(); u++ )
{
CTableCompareABC::Field fld;
fld.strName = std::string( rsSource.GetFieldName( u ) );
fld.bIsNull = rsSource.IsNull( u );
if( !fld.bIsNull )
{
fld.strValue = std::string( rsSource.GetFieldValue( u ) );
}
fld.uType = rsSource.GetFieldType( u );
fld.bIsPrimaryKey = rsSource.IsFieldPrimaryKey( u );
fld.bIsUniqueKey = rsSource.IsFieldUniqueKey( u );
rec.arrFields.push_back( fld );
}
mapSourceRecords[ rec.strHash ] = rec;
if( m_bDisplayProgress )
{
float nPercent = ( (float)rsSource.GetCurrentRowNum() / (float)rsSource.GetRowCount() ) * 100;
_tprintf( _T("\r Calculating hash: %3.0f%% (%d of %d)"), nPercent, static_cast<UINT>(rsSource.GetCurrentRowNum()), static_cast<UINT>(rsSource.GetRowCount()) );
}
rsSource.MoveNext();
}
std::map< std::string, bool > mapRecordsProcessed;
//
// Now go through our target database and compare cached rows
XMySQL::CResultSet rsTarget( m_myTarget, XMySQL::CResultSet::Client );
rsTarget.Execute( dataRequestStream.str().c_str() );
while( !rsTarget.IsEOF() )
{
const std::string strTargetKey( rsTarget.GetString( 0 ) );
mapRecordsProcessed[ strTargetKey ] = true;
std::map< std::string, CTableCompareABC::Record >::iterator it = mapSourceRecords.find( strTargetKey );
if( it == mapSourceRecords.end() )
{
// Target doesnt exist in source, delete it...
std::stringstream deleteStatementStream;
deleteStatementStream << _T("DELETE FROM `") << tbl.strName << _T("`");
deleteStatementStream << BuildWhereClauseForKeys( rsTarget ) << _T(";\n");
m_arrDelete.push_back( deleteStatementStream.str() );
m_uRecordsDeleted++;
}
else
{
// Found record in target, compare values and then remove it from source
CTableCompareABC::Record rec = it->second;
bool bDifferent = false;
for( UINT uTgt = 0; uTgt < rsTarget.GetFieldCount(); uTgt++ ) // skip first field which is the hash field
{
const char * pszTargetFieldName = rsTarget.GetFieldName( uTgt );
Field &fld = rec.arrFields.at( uTgt );
fld.bIsDifferent = false;
if( !_stricmp( fld.strName.c_str(), pszTargetFieldName ) )
{
// Found field, compare values
const char * pszTargetFieldValue = rsTarget.GetFieldValue( uTgt );
if( fld.bIsNull && pszTargetFieldValue == NULL )
{
// Both same, no compare
}
else if( ( fld.bIsNull && pszTargetFieldValue != NULL ) || ( !fld.bIsNull && pszTargetFieldValue == NULL ) )
{
// One is a null, the other is not, so different
fld.bIsDifferent = true;
bDifferent = true;
}
else
{
// Both same and not null so compare values, case sensitive
if( strcmp( fld.strValue.c_str(), pszTargetFieldValue ) )
{
fld.bIsDifferent = true;
bDifferent = true;
}
}
}
else
{
_tprintf( _T("\nERROR: Fields are in a different order between source and target and cannot be compared") );
}
}
if( bDifferent )
{
std::stringstream updateStatementStream;
updateStatementStream << _T("UPDATE `") << tbl.strName << _T("`");
updateStatementStream << _T(" SET ") << BuildDifferentFieldValues( rec ) << BuildWhereClauseForKeys( rec ) << _T(";\n");
m_arrUpdate.push_back( updateStatementStream.str() );
m_uRecordsDifferent++;
}
else
{
m_uRecordsIdentical++;
}
}
if( m_bDisplayProgress )
{
float nPercent = ( (float)rsTarget.GetCurrentRowNum() / (float)rsTarget.GetRowCount() ) * 100;
_tprintf( _T("\r Analysing: %3.0f%% (%d of %d) "), nPercent, static_cast<UINT>(rsTarget.GetCurrentRowNum()), static_cast<UINT>(rsTarget.GetRowCount()) );
}
rsTarget.MoveNext();
}
//
// Any records in source that werent in target need inserting into target
const std::string bulkInsertStartStatement( _T("INSERT INTO `") + tbl.strName + _T("`(") + strAllFields + _T(") VALUES ") );
static const std::string endStatement( _T(";\n") );
std::string bulkInsertValuesStatement;
std::map< std::string, CTableCompareABC::Record >::iterator itSource = mapSourceRecords.begin();
for( itSource = mapSourceRecords.begin(); itSource != mapSourceRecords.end(); itSource++ )
{
const CTableCompareABC::Record rec = itSource->second;
std::map< std::string, bool >::iterator itProcessed = mapRecordsProcessed.find( rec.strHash );
if( itProcessed == mapRecordsProcessed.end() )
{
if( m_bUseBulkInserts )
{
// INSERT INTO programmekeyword(nProgrammeID, nKeywordID) VALUES
// (740044, 37),
// (740044, 42),
// (740044, 560);
if( m_uRecordsInserted == 0 )
{
m_arrInsert.push_back( bulkInsertStartStatement );
}
{
if( bulkInsertValuesStatement.size() )
bulkInsertValuesStatement += _T(",");
bulkInsertValuesStatement += _T("\n(") + BuildBulkInsertFieldValues( rec ) + _T(")");
}
if( m_uRecordsInserted && m_uRecordsInserted % 200 == 0 )
{
m_arrInsert.push_back( bulkInsertValuesStatement );
m_arrInsert.push_back( endStatement );
m_arrInsert.push_back( bulkInsertStartStatement );
bulkInsertValuesStatement.clear();
}
}
else
{
std::stringstream streamStatement;
streamStatement << _T("INSERT INTO `") << tbl.strName << _T("`");
streamStatement << _T(" SET ") << BuildDifferentFieldValues( rec ) << _T(";\n");
m_arrInsert.push_back( streamStatement.str() );
}
m_uRecordsInserted++;
}
}
if( m_bUseBulkInserts && bulkInsertValuesStatement.size() )
{
// Some left over
m_arrInsert.push_back( bulkInsertValuesStatement );
m_arrInsert.push_back( endStatement );
}
}
std::string CTableCompareStream::BuildDifferentFieldValues( const CTableCompareABC::Record &rec )
{
// Build a stream of all our name=value column value pairs
std::stringstream streamFields;
for( UINT u = 1; u < rec.arrFields.size(); u++ ) // first field is always KeyHash field so skip past it
{
const CTableCompareABC::Field &fld = rec.arrFields.at( u );
if( !fld.bIsDifferent )
{
continue;
}
if( streamFields.str().size() )
{
streamFields << _T(",");
}
streamFields << fld.strName;
streamFields << _T("=");
if( fld.bIsNull )
{
streamFields << _T("NULL");
}
else
{
streamFields << _T("'");
if( fld.uType == MYSQL_TYPE_STRING || fld.uType == MYSQL_TYPE_VAR_STRING || fld.uType == MYSQL_TYPE_VARCHAR || fld.uType == MYSQL_TYPE_BLOB )
{
std::string sValue( fld.strValue );
m_mySource.EscapeString( sValue );
streamFields << sValue;
}
else
{
streamFields << fld.strValue;
}
streamFields << _T("'");
}
}
return streamFields.str();
}
std::string CTableCompareStream::BuildBulkInsertFieldValues( const CTableCompareABC::Record &rec )
{
// Build a stream of all our name=value column value pairs
std::stringstream streamFields;
for( UINT u = 1; u < rec.arrFields.size(); u++ ) // first field is always KeyHash field so skip past it
{
const CTableCompareABC::Field &fld = rec.arrFields.at( u );
if( !fld.bIsDifferent )
{
continue;
}
if( streamFields.str().size() )
{
streamFields << _T(",");
}
if( fld.bIsNull )
{
streamFields << _T("NULL");
}
else
{
streamFields << _T("'");
if( fld.uType == MYSQL_TYPE_STRING || fld.uType == MYSQL_TYPE_VAR_STRING || fld.uType == MYSQL_TYPE_VARCHAR || fld.uType == MYSQL_TYPE_BLOB )
{
std::string sValue( fld.strValue );
m_mySource.EscapeString( sValue );
streamFields << sValue;
}
else
{
streamFields << fld.strValue;
}
streamFields << _T("'");
}
}
return streamFields.str();
}
std::string CTableCompareStream::BuildWhereClauseForKeys( const CTableCompareABC::Record &rec )
{
// Build a stream of all our keyed name=value column value pairs
std::stringstream streamFields;
streamFields << _T(" WHERE ");
UINT uFieldAddedCount = 0;
for( UINT u = 0; u < rec.arrFields.size(); u++ )
{
const CTableCompareABC::Field &fld = rec.arrFields.at( u );
if( fld.bIsPrimaryKey || fld.bIsUniqueKey )
{
if( uFieldAddedCount )
{
streamFields << _T(" AND ");
}
streamFields << fld.strName;
streamFields << _T("=");
if( fld.bIsNull )
{
streamFields << _T("NULL");
}
else
{
streamFields << _T("'");
if( fld.uType == MYSQL_TYPE_STRING || fld.uType == MYSQL_TYPE_VAR_STRING || fld.uType == MYSQL_TYPE_VARCHAR || fld.uType == MYSQL_TYPE_BLOB )
{
std::string sValue( fld.strValue );
m_mySource.EscapeString( sValue );
streamFields << sValue;
}
else
{
streamFields << fld.strValue;
}
streamFields << _T("'");
}
uFieldAddedCount++;
}
}
return streamFields.str();
}
std::string CTableCompareStream::BuildWhereClauseForKeys( const XMySQL::CResultSet &rs )
{
// Build a stream of all our keyed name=value column value pairs
std::stringstream streamFields;
streamFields << _T(" WHERE ");
UINT uFieldAddedCount = 0;
const UINT uFieldCount = rs.GetFieldCount();
for( UINT u = 0; u < uFieldCount; u++ )
{
const bool bIsPrimaryKey = rs.IsFieldPrimaryKey( u );
const bool bIsUniqueKey = rs.IsFieldUniqueKey( u );
if( bIsPrimaryKey || bIsUniqueKey )
{
const UINT uFieldType = rs.GetFieldType( u );
if( uFieldAddedCount )
{
streamFields << _T(" AND ");
}
streamFields << rs.GetFieldName( u );
streamFields << _T("=");
if( rs.IsNull( u ) )
{
streamFields << _T("NULL");
}
else
{
const char * pcszFieldValue = rs.GetFieldValue( u );
streamFields << _T("'");
if( uFieldType == MYSQL_TYPE_STRING || uFieldType == MYSQL_TYPE_VAR_STRING || uFieldType == MYSQL_TYPE_VARCHAR || uFieldType == MYSQL_TYPE_BLOB )
{
std::string sValue( pcszFieldValue );
m_mySource.EscapeString( sValue );
streamFields << sValue;
}
else
{
streamFields << pcszFieldValue;
}
streamFields << _T("'");
}
uFieldAddedCount++;
}
}
return streamFields.str();
}