2016-05-12 14:32:50 +08:00
|
|
|
# Nut
|
2016-05-12 14:25:07 +08:00
|
|
|
|
2020-07-27 21:14:53 +08:00
|
|
|
|
|
|
|
|
[](https://github.com/HamedMasafi/Nut/actions)
|
2019-03-10 00:09:33 +08:00
|
|
|
[](https://gitlicense.com/license/hamedmasafi/nut)
|
2019-08-04 22:11:16 +08:00
|
|
|
[](https://www.codacy.com/app/HamedMasafi/Nut?utm_source=github.com&utm_medium=referral&utm_content=HamedMasafi/Nut&utm_campaign=Badge_Grade)
|
2018-01-08 17:35:26 +08:00
|
|
|
|
2016-05-12 14:32:50 +08:00
|
|
|
## Advanced, Powerful and easy to use ORM for Qt5
|
2016-05-12 14:25:07 +08:00
|
|
|
|
2020-02-11 03:11:27 +08:00
|
|
|
## Features
|
2019-08-09 03:34:13 +08:00
|
|
|
- Easy to use
|
|
|
|
|
- Support PosgtreSQL, MySQL, SQLite and Microsoft Sql Server
|
|
|
|
|
- Automatically create and update database
|
2020-02-11 03:11:27 +08:00
|
|
|
- Support for IDE autocomplete. No hard-coding is needed
|
|
|
|
|
- Detecting table joins
|
|
|
|
|
- Support common C++ and Qt-specific types ([Full list](doc/datatypes.md))
|
2021-12-13 23:02:42 +08:00
|
|
|
- Bulk insertation
|
|
|
|
|
|
|
|
|
|
## Qick start
|
|
|
|
|
### Create table
|
|
|
|
|
|
|
|
|
|
#### sampletable.h
|
2021-12-13 23:07:35 +08:00
|
|
|
```cpp
|
2021-12-13 23:02:42 +08:00
|
|
|
#ifndef SAMPLETABLE_H
|
|
|
|
|
#define SAMPLETABLE_H
|
|
|
|
|
|
|
|
|
|
#include <QtNut/table.h>
|
|
|
|
|
|
|
|
|
|
class SampleTable : public Nut::Table
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_PROPERTY(int id READ id WRITE setId NOTIFY idChanged)
|
|
|
|
|
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
|
|
|
|
|
|
|
|
|
|
int m_id;
|
|
|
|
|
QString m_name;
|
|
|
|
|
|
|
|
|
|
// BEGIN OF NUT MACROS
|
|
|
|
|
NUT_PRIMARY_KEY(id)
|
|
|
|
|
NUT_FIELD(int, id)
|
|
|
|
|
NUT_FIELD(QString, name)
|
|
|
|
|
//END OF NUT MACROS
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit SampleTable(QObject *parent = nullptr);
|
|
|
|
|
|
|
|
|
|
int id() const;
|
|
|
|
|
QString name() const;
|
|
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
|
void setId(int id);
|
|
|
|
|
void setName(QString name);
|
|
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
|
void idChanged(int id);
|
|
|
|
|
void nameChanged(QString name);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // SAMPLETABLE_H
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### sampletable.cpp:
|
2021-12-13 23:07:35 +08:00
|
|
|
```cpp
|
2021-12-13 23:02:42 +08:00
|
|
|
#include "sampletable.h"
|
|
|
|
|
|
|
|
|
|
SampleTable::SampleTable(QObject *parent) : Nut::Table(parent)
|
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SampleTable::id() const
|
|
|
|
|
{
|
|
|
|
|
return m_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SampleTable::name() const
|
|
|
|
|
{
|
|
|
|
|
return m_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SampleTable::lastName() const
|
|
|
|
|
{
|
|
|
|
|
return m_lastName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SampleTable::setId(int id)
|
|
|
|
|
{
|
|
|
|
|
if (m_id == id)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_id = id;
|
|
|
|
|
Q_EMIT idChanged(m_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SampleTable::setName(QString name)
|
|
|
|
|
{
|
|
|
|
|
if (m_name == name)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_name = name;
|
|
|
|
|
Q_EMIT nameChanged(m_name);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Create database
|
|
|
|
|
#### sampledatabase.h
|
2021-12-13 23:07:35 +08:00
|
|
|
```cpp
|
2021-12-13 23:02:42 +08:00
|
|
|
#ifndef SAMPLEDATABASE_H
|
|
|
|
|
#define SAMPLEDATABASE_H
|
|
|
|
|
|
|
|
|
|
#include <QtNut/Database>
|
|
|
|
|
|
|
|
|
|
class SampleTable;
|
|
|
|
|
class SampleDataBase : public NUT_WRAP_NAMESPACE(Database)
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
NUT_DB_VERSION(1)
|
|
|
|
|
NUT_DECLARE_TABLE(SampleTable, items)
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SampleDataBase();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // SAMPLEDATABASE_H
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
sampledatabase.cpp
|
2021-12-13 23:07:35 +08:00
|
|
|
```cpp
|
2021-12-13 23:02:42 +08:00
|
|
|
#include "sampledatabase.h"
|
|
|
|
|
#include "sampletable.h"
|
|
|
|
|
|
|
|
|
|
SampleDataBase::SampleDataBase() : Nut::Database()
|
|
|
|
|
, m_items(new Nut::TableSet<SampleTable>(this))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Sample codes:
|
2021-12-13 23:07:35 +08:00
|
|
|
```cpp
|
2021-12-13 23:02:42 +08:00
|
|
|
qRegisterMetaType<SampleTable*>();
|
|
|
|
|
qRegisterMetaType<SampleDataBase*>();
|
|
|
|
|
|
|
|
|
|
db.setDriver("QSQLITE");
|
|
|
|
|
db.setDatabaseName("data.sb");
|
|
|
|
|
|
|
|
|
|
if (db.open()) {
|
|
|
|
|
qFatal() << "Unable to open the database";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read add rows from database
|
|
|
|
|
auto list = db.items()->query().toList();
|
|
|
|
|
// list is QList<QSharedPointer<SampleTable>>
|
|
|
|
|
|
|
|
|
|
//Select all the people named David.
|
|
|
|
|
auto onlyDavids = db.items()->query().where(SampleTable::nameField() == "David").toList();
|
|
|
|
|
onlyDavids.at(0)->setName("John"); // change him name to John
|
|
|
|
|
db.saveChanges(); // save changed to the database
|
|
|
|
|
|
|
|
|
|
// Remove all Johns from the database
|
|
|
|
|
db.items()->query().where(SampleTable::nameField() == "John").remove();
|
|
|
|
|
|
|
|
|
|
// Select rows with folowwing ids: 1, 4, 5, 6
|
|
|
|
|
db.items()->query().where(SampleTable::idField().in({1, 4, 5, 6}));
|
|
|
|
|
|
|
|
|
|
// Select from id 10 to 20
|
|
|
|
|
db.items()->query().where(SampleTable::idField().between(10, 20));
|
|
|
|
|
|
|
|
|
|
// Some other samples
|
|
|
|
|
db.items()->query().where(SampleTable::idField() <= 7);
|
|
|
|
|
db.items()->query().where(SampleTable::idField() > 0 || SampleTable::idField() == -3);
|
|
|
|
|
db.items()->query().where(SampleTable::idField() > 10 && (SampleTable::nameField() == "John" || SampleTable::nameField() == "Jim"));
|
|
|
|
|
|
|
|
|
|
// Select biggest id
|
|
|
|
|
auto biggestId = db.items()->query().max(SampleTable::idField());
|
|
|
|
|
```
|
2016-05-12 14:32:50 +08:00
|
|
|
|
2020-02-11 03:11:27 +08:00
|
|
|
## Getting started
|
2019-08-09 03:34:13 +08:00
|
|
|
- [Sample codes](doc/start.md)
|
2019-12-09 14:22:03 +08:00
|
|
|
- [Shared pointer and regular mode](doc/sharedpointer.md)
|
|
|
|
|
- [Create database class](doc/database.md)
|
|
|
|
|
- [Create table class](doc/table.md)
|
|
|
|
|
- [Using queries](doc/query.md)
|
|
|
|
|
- [Supported data types](doc/datatypes.md)
|
2016-05-24 14:59:29 +08:00
|
|
|
|
2020-01-08 15:08:14 +08:00
|
|
|
## Help needed!
|
|
|
|
|
We need more documentation or wiki. If you can help to improve docs please fork now!
|
|
|
|
|
|
2018-10-15 22:34:50 +08:00
|
|
|
### Donate
|
2020-02-11 03:11:27 +08:00
|
|
|
Bitcoin address: 1Dn1WHKkaxanXe4cTGDk4cFRRABxLUpEVj
|
2018-10-15 22:35:56 +08:00
|
|
|
|
|
|
|
|
|
2020-02-11 03:11:27 +08:00
|
|
|

|
2018-10-15 22:34:50 +08:00
|
|
|
|
2021-12-13 23:02:42 +08:00
|
|
|
For more information read [Wiki](wiki).
|