windows下静态使用QxOrm框架并使用泛型编程 (二)

写bug我是专业的

发布日期: 2019-07-23 14:38:57 浏览量: 195
评分:
star star star star star star star star star_border star_border
*转载请注明来自write-bug.com

这篇开始讲实际编程并且抽象化,让代码书写更少。

为了模块划分我在Demo文件夹下新增了一个SQLModule文件夹,在此文件夹下又新增QxHandler,QxMapped,QxObject三个文件夹,QxObject是用来存储数据对象类的一个文件夹,QxMapped是存储键值映射的文件夹,QxHandler是操作数据库的实际句柄类文件夹。

QxObject类

User.h

  1. #ifndef USER_H
  2. #define USER_H
  3. #include "common.h"
  4. #include
  5. #include <QJsonObject>
  6. class User
  7. {
  8. QX_REGISTER_FRIEND_CLASS(User) //用于将类注册到QxOrm的宏定义
  9. public:
  10. User();
  11. long getId();
  12. QString getName();
  13. int getAge();
  14. public:
  15. QJsonObject toJsonObject();
  16. bool analyzeJson(QByteArray &json);
  17. void analyzeJson(QJsonObject &json);
  18. private:
  19. long m_lId
  20. QString m_sName;
  21. int m_nAge;
  22. };
  23. //QX_REGISTER_PRIMARY_KEY(User, int) //主键不是整数类型的时候使用
  24. QX_REGISTER_HPP_IMPORT_DLL(User, qx::trait::no_base_class_defined, DATABASE_VERSION) //用于将类注册到QxOrm的宏定义 第一个参数为类名 第二个为默认值 第三个为数据库版本
  25. #endif // USER_H

User.cpp

  1. #include "User.h"
  2. QX_REGISTER_CPP_IMPORT_DLL(User) //用于将类注册到QxOrm的宏定义
  3. namespace qx //用于将类注册到QxOrm的方法
  4. {
  5. template <> void register_class(QxClass<User> & t)
  6. {
  7. qx::IxDataMember * pData = NULL; Q_UNUSED(pata);
  8. qx::IxSqlRelation * pRelation = NULL; Q_UNUSED(pRelation);
  9. qx::IxFunction * pFct = NULL; Q_UNUSED(pFct)
  10. qx::IxValidator * pValidator = NULL; Q_UNUSED(pValidator);
  11. // Register
  12. pData =t.id(& User::m_lId, "id",DATABASE_VERSION);
  13. pData =t.data(& User::m_nAge, "name",DATABASE_VERSION);
  14. pData =t.data(& User::m_nAge, "age",DATABASE_VERSION);
  15. qx::QxValidatorX<User> * pAllValidator = t.getAllValidator(); Q_UNUSED(pAllValidator);
  16. }
  17. }
  18. User::User()
  19. :m_lId(0)
  20. ,m_sName("")
  21. ,m_nAge(0)
  22. {
  23. }
  24. long User::getId()
  25. {
  26. return m_lId;
  27. }
  28. QString User::getName()
  29. {
  30. return m_sName;
  31. }
  32. int User::getAge()
  33. {
  34. return m_nAge
  35. }
  36. JsonObject User::toJsonObject()
  37. {
  38. QJsonObject subObject;
  39. subObject.insert("Name",m_sName);
  40. subObject.insert("Age",m_nAge);
  41. return subObject;
  42. }
  43. bool User::analyzeJson(QByteArray &json)
  44. {
  45. bool success=false;
  46. QJsonParseError error;
  47. QJsonDocument document=QJsonDocument::fromJson(json,&error);
  48. if (!document.isNull() && (error.error == QJsonParseError::NoError))
  49. {
  50. QJsonObject rootObject =documnt.object();
  51. this->analyzeJson(rootObject);
  52. success=true;
  53. }
  54. else
  55. {
  56. success=false;
  57. qDebug()<<error.error;
  58. qDebug("LockoutFun analyze falied");
  59. }
  60. return success;
  61. }
  62. void User::analyzeJson(QJsonObject &json)
  63. {
  64. QJsonObject rootObject = json.value("User").toObject();
  65. m_sName=rootObject.value(QString("Name")).toString();
  66. m_nAge=rootObject.value(QString("Age")).toInt();
  67. }

