ormtable.cpp
30.1 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
#include "ormdata.h"
#include "ormtable.h"
#include "ormhandler.h"
#include "log.h"
#include "multisortfilterproxymodel.h"
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlQuery>
#include <QtDebug>
#include <QtCore/qnamespace.h>
using namespace osdev::components;
OrmTable::OrmTable(const DbConnector &db, QObject *_parent)
: QSqlRelationalTableModel( _parent, db.getDatabase() )
, m_className( "OrmTable" )
, m_recTrackField()
, m_db( db )
, m_qhRelTables()
, m_qhRelations()
, m_qhFieldTypes()
, m_qhTimelines()
{
if ( !QSqlRelationalTableModel::database().isOpen() )
{
LogWarning( m_className, "Database is closed.");
}
}
OrmTable::~OrmTable()
{
}
void OrmTable::setTable( const QString& _table )
{
// Set tablename in the base class
QSqlRelationalTableModel::setTable( m_db.quoteTableName( _table ) );
m_className += "::" + this->tableName();
// Make every change being written to the database.
QSqlRelationalTableModel::setEditStrategy( QSqlTableModel::OnRowChange );
// Now we know the tablename, we can retrieve the fieldtypes by their name. To prevent a functioncall
// For each and every fieldname we're looking for, we store this in an internal Hash. Must be quicker than
// then accessing the database layer.
m_qhFieldTypes = m_db.getFieldNamesAndTypes( _table );
QSqlRecord _headerData = QSqlRelationalTableModel::database().record( m_db.quoteTableName( _table ) );
for( int nCount = 0; nCount < _headerData.count(); nCount++ )
{
QSqlRelationalTableModel::setHeaderData( nCount, Qt::Horizontal, _headerData.fieldName(nCount) );
}
// Now the table is set, headerinfo loaded and commit behaviour set :
// Open the table and read from the database.
QSqlRelationalTableModel::select();
}
bool OrmTable::insertRecord( int _row, const QSqlRecord &_record )
{
if ( QSqlRelationalTableModel::database().isOpen() )
{
LogDebug( m_className, "Database is open." );
}
else
{
LogWarning( m_className, "Database is closed." );
}
bool b_result = QSqlRelationalTableModel::insertRecord( _row, _record );
if ( !b_result )
{
LogError( QString( "[OrmTable - %1]" ).arg( QSqlRelationalTableModel::tableName() ),
QString( "Database gave error : %1")
.arg( this->lastError().text() ) );
}
return b_result;
}
bool OrmTable::insertRecords(int row, const QList<QSqlRecord>& _records )
{
bool l_bResult = true;
QSqlRelationalTableModel::setEditStrategy( QSqlTableModel::OnRowChange );
for( const QSqlRecord& _record : _records )
{
l_bResult &= insertRecord( row, _record );
}
QSqlRelationalTableModel::submitAll();
// Reset the transaction method.
QSqlRelationalTableModel::setEditStrategy( QSqlTableModel::OnRowChange );
return l_bResult;
}
void OrmTable::writeData( const QSharedPointer<ORMRelData>& dataObject )
{
// Reset the filter for this table before doing anything else
this->resetFilter();
// Relations can be set on different fields. We have to take in account those
// fields could be given in a list.
ORMData* pMainTable = this->solveRelations( dataObject );
if( nullptr == pMainTable || pMainTable->getContainerName().isEmpty() )
{
LogDebug( "[OrmTable::writeData]", QString( "No maintable defined. Exiting..." ) );
emit signalRejectedData( dataObject );
return;
}
bool updateSuccess = false;
/// Here we check for the variable list. See if one of the variables is of type :
/// QVariant( QList<Qvariant) ) Instead of a normal update, we should use
/// a batched_update.
if( pMainTable->hasLists() )
{
/// @todo: This is kind of a special case. For now we assume there is an inclusive
/// and exclusive way of updating these records. All these updates should be done
/// in a single transaction. The 'not-mentioned records' should be de-coupled.
/// If those fail, the entire package should be rejected, including the ones that were
/// mentioned. Default behaviour is "Each - change - a - transaction" so we need some
/// extra precautions here.
// Check if all items in the list exist in the database. If not the package is rejected.
if (!m_db.recordsExist(pMainTable->getContainerName(), pMainTable->getKeyField(), pMainTable->getValue( pMainTable->getKeyField() ).toList()))
{
LogInfo( "[OrmTable::writeData]", "Not all records are present. Package is rejected." );
emit signalRejectedData( dataObject );
return;
}
// Set the TransactionScopeGuard. If it goes out-of-scope, for whatever reason,
// it will do a Rollback if a transaction was started.
OrmTransGuard l_scopeGuard;
// Check to see if the database supports transactions
if( m_db.supportTransactions() )
{
if( !m_db.transactionBegin() )
{
LogDebug( "[OrmTable::writeData]", "Transaction failed to start." );
emit signalRejectedData( dataObject );
return;
}
l_scopeGuard = OrmTransGuard( m_db );
}
std::unique_ptr<ORMData> mainTableCopy(pMainTable->clone());
updateSuccess = batchUpdate( mainTableCopy.get(), dataObject->getSourceId() );
if( !updateSuccess )
{
emit signalRejectedData( dataObject );
return;
}
if( ( static_cast<int>(TableActions::actionMergeUpdate) == mainTableCopy->getDefaultAction() )
&& ( !mainTableCopy->isMergeable() ) ) // If there is more than one key besides the keyfield, we cannot handle this (yet)
{
LogWarning( "[OrmTable::writeData]", QString( "Dataobject (ORMData) is not fit for mergeUpdate and is discarded : %1" ).arg( mainTableCopy->asString() ) );
return;
}
if( ( static_cast<int>(TableActions::actionMergeUpdate) == mainTableCopy->getDefaultAction() )
&& ( mainTableCopy->isMergeable() ) )
{
deCoupleRecords( mainTableCopy.get() );
}
else if (static_cast<int>(TableActions::actionUpdate) == mainTableCopy->getDefaultAction())
{
LogDebug( "[OrmTable::writeData]", QString( "Action is a \"normal\" update : %1" ).arg( mainTableCopy->asString() ) );
}
if( !this->commit() )
{
emit signalRejectedData( dataObject );
}
auto timeline = m_qhTimelines[dataObject->getSourceId()];
if (timeline)
{
timeline->commit();
}
return;
}
else
{
if( static_cast<int>(TableActions::actionUpdate) == pMainTable->getDefaultAction() )
{
// ===================================================================================
// == S I N G L E R E C O R D U P D A T E ==
// ===================================================================================
// See if we can actually update this record by checking its timestamp validity.
//
if( m_recTrackField.isEmpty() || m_recTrackField.isNull() )
{
updateSuccess = this->updateRecord( pMainTable );
}
else
{
if( this->isPackageValid( pMainTable ) )
{
pMainTable->setValue( m_recTrackField, QVariant( pMainTable->timeStamp() ) );
updateSuccess = this->updateRecord( pMainTable );
}
else
{
// Nope. Package is older. Discard.
LogInfo( "[OrmTable::writeData]", QString( "Discarding package because it is older than information in the database" ) );
return;
}
}
}
if( updateSuccess )
{
return;
}
}
// ===================================================================================
// == I N S E R T ==
// ===================================================================================
// If a timestamp is used in the database, add it to the table definition.
if( !m_recTrackField.isEmpty() || !m_recTrackField.isNull() )
{
pMainTable->setValue( m_recTrackField, pMainTable->timeStamp() );
}
// If we get to here, we're ready to create the new SQLRecord and add it to the table.
QSqlRecord newRecord = this->buildSqlRecord( pMainTable );
if( !newRecord.isEmpty() )
{
if( !m_db.createRecord( newRecord, this->tableName() ) )
{
// @todo : Change to a more database dependant, generic error checking based on keywords....
if( this->lastError().text().contains( "violates unique constraint" ) ||
this->lastError().text().contains( "violates not-null constraint" ) )
{
// Just drop the package and be done with it. This is a unique constraint violation
LogDebug( "[OrmTable::writeData]", QString( "There was and error creating the record : %1" ).arg( this->lastError().text() ) );
return;
}
else
{
LogDebug( "[OrmTable::writeData]", QString( "There was and error creating the record : %1" ).arg( this->lastError().text() ) );
LogDebug( "[OrmTable::writeData]", "Deferring the datapackage to the TransQueue." );
emit signalRejectedData( dataObject );
}
}
else
{
return;
}
}
else
{
LogDebug( "[OrmTable::writeData]", QString( "Creating the new record failed.! : %1" ).arg( this->lastError().text() ) );
}
}
bool OrmTable::updateRecord( ORMData* dataObject )
{
auto lNumRecords = tableFilter( dataObject );
if( lNumRecords > 1 )
{
LogError( "[OrmTable::updateRecord]", QString( "Multiple records found..." ) );
return false;
}
else if( lNumRecords == 0 )
{
LogWarning( "[OrmTable::updateRecord]", QString( "No record found...Falling back to insert...." ) );
return false;
}
else
{
// All criteria are met.. Time to update the record.
// Each and and every field will be written, with the exception of the keyfield..
// First get a list of all field names.
bool updateSuccess = true;
QStringList fieldNames = dataObject->getFieldNames();
for( const QString& fieldName : fieldNames )
{
if( fieldName != dataObject->getKeyField() )
{
int colIndex = this->fieldIndex( fieldName );
if( -1 == colIndex )
{
LogError( "[OrmTable::updateRecord]", QString( "No index found for : %1" ).arg( fieldName ) );
}
else
{
LogDebug( "[OrmTable::updateRecord]", QString( "Updating field : %1 (Index : %2) with value : %3" )
.arg( fieldName )
.arg( colIndex )
.arg( dataObject->getValue( fieldName ).toString() ) );
this->setData( this->index( 0, colIndex ),
ConversionUtils::convertToType(dataObject->getValue( fieldName ) ,m_qhFieldTypes.value( fieldName )),
Qt::EditRole );
}
}
else
{
LogDebug( "[OrmTable::updateRecord]", QString( "Skipping keyfield %1 ..." ).arg( fieldName ) );
}
}
// Process all changes.
updateSuccess = this->submit();
if( !updateSuccess )
{
LogError( "[OrmTable::updateRecord]",
QString( "Record failed to update, Database gave error : %1").arg( this->lastError().text() ) );
}
this->setFilter( "" );
this->select();
return updateSuccess;
}
}
bool OrmTable::batchUpdate( ORMData* dataObject, const QUuid& sourceId )
{
// If MergedUpdate = true, we have to do an ex- and inclusive update.
// (Inclusive meaning everyting that is mentioned, Exclusive : everything that is not in range..)
// Similar to WHERE <field> NOT IN ( <VALUE1>, <Value2>, <VALUE..> )
QString fieldName;
// Get all fields to update, without the keyfield.
// We only support a single field update at this moment.
for( const auto& fn : dataObject->getFieldNames() )
{
if( fn != dataObject->getKeyField() )
{
fieldName = fn;
break;
}
}
auto timeline = m_qhTimelines[sourceId];
if (!timeline)
{
auto pTimeline = QSharedPointer<Timeline>::create();
timeline.swap(pTimeline);
m_qhTimelines[sourceId] = timeline;
}
OrmBatchChange obc(Timestamp(dataObject->timeStamp()), dataObject->getValue( fieldName ), OrmBatchChange::toSet(dataObject->getValue( dataObject->getKeyField() ).toList()));
auto proposed = timeline->evaluate(obc, static_cast<int>(TableActions::actionMergeUpdate) == dataObject->getDefaultAction());
if (!proposed.valid())
{
LogInfo( "[OrmTable::batchUpdate]", "Proposed update will not be executed" );
return false;
}
dataObject->setValue( dataObject->getKeyField(), OrmBatchChange::toList(proposed.changeSet()));
QString strTable = dataObject->getContainerName();
return m_db.associate(strTable, dataObject->getKeyField(), dataObject->getValue( dataObject->getKeyField() ).toList(), fieldName, dataObject->getValue( fieldName ));
}
bool OrmTable::deCoupleRecords( ORMData* dataObject )
{
QString fieldName;
// Get all fields to update, without the keyfield.
// We only support a single field update at this moment.
for( const auto& fn : dataObject->getFieldNames() )
{
if( fn != dataObject->getKeyField() )
{
fieldName = fn;
break;
}
}
QString strTable = dataObject->getContainerName();
return m_db.disassociate(strTable, dataObject->getKeyField(), dataObject->getValue( dataObject->getKeyField() ).toList(), fieldName, dataObject->getValue( fieldName ));
}
QList<OrmTableRow> OrmTable::readData( const QString& fieldName, const QString& filterExp )
{
int columnIndex = this->fieldIndex( fieldName );
QSortFilterProxyModel filterModel;
filterModel.setSourceModel( this );
if( columnIndex > -1 )
{
filterModel.setFilterKeyColumn( columnIndex );
filterModel.setFilterRegExp( QRegExp( filterExp, Qt::CaseSensitive, QRegExp::Wildcard ) );
}
if( filterModel.rowCount() > 0 )
{
// Looks like there was a result. Pack into a structure..
int rows = filterModel.rowCount();
int columns = filterModel.columnCount();
// For each row and column, iterate the data structure..
for( int _rowCount = 0; _rowCount < rows; _rowCount++ )
{
qDebug() << "=____";
for( int _colCount = 0; _colCount < columns; _colCount++ )
{
qDebug() << filterModel.headerData( _colCount, Qt::Horizontal, Qt::DisplayRole ) << " : "
<< filterModel.data( filterModel.index( _rowCount, _colCount ), Qt::DisplayRole );
}
}
}
return QList<OrmTableRow>();
}
QList<DbRelation *> OrmTable::getRelationsByTableName( const QString& _tableName )
{
QList<DbRelation*> lstResult;
for( auto relName : m_qhRelations.keys() )
{
if( m_qhRelations.value( relName )->foreignTable() == _tableName )
{
lstResult.append( m_qhRelations.value( relName ) );
}
}
return lstResult;
}
QSqlRecord OrmTable::buildSqlRecord( ORMData* tableObject )
{
LogDebug( "[OrmTable::buildSqlRecord]", QString( "Building record with : %1 " ).arg( tableObject->asString() ) );
QStringList fieldNames = tableObject->getFieldNames();
QSqlRecord sqlRecord;
for( const QString& fieldName : fieldNames )
{
if( fieldName != "container" )
{
QSqlField sqlField( fieldName, m_qhFieldTypes.value( fieldName ) );
sqlField.setValue( ConversionUtils::convertToType( tableObject->getValue( fieldName ), // The value
m_qhFieldTypes.value( fieldName ) ) ); // The Type
sqlRecord.append( sqlField );
}
}
return sqlRecord;
}
void OrmTable::setRelatedTable(const QString& _tableName, QSortFilterProxyModel* pTable)
{
m_qhRelTables.insert( _tableName, pTable );
}
QSortFilterProxyModel* OrmTable::getRelatedTable(const QString& _tableName) const
{
return m_qhRelTables.value( _tableName );
}
void OrmTable::saveRelation( const QString& relationName, DbRelation* pRelation )
{
LogDebug( "[OrmTable::saveRelation]", QString( "Saving Relation : %1" ).arg( relationName ) );
m_qhRelations.insert( relationName, pRelation );
}
DbRelation* OrmTable::getRelation( const QString& relationName ) const
{
return m_qhRelations.value( relationName, nullptr );
}
QStringList OrmTable::getRelationNames() const
{
return QStringList( m_qhRelations.keys() );
}
ORMData* OrmTable::solveRelations( const QSharedPointer<ORMRelData>& dataObject )
{
// First we get the Maintable.... This will be our return object.
ORMData* pMainTable = dataObject->getMainTableContainer();
if( nullptr == pMainTable )
{
return nullptr;
}
if( pMainTable->hasLists() )
{
LogWarning( "OrmTable::solveRelations", QString( "Maintable has Lists.... : %1" ).arg( pMainTable->asString() ) );
}
QStringList constraintList = this->getRelationNames();
for( auto constraint : constraintList )
{
const DbRelation* l_relation = QPointer<DbRelation>( this->getRelation( constraint ) );
QString foreignPrimKey;
QString foreignFieldName;
QVariant foreignFieldValue;
LogDebug("[OrmTable::solveRelations]", QString("Get related table %1").arg(l_relation->foreignTable()));
ORMData* pTable = dataObject->getRelatedContainer( l_relation->foreignTable() );
if( nullptr != pTable )
{
// We assume there is only 1 relation between two tables
int numFields = pTable->getFieldCount();
if( 0 == numFields )
{
LogWarning( "[OrmTable::solveRelations]", QString( "No relations found to table : %1 " ).arg( l_relation->foreignTable() ) );
return nullptr;
}
else if( 1 == numFields )
{
foreignPrimKey = l_relation->foreignPrimKey();
foreignFieldName = pTable->getFieldNames().first();
// ===========================================================================================
// L I S T O R S I N G L E V A L U E R E L A T I O N
// ===========================================================================================
foreignFieldValue = pTable->getValue( foreignFieldName );
if( foreignFieldValue.isNull() )
{
// Some constraints are allowed to be absent. For now treat this constraint as such.
// If it is needed afterall it will prevent the write in a later stage
pMainTable->setValue( l_relation->indexColumn(), QVariant() );
continue;
}
// Looks like we have all data collected. Retrieve the Primary key value needed.
// If foreignPrimKey is empty it will be retrieved from the database meta data.
QVariant l_primKeyValue = m_db.getRelationKey( l_relation->foreignTable(), foreignFieldName, foreignFieldValue, foreignPrimKey );
if( !l_primKeyValue.isNull() )
{
// We have a value of the IndexColumn. Complete the mainTable....
pMainTable->setValue( l_relation->indexColumn(), l_primKeyValue );
}
else
{
LogError( "[OrmTable::solveRelations]", QString( "No primary key found for table : %1 " ).arg( l_relation->foreignTable() ) );
return nullptr;
}
// ===========================================================================================
}
else
{
/// @todo : At this moment we don't handle multiple relations. Lotsplitting is requiring this, so it must be implemented as soon as
/// the requirements are defined.
LogError( "[OrmTable::solveRelations]", QString( "multiple relations found to table : %1 " ).arg( l_relation->foreignTable() ) );
return nullptr;
}
}
else
{
LogDebug( "[OrmTable::solveRelations]", QString( "Unresolved constraint %1. We assume all data is provided by the object.." ).arg( constraint ) );
}
}
return pMainTable;
}
int OrmTable::tableFilter( ORMData* dataObject )
{
int nResult = -1;
// Check for a valid pointer.
if( nullptr == dataObject )
{
LogError( "[OrmTable::tableFilter]", QString( "No data structure given." ) );
return 0;
}
// Check if update is the default action
if( static_cast<int>(TableActions::actionUpdate) != dataObject->getDefaultAction() )
{
LogWarning( "[OrmTable::tableFilter]", QString( "Update is not the default action for table : %1" ).arg( dataObject->getContainerName() ) );
return 0;
}
/// @todo : Create an AND / OR filter.
// +----------------------------------+
// | Keyfield | KeyValue | TypeFilter |
// +----------+----------+------------+
// | = 1 | = 1 | equals |
// | = 1 | > 1 | OR | ( WHERE IN ( a, b, c, d...) )
// | > x | > y | AND | ( x equals y)
// | <> KV | <> KF | INVALID |
// +----------+----------+------------+
//
// Check if there is a record that matches our criteria.
const auto keyFields = dataObject->getKeyFieldList();
for (const auto& field : keyFields)
{
if( this->fieldIndex(field) == -1 )
{
LogWarning( "[OrmTable::tableFilter]", QString( "No columnIndex found for : %1" ).arg(field) );
return 0;
}
}
QString strFilter;
const auto numberOfKeys = keyFields.length();
if (0 == numberOfKeys)
{
LogWarning( "[OrmTable::tableFilter]", QString( "No keyField given for table : %1" ).arg( dataObject->getContainerName() ) );
return 0;
}
else if (1 == numberOfKeys)
{
const auto& keyField = *keyFields.begin();
if( dataObject->getValue( keyField ).type() == QVariant::List ) // OR
{
QList<QVariant> tmpList = dataObject->getValue( keyField ).toList();
for( int lstCount = 0; lstCount < tmpList.count(); lstCount++ )
{
if( tmpList.at(lstCount).type() == QVariant::Int )
{
strFilter += keyField + "=" + tmpList.at( lstCount ).toString();
}
else
{
strFilter += "\"" + keyField + "\"='" + tmpList.at( lstCount ).toString().replace("{","").replace("}","") + "'";
}
if( ( lstCount + 1 ) < tmpList.count() )
{
strFilter += " OR ";
}
}
}
else // equals
{
if( dataObject->getValue( keyField ).type() == QVariant::Int )
{
strFilter = "\"" + keyField + "\"="+ dataObject->getValue( keyField ).toString();
}
else
{
strFilter = "\"" + keyField + "\"='" + dataObject->getValue( keyField ).toString().replace("{","").replace("}","") + "'";
}
}
}
else // numberOfKeys > 1, AND
{
// =========================================================
// @todo: Setup a multiple fields filter. For now it is just a multiple values value filter.
auto iter = keyFields.constBegin();
while (keyFields.constEnd() != iter)
{
if( dataObject->getValue( *iter ).type() == QVariant::Int )
{
strFilter += "\"" + *iter + "\"=" + dataObject->getValue( *iter ).toString();
}
else
{
strFilter += "\"" + *iter + "\"='" + dataObject->getValue( *iter ).toString().replace("{","").replace("}","") + "'";
}
++iter;
if( keyFields.end() != iter )
{
strFilter += " AND ";
}
}
}
this->setFilter( strFilter );
LogDebug( "[OrmTable::tableFilter]", QString( "Filter : %1" ).arg( this->filter() ) );
if( this->select() )
{
nResult = this->rowCount();
}
else
{
LogError( "[OrmTable::tableFilter]", QString( "There was a problem executing the query : %1" ).arg( this->selectStatement() ) );
}
// =========================================================
return nResult;
}
int OrmTable::resetFilter()
{
this->setFilter( "" );
this->select();
return rowCount();
}
bool OrmTable::isPackageValid( ORMData* dataObject )
{
if( nullptr == dataObject )
{
LogError( "[OrmTable::isPackageValid]", "No ORMData dataobject passed." );
return false;
}
// Determine the field indices for the proxy filter
auto keyFields = dataObject->getKeyFieldList();
QList<qint32> fieldIndices;
auto iter = keyFields.begin();
while (keyFields.end() != iter)
{
auto idx = this->fieldIndex(*iter);
if (idx > -1)
{
fieldIndices.push_back(idx);
++iter;
}
else
{
LogWarning("[OrmTable::isPackageValid]", QString("Keyfield \"%1\" is unknown. Not using it in filter.").arg(*iter));
iter = keyFields.erase(iter);
}
}
// Create the filter model and set its filter criteria
MultiSortFilterProxyModel oModelFilter;
oModelFilter.setFilterKeyColumns( fieldIndices );
// Build the filter expression.
for (qint32 i = 0; i < fieldIndices.size(); ++i)
{
oModelFilter.setFilterFixedString( fieldIndices[i], QString( "%1" ).arg( dataObject->getValue( keyFields[i] ).toString() ) );
}
// Attach filter to this table model.
oModelFilter.setSourceModel( this );
// If the number of record = 1, we seem to have found our record.
if( 0 == oModelFilter.rowCount() )
{
// Seems like we are good to go.
// This is a first timer...
LogDebug("[OrmTable::isPackageValid]", "No record found");
return true;
}
else if ( 1 == oModelFilter.rowCount() )
{
QModelIndex modIndex = oModelFilter.index( 0, this->fieldIndex( m_recTrackField ) );
unsigned long long tmp_timestamp = oModelFilter.data( modIndex, Qt::DisplayRole ).toULongLong();
if( dataObject->timeStamp() > tmp_timestamp )
{
return true;
}
}
else
{
LogError( "[OrmTable::isPackageValid]", QString( "Multiple records found (%1) for %2 with value %3" )
.arg( oModelFilter.rowCount() )
.arg( dataObject->getKeyField() )
.arg( dataObject->getValue( dataObject->getKeyField() ).toString() ) );
}
return false;
}
bool OrmTable::commit()
{
if( m_db.supportTransactions() )
{
if( m_db.transactionCommit() )
{
return true;
}
else
{
LogWarning( "[OrmTable::commit]", QString( "Transaction failed to commit with error : %1" )
.arg( m_db.getDatabase().driver()->lastError().text() ) );
}
}
else
{
if( m_db.getDatabase().driver()->lastError().driverText().isEmpty() )
{
return true;
}
else
{
LogDebug( "[OrmTable::commit]", QString( "Transaction not supported but last query failed with error : %1" )
.arg( m_db.getDatabase().driver()->lastError().driverText() ) );
}
}
return false;
}