QDjango
Loading...
Searching...
No Matches
QDjangoMetaModel.h
1/*
2 * Copyright (C) 2010-2015 Jeremy Lainé
3 * Contact: https://github.com/jlaine/qdjango
4 *
5 * This file is part of the QDjango Library.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17
18#ifndef QDJANGOMETAMODEL_H
19#define QDJANGOMETAMODEL_H
20
21#include <QMap>
22#include <QSharedDataPointer>
23#include <QVariant>
24
25#include "QDjango_p.h"
26
27class QDjangoMetaFieldPrivate;
28class QDjangoMetaModelPrivate;
29
34class QDJANGO_DB_EXPORT QDjangoMetaField
35{
36public:
41
42 QString column() const;
43 bool isAutoIncrement() const;
44 bool isBlank() const;
45 bool isNullable() const;
46 bool isUnique() const;
47 bool isValid() const;
48 QString name() const;
49 int maxLength() const;
50 QVariant toDatabase(const QVariant &value) const;
51
52private:
53 QSharedDataPointer<QDjangoMetaFieldPrivate> d;
54 friend class QDjangoMetaModel;
55};
56
64class QDJANGO_DB_EXPORT QDjangoMetaModel
65{
66public:
67 QDjangoMetaModel(const QMetaObject *model = 0);
71
72 bool isValid() const;
73
74 bool createTable() const;
75 QStringList createTableSql() const;
76 bool dropTable() const;
77
78 void load(QObject *model, const QVariantList &props, int &pos) const;
79 bool remove(QObject *model) const;
80 bool save(QObject *model) const;
81
82 QObject *foreignKey(const QObject *model, const char *name) const;
83 void setForeignKey(QObject *model, const char *name, QObject *value) const;
84
85 QString className() const;
86 QDjangoMetaField localField(const char *name) const;
87 QList<QDjangoMetaField> localFields() const;
88 QMap<QByteArray, QByteArray> foreignFields() const;
89 QByteArray primaryKey() const;
90 QString table() const;
91
92private:
93 QSharedDataPointer<QDjangoMetaModelPrivate> d;
94};
95
96#endif
The QDjangoMetaField class holds the database schema for a field.
Definition QDjangoMetaModel.h:35
QString column() const
Returns the database column for this meta field.
Definition QDjangoMetaModel.cpp:136
bool isNullable() const
Returns true if this field is nullable.
Definition QDjangoMetaModel.cpp:144
bool isBlank() const
Returns true if this field can be empty.
Definition QDjangoMetaModel.cpp:176
QVariant toDatabase(const QVariant &value) const
Transforms the given field value for database storage.
Definition QDjangoMetaModel.cpp:200
int maxLength() const
Returns the max length of this field.
Definition QDjangoMetaModel.cpp:192
QString name() const
Returns name of this meta field.
Definition QDjangoMetaModel.cpp:184
bool isUnique() const
Returns true if this field is unique.
Definition QDjangoMetaModel.cpp:168
QDjangoMetaField()
Constructs a new QDjangoMetaField.
Definition QDjangoMetaModel.cpp:104
bool isAutoIncrement() const
Returns true if this field is auto incremented.
Definition QDjangoMetaModel.cpp:160
QDjangoMetaField & operator=(const QDjangoMetaField &other)
Assigns other to this meta field.
Definition QDjangoMetaModel.cpp:127
bool isValid() const
Returns true if this is a valid field.
Definition QDjangoMetaModel.cpp:152
QList< QDjangoMetaField > localFields() const
Returns the list of local fields.
Definition QDjangoMetaModel.cpp:772
QDjangoMetaModel(const QMetaObject *model=0)
Constructs a new QDjangoMetaModel by inspecting the given meta model.
Definition QDjangoMetaModel.cpp:245
QObject * foreignKey(const QObject *model, const char *name) const
Retrieves the QDjangoModel pointed to by the given foreign-key.
Definition QDjangoMetaModel.cpp:667
bool remove(QObject *model) const
Removes the given model instance from the database.
Definition QDjangoMetaModel.cpp:796
void setForeignKey(QObject *model, const char *name, QObject *value) const
Sets the QDjangoModel pointed to by the given foreign-key.
Definition QDjangoMetaModel.cpp:702
QDjangoMetaField localField(const char *name) const
Return the local field with the specified name.
Definition QDjangoMetaModel.cpp:759
QString table() const
Returns the name of the database table.
Definition QDjangoMetaModel.cpp:788
bool dropTable() const
Drops the database table for this QDjangoMetaModel.
Definition QDjangoMetaModel.cpp:650
QByteArray primaryKey() const
Returns the name of the primary key for the current QDjangoMetaModel.
Definition QDjangoMetaModel.cpp:780
QDjangoMetaModel & operator=(const QDjangoMetaModel &other)
Assigns other to this meta model.
Definition QDjangoMetaModel.cpp:414
bool createTable() const
Creates the database table for this QDjangoMetaModel.
Definition QDjangoMetaModel.cpp:425
bool isValid() const
Determine whether this is a valid model, or just default constructed.
Definition QDjangoMetaModel.cpp:406
QStringList createTableSql() const
Returns the SQL queries to create the database table for this QDjangoMetaModel.
Definition QDjangoMetaModel.cpp:439
bool save(QObject *model) const
Saves the given model instance to the database.
Definition QDjangoMetaModel.cpp:809
QMap< QByteArray, QByteArray > foreignFields() const
Returns the foreign field mapping.
Definition QDjangoMetaModel.cpp:751
void load(QObject *model, const QVariantList &props, int &pos) const
Loads the given properties into a model instance.
Definition QDjangoMetaModel.cpp:728