Knowledge Base Nr: 00189 otlsample.cpp - http://www.swe-kaiser.de
Downloads:
C++ Datenbankanbindung mit OTL
//------------defines und header
#define OTL_ORA9I
#define OTL_STL
#include "otlv4.h"
//------------init
FDatabase::FDatabase()
{
otl_connect::otl_initialize();
}
//------------connect/disconnect/exceptionhandling
bool FDatabase::connect(const char *user, const char *pwd, const char *dbname)
{
char tmp[255];
sprintf (tmp, "%s/%s@%s", user, pwd, dbname);
try
{
m_db.rlogon (tmp);
otl_stream i(50, "ALTER SESSION SET NLS_NUMERIC_CHARACTERS=',.'", m_db);
}
catch (otl_exception& p)
{
exception ("connect", p);
return false;
}
return true;
}
bool FDatabase::disconnect()
{
m_db.logoff();
return true;
}
void FDatabase::exception (char *strFuncName, otl_exception &p)
{
LogMessage(LOG_PRIO_ERR, "%s: Oracle Fehlermeldung: %s", strFuncName, p.msg);
LogMessage(LOG_PRIO_ERR, "%s: SQL Statement: %s", strFuncName, p.stm_text);
LogMessage(LOG_PRIO_ERR, "%s: SQL Variableninfo: %s", strFuncName, p.var_info);
// Rollback bei exception ...
m_db.rollback();
}
//------------select
int FDatabase::readChargenliste(QListView* pListview, const char* lpszFertNr)
{
memset(&m_chargen, 0, sizeof(sCHARGEN));
char szQuery[255];
pListview->setSorting(-1);
pListview->clear();
QListViewItem* pItem = 0;
try
{
sprintf(szQuery, "SELECT FERTNR, CFERTNR, CHNRA, FERTSCH, STATUS"
" FROM V_CHARGEN"
" WHERE FERTNR = %s and SUBSTR(fertsch,3,6) = '%s'"
, lpszFertNr, m_arb.szFertsch);
otl_stream i(50, szQuery, m_db);
if (i.eof())
{
LogMessage(LOG_PRIO_ERR, "Keine Daten gefunden (V_CHARGEN)!!!");
}
while (!i.eof())
{
LogMessage(LOG_PRIO_INFO, "Auslesen von V_CHARGEN");
i>>m_chargen.nFERTNR;
i>>m_chargen.nCFERTNR;
i>>m_chargen.nCHNRA;
i>>m_chargen.szFERTSCH;
i>>m_chargen.szSTATUS;
LogMessage(LOG_PRIO_MSG, "FERTNR: %d", m_chargen.nFERTNR);
pItem = new QListViewItem( pListview, pItem
, QString("%1").arg(m_chargen.nFERTNR)
, QString("%1").arg(m_chargen.nCFERTNR)
, QString("%1").arg(m_chargen.nCHNRA)
, m_chargen.szFERTSCH
, m_chargen.szSTATUS
);
}
}
catch (otl_exception& p)
{
exception ("readChargenliste", p);
return -1;
}
pItem = pListview->currentItem();
if (pItem)
pListview->setSelected(pItem, true);
return 0;
}
//------------update
int FDatabase::setAktivAuftrag(int nFertnr)
{
QString strUpdate;
strUpdate.sprintf ("UPDATE arbplatz SET fertnr = %d", nFertnr);
LogMessage(LOG_PRIO_INFO, strUpdate);
try
{
otl_stream o(1, strUpdate, m_db);
m_db.commit();
}
catch (otl_exception& p)
{
exception ("setAktivAuftrag", p);
return -1;
}
m_arb.nFertnr = nFertnr;
return 0;
}
//------------db procedure/function
int FDatabase::getInlineAuftrag() //return >0: FertNr 0:kein aktiver auftrag <0:fehler
{
char strQuery[1024];
try
{
sprintf(strQuery, "begin "
" :ec<int,out> := GET_INLINE_AUFTRAG(:V_ARBPLATZ<char[255],in>, :oracle_err<char[255],out>); "
"end;"
);
otl_stream i(1, strQuery, m_db);
i<<m_arb.szFertsch;
i.flush();
while (!i.eof())
{
LogMessage(LOG_PRIO_INFO, "Auslesen von GET_INLINE_AUFTRAG");
int nFertnr;
char oracle_err[255];
i>>nFertnr;
i>>oracle_err;
if (nFertnr == 0)
{
LogMessage(LOG_PRIO_MSG, "GET_INLINE_AUFTRAG: kein auftrag");
return 0;
}
else if (nFertnr < 0)
{
LogMessage(LOG_PRIO_MSG, "Error von GET_INLINE_AUFTRAG(): ec:%d err:%s", nFertnr, oracle_err);
return -1;
}
LogMessage(LOG_PRIO_MSG, "GET_INLINE_AUFTRAG: auftrag starten: %d", nFertnr);
return nFertnr;
}
}
catch (otl_exception& p)
{
exception ("getInlineAuftrag", p);
return -2;
}
return 0;
}