summaryrefslogtreecommitdiff
path: root/noncore/apps/tableviewer/db/common.h
blob: bb0a9533641795eb768e87eb926fb0d1281a6e64 (plain)
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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/


/* This file represents shared data structures that will be passed
 * around often.
 */
#ifndef __SHAREDDATA_H__
#define __SHAREDDATA_H__

// TODO rename this to a sensable class name

#include <qvector.h>
#include <qstring.h>
#include <qdatetime.h>
#include <qcstring.h>
#include <qdatastream.h>
#include <qintdict.h>

class DBStore;

/* helper classes to common classes */
class QStringVector : public QVector<QString> 
{ 
public: 
    int compareItems(Item a, Item b);
};

/* in QT 2.3, dates and times not supported int QVariant.  So.....
 * for now use my special Variant type which is basically identical
 * except that does it for my types.  TODO replace with QVariant when
 * qvariant supports all the types we require */

class TVVariantPrivate;

class TVVariant 
{
public:
    enum KeyType {
        Invalid = 0,
        Int,     
        String, 
        Date, 
        Time,
    };

    TVVariant();
    ~TVVariant();

    TVVariant(const TVVariant&);
    TVVariant(QDataStream&);

    TVVariant(const QString &);
    TVVariant(const int);
    TVVariant(const QDate &);
    TVVariant(const QTime &);

    TVVariant& operator=(const TVVariant& );
    bool operator==(const TVVariant&) const;
    bool operator!=(const TVVariant&) const;
    bool operator<(const TVVariant&) const;
    bool operator>(const TVVariant&) const;

    bool closer(TVVariant, TVVariant);
    bool close(TVVariant);

    KeyType type() const;
    const QString typeName() const;
    int numTypes() const;

    const QString typeName(KeyType) const;
    bool canCast(KeyType) const;
    bool isValid() const;
    void clear();

    const QString toString() const;
    const QDate toDate() const;
    const QTime toTime() const;
    int toInt() const;

    QString& asString();
    QDate& asDate();
    QTime& asTime();
    int& asInt();

    void load(QDataStream&);
    void save(QDataStream&) const;

    static const QString typeToName(KeyType typ);
    static KeyType nameToType(const QString &);
private:
    void detach();

    TVVariantPrivate *d;
};

class TVVariantPrivate : public QShared
{
    public: 
        TVVariantPrivate(); 
        TVVariantPrivate(TVVariantPrivate *); 

        ~TVVariantPrivate(); 
        
        void clear(); 
        
        TVVariant::KeyType typ; 
        
        union { 
            int i; 
            void *ptr; 
        } value;
};

inline TVVariant::KeyType TVVariant::type() const
{
    return d->typ;
}

inline bool TVVariant::isValid() const
{
    return (d->typ != Invalid);
}

inline int TVVariant::numTypes() const
{
    return 4;
}

class Key {
public:
    Key();
    Key(QString name, TVVariant example, int flags = 0);
	Key(const Key &);
    Key& operator=(const Key& );

    QString name() const;
    TVVariant example() const;
    TVVariant::KeyType type() const;
    int flags() const;

    void setName(const QString &);
    void setExample(const TVVariant &);
    void setFlags(int);

    bool delFlag() const;
    bool newFlag() const;

    void setDelFlag(bool);
    void setNewFlag(bool);

private:
    QString kname;
    TVVariant kexample;
    int kflags;
};

class KeyList : public QIntDict<Key> {
public:
    KeyList();
    KeyList(const KeyList&);

    ~KeyList();

	bool operator!=(const KeyList &);

    int getNumFields() const;

    int addKey(QString KeyName, TVVariant example);
    int addKey(QString KeyName, TVVariant::KeyType type);

    TVVariant getKeyExample(int ) const;
    void setKeyExample(int, TVVariant e);

    QString getKeyName(int i) const;
    void setKeyName(int i, const QString &n);

    TVVariant::KeyType getKeyType(int i) const;
    void setKeyType(int i, TVVariant::KeyType);

    int getKeyIndex(QString q) const;

    int getKeyFlags(int i) const;
    void setKeyFlags(int i, int flag);

    /* Below should be abstracted a bit more */
    bool checkNewFlag(int i) const;
    void setNewFlag(int i, bool f);
    bool checkDeleteFlag(int i) const;
    void setDeleteFlag(int i, bool f);

    bool validIndex(int) const;
};

class KeyListIterator : public QIntDictIterator<Key>
{
public:
    KeyListIterator(const KeyList &k) : QIntDictIterator<Key>(k) {};
};

/* TODO start using this */
class DataElem {
public:
    DataElem(DBStore *container);
    ~DataElem();

    int getNumFields() const; 
    KeyList getKeys() const;

    bool hasValidValue(int) const; 
    bool hasValidValue(QString) const; 
    TVVariant::KeyType getFieldType(int) const; 
    TVVariant::KeyType getFieldType(QString) const; 
    TVVariant getField(int) const;
    TVVariant getField(QString) const;

    void setField(int, QString);
    void setField(int, TVVariant);
    void setField(QString, QString);
    void setField(QString, TVVariant);
    void unsetField(int);
    void unsetField(QString);

    QString toQString() const;
    QString toQString(int i) const;
    QString toSortableQString(int i) const;

    /* compare functions */
    bool lessThan(int i, TVVariant) const;
    bool moreThan(int i, TVVariant) const;
    bool equalTo(int i, TVVariant) const;
    bool contains(int i, TVVariant) const;
    bool startsWith(int i, TVVariant) const;
    bool endsWith(int i, TVVariant) const;

    /* class functions... Compare is based of the primary key, which
       is determined by the containing DBStores of each element. */
    static int compare(const TVVariant, const TVVariant, int i);

    /* False, second element's primary key is closer to target. 
     * True, first element's primary key is a closer match to target */
    static bool closer(DataElem*, DataElem *, TVVariant, int column);
private:
    QIntDict<TVVariant> values;
    DBStore *contained;
};

typedef struct _TableState {
    int current_column;
    KeyList *kRep;
    DataElem *current_elem;
} TableState;

/* Stream functions */
#ifndef QT_NO_DATASTREAM
Q_EXPORT QDataStream &operator<<( QDataStream &, const KeyList & );
Q_EXPORT QDataStream &operator<<( QDataStream &, const DataElem & );
Q_EXPORT QDataStream &operator>>( QDataStream &, KeyList & );
Q_EXPORT QDataStream &operator>>( QDataStream &, DataElem & );


Q_EXPORT QDataStream &operator>>( QDataStream &, TVVariant & );
Q_EXPORT QDataStream &operator<<( QDataStream &, const TVVariant & );
Q_EXPORT QDataStream &operator>>( QDataStream &, TVVariant::KeyType& );
Q_EXPORT QDataStream &operator<<( QDataStream &, const TVVariant::KeyType& );
#endif

#endif