上一篇 windows下静态使用QxOrm框架并使用泛型编程 (一)
这篇开始讲实际编程并且抽象化,让代码书写更少。
为了模块划分我在Demo文件夹下新增了一个SQLModule文件夹,在此文件夹下又新增QxHandler,QxMapped,QxObject三个文件夹,QxObject是用来存储数据对象类的一个文件夹,QxMapped是存储键值映射的文件夹,QxHandler是操作数据库的实际句柄类文件夹。
QxObject类
User.h
#ifndef USER_H
#define USER_H
#include "common.h"
#include
#include
class User
{
QX_REGISTER_FRIEND_CLASS(User) //用于将类注册到QxOrm的宏定义
public:
User();
long getId();
QString getName();
int getAge();
public:
QJsonObject toJsonObject();
bool analyzeJson(QByteArray &json);
void analyzeJson(QJsonObject &json);
private:
long m_lId;
QString m_sName;
int m_nAge;
};
//QX_REGISTER_PRIMARY_KEY(User, int) //主键不是整数类型的时候使用
QX_REGISTER_HPP_IMPORT_DLL(User, qx::trait::no_base_class_defined, DATABASE_VERSION) //用于将类注册到QxOrm的宏定义 第一个参数为类名 第二个为默认值 第三个为数据库版本
#endif // USER_H
User.cpp
#include "User.h"
QX_REGISTER_CPP_IMPORT_DLL(User) //用于将类注册到QxOrm的宏定义
namespace qx //用于将类注册到QxOrm的方法
{
template <> void register_class(QxClass
& t) {
qx::IxDataMember * pData = NULL; Q_UNUSED(pData);
qx::IxSqlRelation * pRelation = NULL; Q_UNUSED(pRelation);
qx::IxFunction * pFct = NULL; Q_UNUSED(pFct);
qx::IxValidator * pValidator = NULL; Q_UNUSED(pValidator);
// Register
pData =t.id(& User::m_lId, "id",DATABASE_VERSION);
pData =t.data(& User::m_nAge, "name",DATABASE_VERSION);
pData =t.data(& User::m_nAge, "age",DATABASE_VERSION);
qx::QxValidatorX
* pAllValidator = t.getAllValidator(); Q_UNUSED(pAllValidator); }
}
User::User()
:m_lId(0)
,m_sName("")
,m_nAge(0)
{
}
long User::getId()
{
return m_lId;
}
QString User::getName()
{
return m_sName;
}
int User::getAge()
{
return m_nAge;
}
QJsonObject User::toJsonObject()
{
QJsonObject subObject;
subObject.insert("Name",m_sName);
subObject.insert("Age",m_nAge);
return subObject;
}
bool User::analyzeJson(QByteArray &json)
{
bool success=false;
QJsonParseError error;
QJsonDocument document=QJsonDocument::fromJson(json,&error);
if (!document.isNull() && (error.error == QJsonParseError::NoError))
{
QJsonObject rootObject =document.object();
this->analyzeJson(rootObject);
success=true;
}
else
{
success=false;
qDebug()<
qDebug("LockoutFun analyze falied");
}
return success;
}
void User::analyzeJson(QJsonObject &json)
{
QJsonObject rootObject = json.value("User").toObject();
m_sName=rootObject.value(QString("Name")).toString();
m_nAge=rootObject.value(QString("Age")).toInt();
}
QxMapped
IMapped 映射抽象类
#ifndef IMAPPED_H
#define IMAPPED_H
/**
* @author tianmin
* @brief The IMapped class 抽象出来的映射类
* @date 2019-07-22
*/
#include
#include
#include
class IMapped
{
public:
//字符map初始化
virtual void initMapped() = 0;
//获取QString列表
virtual QStringList getListMapped(int en) =0;
//获取QString 字段
virtual QString getMapped(int en) = 0;
virtual ~IMapped(){}
};
#endif // IMAPPED_H
UserMapped 实际使用的类
UserMapped .h
#ifndef USERMAPPED_H
#define USERMAPPED_H
#include "IMapped.h"
namespace USER {
enum USER_MAPPED
{
EN_ID = 0x00000001,
EN_NAME = 0x00000002,
EN_AGE = 0x00000004,
EN_ALL = 0xFFFFFFFF
};
}
class UserMapped:public IMapped
{
public:
UserMapped();
virtual ~UserMapped();
public:
virtual void initMapped();
virtual QStringList getListMapped(int en);
virtual QString getMapped(int en);
private:
QMap
m_map; };
#endif // USERMAPPED_H
UserMapped.cpp
#include "UserMapped.h"
UserMapped::UserMapped()
{
}
UserMapped::~UserMapped()
{
}
void UserMapped::initMapped()
{
m_map.clear();
m_map.insert(USER::EN_ID, "id");
m_map.insert(USER::EN_NAME ,"name");
m_map.insert(USER::EN_AGE ,"age");
}
QStringList UserMapped::getListMapped(int en)
{
QStringList temp;
QString str;
QMap
::iterator i; for (i = m_map.begin(); i != m_map.end(); ++i)
{
if(en&(i.key()))
{
str=i.value();
temp.append(str);
str.clear();
}
}
return temp;
}
QString UserMapped::getMapped(int en)
{
QString str;
QMap
::iterator i; for (i = m_map.begin(); i != m_map.end(); ++i)
{
if(en==i.key())
{
str =i.value();
return str;
}
}
return QString();
}
QxHandler
IHandler 抽象化的类模板
这里只抽象了部分方法 还有save 和事务以及关系的方法未抽象完成
#ifndef IHANDLER_H
#define IHANDLER_H
#include
#include
#include
#include
#include
/**
* @brief The ISqlInterface class 数据库操作的抽象方法类 并且模板化减少代码的繁杂
*/
class ISqlInterface
{
public:
ISqlInterface(){}
virtual ~ISqlInterface(){}
//数据库连接初始化
protected:
virtual void initSqlconnect()=0;
//建表
virtual bool createTable()=0;
//断开连接
virtual void disconnect()=0;
};
template
class IHandler
{
public:
IHandler(){}
virtual ~IHandler(){}
virtual bool createTable(QSqlDatabase &m_SqlDatabase)
{
QSqlError error= qx::dao::create_table
(&m_SqlDatabase); return !error.isValid();
}
virtual bool insert(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase)
{
QMutexLocker locker(&m_Mutex);
if(!m_SqlDatabase.isOpen())
return false;
if(qx::dao::exist(t,&m_SqlDatabase).getValue()!=false)
return false;
QSqlError error= qx::dao::insert(t,&m_SqlDatabase);
return !error.isValid();
}
virtual bool deleteObject(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,bool isDestroy)
{
QMutexLocker locker(&m_Mutex);
if(!m_SqlDatabase.isOpen())
return false;
QSqlError error;
if(qx::dao::exist(t,&m_SqlDatabase).getValue()==false)
return false;
if(isDestroy==false)
{
error= qx::dao::delete_by_id(t,&m_SqlDatabase);
}
else
{
error= qx::dao::destroy_by_id(t,&m_SqlDatabase);
}
return !error.isValid();
}
virtual bool update(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,QStringList &list)
{
QMutexLocker locker(&m_Mutex);
if(!m_SqlDatabase.isOpen())
return false;
if(qx::dao::exist(t,&m_SqlDatabase).getValue()==false)
return false;
QSqlError error= qx::dao::update(t,&m_SqlDatabase,list);
return !error.isValid();
}
virtual bool select(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,QStringList &list)
{
QMutexLocker locker(&m_Mutex);
if(!m_SqlDatabase.isOpen())
return false;
QSqlError error;
if(qx::dao::exist(t,&m_SqlDatabase).getValue()==false)
return false;
error= qx::dao::fetch_by_id(t,&m_SqlDatabase,list);
return !error.isValid();
}
virtual bool selectByQuery(T2 &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,qx::QxSqlQuery &query,QStringList &list)
{
QMutexLocker locker(&m_Mutex);
if(!m_SqlDatabase.isOpen())
return false;
QSqlError error=qx::dao::fetch_by_query(query,t,&m_SqlDatabase,list);
return !error.isValid();
}
};
#endif // IHANDLER_H
UserHandler 实际操作句柄类
UserHandler.h
#ifndef USERHANDLER_H
#define USERHANDLER_H
#include
#include
#include
#include
#include
#include
#include "IHandler.h"
#include "SQLModule/QxMapped/UserMapped.h"
#include "SQLModule/QxObject/User.h"
namespace USER
{
const QString DATABASE_TYPE="QSQLITE";
const QString CONNECT_NAME="USER_CONNECTED";
const QString DATABASENAME="C:/Users/we/Desktop/workTools/demo/qxorm.db";
const QString HOSTNAME="localhost";
const QString USERNAME="root";
const QString PASSWORD="";
}
using namespace USER;
class User; //OBJECT 类
typedef QSharedPointer
Shared_User; //User类智能指针 typedef QList
List_User; //User类数组 typedef qx::QxCollection
Collection_User; //User容器 class UserHandler:public IHandler
,public ISqlInterface {
public:
UserHandler();
virtual ~UserHandler();
/**
* @brief insert 插入数据至数据库
* @param t 插入的单条数据 不需要指定ID值 自增
* @return 0失败 1成功
*/
bool insert(Shared_User &t);
/**
* @brief deleteObject 从数据库中删除指定数据
* @param t 删除的单条数据 需要指定ID值
* @param isDestroy 是否软删除
* @return 0失败 1成功
*/
bool deleteObject(Shared_User &t,bool isDestroy=false);
/**
* @brief update 根据ID值更新数据
* @param t 数据
* @param en 更新字段的映射值
* @return 0失败 1成功
*/
bool update(Shared_User &t,int en=EN_ALL);
/**
* @brief select 根据ID值查询数据
* @param t 数据
* @param en 映射值
* @return 0失败 1成功
*/
bool select(Shared_User &t,int en=EN_ALL);
/**
* @brief selectByQuery 根据搜寻条件查找
* @param t 数据集合
* @param query 搜寻语句
* @param en 数据库列映射值
* @return 0失败 1成功
*/
bool selectByQuery(Collection_User &t,qx::QxSqlQuery &query,int en=EN_ALL);
protected:
virtual void initSqlconnect();
virtual bool createTable();
virtual void disconnect();
private:
UserMapped m_Mapped;
QMutex m_Mutex;
QSqlDatabase m_SqlDatabase;
};
#endif // USERHANDLER_H
UserHandler.cpp
#include "UserHandler.h"
UserHandler::UserHandler()
{
initSqlconnect();
createTable();
}
UserHandler::~UserHandler()
{
disconnect();
}
void UserHandler::initSqlconnect()
{
QMutexLocker locker(&m_Mutex);
if(QSqlDatabase::contains(CONNECT_NAME))
m_SqlDatabase = QSqlDatabase::database(CONNECT_NAME);
else
m_SqlDatabase= QSqlDatabase::addDatabase(DATABASE_TYPE,CONNECT_NAME);
m_SqlDatabase.setDatabaseName(DATABASENAME);
m_SqlDatabase.setHostName(HOSTNAME);
m_SqlDatabase.setUserName(USERNAME);
m_SqlDatabase.setPassword(PASSWORD);
m_SqlDatabase.open();
}
bool UserHandler::createTable()
{
return IHandler
::createTable(m_SqlDatabase); }
void UserHandler::disconnect()
{
QMutexLocker locker(&m_Mutex);
if(m_SqlDatabase.isOpen())
m_SqlDatabase.close();
QSqlDatabase::removeDatabase(CONNECT_NAME);
}
bool UserHandler::insert(Shared_User &t)
{
return IHandler
::insert(t,m_Mutex,m_SqlDatabase); }
bool UserHandler::deleteObject(Shared_User &t,bool isDestroy)
{
return IHandler
::deleteObject(t,m_Mutex,m_SqlDatabase,isDestroy); }
bool UserHandler::update(Shared_User &t, int en)
{
QStringList list= m_Mapped.getListMapped(en);
return IHandler
::update(t,m_Mutex,m_SqlDatabase,list); }
bool UserHandler::select(Shared_User &t, int en)
{
QStringList list= m_Mapped.getListMapped(en);
return IHandler
::select(t,m_Mutex,m_SqlDatabase,list); }
bool UserHandler::selectByQuery(Collection_User &t,qx::QxSqlQuery &query,int en)
{
QStringList list= m_Mapped.getListMapped(en);
return IHandler
::selectByQuery(t,m_Mutex,m_SqlDatabase,query,list); }
下一篇讲如何整合所有表,并且升级数据库的方法。
下一篇windows下静态使用QxOrm框架并使用泛型编程 (三)