Skip to content

Commit

Permalink
添加db
Browse files Browse the repository at this point in the history
  • Loading branch information
hu419087137 authored and zjtdd committed Nov 15, 2018
1 parent 95676a1 commit 9a27a16
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 129 deletions.
4 changes: 2 additions & 2 deletions Core/BaseThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "BaseThread.h"
#include "BaseThread.h"


QString BaseThread::_lastError = "";
BaseThread::BaseThread(QObject *parent) : QThread(parent)
{

Expand Down
13 changes: 6 additions & 7 deletions Core/BaseThread.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef BaseThread_H
#ifndef BaseThread_H
#define BaseThread_H

#include <QThread>
#include <QtDebug>
#include <QMutex>


class BaseThread : public QThread
{
Expand All @@ -12,8 +12,8 @@ class BaseThread : public QThread
explicit BaseThread(QObject *parent = 0);
virtual ~BaseThread();
//错误返回
static QString lastError();
static void setLastError(const QString &lastError);
QString lastError();
void setLastError(const QString &lastError);
//定义线程需要执行任务类型
int threadType() const;
void setThreadType(int threadType);
Expand All @@ -37,9 +37,8 @@ public slots:
//是否停止线程,该变量用于跳出while(!_isNeedStop)循环
bool _isNeedStop = false;
//最后错误信息
static QString _lastError;
//线程锁,注意return函数中记得解锁
QMutex _mutex;
QString _lastError;

};

#endif // BaseThread_H
107 changes: 107 additions & 0 deletions Core/SqliteClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "SqliteClass.h"
#include <QFileInfo>

SqliteClass::SqliteClass(QObject *parent) : QObject(parent)
{
}
SqliteClass::~SqliteClass()
{
if(_pProtocolClass){
delete _pProtocolClass;
}
//之所以这么写,是为了消除警告
//QSqlDatabasePrivate::removeDatabase: connection 'XXX' is still in use, all queries will cease to work

QSqlDatabase *db = & QSqlDatabase::database(_connectionName);
if(db->isValid() ){
if(db->isOpen())
db->close();
QSqlDatabase::removeDatabase(_connectionName);
}
}

QString SqliteClass::errorString() const
{
return _errorString;
}

void SqliteClass::setErrorString(const QString &errorString)
{
_errorString = errorString;
}

//建立一个数据库连接
bool SqliteClass::createConnection(const QString & filePath)
{
QFileInfo fileInfo(filePath);
if( ! fileInfo.exists() ){
setErrorString(tr("Not found file: %1").arg(filePath));
return false;
}
_connectionName = fileInfo.completeBaseName();
//fileInfo.completeBaseName() 数据库的文件名,来作为连接名(如果多个db名字一致,则修改此处)
QSqlDatabase db = QSqlDatabase::database(_connectionName);

if(!db.isValid()){ //如果db不可用,则添加
db = QSqlDatabase::addDatabase("QSQLITE", _connectionName);
db.setDatabaseName(filePath);
}
if( !db.open()){ //判断db是否已经打开
setErrorString(tr("Sqlite not open").arg(filePath));
return false;
}
return true;
}

QSqlDatabase & SqliteClass::dbConnection()
{
QSqlDatabase db = QSqlDatabase::database(_connectionName);

if( !db.open()){ //判断db是否已经打开
setErrorString(tr("Sqlite not open").arg(db.connectionName()));
}
return db;
}

/** 获取指令数据类
* @brief ProtocolClass::getProtocol
* @return
*/
ProtocolClass * SqliteClass::getProtocol()
{
if(!_pProtocolClass){
return queryProtocol();
}
return _pProtocolClass;
}

/** 查询指令库
* @brief SqliteClass::queryProtocol
* @return
*/
ProtocolClass * SqliteClass::queryProtocol()
{
QSqlQuery query(dbConnection());
QString mysql;

mysql= QString("select b.reqValue,a.port,b.req,b.reqDescription "
"from RobotProtocol a,SCProtocol b where a.type = b.type");
if(!query.exec(mysql)){
setErrorString(QString("%1:%2").arg(mysql).arg(query.lastError().text()));
return Q_NULLPTR;
}
if(_pProtocolClass){
delete _pProtocolClass;
}
_pProtocolClass = new ProtocolClass();
while(query.next()){
_pProtocolClass->addData(
query.value("reqValue").toInt(),
query.value("port").toInt(),
query.value("req").toString(),
query.value("reqDescription").toString());
}
query.clear();

return _pProtocolClass;
}
81 changes: 81 additions & 0 deletions Core/SqliteClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#ifndef SQLITECLASS_H
#define SQLITECLASS_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QTime>
#include <QSqlError>
#include <QtDebug>
#include <QSqlDriver>
#include <QSqlRecord>

#define SCDebug qDebug() <<"[Debug]["<<__FILE__<<"]$"<<0<<"$"<<"["<<__LINE__<<"]["<<__FUNCTION__ <<"]:"
#define SCInfo qInfo() <<"[Info]["<<__FILE__<<"]$"<<0<<"$"<<"["<<__LINE__<<"]["<<__FUNCTION__<<"]:"
#define SCWarning qWarning()<<"[Warning][" <<__FILE__<<"]$"<<0<<"$"<<"["<<__LINE__<<"]["<<__FUNCTION__<<"]:"
#define SCritical qCritical() <<"[Error]["<<__FILE__<<"]$"<<0<<"$"<<"["<<__LINE__<<"]["<<__FUNCTION__<<"]:"

class ProtocolClass{
public:
ProtocolClass(){}
~ProtocolClass(){

ReqValueReqMap.clear();
ReqValueReqDescriptionMap.clear();
ReqValuePortMap.clear();
}
void addData(
int reqValue,
int port,
const QString & req,
const QString & reqDescription)
{
ReqValueReqMap.insert(reqValue,req);
ReqValueReqDescriptionMap.insert(reqValue,reqDescription);
ReqValuePortMap.insert(reqValue,port);
}
//根据数据库中 reqValue 获取数据库中 reqDescription 字段
QString getReqDescription(int reqValue) const {
return ReqValueReqDescriptionMap.value(reqValue);
}
//根据数据库中 reqValue 获取数据库中 req 字段
QString getReq(int reqValue) const {
return ReqValueReqMap.value(reqValue);
}
//根据数据库中 reqValue 获取数据库中 port 字段
int getPort(int reqValue) const {
return ReqValuePortMap.value(reqValue);
}
//QMap<reqValue, req>
QMap<int,QString> ReqValueReqMap;
//QMap<reqValue, reqDescription>
QMap<int,QString> ReqValueReqDescriptionMap;
//QMap<reqValue, port>
QMap<int,int>ReqValuePortMap;

};

class SqliteClass : public QObject
{
Q_OBJECT
public:
explicit SqliteClass(QObject *parent = nullptr);
~SqliteClass();
QString errorString() const;
void setErrorString(const QString &errorString);

QSqlDatabase & dbConnection();
bool createConnection(const QString &filePath);

ProtocolClass *queryProtocol();
ProtocolClass *getProtocol();
signals:

public slots:
private:
QString _connectionName; //连接 db 名(用于多个db库访问时)
QString _errorString;
ProtocolClass * _pProtocolClass = Q_NULLPTR;
};

#endif // SQLITECLASS_H
4 changes: 2 additions & 2 deletions ProtoBufTool/ProtobufThread.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ProtobufThread.h"
#include "ProtobufThread.h"
#include <ostream>
using namespace std;
ProtobufThread::ProtobufThread(QObject *parent) : BaseThread(parent)
Expand Down Expand Up @@ -142,7 +142,7 @@ void ProtobufThread::run()
if(file.open(QIODevice::ReadOnly)){
jsonStr = QString( file.readAll());
}else{
setLastError(QString("打开【%1】文件失败").arg(_binaryMessageName));
setLastError(QString(QStringLiteral("打开【%1】文件失败")).arg(_binaryMessageName));
}
}else {
jsonStr = jsonData();
Expand Down
28 changes: 14 additions & 14 deletions ProtoBufTool/ProtobufWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ProtobufWidget.h"
#include "ProtobufWidget.h"
#include "ui_ProtobufWidget.h"
#include <QFileDialog>

Expand All @@ -7,8 +7,8 @@ ProtobufWidget::ProtobufWidget(QWidget *parent) :
ui(new Ui::ProtobufWidget)
{
ui->setupUi(this);
pProtobufThread = new ProtobufThread(this);
connect(pProtobufThread,&ProtobufThread::finished,this,&ProtobufWidget::slotProtobufThreadFinish);
_pProtobufThread = new ProtobufThread(this);
connect(_pProtobufThread,&ProtobufThread::finished,this,&ProtobufWidget::slotProtobufThreadFinish);
}

ProtobufWidget::~ProtobufWidget()
Expand All @@ -18,7 +18,7 @@ ProtobufWidget::~ProtobufWidget()

void ProtobufWidget::on_pushButton_Conversion_clicked()
{
if(pProtobufThread->isRunning()) {
if(_pProtobufThread->isRunning()) {
ui->label_status->setText(tr("线程正在运行中..."));
return;
}
Expand All @@ -28,7 +28,7 @@ void ProtobufWidget::on_pushButton_Conversion_clicked()
}else if(ui->radioButton_2->isChecked()){
threadType = 1;
}
if(pProtobufThread->initProtobufThread(ui->lineEdit_protoFilePath->text(),
if(_pProtobufThread->initProtobufThread(ui->lineEdit_protoFilePath->text(),
ui->lineEdit_packageName->text(),
ui->lineEdit_messageName->text(),
ui->lineEdit_conversionFilePath->text(),
Expand All @@ -38,21 +38,21 @@ void ProtobufWidget::on_pushButton_Conversion_clicked()
))
{
ui->label_status->setText("正在转换...");
pProtobufThread->start();
_pProtobufThread->start();
}else{
ui->label_status->setText(QString("error: %1").arg(pProtobufThread->lastError()));
ui->label_status->setText(QString("error: %1").arg(_pProtobufThread->lastError()));
}
}
void ProtobufWidget::slotProtobufThreadFinish()
{
if(!pProtobufThread->lastError().isEmpty()){
ui->label_status->setText(QString("error: %1").arg(pProtobufThread->lastError()));
if(!_pProtobufThread->lastError().isEmpty()){
ui->label_status->setText(QString("error: %1").arg(_pProtobufThread->lastError()));
}else{
switch (pProtobufThread->threadType()) {
switch (_pProtobufThread->threadType()) {
case 0:
{
ui->label_status->setText(tr("proto 二进制转 json 成功."));
ui->textEdit_json->setText(pProtobufThread->jsonData());
ui->textEdit_json->setText(_pProtobufThread->jsonData());
QString path = QFileDialog::getSaveFileName(this, tr("json"), QString("./untitled.json"), tr("JSON(*.json)"));
if (!path.isEmpty()) {
QFile file(path);
Expand All @@ -69,9 +69,9 @@ void ProtobufWidget::slotProtobufThreadFinish()
// if (!path.isEmpty()) {
// QFile file(path);
// if(file.open(QIODevice::WriteOnly)){
// if(!pProtobufThread->msgByteArray().isEmpty()){
// if(!_pProtobufThread->msgByteArray().isEmpty()){

// file.write(pProtobufThread->msgByteArray());
// file.write(_pProtobufThread->msgByteArray());

// }else{
// ui->label_status->setText(tr("message二进制文件数据为空."));
Expand All @@ -81,7 +81,7 @@ void ProtobufWidget::slotProtobufThreadFinish()
// ui->label_status->setText(QString("打开【%1】文件失败.").arg(path));
// }
// ui->label_status->setText(QString("已保存:%1").arg(path));
ui->label_status->setText(QString("已保存:%1").arg(pProtobufThread->outBinaryFilePath()));
ui->label_status->setText(QString("已保存:%1").arg(_pProtobufThread->outBinaryFilePath()));
// }
}
break;
Expand Down
4 changes: 2 additions & 2 deletions ProtoBufTool/ProtobufWidget.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef PROTOBUFWIDGET_H
#ifndef PROTOBUFWIDGET_H
#define PROTOBUFWIDGET_H

#include <QWidget>
Expand Down Expand Up @@ -31,7 +31,7 @@ private slots:

private:
Ui::ProtobufWidget *ui;
ProtobufThread *pProtobufThread;
ProtobufThread *_pProtobufThread = Q_NULLPTR;
};

#endif // PROTOBUFWIDGET_H
Loading

0 comments on commit 9a27a16

Please sign in to comment.