Nut/src/defines.h

116 lines
6.2 KiB
C
Raw Normal View History

2016-05-12 14:08:58 +08:00
/**************************************************************************
**
** This file is part of Nut project.
** https://github.com/HamedMasafi/Nut
**
** Nut is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Nut 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************/
#ifndef SYNTAX_DEFINES_H
#define SYNTAX_DEFINES_H
2017-02-01 18:01:21 +08:00
#define NUT_NAMESPACE Nut
2016-05-12 14:08:58 +08:00
#include "defines_p.h"
2017-02-01 18:01:21 +08:00
#include "qglobal.h"
2016-05-12 14:08:58 +08:00
#ifdef NUT_COMPILE_STATIC
# define NUT_EXPORT
#else
# define NUT_EXPORT Q_DECL_EXPORT
#endif
// Database
2017-08-10 23:09:41 +08:00
//TODO: remove minor version
2017-10-07 19:45:20 +08:00
#define NUT_DB_VERSION(version) \
2018-01-08 17:08:10 +08:00
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_DB_VERSION), #version)
2016-05-12 14:08:58 +08:00
#define NUT_DECLARE_TABLE(type, name) \
2016-06-05 20:22:26 +08:00
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #name) \
2016-05-12 14:08:58 +08:00
Q_PROPERTY(type* name READ name) \
2018-01-09 00:59:16 +08:00
Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type*>) name##s READ name##s) \
2016-05-12 14:08:58 +08:00
type* m_##name; \
2018-01-09 00:59:16 +08:00
NUT_WRAP_NAMESPACE(TableSet<type*>) *m_##name##s; \
2016-05-12 14:08:58 +08:00
public: \
static const type _##name; \
type* name() const{ return m_##name; } \
2018-01-09 00:59:16 +08:00
NUT_WRAP_NAMESPACE(TableSet<type*>) *name##s() const { return m_##name##s; }
2016-05-12 14:08:58 +08:00
//Table
#define NUT_DECLARE_FIELD(type, name, read, write) \
Q_PROPERTY(type name READ read WRITE write) \
2016-06-05 20:22:26 +08:00
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name " " __nut_FIELD), #name) \
2016-05-12 14:08:58 +08:00
type m_##name; \
public: \
2017-08-10 23:09:41 +08:00
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) name ## Field(){ \
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) f = NUT_WRAP_NAMESPACE(FieldPhrase<type>)(staticMetaObject.className(), #name); \
2016-05-21 16:09:03 +08:00
return f; \
} \
2016-05-12 14:08:58 +08:00
type read() const{ \
return m_##name; \
} \
void write(type name){ \
m_##name = name; \
propertyChanged(#name); \
}
2016-05-21 16:09:03 +08:00
#define NUT_FOREGION_KEY(type, keytype, name, read, write) \
2016-05-12 14:08:58 +08:00
Q_PROPERTY(type* name READ read WRITE write) \
2016-05-21 16:09:03 +08:00
NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id) \
2016-06-05 20:22:26 +08:00
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name "Id " __nut_FOREGION_KEY), #type) \
2016-05-12 14:08:58 +08:00
type *m_##name; \
public: \
type *read() const { return m_##name ; } \
void write(type *name){ \
m_##name = name; \
}
2016-05-21 16:09:03 +08:00
#define NUT_DECLARE_CHILD_TABLE(type, n) \
2016-05-12 14:08:58 +08:00
private: \
2018-01-09 00:59:16 +08:00
NUT_WRAP_NAMESPACE(TableSet)<type*> *m_##n; \
2016-05-12 14:08:58 +08:00
public: \
2017-02-01 18:01:21 +08:00
static type *n##Table(){ \
2016-05-24 14:47:37 +08:00
static type *f = new type(); \
2017-02-01 18:01:21 +08:00
return f; \
2016-05-24 14:47:37 +08:00
} \
2018-01-09 00:59:16 +08:00
NUT_WRAP_NAMESPACE(TableSet)<type*> *n(){ \
2016-05-12 14:08:58 +08:00
return m_##n; \
}
#define NUT_INDEX(name, field, order)
2016-06-05 20:22:26 +08:00
#define NUT_PRIMARY_KEY(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_PRIMARY_KEY), #x)
#define NUT_AUTO_INCREMENT(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_AUTO_INCREMENT), #x)
2016-05-12 14:08:58 +08:00
#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \
NUT_AUTO_INCREMENT(x)
2016-06-05 20:22:26 +08:00
#define NUT_UNIQUE(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_UNIQUE), #x)
2017-06-12 00:50:43 +08:00
#define NUT_LEN(field, len) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #field " " __nut_LEN), #len)
2016-06-05 20:22:26 +08:00
#define NUT_DEFAULT_VALUE(x, n) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_DEFAULT_VALUE), #n)
#define NUT_NOT_NULL(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_NOT_NULL), "1")
2016-05-12 14:08:58 +08:00
#ifndef NUT_NO_KEYWORDS
2017-08-09 21:19:35 +08:00
# define FROM(x) (x->query())
2016-05-21 16:09:03 +08:00
# define WHERE(x) ->setWhere(x)
2016-05-12 14:08:58 +08:00
# define JOIN(x) ->join(#x)
2017-08-16 23:52:35 +08:00
# define ORDERBY(x) ->orderBy(#x);
# define ORDERBY_DESC(x) ->orderBy(!#x);
2016-05-21 16:09:03 +08:00
2016-05-12 14:08:58 +08:00
# define SELECT() ->toList()
# define COUNT() ->count()
# define DELETE() ->remove()
# define FIRST() ->first()
#endif // NUT_NO_KEYWORDS
#endif // SYNTAX_DEFINES_H