1 数据库语句生成帮助类、
2 数据管理类
This commit is contained in:
parent
6cd4db6d95
commit
5d1c1b6c98
Binary file not shown.
211
xyylMCWEACSystem/SqlGenerate.cpp
Normal file
211
xyylMCWEACSystem/SqlGenerate.cpp
Normal file
@ -0,0 +1,211 @@
|
||||
|
||||
#include "SqlGenerate.h"
|
||||
#include <QMapIterator>
|
||||
|
||||
SqlGenerate::SqlGenerate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SqlGenerate::~SqlGenerate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString SqlGenerate::createTable(QString table, QMap<QString, QString> map)
|
||||
{
|
||||
QString content = QString("create table %1 (").arg(table);
|
||||
QMapIterator<QString, QString> i(map);
|
||||
while (i.hasNext())
|
||||
{
|
||||
i.next();
|
||||
content.append(QString("%1 %2 ").arg(i.key()).arg(i.value()));
|
||||
if (i.hasNext())
|
||||
{
|
||||
content.append(",");
|
||||
}
|
||||
}
|
||||
content.append(")");
|
||||
content += QString(";");
|
||||
return content;
|
||||
}
|
||||
|
||||
QString SqlGenerate::insertData(QString table, QVariantMap map)
|
||||
{
|
||||
//insert or replace into
|
||||
QString content = QString("insert or replace into %1 (").arg(table);
|
||||
QString values = QString("values(");
|
||||
QMapIterator<QString, QVariant> i(map);
|
||||
while (i.hasNext())
|
||||
{
|
||||
i.next();
|
||||
content.append(QString("%1").arg(i.key()));
|
||||
values.append("'" + i.value().toString().replace("'", "''") + "'"); //
|
||||
if (i.hasNext())
|
||||
{
|
||||
content.append(", ");
|
||||
values.append(",");
|
||||
}
|
||||
}
|
||||
content.append(") ");
|
||||
values.append(")");
|
||||
content.append(values);
|
||||
content += QString(";");
|
||||
return content;
|
||||
}
|
||||
|
||||
QString SqlGenerate::selectWhere(QString table, QStringList Name, QString condition)
|
||||
{
|
||||
//QString content = QString("select from %1 ").arg(table);
|
||||
QString content = QString("select ");// .arg(table);
|
||||
QListIterator<QString> i(Name);
|
||||
while (i.hasNext())
|
||||
{
|
||||
i.next();
|
||||
if (i.hasNext())
|
||||
{
|
||||
content += QString(" %1 ,").arg(i.previous());
|
||||
}
|
||||
else
|
||||
{
|
||||
content += QString(" %1 ").arg(i.previous());
|
||||
}
|
||||
i.next();
|
||||
|
||||
}
|
||||
/*for(int i = 0; i < Name.size(); i++)
|
||||
{
|
||||
if (i + 1 == Name.size())
|
||||
{
|
||||
content += QString("%1 ").arg(Name.at(i));
|
||||
}
|
||||
else
|
||||
{
|
||||
content += QString("%1,").arg(Name.at(i));
|
||||
}
|
||||
}*/
|
||||
content += QString("from %1").arg(table);
|
||||
if (!condition.isEmpty())
|
||||
{
|
||||
content += condition;// QString(" where %1").arg(condition);
|
||||
}
|
||||
content += QString(";");//
|
||||
|
||||
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
QString SqlGenerate::selectUnion(QStringList sTables, QStringList Name, QString condition /*= ""*/)
|
||||
{
|
||||
QString s_select_Data;
|
||||
for (int j = 0; j < sTables.size(); j++)
|
||||
{
|
||||
|
||||
QString content = QString("select ");// .arg(table);
|
||||
QListIterator<QString> i(Name);
|
||||
while (i.hasNext())
|
||||
{
|
||||
i.next();
|
||||
if (i.hasNext())
|
||||
{
|
||||
content += QString(" %1 ,").arg(i.previous());
|
||||
}
|
||||
else
|
||||
{
|
||||
content += QString(" %1 ").arg(i.previous());
|
||||
}
|
||||
i.next();
|
||||
|
||||
}
|
||||
content += QString("from %1").arg(sTables.at(j));
|
||||
|
||||
if (s_select_Data.isEmpty())
|
||||
{
|
||||
s_select_Data = content;
|
||||
}
|
||||
else
|
||||
{
|
||||
s_select_Data = s_select_Data + " union all " + content;
|
||||
}
|
||||
}
|
||||
if (!condition.isEmpty())
|
||||
{
|
||||
s_select_Data += condition;// QString(" where %1").arg(condition);
|
||||
}
|
||||
s_select_Data += QString(";");//
|
||||
|
||||
return s_select_Data;
|
||||
}
|
||||
|
||||
QString SqlGenerate::createIndex(QString table, QString name)
|
||||
{
|
||||
QString content = QString("create index %1_index on %2 (%3)").arg(name).arg(table).arg(name);
|
||||
content += QString(";");//
|
||||
return content;
|
||||
}
|
||||
|
||||
QString SqlGenerate::updateData(QString table, QVariantMap map, QString condition)
|
||||
{
|
||||
QString content = QString("update %1 set ").arg(table);
|
||||
QMapIterator<QString, QVariant>i(map);
|
||||
while (i.hasNext())
|
||||
{
|
||||
i.next();
|
||||
if (i.hasNext())
|
||||
{
|
||||
content += QString("%1 = '%2',").arg(i.key()).arg(i.value().toString().replace("'", "''"));
|
||||
}
|
||||
else
|
||||
{
|
||||
content += QString("%1= '%2' ").arg(i.key()).arg(i.value().toString().replace("'", "''"));
|
||||
}
|
||||
|
||||
}
|
||||
content += QString("where %1").arg(condition);
|
||||
content += QString(";");
|
||||
return content;
|
||||
}
|
||||
|
||||
QString SqlGenerate::deleteData(QString table, QString condition)
|
||||
{
|
||||
QString content = QString("delete %1 ").arg(table);
|
||||
content += QString("where %1").arg(condition);
|
||||
content += QString(";");
|
||||
return content;
|
||||
}
|
||||
|
||||
bool SqlGenerate::checkCheckParameterSql(const QString& str)
|
||||
{
|
||||
QStringList keys;
|
||||
keys << "and";
|
||||
keys << "or";
|
||||
keys << "*";
|
||||
keys << "=";
|
||||
keys << " ";
|
||||
keys << "%0a";
|
||||
keys << "%0d";
|
||||
keys << "%";
|
||||
keys << "/";
|
||||
keys << "union";
|
||||
keys << "|";
|
||||
keys << "&";
|
||||
keys << "^";
|
||||
keys << "#";
|
||||
keys << "/*";
|
||||
keys << "*/";
|
||||
keys << "delete";
|
||||
keys << "insert";
|
||||
keys << "select";
|
||||
keys << "update";
|
||||
keys << "drop";
|
||||
for (int i = 0; i < keys.size(); i++)
|
||||
{
|
||||
if (str.contains(keys[i])) // != string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
33
xyylMCWEACSystem/SqlGenerate.h
Normal file
33
xyylMCWEACSystem/SqlGenerate.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef _SqlGenerate_H__
|
||||
#define _SqlGenerate_H__
|
||||
#include <QString>
|
||||
#include <QMap>
|
||||
#include <QVariantMap>
|
||||
|
||||
/*
|
||||
\author: zym
|
||||
\brief : Êý¾Ý¿â×é֯УÑéÆ÷
|
||||
\version 1.0
|
||||
\note
|
||||
*/
|
||||
|
||||
class SqlGenerate
|
||||
{
|
||||
public:
|
||||
SqlGenerate();
|
||||
virtual ~SqlGenerate();
|
||||
QString createTable(QString table, QMap<QString, QString> map);
|
||||
QString insertData(QString table, QVariantMap map);
|
||||
QString selectWhere(QString table, QStringList Name, QString condition = "");
|
||||
QString selectUnion(QStringList table, QStringList Name, QString condition = "");
|
||||
QString createIndex(QString table, QString name);
|
||||
QString updateData(QString table, QVariantMap map, QString condition);
|
||||
QString deleteData(QString table, QString sWhere);
|
||||
private:
|
||||
bool checkCheckParameterSql(const QString& str);
|
||||
protected:
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
#endif // SqlGenerate_h__
|
24
xyylMCWEACSystem/datamanager.cpp
Normal file
24
xyylMCWEACSystem/datamanager.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "datamanager.h"
|
||||
DataManager::DataManager(QObject * parent )
|
||||
{
|
||||
|
||||
};
|
||||
DataManager::~DataManager()
|
||||
{
|
||||
|
||||
}
|
||||
DataManager& DataManager::instance()
|
||||
{
|
||||
static DataManager dataManager;
|
||||
return dataManager;
|
||||
|
||||
}
|
||||
|
||||
void DataManager::init()
|
||||
{
|
||||
|
||||
}
|
||||
void DataManager::initTable()
|
||||
{
|
||||
|
||||
}
|
22
xyylMCWEACSystem/datamanager.h
Normal file
22
xyylMCWEACSystem/datamanager.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef DATAMANAGER_H
|
||||
#define DATAMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include "SqlCore.h"
|
||||
#include "SqlGenerate.h"
|
||||
class DataManager:public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
DataManager(QObject * parent =NULL);
|
||||
~DataManager();
|
||||
public:
|
||||
static DataManager& instance();
|
||||
void init();
|
||||
void initTable();
|
||||
private:
|
||||
SqlCore m_sqlcore;
|
||||
|
||||
};
|
||||
|
||||
#endif // DATAMANAGER_H
|
@ -27,8 +27,10 @@ CONFIG += c++11
|
||||
SOURCES += \
|
||||
SqlCore.cpp \
|
||||
SqlExecute.cpp \
|
||||
SqlGenerate.cpp \
|
||||
btngroupwidget.cpp \
|
||||
curchatwidget.cpp \
|
||||
datamanager.cpp \
|
||||
dataprocesswidget.cpp \
|
||||
devconwidget.cpp \
|
||||
eggwidget.cpp \
|
||||
@ -50,8 +52,10 @@ SOURCES += \
|
||||
HEADERS += \
|
||||
SqlCore.h \
|
||||
SqlExecute.h \
|
||||
SqlGenerate.h \
|
||||
btngroupwidget.h \
|
||||
curchatwidget.h \
|
||||
datamanager.h \
|
||||
dataprocesswidget.h \
|
||||
devconwidget.h \
|
||||
eggwidget.h \
|
||||
|
Loading…
x
Reference in New Issue
Block a user