Code Monkey home page Code Monkey logo

qrorm's Introduction

database structure

class tst_db : public Qters::QrOrm::QrSqlDatabase
{
public:
    static tst_db* instance() {
		static tst_db* instance = new tst_db();
		return instance;
	}
	
private:
    tst_db() : QrSqlDatabase() {
		static QrSqlDatabaseParams param;
		param.driverName = "QSQLITE";
		param.databaseName = "tst_db.db";
		setParams (param);
	}
public:
    virtual ~tst_db() = default;
};

table structure

class tst_table : public Qters::QrOrm::QrSqlTable
{
public:
    Q_OBJECT
    Q_PROPERTY(QString key READ getKey WRITE setKey)
    Q_PROPERTY(QString value READ getValue WRITE setValue)
    Q_PROPERTY(double price READ getPrice WRITE setPrice)

public:
    virtual QString tableName() const { return "tst_table"; }

public:
    double getPrice() const { return price; }
    void setPrice(double value) { price = value; }

    QString getKey() const { return key; }
    void setKey(const QString &value) { key = value; }

    QString getValue() const { return value; }
    void setValue(const QString &value) { this->value = value; }

private:
    QString key;
    QString value;
    double price;
};

create database

QrSqlHelper::makesureDbExist(tst_db::instance())

get database

auto database = tst_db::instance()->getDatabase();

create table

tst_table tableStructure;
QrCreateSql createSql;
QrSqlHelper::call_query(
	createSql.setTable(&tableStructure).getSqlQuery(),
	&database);

insert

tst_table rowRecord;
rowRecord.setKey("2");
rowRecord.setValue("first");
rowRecord.setPrice(1.1);
QrInsertSql insertSql;
QrSqlHelper::call_query(
   insertSql.setTable(&rowRecord).getSqlQuery(),
   &database);

select

tst_table selectTable;
QrSelectSql selectSql;
auto query = 
	selectSql.setTable(&selectTable)
	.columns("*")
	.where("key").isEqualTo("2")
	.limit(2).getSqlQuery();
QrSqlHelper::call_query(query, &database);

qDebug() << "\nsql results:";
query.dumpSqlResult();

qDebug() << "\nobject reflection results:";
QrDbResultsGuard<tst_table> dbObjs;
Q_FOREACH(auto result, dbObjs.fetchResultsBySqlQuery(query)) {
	result->dumpValues();
}

update

tst_table updateTable;
QrUpdateSql updateSql;
QrSqlHelper::call_query(
	updateSql.setTable(&updateTable)
	.colvals(qMakePair<QString, QVariant>("value", "update"))
	.where("key").isEqualTo("3").getSqlQuery(),
	&database);

delete

tst_table deleteTable;
QrDeleteSql deleteSql;
QrSqlHelper::call_query(
	deleteSql.setTable(&deleteTable)
	.where("key").isEqualTo("2")
	.and_("value").isEqualTo("second").getSqlQuery(),
	&database);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.