Qt 4.8
qsqlfield.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSql module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsqlfield.h"
43 #include "qatomic.h"
44 #include "qdebug.h"
45 
47 
49 {
50 public:
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 
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 
87  uint ro: 1;
90  int len;
91  int prec;
93  int tp;
94  uint gen: 1;
96 };
97 
98 
169 {
170  d = new QSqlFieldPrivate(fieldName, type);
171  val = QVariant(type);
172 }
173 
179 {
180  d = other.d;
181  d->ref.ref();
182  val = other.val;
183 }
184 
190 {
191  qAtomicAssign(d, other.d);
192  val = other.val;
193  return *this;
194 }
195 
196 
209 bool QSqlField::operator==(const QSqlField& other) const
210 {
211  return ((d == other.d || *d == *other.d)
212  && val == other.val);
213 }
214 
220 {
221  if (!d->ref.deref())
222  delete d;
223 }
224 
231 {
232  detach();
233  d->req = required;
234 }
235 
254 void QSqlField::setLength(int fieldLength)
255 {
256  detach();
257  d->len = fieldLength;
258 }
259 
265 void QSqlField::setPrecision(int precision)
266 {
267  detach();
268  d->prec = precision;
269 }
270 
277 {
278  detach();
279  d->def = value;
280 }
281 
286 {
287  detach();
288  d->tp = type;
289 }
290 
300 {
301  detach();
302  d->gen = gen;
303 }
304 
305 
320 void QSqlField::setValue(const QVariant& value)
321 {
322  if (isReadOnly())
323  return;
324  val = value;
325 }
326 
335 {
336  if (isReadOnly())
337  return;
338  val = QVariant(type());
339 }
340 
348 {
349  detach();
350  d->nm = name;
351 }
352 
358 void QSqlField::setReadOnly(bool readOnly)
359 {
360  detach();
361  d->ro = readOnly;
362 }
363 
381 {
382  return d->nm;
383 }
384 
395 {
396  return d->type;
397 }
398 
405 {
406  detach();
407  d->type = type;
408  if (!val.isValid())
409  val = QVariant(type);
410 }
411 
412 
420 { return d->ro; }
421 
428 bool QSqlField::isNull() const
429 { return val.isNull(); }
430 
434 {
435  qAtomicDetach(d);
436 }
437 
445 {
446  return d->req;
447 }
448 
457 int QSqlField::length() const
458 {
459  return d->len;
460 }
461 
472 {
473  return d->prec;
474 }
475 
482 {
483  return d->def;
484 }
485 
497 int QSqlField::typeID() const
498 {
499  return d->tp;
500 }
501 
509 {
510  return d->gen;
511 }
512 
517 bool QSqlField::isValid() const
518 {
519  return d->type != QVariant::Invalid;
520 }
521 
522 #ifndef QT_NO_DEBUG_STREAM
524 {
525 #ifndef Q_BROKEN_DEBUG_STREAM
526  dbg.nospace() << "QSqlField(" << f.name() << ", " << QVariant::typeToName(f.type());
527  if (f.length() >= 0)
528  dbg.nospace() << ", length: " << f.length();
529  if (f.precision() >= 0)
530  dbg.nospace() << ", precision: " << f.precision();
532  dbg.nospace() << ", required: "
533  << (f.requiredStatus() == QSqlField::Required ? "yes" : "no");
534  dbg.nospace() << ", generated: " << (f.isGenerated() ? "yes" : "no");
535  if (f.typeID() >= 0)
536  dbg.nospace() << ", typeID: " << f.typeID();
537  if (!f.defaultValue().isNull())
538  dbg.nospace() << ", auto-value: \"" << f.defaultValue() << '\"';
539  dbg.nospace() << ')';
540  return dbg.space();
541 #else
542  qWarning("This compiler doesn't support streaming QSqlField to QDebug");
543  return dbg;
544  Q_UNUSED(f);
545 #endif
546 }
547 #endif
548 
565 {
566  return d->autoval;
567 }
568 
575 void QSqlField::setAutoValue(bool autoVal)
576 {
577  detach();
578  d->autoval = autoVal;
579 }
580 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
void qAtomicDetach(T *&d)
This is a helper for the detach method of implicitly shared classes.
Definition: qatomic.h:214
void setType(QVariant::Type type)
Set&#39;s the field&#39;s variant type to type.
Definition: qsqlfield.cpp:404
void setRequiredStatus(RequiredStatus status)
Sets the required status of this field to required.
Definition: qsqlfield.cpp:230
bool isValid() const
Returns true if the field&#39;s variant type is valid; otherwise returns false.
Definition: qsqlfield.cpp:517
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
void setReadOnly(bool readOnly)
Sets the read only flag of the field&#39;s value to readOnly.
Definition: qsqlfield.cpp:358
QVariant val
Definition: qsqlfield.h:107
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
QSqlFieldPrivate(const QString &name, QVariant::Type type)
Definition: qsqlfield.cpp:51
void setName(const QString &name)
Sets the name of the field to name.
Definition: qsqlfield.cpp:347
QSqlField::RequiredStatus req
Definition: qsqlfield.cpp:89
void setPrecision(int precision)
Sets the field&#39;s precision.
Definition: qsqlfield.cpp:265
void setDefaultValue(const QVariant &value)
Sets the default value used for this field to value.
Definition: qsqlfield.cpp:276
QAtomicInt ref
Definition: qsqlfield.cpp:85
bool isReadOnly() const
Returns true if the field&#39;s value is read-only; otherwise returns false.
Definition: qsqlfield.cpp:419
The QString class provides a Unicode character string.
Definition: qstring.h:83
static const char * typeToName(Type type)
Converts the enum representation of the storage type, typ, to its string representation.
Definition: qvariant.cpp:2008
QVariant defaultValue() const
Returns the field&#39;s default value (which may be NULL).
Definition: qsqlfield.cpp:481
QString name() const
Returns the name of the field.
Definition: qsqlfield.cpp:380
~QSqlField()
Destroys the object and frees any allocated resources.
Definition: qsqlfield.cpp:219
void setGenerated(bool gen)
Sets the generated state.
Definition: qsqlfield.cpp:299
bool isAutoValue() const
Returns true if the value is auto-generated by the database, for example auto-increment primary key v...
Definition: qsqlfield.cpp:564
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
RequiredStatus
Specifies whether the field is required or optional.
Definition: qsqlfield.h:59
QDebug operator<<(QDebug dbg, const QSqlField &f)
Definition: qsqlfield.cpp:523
QVariant::Type type
Definition: qsqlfield.cpp:88
QSqlFieldPrivate(const QSqlFieldPrivate &other)
Definition: qsqlfield.cpp:58
const char * name
void clear()
Clears the value of the field and sets it to NULL.
Definition: qsqlfield.cpp:334
void setLength(int fieldLength)
Sets the field&#39;s length to fieldLength.
Definition: qsqlfield.cpp:254
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394
int precision() const
Returns the field&#39;s precision; this is only meaningful for numeric types.
Definition: qsqlfield.cpp:471
int length() const
Returns the field&#39;s length.
Definition: qsqlfield.cpp:457
bool operator==(const QSqlFieldPrivate &other) const
Definition: qsqlfield.cpp:72
void detach()
Definition: qsqlfield.cpp:433
bool isNull() const
Returns true if the field&#39;s value is NULL; otherwise returns false.
Definition: qsqlfield.cpp:428
int typeID() const
Returns the type ID for the field.
Definition: qsqlfield.cpp:497
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195
bool operator==(const QSqlField &other) const
Returns true if the field is equal to other; otherwise returns false.
Definition: qsqlfield.cpp:209
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
void setAutoValue(bool autoVal)
Marks the field as an auto-generated value if autoVal is true.
Definition: qsqlfield.cpp:575
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QSqlField(const QString &fieldName=QString(), QVariant::Type type=QVariant::Invalid)
Constructs an empty field called fieldName of variant type type.
Definition: qsqlfield.cpp:168
bool isGenerated() const
Returns true if the field is generated; otherwise returns false.
Definition: qsqlfield.cpp:508
QSqlField & operator=(const QSqlField &other)
Sets the field equal to other.
Definition: qsqlfield.cpp:189
void setValue(const QVariant &value)
Sets the value of the field to value.
Definition: qsqlfield.cpp:320
RequiredStatus requiredStatus() const
Returns true if this is a required field; otherwise returns false.
Definition: qsqlfield.cpp:444
void setSqlType(int type)
Definition: qsqlfield.cpp:285