summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/fieldlist.h
blob: fd47459eb9c7b8d428808ca8c3b920c1deb7c86b (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
/* This file is part of the KDE project
   Copyright (C) 2003-2007 Jaroslaw Staniek <js@iidea.pl>

   This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KEXIDB_FIELDLIST_H
#define KEXIDB_FIELDLIST_H

#include <qvaluelist.h>
#include <qdict.h>
#include <qstring.h>

#include <kexidb/field.h>
#include <kexidb/driver.h>

namespace KexiDB {

class Connection;

/*! Helper class that stores list of fields.
*/

class KEXI_DB_EXPORT FieldList
{
	public:
		/*! Creates empty list of fields. If \a owner is true, the list will be
		 owner of any added field, what means that these field 
		 will be removed on the list destruction. Otherwise, the list
		 just points any field that was added. 
		 \sa isOwner()
		*/
		FieldList(bool owner = false);

		/*! Copy constructor. 
		 If \a deepCopyFields is true, all fields are deeply copied, else only pointer are copied.
		 Reimplemented in QuerySchema constructor. */
		FieldList(const FieldList& fl, bool deepCopyFields = true);

		/*! Destroys the list. If the list owns fields (see constructor),
		 these are also deleted. */
		virtual ~FieldList();
		
		/*! \return number of fields in the list. */
		inline uint fieldCount() const { return m_fields.count(); }

		/*! Adds \a field at the and of field list. */
		FieldList& addField(Field *field);

		/*! Inserts \a field into a specified position (\a index).
		
		 Note: You can reimplement this method but you should still call 
		 this implementation in your subclass. */
		virtual FieldList& insertField(uint index, Field *field);
		
		/*! Removes field from the field list. Use with care. 
		 
		 Note: You can reimplement this method but you should still call 
		 this implementation in your subclass. */
		virtual void removeField(KexiDB::Field *field);

		/*! \return field id or NULL if there is no such a field. */
		inline Field* field(uint id) { return (id < m_fields.count()) ? m_fields.at(id) : 0; }
		
		/*! \return field with name \a name or NULL if there is no such a field. */
		virtual Field* field(const QString& name);

		/*! \return true if this list contains given \a field. */
		inline bool hasField(const Field* field) { return m_fields.findRef(field)!=-1; }

		/*! \return first occurrence of \a field in the list 
		 or -1 if this list does not contain this field. */
		inline int indexOf(const Field* field) { return m_fields.findRef(field); }

		/*! \return list of field names for this list. */
		QStringList names() const;
		
		Field::ListIterator fieldsIterator() const { return Field::ListIterator(m_fields); }

		inline Field::List* fields() { return &m_fields; }

		/*! \return list of autoincremented fields. The list is owned by this FieldList object. */
		Field::List* autoIncrementFields();

		/*! \return true if fields in the list are owned by this list. */
		inline bool isOwner() const { return m_fields.autoDelete(); }

		/*! Removes all fields from the list. */
		virtual void clear();

		/*! \return String for debugging purposes. */
		virtual QString debugString();
		
		/*! Shows debug information about all fields in the list. */
		void debug();

		/*! Creates and returns a list that contain fields selected by name.
		 At least one field (exising on this list) should be selected, otherwise 0 is
		 returned. Returned FieldList object is not owned by any parent (so you need 
		 to destroy yourself) and Field objects included in it are not owned by it 
		 (but still as before, by 'this' object).
		 Returned list can be usable e.g. as argument for Connection::insertRecord().
		 0 is returned if at least one name cannot be found.
		*/
		FieldList* subList(const QString& n1, const QString& n2 = QString::null, 
			const QString& n3 = QString::null, const QString& n4 = QString::null,
			const QString& n5 = QString::null, const QString& n6 = QString::null,
			const QString& n7 = QString::null, const QString& n8 = QString::null,
			const QString& n9 = QString::null, const QString& n10 = QString::null,
			const QString& n11 = QString::null, const QString& n12 = QString::null,
			const QString& n13 = QString::null, const QString& n14 = QString::null,
			const QString& n15 = QString::null, const QString& n16 = QString::null,
			const QString& n17 = QString::null, const QString& n18 = QString::null
		);

		/*! Like above, but with a QStringList */
		FieldList* subList(const QStringList& list);

		/*! Like above, but with a list of field indices */
		FieldList* subList(const QValueList<uint>& list);

		/*! \return a string that is a result of all field names concatenated 
		 and with \a separator. This is usable e.g. as argument like "field1,field2" 
		 for "INSERT INTO (xxx) ..". The result of this method is effectively cached,
		 and it is invalidated when set of fields changes (e.g. using clear() 
		 or addField()).
		 \a tableAlias, if provided is prepended to each field, so the resulting 
		 names will be in form tableAlias.fieldName. This option is used for building 
		 queries with joins, where fields have to be spicified without ambiguity. 
		 See @ref Connection::selectStatement() for example use.
		 \a drvEscaping can be used to alter default escaping type.
		*/
		QString sqlFieldsList(Driver *driver, const QString& separator = QString::fromLatin1(","), 
			const QString& tableAlias = QString::null,
			int drvEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary);

		/*! Like above, but this is convenient static function, so you can pass any \a list here. */
		static QString sqlFieldsList(Field::List* list, Driver *driver,
			const QString& separator = QString::fromLatin1(","), const QString& tableAlias = QString::null,
			int drvEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary);

		/*! @internal Renames field \a oldName to \a newName. 
		 Do not use this for physical renaming columns. Use AlterTableHandler instead. */
		void renameField(const QString& oldName, const QString& newName);

		/*! @internal 
		 \overload void renameField(const QString& oldName, const QString& newName) */
		void renameField(KexiDB::Field *field, const QString& newName);

	protected:
		Field::List m_fields;
		QDict<Field> m_fields_by_name; //!< Fields collected by name. Not used by QuerySchema.
		Field::List *m_autoinc_fields;
	
	private:
		//! cached
		QString m_sqlFields;
};

} //namespace KexiDB

#endif