QxMapped

IMapped 映射抽象类

  1. #ifndef IMAPPED_H
  2. #define IMAPPED_H
  3. /**
  4. * @author tianmin
  5. * @brief The IMapped class 抽象出来的映射类
  6. * @date 2019-07-22
  7. */
  8. #include <QStringList>
  9. #include <QSting>
  10. #include <QMap
  11. class IMapped
  12. {
  13. public
  14. //字符map初始化
  15. virtual void initMapped() = 0;
  16. //获取QString列表
  17. virtual QStringList getListMapped(int en) =0;
  18. //获取QString 字段
  19. virtual QString getMapped(int en) = 0;
  20. virtual ~IMapped(){}
  21. };
  22. #endif // IMAPPED_H

UserMapped 际使用的类

UserMapped .h

  1. #ifndef USERMAPPED_H
  2. #define USERMAPPED_H
  3. #include "IMapped.h"
  4. namespace USER {
  5. enum USER_MAPPED
  6. {
  7. EN_ID = 0x00000001,
  8. EN_NAME = 0x00000002,
  9. EN_AGE = 0x00000004,
  10. EN_ALL = 0xFFFFFFFF
  11. };
  12. }
  13. class UserMapped:public IMapped
  14. {
  15. public:
  16. UserMapped();
  17. virtual ~UserMapped();
  18. public:
  19. virtual void initMapped()
  20. virtual QStringList getListMapped(int en);
  21. virtual QString getMapped(int en);
  22. private:
  23. QMap<int,QString> m_map;
  24. };
  25. #endif // USERMAPPED_H

UserMapped.cpp

  1. #include "UserMapped.h"
  2. UserMapped::UserMapped()
  3. {
  4. }
  5. UserMapped::~UserMapped()
  6. {
  7. }
  8. void UserMapped::initMapped()
  9. {
  10. m_map.clear();
  11. m_map.insert(USER::EN_ID, "id");
  12. m_map.insert(USER::EN_NAME ,"name");
  13. m_map.insert(USER::EN_AGE ,"age");
  14. }
  15. QStringList UserMapped::getListMapped(int en)
  16. {
  17. QStringList temp;
  18. QString str;
  19. QMap<int,QString>::iterator i;
  20. or (i = m_map.begin(); i != m_map.end(); ++i)
  21. {
  22. if(en&(i.key()))
  23. {
  24. str=i.value();
  25. temp.append(str);
  26. str.clear();
  27. }
  28. }
  29. return temp;
  30. }
  31. QString UserMapped::getMapped(int en)
  32. {
  33. QString str
  34. QMap<int,QString>::iterator i;
  35. for (i = m_map.begin(); i != m_map.end(); ++i)
  36. {
  37. if(en==i.key())
  38. {
  39. str =i.value();
  40. return str;
  41. }
  42. }
  43. return QString();
  44. }

QxHandler

IHandler 抽象化的类模板

