您的位置:首页 > 其它

简单通用数据访问映射层

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: