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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
|
/* This file is part of the KDE project
Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
Copyright (C) 2003-2006 Jaroslaw Staniek <js@iidea.pl>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
Original Author: Till Busch <till@bux.at>
Original Project: buX (www.bux.at)
*/
#include "kexitableviewdata.h"
#include <kexiutils/validator.h>
#include <kexidb/field.h>
#include <kexidb/queryschema.h>
#include <kexidb/roweditbuffer.h>
#include <kexidb/cursor.h>
#include <kexidb/utils.h>
#include <kexi.h>
#include <kdebug.h>
#include <klocale.h>
#include <tqapplication.h>
unsigned short KexiTableViewData::charTable[]=
{
#include "chartable.txt"
};
KexiTableViewColumn::KexiTableViewColumn(KexiDB::Field& f, bool owner)
: columnInfo(0)
, visibleLookupColumnInfo(0)
, m_field(&f)
{
isDBAware = false;
m_fieldOwned = owner;
m_captionAliasOrName = m_field->captionOrName();
init();
}
KexiTableViewColumn::KexiTableViewColumn(const TQString& name, KexiDB::Field::Type ctype,
uint cconst,
uint options,
uint length, uint precision,
TQVariant defaultValue,
const TQString& caption, const TQString& description, uint width
)
: columnInfo(0)
, visibleLookupColumnInfo(0)
{
m_field = new KexiDB::Field(
name, ctype,
cconst,
options,
length, precision,
defaultValue,
caption, description, width);
isDBAware = false;
m_fieldOwned = true;
m_captionAliasOrName = m_field->captionOrName();
init();
}
KexiTableViewColumn::KexiTableViewColumn(const TQString& name, KexiDB::Field::Type ctype,
const TQString& caption, const TQString& description)
: columnInfo(0)
, visibleLookupColumnInfo(0)
{
m_field = new KexiDB::Field(
name, ctype,
KexiDB::Field::NoConstraints,
KexiDB::Field::NoOptions,
0, 0,
TQVariant(),
caption, description);
isDBAware = false;
m_fieldOwned = true;
m_captionAliasOrName = m_field->captionOrName();
init();
}
// db-aware
KexiTableViewColumn::KexiTableViewColumn(
const KexiDB::QuerySchema &query, KexiDB::QueryColumnInfo& aColumnInfo,
KexiDB::QueryColumnInfo* aVisibleLookupColumnInfo)
: columnInfo(&aColumnInfo)
, visibleLookupColumnInfo(aVisibleLookupColumnInfo)
, m_field(aColumnInfo.field)
{
isDBAware = true;
m_fieldOwned = false;
//setup column's caption:
if (!columnInfo->field->caption().isEmpty()) {
m_captionAliasOrName = columnInfo->field->caption();
}
else {
//reuse alias if available:
m_captionAliasOrName = columnInfo->alias;
//last hance: use field name
if (m_captionAliasOrName.isEmpty())
m_captionAliasOrName = columnInfo->field->name();
//todo: compute other auto-name?
}
init();
//setup column's readonly flag: true, if
// - it's not from parent table's field, or
// - if the query itself is coming from read-only connection, or
// - if the query itself is stored (i.e. has connection) and lookup column is defined
const bool columnFromMasterTable = query.masterTable()==columnInfo->field->table();
m_readOnly = !columnFromMasterTable
|| (query.connection() && query.connection()->isReadOnly());
// || (query.connection() && (query.connection()->isReadOnly() || visibleLookupColumnInfo));
//! @todo 2.0: remove this when queries become editable ^^^^^^^^^^^^^^
// kdDebug() << "KexiTableViewColumn: query.masterTable()=="
// << (query.masterTable() ? query.masterTable()->name() : "notable") << ", columnInfo->field->table()=="
// << (columnInfo->field->table() ? columnInfo->field->table()->name() : "notable") << endl;
// m_visible = query.isFieldVisible(&f);
}
KexiTableViewColumn::KexiTableViewColumn(bool)
: columnInfo(0)
, visibleLookupColumnInfo(0)
, m_field(0)
{
isDBAware = false;
init();
}
KexiTableViewColumn::~KexiTableViewColumn()
{
if (m_fieldOwned)
delete m_field;
setValidator( 0 );
delete m_relatedData;
}
void KexiTableViewColumn::init()
{
m_relatedData = 0;
m_readOnly = false;
m_visible = true;
m_data = 0;
m_validator = 0;
m_relatedDataEditable = false;
m_headerTextVisible = true;
}
void KexiTableViewColumn::setValidator( KexiUtils::Validator* v )
{
if (m_validator) {//remove old one
if (!m_validator->parent()) //destroy if has no parent
delete m_validator;
}
m_validator = v;
}
void KexiTableViewColumn::setRelatedData(KexiTableViewData *data)
{
if (isDBAware)
return;
if (m_relatedData)
delete m_relatedData;
m_relatedData = 0;
if (!data)
return;
//find a primary key
KexiTableViewColumn::ListIterator it( data->columns );
for (int id = 0;it.current();++it, id++) {
if (it.current()->field()->isPrimaryKey()) {
//found, remember
m_relatedDataPKeyID = id;
m_relatedData = data;
return;
}
}
}
void KexiTableViewColumn::setRelatedDataEditable(bool set)
{
m_relatedDataEditable = set;
}
bool KexiTableViewColumn::isReadOnly() const
{
return m_readOnly || (m_data && m_data->isReadOnly());
}
bool KexiTableViewColumn::acceptsFirstChar(const TQChar& ch) const
{
// the field we're looking at can be related to "visible lookup column"
// if lookup column is present
KexiDB::Field *visibleField = visibleLookupColumnInfo
? visibleLookupColumnInfo->field : m_field;
if (visibleField->isNumericType()) {
if (ch=='.' || ch==',')
return visibleField->isFPNumericType();
if (ch=='-')
return !visibleField->isUnsigned();
if (ch=='+' || ((int)ch>='0' && (int)ch<='9'))
return true;
return false;
}
switch (visibleField->type()) {
case KexiDB::Field::Boolean:
return false;
case KexiDB::Field::Date:
case KexiDB::Field::DateTime:
case KexiDB::Field::Time:
return (int)ch>='0' && (int)ch<='9';
default:;
}
return true;
}
//------------------------------------------------------
KexiTableViewData::KexiTableViewData()
: TQObject()
, KexiTableViewDataBase()
{
init();
}
// db-aware ctor
KexiTableViewData::KexiTableViewData(KexiDB::Cursor *c)
: TQObject()
, KexiTableViewDataBase()
{
init();
m_cursor = c;
m_containsROWIDInfo = m_cursor->containsROWIDInfo();
if (m_cursor && m_cursor->query()) {
const KexiDB::QuerySchema::FieldsExpandedOptions fieldsExpandedOptions
= m_containsROWIDInfo ? KexiDB::QuerySchema::WithInternalFieldsAndRowID
: KexiDB::QuerySchema::WithInternalFields;
m_itemSize = m_cursor->query()->fieldsExpanded( fieldsExpandedOptions ).count();
}
else
m_itemSize = columns.count()+(m_containsROWIDInfo?1:0);
// Allocate KexiTableViewColumn objects for each visible query column
const KexiDB::QueryColumnInfo::Vector fields = m_cursor->query()->fieldsExpanded();
const uint fieldsCount = fields.count();
for (uint i=0;i < fieldsCount;i++) {
KexiDB::QueryColumnInfo *ci = fields[i];
if (ci->visible) {
KexiDB::QueryColumnInfo *visibleLookupColumnInfo = 0;
if (ci->indexForVisibleLookupValue() != -1) {
//Lookup field is defined
visibleLookupColumnInfo = m_cursor->query()->expandedOrInternalField( ci->indexForVisibleLookupValue() );
/* not needed
if (visibleLookupColumnInfo) {
// 2. Create a KexiTableViewData object for each found lookup field
}*/
}
KexiTableViewColumn* col = new KexiTableViewColumn(*m_cursor->query(), *ci, visibleLookupColumnInfo);
addColumn( col );
}
}
}
KexiTableViewData::KexiTableViewData(
const TQValueList<TQVariant> &keys, const TQValueList<TQVariant> &values,
KexiDB::Field::Type keyType, KexiDB::Field::Type valueType)
: TQObject()
, KexiTableViewDataBase()
{
init(keys, values, keyType, valueType);
}
KexiTableViewData::KexiTableViewData(
KexiDB::Field::Type keyType, KexiDB::Field::Type valueType)
{
const TQValueList<TQVariant> empty;
init(empty, empty, keyType, valueType);
}
KexiTableViewData::~KexiTableViewData()
{
emit destroying();
clearInternal();
}
void KexiTableViewData::init(
const TQValueList<TQVariant> &keys, const TQValueList<TQVariant> &values,
KexiDB::Field::Type keyType, KexiDB::Field::Type valueType)
{
init();
KexiDB::Field *keyField = new KexiDB::Field("key", keyType);
keyField->setPrimaryKey(true);
KexiTableViewColumn *keyColumn = new KexiTableViewColumn(*keyField, true);
keyColumn->setVisible(false);
addColumn(keyColumn);
KexiDB::Field *valueField = new KexiDB::Field("value", valueType);
KexiTableViewColumn *valueColumn = new KexiTableViewColumn(*valueField, true);
addColumn(valueColumn);
uint cnt = TQMIN(keys.count(), values.count());
TQValueList<TQVariant>::ConstIterator it_keys = keys.constBegin();
TQValueList<TQVariant>::ConstIterator it_values = values.constBegin();
for (;cnt>0;++it_keys, ++it_values, cnt--) {
KexiTableItem *item = new KexiTableItem(2);
(*item)[0] = (*it_keys);
(*item)[1] = (*it_values);
append( item );
}
}
void KexiTableViewData::init()
{
m_sortedColumn = 0;
m_realSortedColumn = 0;
// m_order = 1;
m_order = 0;
m_type = 1;
m_pRowEditBuffer = 0;
m_cursor = 0;
m_readOnly = false;
m_insertingEnabled = true;
setAutoDelete(true);
columns.setAutoDelete(true);
m_visibleColumnsCount=0;
m_visibleColumnsIDs.resize(100);
m_globalColumnsIDs.resize(100);
m_autoIncrementedColumn = -2;
m_containsROWIDInfo = false;
m_itemSize = 0;
}
void KexiTableViewData::deleteLater()
{
m_cursor = 0;
TQObject::deleteLater();
}
void KexiTableViewData::addColumn( KexiTableViewColumn* col )
{
// if (!col->isDBAware) {
// if (!m_simpleColumnsByName)
// m_simpleColumnsByName = new TQDict<KexiTableViewColumn>(101);
// m_simpleColumnsByName->insert(col->caption,col);//for faster lookup
// }
columns.append( col );
col->m_data = this;
if (m_globalColumnsIDs.size() < columns.count()) {//sanity
m_globalColumnsIDs.resize( m_globalColumnsIDs.size()*2 );
}
if (col->visible()) {
m_visibleColumnsCount++;
if (m_visibleColumnsIDs.size() < m_visibleColumnsCount) {//sanity
m_visibleColumnsIDs.resize( m_visibleColumnsIDs.size()*2 );
}
m_visibleColumnsIDs[ columns.count()-1 ] = m_visibleColumnsCount-1;
m_globalColumnsIDs[ m_visibleColumnsCount-1 ] = columns.count()-1;
}
else {
m_visibleColumnsIDs[ columns.count()-1 ] = -1;
}
m_autoIncrementedColumn = -2; //clear cache;
if (!m_cursor || !m_cursor->query())
m_itemSize = columns.count()+(m_containsROWIDInfo?1:0);
}
TQString KexiTableViewData::dbTableName() const
{
if (m_cursor && m_cursor->query() && m_cursor->query()->masterTable())
return m_cursor->query()->masterTable()->name();
return TQString();
}
void KexiTableViewData::setSorting(int column, bool ascending)
{
if (column>=0 && column<(int)columns.count()) {
m_order = (ascending ? 1 : -1);
}
else {
m_order = 0;
m_sortedColumn = -1;
m_realSortedColumn = -1;
return;
}
// find proper column information for sorting (lookup column points to alternate column with visible data)
const KexiTableViewColumn *tvcol = columns.at(column);
KexiDB::QueryColumnInfo* visibleLookupColumnInfo = tvcol->visibleLookupColumnInfo;
const KexiDB::Field *field = visibleLookupColumnInfo ? visibleLookupColumnInfo->field : tvcol->field();
m_sortedColumn = column;
m_realSortedColumn = tvcol->columnInfo->indexForVisibleLookupValue()!=-1
? tvcol->columnInfo->indexForVisibleLookupValue() : m_sortedColumn;
// setup compare function
const int t = field->type();
if (field->isTextType())
cmpFunc = &KexiTableViewData::cmpStr;
else if (KexiDB::Field::isFPNumericType(t))
cmpFunc = &KexiTableViewData::cmpDouble;
else if (t==KexiDB::Field::BigInteger) {
if (field->isUnsigned())
cmpFunc = &KexiTableViewData::cmpULongLong;
else
cmpFunc = &KexiTableViewData::cmpLongLong;
}
else if (t == KexiDB::Field::Integer && field->isUnsigned())
cmpFunc = &KexiTableViewData::cmpUInt;
else if (t == KexiDB::Field::Boolean || KexiDB::Field::isNumericType(t))
cmpFunc = &KexiTableViewData::cmpInt; //other integers
else if (t == KexiDB::Field::Date)
cmpFunc = &KexiTableViewData::cmpDate;
else if (t == KexiDB::Field::Time)
cmpFunc = &KexiTableViewData::cmpTime;
else if (t == KexiDB::Field::DateTime)
cmpFunc = &KexiTableViewData::cmpDateTime;
else if (t == KexiDB::Field::BLOB)
//! TODO allow users to define BLOB sorting function?
cmpFunc = &KexiTableViewData::cmpBLOB;
else
cmpFunc = &KexiTableViewData::cmpStr; //anything else
}
int KexiTableViewData::compareItems(Item item1, Item item2)
{
return ((this->*cmpFunc) (item1, item2));
}
//! compare NULLs : NULL is smaller than everything
#define CMP_NULLS(item1, item2) \
m_leftTmp = ((KexiTableItem *)item1)->at(m_realSortedColumn); \
if (m_leftTmp.isNull()) \
return -m_order; \
m_rightTmp = ((KexiTableItem *)item2)->at(m_realSortedColumn); \
if (m_rightTmp.isNull()) \
return m_order
#define CAST_AND_COMPARE(casting, item1, item2) \
CMP_NULLS(item1, item2); \
if (m_leftTmp.casting() < m_rightTmp.casting()) \
return -m_order; \
if (m_leftTmp.casting() > m_rightTmp.casting()) \
return m_order; \
return 0
int KexiTableViewData::cmpInt(Item item1, Item item2)
{
CAST_AND_COMPARE(toInt, item1, item2);
}
int KexiTableViewData::cmpUInt(Item item1, Item item2)
{
CAST_AND_COMPARE(toUInt, item1, item2);
}
int KexiTableViewData::cmpLongLong(Item item1, Item item2)
{
CAST_AND_COMPARE(toLongLong, item1, item2);
}
int KexiTableViewData::cmpULongLong(Item item1, Item item2)
{
CAST_AND_COMPARE(toULongLong, item1, item2);
}
int KexiTableViewData::cmpDouble(Item item1, Item item2)
{
CAST_AND_COMPARE(toDouble, item1, item2);
}
int KexiTableViewData::cmpDate(Item item1, Item item2)
{
CAST_AND_COMPARE(toDate, item1, item2);
}
int KexiTableViewData::cmpDateTime(Item item1, Item item2)
{
CAST_AND_COMPARE(toDateTime, item1, item2);
}
int KexiTableViewData::cmpTime(Item item1, Item item2)
{
CAST_AND_COMPARE(toDate, item1, item2);
}
int KexiTableViewData::cmpStr(Item item1, Item item2)
{
CMP_NULLS(item1, item2);
const TQString &as = m_leftTmp.toString();
const TQString &bs = m_rightTmp.toString();
const TQChar *a = as.tqunicode();
const TQChar *b = bs.tqunicode();
if ( a == b )
return 0;
if ( a == 0 )
return -1;
if ( b == 0 )
return 1;
unsigned short au;
unsigned short bu;
int l=TQMIN(as.length(),bs.length());
au = a->tqunicode();
bu = b->tqunicode();
au = (au <= 0x17e ? charTable[au] : 0xffff);
bu = (bu <= 0x17e ? charTable[bu] : 0xffff);
while (l-- && au == bu)
{
a++,b++;
au = a->tqunicode();
bu = b->tqunicode();
au = (au <= 0x17e ? charTable[au] : 0xffff);
bu = (bu <= 0x17e ? charTable[bu] : 0xffff);
}
if ( l==-1 )
return m_order*(as.length()-bs.length());
return m_order*(au-bu);
}
int KexiTableViewData::cmpBLOB(Item item1, Item item2)
{
CMP_NULLS(item1, item2);
return m_leftTmp.toByteArray().size() - m_rightTmp.toByteArray().size();
}
void KexiTableViewData::setReadOnly(bool set)
{
if (m_readOnly == set)
return;
m_readOnly = set;
if (m_readOnly)
setInsertingEnabled(false);
}
void KexiTableViewData::setInsertingEnabled(bool set)
{
if (m_insertingEnabled == set)
return;
m_insertingEnabled = set;
if (m_insertingEnabled)
setReadOnly(false);
}
void KexiTableViewData::clearRowEditBuffer()
{
//init row edit buffer
if (!m_pRowEditBuffer)
m_pRowEditBuffer = new KexiDB::RowEditBuffer(isDBAware());
else
m_pRowEditBuffer->clear();
}
bool KexiTableViewData::updateRowEditBufferRef(KexiTableItem *item,
int colnum, KexiTableViewColumn* col, TQVariant& newval, bool allowSignals,
TQVariant *visibleValueForLookupField)
{
m_result.clear();
if (allowSignals)
emit aboutToChangeCell(item, colnum, newval, &m_result);
if (!m_result.success)
return false;
kdDebug() << "KexiTableViewData::updateRowEditBufferRef() column #"
<< colnum << " = " << newval.toString() << endl;
if (!col) {
kdWarning() << "KexiTableViewData::updateRowEditBufferRef(): column #"
<< colnum << " not found! col==0" << endl;
return false;
}
if (!m_pRowEditBuffer)
m_pRowEditBuffer = new KexiDB::RowEditBuffer(isDBAware());
if (m_pRowEditBuffer->isDBAware()) {
if (!(col->columnInfo)) {
kdWarning() << "KexiTableViewData::updateRowEditBufferRef(): column #"
<< colnum << " not found!" << endl;
return false;
}
m_pRowEditBuffer->insert( *col->columnInfo, newval);
if (col->visibleLookupColumnInfo && visibleValueForLookupField) {
//this is value for lookup table: update visible value as well
m_pRowEditBuffer->insert( *col->visibleLookupColumnInfo, *visibleValueForLookupField);
}
return true;
}
if (!(col->field())) {
kdDebug() << "KexiTableViewData::updateRowEditBufferRef(): column #" << colnum<<" not found!" << endl;
return false;
}
//not db-aware:
const TQString colname = col->field()->name();
if (colname.isEmpty()) {
kdDebug() << "KexiTableViewData::updateRowEditBufferRef(): column #" << colnum<<" not found!" << endl;
return false;
}
m_pRowEditBuffer->insert(colname, newval);
return true;
}
//get a new value (if present in the buffer), or the old one, otherwise
//(taken here for optimization)
#define GET_VALUE if (!val) { \
val = m_cursor \
? m_pRowEditBuffer->at( *it_f.current()->columnInfo, true /* useDefaultValueIfPossible */ ) \
: m_pRowEditBuffer->at( *f ); \
if (!val) \
val = &(*it_r); /* get old value */ \
}
//! @todo if there're multiple views for this data, we need multiple buffers!
bool KexiTableViewData::saveRow(KexiTableItem& item, bool insert, bool repaint)
{
if (!m_pRowEditBuffer)
return true; //nothing to do
//check constraints:
//-check if every NOT NULL and NOT EMPTY field is filled
KexiTableViewColumn::ListIterator it_f(columns);
KexiDB::RowData::ConstIterator it_r = item.constBegin();
int col = 0;
const TQVariant *val;
for (;it_f.current() && it_r!=item.constEnd();++it_f,++it_r,col++) {
KexiDB::Field *f = it_f.current()->field();
val = 0;
if (f->isNotNull()) {
GET_VALUE;
//check it
if (val->isNull() && !f->isAutoIncrement()) {
//NOT NULL violated
m_result.msg = i18n("\"%1\" column requires a value to be entered.")
.arg(f->captionOrName()) + "\n\n" + Kexi::msgYouCanImproveData();
m_result.desc = i18n("The column's constraint is declared as NOT NULL.");
m_result.column = col;
return false;
}
}
if (f->isNotEmpty()) {
GET_VALUE;
if (!f->isAutoIncrement() && (val->isNull() || KexiDB::isEmptyValue( f, *val ))) {
//NOT EMPTY violated
m_result.msg = i18n("\"%1\" column requires a value to be entered.")
.arg(f->captionOrName()) + "\n\n" + Kexi::msgYouCanImproveData();
m_result.desc = i18n("The column's constraint is declared as NOT EMPTY.");
m_result.column = col;
return false;
}
}
}
if (m_cursor) {//db-aware
if (insert) {
if (!m_cursor->insertRow( static_cast<KexiDB::RowData&>(item), *m_pRowEditBuffer,
m_containsROWIDInfo/*also retrieve ROWID*/ ))
{
m_result.msg = i18n("Row inserting failed.") + "\n\n"
+ Kexi::msgYouCanImproveData();
KexiDB::getHTMLErrorMesage(m_cursor, &m_result);
/* if (desc)
*desc =
js: TODO: use KexiMainWindowImpl::showErrorMessage(const TQString &title, KexiDB::Object *obj)
after it will be moved somewhere to kexidb (this will require moving other
showErrorMessage() methods from KexiMainWindowImpl to libkexiutils....)
then: just call: *desc = KexiDB::errorMessage(m_cursor);
*/
return false;
}
}
else { // row updating
// if (m_containsROWIDInfo)
// ROWID = item[columns.count()].toULongLong();
if (!m_cursor->updateRow( static_cast<KexiDB::RowData&>(item), *m_pRowEditBuffer,
m_containsROWIDInfo/*use ROWID*/))
{
m_result.msg = i18n("Row changing failed.") + "\n\n" + Kexi::msgYouCanImproveData();
//! @todo set m_result.column if possible
KexiDB::getHTMLErrorMesage(m_cursor, m_result.desc);
return false;
}
}
}
else {//not db-aware version
KexiDB::RowEditBuffer::SimpleMap b = m_pRowEditBuffer->simpleBuffer();
for (KexiDB::RowEditBuffer::SimpleMap::ConstIterator it = b.constBegin();it!=b.constEnd();++it) {
uint i=0;
for (KexiTableViewColumn::ListIterator it2(columns);it2.current();++it2, i++) {
if (it2.current()->field()->name()==it.key()) {
kdDebug() << it2.current()->field()->name()<< ": "<<item[i].toString()<<" -> "<<it.data().toString()<<endl;
item[i] = it.data();
}
}
}
}
m_pRowEditBuffer->clear();
if (repaint)
emit rowRepaintRequested(item);
return true;
}
bool KexiTableViewData::saveRowChanges(KexiTableItem& item, bool repaint)
{
kdDebug() << "KexiTableViewData::saveRowChanges()..." << endl;
m_result.clear();
emit aboutToUpdateRow(&item, m_pRowEditBuffer, &m_result);
if (!m_result.success)
return false;
if (saveRow(item, false /*update*/, repaint)) {
emit rowUpdated(&item);
return true;
}
return false;
}
bool KexiTableViewData::saveNewRow(KexiTableItem& item, bool repaint)
{
kdDebug() << "KexiTableViewData::saveNewRow()..." << endl;
m_result.clear();
emit aboutToInsertRow(&item, &m_result, repaint);
if (!m_result.success)
return false;
if (saveRow(item, true /*insert*/, repaint)) {
emit rowInserted(&item, repaint);
return true;
}
return false;
}
bool KexiTableViewData::deleteRow(KexiTableItem& item, bool repaint)
{
m_result.clear();
emit aboutToDeleteRow(item, &m_result, repaint);
if (!m_result.success)
return false;
if (m_cursor) {//db-aware
m_result.success = false;
if (!m_cursor->deleteRow( static_cast<KexiDB::RowData&>(item), m_containsROWIDInfo/*use ROWID*/ )) {
m_result.msg = i18n("Row deleting failed.");
/*js: TODO: use KexiDB::errorMessage() for description (desc) as in KexiTableViewData::saveRow() */
KexiDB::getHTMLErrorMesage(m_cursor, &m_result);
m_result.success = false;
return false;
}
}
if (!removeRef(&item)) {
//aah - this shouldn't be!
kdWarning() << "KexiTableViewData::deleteRow(): !removeRef() - IMPL. ERROR?" << endl;
m_result.success = false;
return false;
}
emit rowDeleted();
return true;
}
void KexiTableViewData::deleteRows( const TQValueList<int> &rowsToDelete, bool repaint )
{
Q_UNUSED( repaint );
if (rowsToDelete.isEmpty())
return;
int last_r=0;
first();
for (TQValueList<int>::ConstIterator r_it = rowsToDelete.constBegin(); r_it!=rowsToDelete.constEnd(); ++r_it) {
for (; last_r<(*r_it); last_r++) {
next();
}
remove();
last_r++;
}
//DON'T CLEAR BECAUSE KexiTableViewPropertyBuffer will clear BUFFERS!
//--> emit reloadRequested(); //! \todo more effective?
emit rowsDeleted( rowsToDelete );
}
void KexiTableViewData::insertRow(KexiTableItem& item, uint index, bool repaint)
{
if (!insert( index = TQMIN(index, count()), &item ))
return;
emit rowInserted(&item, index, repaint);
}
void KexiTableViewData::clearInternal()
{
clearRowEditBuffer();
// tqApp->processEvents( 1 );
//TODO: this is time consuming: find better data model
// KexiTableViewDataBase::clear();
const uint c = count();
for (uint i=0; i<c; i++) {
removeLast();
#ifndef KEXI_NO_PROCESS_EVENTS
if (i % 1000 == 0)
tqApp->processEvents( 1 );
#endif
}
}
bool KexiTableViewData::deleteAllRows(bool repaint)
{
clearInternal();
bool res = true;
if (m_cursor) {
//db-aware
res = m_cursor->deleteAllRows();
}
if (repaint)
emit reloadRequested();
return res;
}
int KexiTableViewData::autoIncrementedColumn()
{
if (m_autoIncrementedColumn==-2) {
//find such a column
m_autoIncrementedColumn = 0;
KexiTableViewColumn::ListIterator it(columns);
for (; it.current(); ++it, m_autoIncrementedColumn++) {
if (it.current()->field()->isAutoIncrement())
break;
}
if (!it.current())
m_autoIncrementedColumn = -1;
}
return m_autoIncrementedColumn;
}
void KexiTableViewData::preloadAllRows()
{
if (!m_cursor)
return;
//const uint fcount = m_cursor->fieldCount() + (m_containsROWIDInfo ? 1 : 0);
m_cursor->moveFirst();
for (int i=0;!m_cursor->eof();i++) {
KexiTableItem *item = new KexiTableItem(0);
m_cursor->storeCurrentRow(*item);
// item->debug();
append( item );
m_cursor->moveNext();
#ifndef KEXI_NO_PROCESS_EVENTS
if ((i % 1000) == 0)
tqApp->processEvents( 1 );
#endif
}
}
bool KexiTableViewData::isReadOnly() const
{
return m_readOnly || (m_cursor && m_cursor->connection()->isReadOnly());
}
#include "kexitableviewdata.moc"
|