forked from seer-robotics/SeerTCPTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95676a1
commit 9a27a16
Showing
13 changed files
with
358 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.