Go to file
Hamed Masafi 8fa6a3b6d7
Update README.md
2022-04-12 17:09:27 +04:30
.github make master identical with dev 2021-06-25 11:18:02 +04:30
doc update doc 2020-08-12 19:02:32 +04:30
src new submodule commit (#120) 2021-09-06 13:29:28 +04:30
tests fix: column with reserved name in mysql 2021-07-14 12:24:08 +04:30
.gitignore fix: column with reserved name in mysql 2021-07-14 12:24:08 +04:30
.gitmodules fix: relation id in select clause 2021-02-08 18:52:44 +03:30
.qmake.conf revert qmake.conf for invalidate ascii casting 2020-07-30 18:05:58 +04:30
LICENSE Initial commit 2016-03-09 11:05:49 +03:30
README.md Update README.md 2022-04-12 17:09:27 +04:30
btc-qr.png add donate button in readme [skip ci] 2018-10-15 18:05:40 +03:30
deploy.json test module 2020-07-29 13:20:32 +04:30
nut.pro fix: build errors 2020-07-30 13:08:48 +04:30
sync.profile add NutGlobal header 2020-08-12 18:34:59 +04:30

README.md

Nut

CI build GitLicense Codacy Badge

Advanced, Powerful and easy to use ORM for Qt5

Features

  • Easy to use
  • Support PosgtreSQL, MySQL, SQLite and Microsoft Sql Server
  • Automatically create and update database
  • Support for IDE autocomplete. No hard-coding is needed
  • Detecting table joins
  • Support common C++ and Qt-specific types (Full list)
  • Bulk insertation

Qick start

Create table

sampletable.h

#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:

#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

#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

#include "sampledatabase.h"
#include "sampletable.h"

SampleDataBase::SampleDataBase() : Nut::Database()
  , m_items(new Nut::TableSet<SampleTable>(this))
{

}

Sample codes:

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());

Getting started

Help needed!

We need more documentation or wiki. If you can help to improve docs please fork now!

Technology sponsership

Thanks to JetBrains to their useful IDEs.

JetBrains Logo (Main) logoCLion logo