You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
#ifndef REPOSITORY_H
|
|
|
|
#define REPOSITORY_H
|
|
|
|
|
|
|
|
#include <QSharedPointer>
|
|
|
|
#include <QxOrm.h>
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Repository
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief create 创建数据
|
|
|
|
* @param ptr 数据
|
|
|
|
* @return 是否创建成功
|
|
|
|
*/
|
|
|
|
static bool create(QSharedPointer<T> ptr)
|
|
|
|
{
|
|
|
|
qx::QxSqlQuery query("ORDER BY id DESC LIMIT 1");
|
|
|
|
T entity;
|
|
|
|
qx::dao::fetch_by_query(query, entity);
|
|
|
|
ptr->id = entity.id + 1;
|
|
|
|
QSqlError err = qx::dao::insert(ptr);
|
|
|
|
return !err.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief update 更新数据
|
|
|
|
* @param ptr 数据
|
|
|
|
* @return 是否更新成功
|
|
|
|
*/
|
|
|
|
static bool update(QSharedPointer<T> ptr)
|
|
|
|
{
|
|
|
|
QSqlError err = qx::dao::update(ptr);
|
|
|
|
return !err.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief deleteById 根据id删除
|
|
|
|
* @param id 数据id
|
|
|
|
* @return 是否删除成功
|
|
|
|
*/
|
|
|
|
static bool deleteById(long id)
|
|
|
|
{
|
|
|
|
QSharedPointer<T> ptr;
|
|
|
|
ptr.reset(new T());
|
|
|
|
ptr->id = id;
|
|
|
|
QSqlError err = qx::dao::delete_by_id(ptr);
|
|
|
|
return !err.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief findById 通过id查询
|
|
|
|
* @param id 主键id
|
|
|
|
* @return 数据智能指针
|
|
|
|
*/
|
|
|
|
static QSharedPointer<T> findById(long id)
|
|
|
|
{
|
|
|
|
QSharedPointer<T> ptr;
|
|
|
|
ptr.reset(new T());
|
|
|
|
ptr->id = id;
|
|
|
|
QSqlError err = qx::dao::fetch_by_id(ptr);
|
|
|
|
if (err.isValid()) {
|
|
|
|
ptr.reset(new T());
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // REPOSITORY_H
|