这里只抽象了部分方法 还有save 和事务以及关系的方法未抽象完成

  1. #ifndef IHANDLER_H
  2. #define IHANDLER_H
  3. #include
  4. #include <QMutexLocker>
  5. #include <QMutex>
  6. #include <QtSql/QSqlDatabase>
  7. #include <QtSql/QSqlError>
  8. /**
  9. * @brief The ISqlInterface class 数据库操作的抽象方法类 并且模板化减少代码的繁杂
  10. */
  11. class ISqlInterface
  12. {
  13. public:
  14. ISqlInterface(){}
  15. virtual ~ISqlInterface(){}
  16. //数据库连接初始化
  17. protected:
  18. virtual void initSqlconnect()=0;
  19. //建表
  20. virtual bool createTable()=0;
  21. //断开连接
  22. virtual void disconnect()=0;
  23. };
  24. template<class T,class T2,class T3>
  25. class IHandler
  26. {
  27. public:
  28. IHandler(){}
  29. virtual ~IHandler(){}
  30. Virtual bool createTable(QSqlDatabase &m_SqlDatabase)
  31. {
  32. QSqlError error= qx::dao::create_table<T3>(&m_SqlDatabase);
  33. return !error.isValid();
  34. }
  35. virtual bool insert(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase)
  36. {
  37. QMutexLocker locker(&m_Mutex);
  38. if(!m_SqlDatabase.isOpen())
  39. return false;
  40. if(qx::dao::exist(t,&m_SqlDatabase).getValue()!=false)
  41. return false;
  42. QSqlError error= qx::dao::insert(t,&m_SqlDatabase);
  43. return !error.isValid();
  44. }
  45. virtual bool deleteObject(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,bool isDestroy)
  46. {
  47. QMutexLocker locker(&m_Mutex);
  48. if(!m_SqlDatabase.isOpen())
  49. return false;
  50. QSqlError error;
  51. if(qx::dao::exist(t,&m_SqlDatabase).getValue()==false)
  52. return false;
  53. if(isDestroy==false)
  54. {
  55. error= qx::dao::delete_by_id(t,&m_SqlDatabase);
  56. }
  57. else
  58. {
  59. error= qx::dao::destroy_by_id(t,&m_SqlDatabase);
  60. }
  61. return !error.isValid();
  62. }
  63. virtual bool update(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,QStringList &list)
  64. {
  65. QMutexLocker locker(&m_Mutex);
  66. if(!m_SqlDatabas.isOpen())
  67. return false;
  68. if(qx::dao::exist(t,&m_SqlDatabase).getValue()==false)
  69. return false;
  70. QSqlError error= qx::dao::update(t,&m_SqlDatabase,list);
  71. return !error.isValid();
  72. }
  73. virtual bool select(T &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,QStringList &list)
  74. QMutexLocker locker(&m_Mutex);
  75. if(!m_SqlDatabase.isOpen())
  76. return false;
  77. QSqlError error;
  78. if(qx::dao::exist(t,&m_SqlDatabase).getValue()==false)
  79. return false;
  80. error= qx::dao::fetch_by_id(t,&m_SqlDatabase,list);
  81. return !error.isValid();
  82. }
  83. virtual bool selectByQuery(T2 &t,QMutex &m_Mutex,QSqlDatabase &m_SqlDatabase,qx::QxSqlQuery &query,QStringList &list)
  84. {
  85. QMutexLocker locker(&m_Mutx);
  86. if(!m_SqlDatabase.isOpen())
  87. return false;
  88. QSqlError error=qx::dao::fetch_by_query(query,t,&m_SqlDatabase,list);
  89. return !error.isValid();
  90. }
  91. };
  92. #endif // IHANDLER_H

UserHandler 实际操作句柄类

UserHandler.h

  1. #ifndef USERHANDLER_H
  2. #define USERHANDLER_H
  3. #include
  4. #include <QString>
  5. #include <QMutexLocker>
  6. #include <QMutex>
  7. #include <QtSql/QSqlDatabase>
  8. #include <QtSql/QSqlError>
  9. #include "IHandler.h"
  10. #include "SQLModule/QxMapped/UserMapped.h"
  11. #include "SQLModule/QxObject/User.h"
  12. namespace USER
  13. {
  14. const QString DATABASE_TYPE="QSQLITE";
  15. const QString CONNECT_NAME="USER_CONNECTED";
  16. const QString DATABASENAME="C:/Users/we/Desktop/workTools/demo/qxorm.db";
  17. const QString HOSTNAME="localhost";
  18. const QString USERNAME="root";
  19. const QString PASSWORD="";
  20. }
  21. using namespace USER;
  22. class User; //OBJECT 类
  23. typedef QSharedPointer<User> Shared_User; //User类智能指针
  24. typedef QList<Shared_User> List_User; //User类数组
  25. typedef qx::QxCollection<int,Shared_User> Collection_User; //User容器
  26. class UserHandler:public IHandler<Shared_User,Collection_User,User> ,public ISqlInterface
  27. {
  28. public:
  29. UserHandler();
  30. virtual ~UserHandler();
  31. /**
  32. * @brief insert 插入数据至数据库
  33. * @param t 插入的单条数据 不需要指定ID值 自增
  34. * @return 0失败 1成功
  35. */
  36. bool insert(Shared_User &t);
  37. /**
  38. * @brief deleteObject 从数据库中删除指定数据
  39. * @param t 删除的单条数据 需要指定ID值
  40. * @param isDestroy 是否软删除
  41. * @return 0失败 1成功
  42. */
  43. bool deleteObject(Shared_User &t,bool isDestroy=false);
  44. /**
  45. * @brief update 根据ID值更新数据
  46. * @param t 数据
  47. * @param en 更新字段的映射值
  48. * @return 0失败 1成功
  49. */
  50. bool update(Shared_User &t,int en=EN_ALL);
  51. /**
  52. * @brief select 根据ID值查询数据
  53. * @param t 数据
  54. * @param en 映射值
  55. * @return 0失败 1成功
  56. */
  57. bool select(Shared_User &t,int en=EN_ALL);
  58. /**
  59. * @brief selectByQuery 根据搜寻条件查找
  60. * @param t 数据集合
  61. * @param query 搜寻语句
  62. * @param en 数据库列映射值
  63. * @return 0失败 1成功
  64. */
  65. bool selectByQuery(Collection_User &t,qx::QxSqlQuery &query,int en=EN_ALL);
  66. protected:
  67. virtual void initSqlconnect();
  68. virtual bool createTable();
  69. virtual void disconnect();
  70. private:
  71. UserMapped m_Mapped;
  72. QMutex m_Mutex;
  73. QSqlDatabase m_SqlDatabase;
  74. };
  75. #endif // USERHANDLER_H

