简单通用数据访问映射层

#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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wolfseek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值