1
/* This file is part of the KDE project
2
Copyright (C) 2003-2012 Jarosław Staniek <staniek@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#ifndef KEXIDB_FIELDLIST_H
21
#define KEXIDB_FIELDLIST_H
35
/*! Helper class that stores list of fields.
38
class KEXI_DB_EXPORT FieldList
41
/*! Creates empty list of fields. If \a owner is true, the list will be
42
owner of any added field, what means that these field
43
will be removed on the list destruction. Otherwise, the list
44
just points any field that was added.
47
FieldList(bool owner = false);
50
If \a deepCopyFields is true, all fields are deeply copied, else only pointer are copied.
51
Reimplemented in QuerySchema constructor. */
52
FieldList(const FieldList& fl, bool deepCopyFields = true);
54
/*! Destroys the list. If the list owns fields (see constructor),
55
these are also deleted. */
58
/*! \return number of fields in the list. */
59
inline uint fieldCount() const {
60
return m_fields.count();
63
/*! Adds \a field at the and of field list. */
64
FieldList& addField(Field *field);
66
/*! Inserts \a field into a specified position (\a index).
68
Note: You can reimplement this method but you should still call
69
this implementation in your subclass. */
70
virtual FieldList& insertField(uint index, Field *field);
72
/*! Removes field from the field list. Use with care.
74
Note: You can reimplement this method but you should still call
75
this implementation in your subclass. */
76
virtual void removeField(KexiDB::Field *field);
78
/*! \return field id or NULL if there is no such a field. */
79
inline Field* field(uint id) {
80
return m_fields.value(id);
83
/*! \return field with name \a name or NULL if there is no such a field. */
84
virtual Field* field(const QString& name);
86
/*! \return true if this list contains given \a field. */
87
inline bool hasField(Field* field) const {
88
return m_fields.contains(field);
91
/*! \return first occurrence of \a field in the list
92
or -1 if this list does not contain this field. */
93
inline int indexOf(Field* field) const {
94
return m_fields.indexOf(field);
97
/*! \return list of field names for this list. */
98
QStringList names() const;
100
inline Field::ListIterator fieldsIterator() const {
101
return m_fields.constBegin();
103
inline Field::ListIterator fieldsIteratorConstEnd() const {
104
return m_fields.constEnd();
107
inline const Field::List* fields() {
111
/*! \return list of autoincremented fields. The list is owned by this FieldList object. */
112
Field::List* autoIncrementFields();
114
/*! \return true if fields in the list are owned by this list. */
115
inline bool isOwner() const {
116
return m_fields.autoDelete();
119
/*! Removes all fields from the list. */
120
virtual void clear();
122
/*! \return String for debugging purposes. */
123
virtual QString debugString();
125
/*! Shows debug information about all fields in the list. */
128
/*! Creates and returns a list that contain fields selected by name.
129
At least one field (exising on this list) should be selected, otherwise 0 is
130
returned. Returned FieldList object is not owned by any parent (so you need
131
to destroy yourself) and Field objects included in it are not owned by it
132
(but still as before, by 'this' object).
133
Returned list can be usable e.g. as argument for Connection::insertRecord().
134
0 is returned if at least one name cannot be found.
136
FieldList* subList(const QString& n1, const QString& n2 = QString(),
137
const QString& n3 = QString(), const QString& n4 = QString(),
138
const QString& n5 = QString(), const QString& n6 = QString(),
139
const QString& n7 = QString(), const QString& n8 = QString(),
140
const QString& n9 = QString(), const QString& n10 = QString(),
141
const QString& n11 = QString(), const QString& n12 = QString(),
142
const QString& n13 = QString(), const QString& n14 = QString(),
143
const QString& n15 = QString(), const QString& n16 = QString(),
144
const QString& n17 = QString(), const QString& n18 = QString()
147
/*! Like above, but with a QStringList */
148
FieldList* subList(const QStringList& list);
150
/*! Like above, but with a list of field indices */
151
FieldList* subList(const QList<uint>& list);
153
/*! \return a string that is a result of all field names concatenated
154
and with \a separator. This is usable e.g. as argument like "field1,field2"
155
for "INSERT INTO (xxx) ..". The result of this method is effectively cached,
156
and it is invalidated when set of fields changes (e.g. using clear()
158
\a tableAlias, if provided is prepended to each field, so the resulting
159
names will be in form tableAlias.fieldName. This option is used for building
160
queries with joins, where fields have to be spicified without ambiguity.
161
See @ref Connection::selectStatement() for example use.
162
\a drvEscaping can be used to alter default escaping type.
163
\a driver is used for escaping identifiers, if 0, Kexi escaping is used.
165
QString sqlFieldsList(const Driver *driver, const QString& separator = QString::fromLatin1(","),
166
const QString& tableAlias = QString(),
167
int drvEscaping = Driver::EscapeDriver | Driver::EscapeAsNecessary);
169
/*! Like above, but this is convenient static function, so you can pass any \a list here. */
170
static QString sqlFieldsList(Field::List* list, const Driver *driver,
171
const QString& separator = QString::fromLatin1(","), const QString& tableAlias = QString(),
172
int drvEscaping = Driver::EscapeDriver | Driver::EscapeAsNecessary);
174
/*! @internal Renames field \a oldName to \a newName.
175
Do not use this for physical renaming columns. Use AlterTableHandler instead. */
176
void renameField(const QString& oldName, const QString& newName);
179
\overload void renameField(const QString& oldName, const QString& newName) */
180
void renameField(KexiDB::Field *field, const QString& newName);
183
Field::List m_fields;
184
QHash<QString, Field*> m_fields_by_name; //!< Fields collected by name. Not used by QuerySchema.
185
Field::List *m_autoinc_fields;
188
void renameFieldInternal(KexiDB::Field *field, const QString& newNameLower);