UserHandler.cpp

  1. #include "UserHandler.h"
  2. UserHandler::UserHandler()
  3. {
  4. initSqlconnect();
  5. createTable();
  6. }
  7. UserHandler::~UserHandler()
  8. {
  9. disconnect();
  10. }
  11. void UserHandler::initSqlconnect()
  12. {
  13. QMutexLocker locker(&m_Mutex);
  14. if(QSqlDatabase::contains(CONNECT_NAME))
  15. m_SqlDatabase = QSqlDatabase::database(CONNECT_NAME);
  16. else
  17. m_SqlDatabase= QSqlDatabase::addDatabase(DATABASE_TYPE,CONNECT_NAME);
  18. m_SqlDatabase.setDatabaseName(DATABASENAME);
  19. m_SqlDatabase.setHostName(HOSTNAME);
  20. m_SqlDatabase.setUserName(USERNAME);
  21. m_SqlDatabase.setPassword(PASSWORD);
  22. m_SqlDatabase.open();
  23. }
  24. bool UserHandler::createTable()
  25. {
  26. return IHandler<Shared_User,Collection_User,User>::createTable(m_SqlDatabase);
  27. }
  28. void UserHandler::disconnect()
  29. {
  30. QMutexLocker locker(&m_Mutex);
  31. if(m_SqlDatabase.isOpen())
  32. m_SqlDatabase.close();
  33. QSqlDatabase::removeDatabase(CONNECT_NAME);
  34. }
  35. bool UserHandler::insert(Shared_User &t)
  36. {
  37. return IHandler<Shared_User,Collection_User,User>::insert(t,m_Mutex,m_SqlDatabase);
  38. }
  39. bool UserHandler::deleteObject(Shared_User &t,bool isDestroy)
  40. {
  41. return IHandler<Shared_User,Collection_User,User>::deleteObject(t,m_Mutex,m_SqlDatabase,isDestroy);
  42. }
  43. bool UserHandler::update(Shared_User &t, int en)
  44. {
  45. QStringList list= m_Mapped.getListMapped(en);
  46. return IHandler<Shared_User,Collection_User,User>::update(t,m_Mutex,m_SqlDatabase,list);
  47. }
  48. bool UserHandler::select(Shared_User &t, int en)
  49. {
  50. QStringList list= m_Mapped.getListMapped(en);
  51. return IHandler<Shared_User,Collection_User,User>::select(t,m_Mutex,m_SqlDatabase,list);
  52. }
  53. bool UserHandler::selectByQuery(Collection_User &t,qx::QxSqlQuery &query,int en)
  54. {
  55. QStringList list= m_Mapped.getListMapped(en);
  56. return IHandler<Shared_User,Collection_User,User>::selectByQuery(t,m_Mutex,m_SqlDatabase,query,list);
  57. }
上传的附件
eject