I want to be able to use 4 different variables in a select statement in c ++

107 views Asked by At

First off I am new to C++ and I am trying to learn it to update one of my companies legacy apps. Which is on windows xp and visual c++ 6.0 which is not fun.. any ways I have this select statement that reads my database and searches for a ResourceID like what ever I set my m_operSet. I want to be able to use 4 specific variables in this like statement. problem is when I set it to a specific one it will not find any other resources besides what I set it to. here is my code this is my operations.Set.cpp and my bordDlg.cpp.

This is my list of variables I wish to use for the m_operSet

'COB'

'NIC'

'SIC'

'PRS'

Basically I want to use a if else statement.. say if COB use COB ifelse NIC use NIC ifelse SIC use SIC IFelse PRS use PRS

BordDlg.cpp

 // BordDlg.cpp : implementation file

        #include "operationSet.h"
        #include "custSet.h"


        // Initial filter is set in m_custSet Constuctor
        m_custSet.setBaseID(baseID);
        m_custSet.setLotID(lotID);

        // Initial filter is set in m_operSet Constuctor
        m_operSet.setBaseID(baseID);
        m_operSet.setLotID(lotID);
        m_operSet.setSubID(subID);
        m_operSet.setSplitID(splitID);

        // This is where I set this at and where I want to use either 'COB' 'NIC' 'SIC' 'PRS'
        m_operSet.setResourceID("COB");

operationSet.cpp

        // operationSet.cpp : implementation file
        //

        #include "stdafx.h"

        #include "operationSet.h"


         void operationSet::setBaseID(CString baseID)
         {
            m_strFilter += "and WORKORDER_BASE_ID LIKE '" + baseID + " ";
         } 

         void operationSet::setLotID(CString lotID)
         {
            m_strFilter += "and WORKORDER_LOT_ID LIKE '" + lotID + "' ";
         } 

         void operationSet::setSplitID(CString splitID)
         {
            m_strFilter +=  "and [WORKORDER_SPLIT_ID] LIKE '" + splitID + "' ";

         }

         void operationSet::setSubID(CString subID)
         {
            m_strFilter +=  "and [WORKORDER_SUB_ID] LIKE '" + subID + "' ";

         }

         void operationSet::setResourceID(CString resID)
         {
                m_strFilter +=  "and [RESOURCE_ID] LIKE '" + resID + "' ";

         }
1

There are 1 answers

0
bolov On BEST ANSWER

Those functions build a query string like this:

SELECT ..whatever.. FROM ..whatever.. WHERE ... \
... and [WORKORDER_SUB_ID] LIKE 'wid'   and [RESOURCE_ID] LIKE 'rid'   and ...  
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~    ~~~~~~~~~
    ^   build by setSubID               ^ build by setResourceID       ^ by other

you need to build this:

... and [WORKORDER_SUB_ID] LIKE 'wid'  \
and ( [RESOURCE_ID] LIKE 'rid1' or  [RESOURCE_ID] LIKE 'rid2' ) and ...  

the quick fix (not recommended) is:

  m_operSet.setResourceID("COB' or [RESOURCE_ID] LIKE 'NIC' or [RESOURCE_ID] "
                          "LIKE 'SIC' or [RESOURCE_ID] LIKE 'PRS");

and

  m_strFilter += "and ( [RESOURCE_ID] LIKE '" + resID + "' ) ";

This way you end up with some kind of monster where apostrophes are added without consistency both on caller and on called function.


A better way:

void operationSet::setResourceID(std::vector<std::string> const &resIDs) {
  m_strFilter += "and (";
  for (auto it = resIDs.begin(); it != resIDs.end(); ++it) {
    if (it != resIDs.begin())
      m_strFilter += " or ";
    m_strFilter += " [RESOURCE_ID] LIKE '" + *it + "' ";
  }

  m_strFilter += " ) ";
}

m_operSet.setResourceID({"COB", "NIC", "SIC", "PRS"});

things you need to check if your application framework:

  • CString and std::string. Most likely they didn't write an operator + for them. So you have 2 options: use .c_str() if they have an operator + for CString and c strings, or just use std::vector<CString>

  • The code is written in c++11. See if your compiler supports c++11. If not you need to replace auto with std::vector<std::string>::const_iterator

and the call with:

std::string res_id_arr[4] = {"COB", "NIC", "SIC", "PRS"};
std::vector<std::string> res_ids(res_id_arr, res_id_arr + 4);
m_operSet.setResourceID(res_ids);
  • Performance issues: A lot of copies will be passed around this way (but again that is true for CString also). So you really need to compile with c++11 and use move semantics. There are resources on the net for moving in c++11