1
/* This file is part of the KDE project
2
Copyright (C) 2003 Joseph Wenninger <jowenn@kde.org>
3
Copyright (C) 2003-2006 Jarosław Staniek <staniek@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#ifndef KEXIDB_TABLE_H
22
#define KEXIDB_TABLE_H
29
#include "fieldlist.h"
30
#include "schemadata.h"
31
#include "indexschema.h"
32
#include "relationship.h"
38
class LookupFieldSchema;
40
/*! KexiDB::TableSchema provides information about native database table
41
that can be stored using KexiDB database engine.
43
class KEXI_DB_EXPORT TableSchema : public FieldList, public SchemaData
46
typedef QList<TableSchema*> List; //!< Type of tables list
47
typedef QList<TableSchema>::ConstIterator ListIterator; //!< Iterator for tables list
49
TableSchema(const QString & name);
50
TableSchema(const SchemaData& sdata);
54
if \a copyId is true, it's copied as well, otherwise the table id becomes -1,
55
what is usable when we want to store the copy as an independent table. */
56
TableSchema(const TableSchema& ts, bool copyId = true);
58
/*! Copy constructor like \ref TableSchema(const TableSchema&, bool).
59
\a setId is set as the table identifier. This is rarely usable, e.g.
60
in project and data migration routines when we need to need deal with unique identifiers;
61
@see KexiMigrate::performImport(). */
62
TableSchema(const TableSchema& ts, int setId);
64
virtual ~TableSchema();
66
/*! Inserts \a field into a specified position (\a index).
67
'order' property of \a field is set automatically. */
68
virtual FieldList& insertField(uint index, Field *field);
70
/*! Reimplemented for internal reasons. */
71
virtual void removeField(KexiDB::Field *field);
73
/*! \return list of fields that are primary key of this table.
74
This method never returns 0 value,
75
if there is no primary key, empty IndexSchema object is returned.
76
IndexSchema object is owned by the table schema. */
77
IndexSchema* primaryKey() const {
81
/*! Sets table's primary key index to \a pkey. Pass pkey==0 if you want to unassign
82
existing primary key ("primary" property of given IndexSchema object will be
83
cleared then so this index becomes ordinary index, still existing on table indeices list).
85
If this table already has primary key assigned,
86
it is unassigned using setPrimaryKey(0) call.
88
Before assigning as primary key, you should add the index to indices list
89
with addIndex() (this is not done automatically!).
91
void setPrimaryKey(IndexSchema *pkey);
93
const IndexSchema::ListIterator indicesIterator() const {
94
return IndexSchema::ListIterator(m_indices.constBegin());
97
const IndexSchema::List* indices() const {
101
/*! Removes all fields from the list, clears name and all other properties.
102
\sa FieldList::clear() */
103
virtual void clear();
105
/*! \return String for debugging purposes, if \a includeTableName is true,
106
table name, caption, etc. is prepended, else only debug string for
107
the fields are returned. */
108
QString debugString(bool includeTableName);
110
/*! \return String for debugging purposes. Equal to debugString(true). */
111
virtual QString debugString();
113
/*! \return connection object if table was created/retrieved using a connection,
115
Connection* connection() const;
117
/*! \return true if this is KexiDB storage system's table
118
(used internally by KexiDB). This helps in hiding such tables
119
in applications (if desired) and will also enable lookup of system
120
tables for schema export/import functionality.
122
Any internal KexiDB system table's schema (kexi__*) has
123
cleared its SchemaData part, e.g. id=-1 for such table,
124
and no description, caption and so on. This is because
125
it represents a native database table rather that extended Kexi table.
127
isKexiDBSystem()==true implies isNative()==true.
129
By default (after allocation), TableSchema object
130
has this property set to false. */
131
bool isKexiDBSystem() const {
132
return m_isKexiDBSystem;
135
/*! Sets KexiDBSystem flag to on or off. When on, native flag is forced to be on.
136
When off, native flag is not affected.
137
\sa isKexiDBSystem() */
138
void setKexiDBSystem(bool set);
140
/*! \return true if this is schema of native database object,
141
When this is kexiDBSystem table, native flag is forced to be on. */
142
virtual bool isNative() const {
143
return m_native || m_isKexiDBSystem;
146
/* Sets native flag. Does not allow to set this off for system KexiDB table. */
147
virtual void setNative(bool set);
149
/*! \return query schema object that is defined by "select * from <this_table_name>"
150
This query schema object is owned by the table schema object.
151
It is convenient way to get such a query when it is not available otherwise.
152
Always returns non-0. */
153
QuerySchema* query();
155
/*! \return any field not being a part of primary key of this table.
156
If there is no such field, returns 0. */
157
Field* anyNonPKField();
159
/*! Sets lookup field schema \a lookupFieldSchema for \a fieldName.
160
Passing null \a lookupFieldSchema will remove the previously set lookup field.
161
\return true if \a lookupFieldSchema has been added,
162
or false if there is no such field \a fieldName. */
163
bool setLookupFieldSchema(const QString& fieldName, LookupFieldSchema *lookupFieldSchema);
165
/*! \return lookup field schema for \a field.
166
0 is returned if there is no such field in the table or this field has no lookup schema.
167
Note that even id non-zero is returned here, you may want to check whether lookup field's
168
rowSource().name() is empty (if so, the field should behave as there was no lookup field
170
LookupFieldSchema *lookupFieldSchema(const Field& field) const;
172
/*! \overload LookupFieldSchema *TableSchema::lookupFieldSchema( Field& field ) const */
173
LookupFieldSchema *lookupFieldSchema(const QString& fieldName);
175
/*! \return list of lookup field schemas for this table.
176
The order is the same as the order of fields within the table. */
177
const QVector<LookupFieldSchema*>& lookupFieldsList();
180
/*! Automatically retrieves table schema via connection. */
181
TableSchema(Connection *conn, const QString & name = QString());
183
IndexSchema::List m_indices;
185
QPointer<Connection> m_conn;
189
QuerySchema *m_query; //!< cached query schema that is defined by "select * from <this_table_name>"
195
//! Used by some ctors.
198
//! Used by some ctors.
199
void init(const TableSchema& ts, bool copyId);
201
bool m_isKexiDBSystem : 1;
203
friend class Connection;
206
/*! Internal table with a name \a name. Rarely used.
207
Use Connection::createTable() to create a table using this schema.
208
The table will not be visible as user table.
209
For example, 'kexi__blobs' table is created this way by Kexi application. */
210
class KEXI_DB_EXPORT InternalTableSchema : public TableSchema
213
InternalTableSchema(const QString& name);
214
InternalTableSchema(const TableSchema& ts);
215
virtual ~InternalTableSchema();