source: trunk/src/sql/kernel/qsqlfield.cpp@ 135

Last change on this file since 135 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 13.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSql module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsqlfield.h"
43#include "qatomic.h"
44#include "qdebug.h"
45
46QT_BEGIN_NAMESPACE
47
48class QSqlFieldPrivate
49{
50public:
51 QSqlFieldPrivate(const QString &name,
52 QVariant::Type type) :
53 ref(1), nm(name), ro(false), type(type), req(QSqlField::Unknown),
54 len(-1), prec(-1), tp(-1), gen(true), autoval(false)
55 {
56 }
57
58 QSqlFieldPrivate(const QSqlFieldPrivate &other)
59 : ref(1),
60 nm(other.nm),
61 ro(other.ro),
62 type(other.type),
63 req(other.req),
64 len(other.len),
65 prec(other.prec),
66 def(other.def),
67 tp(other.tp),
68 gen(other.gen),
69 autoval(other.autoval)
70 {}
71
72 bool operator==(const QSqlFieldPrivate& other) const
73 {
74 return (nm == other.nm
75 && ro == other.ro
76 && type == other.type
77 && req == other.req
78 && len == other.len
79 && prec == other.prec
80 && def == other.def
81 && gen == other.gen
82 && autoval == other.autoval);
83 }
84
85 QAtomicInt ref;
86 QString nm;
87 uint ro: 1;
88 QVariant::Type type;
89 QSqlField::RequiredStatus req;
90 int len;
91 int prec;
92 QVariant def;
93 int tp;
94 uint gen: 1;
95 uint autoval: 1;
96};
97
98
99/*!
100 \class QSqlField
101 \brief The QSqlField class manipulates the fields in SQL database tables
102 and views.
103
104 \ingroup database
105 \ingroup shared
106 \inmodule QtSql
107
108 QSqlField represents the characteristics of a single column in a
109 database table or view, such as the data type and column name. A
110 field also contains the value of the database column, which can be
111 viewed or changed.
112
113 Field data values are stored as QVariants. Using an incompatible
114 type is not permitted. For example:
115
116 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 2
117
118 However, the field will attempt to cast certain data types to the
119 field data type where possible:
120
121 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 3
122
123 QSqlField objects are rarely created explicitly in application
124 code. They are usually accessed indirectly through \l{QSqlRecord}s
125 that already contain a list of fields. For example:
126
127 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 4
128 \dots
129 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 5
130 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 6
131
132 A QSqlField object can provide some meta-data about the field, for
133 example, its name(), variant type(), length(), precision(),
134 defaultValue(), typeID(), and its requiredStatus(),
135 isGenerated() and isReadOnly(). The field's data can be
136 checked to see if it isNull(), and its value() retrieved. When
137 editing the data can be set with setValue() or set to NULL with
138 clear().
139
140 \sa QSqlRecord
141*/
142
143/*!
144 \enum QSqlField::RequiredStatus
145
146 Specifies whether the field is required or optional.
147
148 \value Required The field must be specified when inserting records.
149 \value Optional The fields doesn't have to be specified when inserting records.
150 \value Unknown The database driver couldn't determine whether the field is required or
151 optional.
152
153 \sa requiredStatus()
154*/
155
156/*!
157 Constructs an empty field called \a fieldName of variant type \a
158 type.
159
160 \sa setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
161*/
162QSqlField::QSqlField(const QString& fieldName, QVariant::Type type)
163{
164 d = new QSqlFieldPrivate(fieldName, type);
165}
166
167/*!
168 Constructs a copy of \a other.
169*/
170
171QSqlField::QSqlField(const QSqlField& other)
172{
173 d = other.d;
174 d->ref.ref();
175 val = other.val;
176}
177
178/*!
179 Sets the field equal to \a other.
180*/
181
182QSqlField& QSqlField::operator=(const QSqlField& other)
183{
184 qAtomicAssign(d, other.d);
185 val = other.val;
186 return *this;
187}
188
189
190/*! \fn bool QSqlField::operator!=(const QSqlField &other) const