简单通用数据访问映射层
2015-03-07 11:02
113 查看
#ifndef QDAM_H
#define QDAM_H
#include <QMap>
#include <QList>
#include <QString>
class QDam
{
struct SActionPair{
QString action;
QString cmd;
int group;
};
typedef QMap<QString,QString> QCmdMap;
typedef QList<SActionPair> QActionGroup;
protected:
QDam();
public:
static QDam * getInstance();
QString mapCmd(const QString &cmd);
QString mapAction(const QString &action);
QString getCmd(const QString &action);
QStringList getActionGroup(int n);
protected:
void readXml();
protected:
QCmdMap m_cmdMap;
QActionGroup m_actionGroup;
};
#endif // QDAM_H
#include "qdam.h"
#include "functemplate.h"
#include <QXmlStreamReader>
#include <QFile>
#include <QApplication>
typedef CSingleton<QDam> QDamSingleton;
QDam::QDam()
{
readXml();
}
QDam * QDam::getInstance()
{
return QDamSingleton::instance();
}
void QDam::readXml()
{
QString path = QApplication::applicationDirPath();
path += "/dam.xml";
QFile file(path);
if(file.open(QIODevice::ReadOnly))
{
QXmlStreamReader reader(&file);
while(!reader.atEnd())
{
reader.readNextStartElement();
QString flag = reader.name().toString();
if(flag == "action")
{
SActionPair pa;
QXmlStreamAttributes attrs = reader.attributes();
if(!attrs.isEmpty())
{
pa.action = attrs.value("name").toString();
pa.cmd = attrs.value("cmd").toString();
pa.group = attrs.value("group").toInt();
m_actionGroup.append(pa);
}
}
else if(flag == "map")
{
QXmlStreamAttributes attrs = reader.attributes();
if(!attrs.isEmpty())
{
QString cmd = attrs.value("cmd").toString();
QString str = reader.readElementText();
m_cmdMap[cmd] = str;
}
}
}
}
}
QString QDam::mapCmd(const QString &cmd)
{
if(m_cmdMap.contains(cmd))
{
return m_cmdMap[cmd];
}
return "";
}
QString QDam::mapAction(const QString &action)
{
QString cmd = getCmd(action);
return mapCmd(cmd);
}
QString QDam::getCmd(const QString &action)
{
QActionGroup::iterator it = m_actionGroup.begin();
for(; it != m_actionGroup.end(); ++it)
{
if(it->action == action)
{
return it->cmd;
}
}
return "";
}
QStringList QDam::getActionGroup(int n)
{
QStringList ls;
QActionGroup::iterator it = m_actionGroup.begin();
for(; it != m_actionGroup.end(); ++it)
{
if(it->group == n)
{
ls << it->action;
}
}
return ls;
}
映射XML文件
<?xml version="1.0" encoding="GBK"?><dam><action name = "所有1" cmd = "get_all0" group = "1"/><action name = "所有2" cmd = "get_all1" group = "1"/>
<action name = "所有3" cmd = "get_all2" group = "1"/>
<action name = "所有4" cmd = "get_all3" group = "1"/><action name = "专项1" cmd = "get_sp1" group = "2"/><action name = "专项2" cmd = "get_sp2" group = "2"/><action name = "专项3" cmd = "get_sp3" group = "2"/><action name = "专项4" cmd = "get_sp4" group = "2"/><action name = "专项5" cmd = "get_sp5" group = "2"/><action name = "专项6" cmd = "get_sp6" group = "2"/><action name = "专项7" cmd = "get_sp7" group = "2"/><map cmd = "get_all0"> select * from baseInfo </map><map cmd = "get_all1"> select * from baseInfo where id in (select baseInfo_id from classFiltter where typeCode = 1)</map><map cmd = "get_all2"> select * from baseInfo where id in (select baseInfo_id from classFiltter where typeCode = 2)</map><map cmd = "get_all3"> select * from baseInfo where id in (select baseInfo_id from classFiltter where typeCode = 3)</map><map cmd = "get_sp_filtter_with"> select * from baseInfo where id in (select baseInfo_id from technique where techniqueName = '%0') </map><map cmd = "get_allxx"> select * from baseInfo where id in (select baseInfo_id from technique where techniqueName = 'cc++')</map><map cmd = "insert_baseInfo"> insert into BaseInfo values(?,?,?,?,?,?,?,?,?)</map></dam>
相关文章推荐
- Access 通用数据访问类(asp.net 2.0 c#)
- 共享一个通用的数据访问类
- 通用的数据访问组建 SqlHelper
- VB数据访问通用模块(Access & SQL SERVER)
- 抛开映射关系,不oo的数据访问写法
- 写一个通用数据访问组件
- SQL Server中编写通用数据访问方法
- Oracle,SqlServer,Access数据库通用访问类设计 ---这个也太简单了
- Asp.net中打造通用数据访问类(c#)[转]
- Access通用数据访问类(asp.net2.0c#)
- Access 通用数据访问类(asp.net 2.0 c#)
- Access 通用数据访问类(asp.net 2.0 c#)
- 共享一个通用的数据访问类
- Access 通用数据访问类(asp.net 2.0 c#)
- 使用数据2分处理的通用分页存储过程 前半部分与后半部分数据访问时间相同,同等访问速度提高一倍
- (转贴)共享一个通用的数据访问类
- 共享一个通用的数据访问类
- SYBASE数据访问接口CTLIB简单的内存池分配
- VC++中数据访问互斥的简单实现方法
- 在asp.net2.0中提供了新一代的数据访问工具--DataSource。它为简单的数据库开发提供了便捷的方式