0a7f913e2d
Files correlati : Ricompilazione Demo : [ ] Commento : Rimosso il programma crpa1 che diventa ps1001200 (per non disperdere troppo le personalizzazioni del CRPA)!! Modifiche a crpa.cpp fatte l'anno scorso e mai committate. git-svn-id: svn://10.65.10.50/trunk@20359 c028cbd2-c16b-5b4b-a496-9718f37d4682
174 lines
3.6 KiB
C++
Executable File
174 lines
3.6 KiB
C++
Executable File
#include <iostream>
|
||
#include <fstream>
|
||
|
||
#include <time.h>
|
||
|
||
#define WINDOWS_EXTRA_LEAN
|
||
#include <windows.h>
|
||
|
||
#pragma warning(disable : 4786)
|
||
#define WIN32COMMON
|
||
#include <occi.h>
|
||
|
||
using namespace oracle::occi;
|
||
using namespace std;
|
||
|
||
class campo2pmc
|
||
{
|
||
private:
|
||
Environment* env;
|
||
Connection* conn;
|
||
|
||
public:
|
||
bool ok() const { return env != NULL && conn != NULL; }
|
||
int executeCommand(const char* command);
|
||
int readCommands(const char* filename);
|
||
void writeResult(const char* resultfile, int error);
|
||
void writeLog(const char* message);
|
||
bool getIniParam(const char* name, string& value);
|
||
|
||
campo2pmc();
|
||
~campo2pmc();
|
||
};
|
||
|
||
campo2pmc::campo2pmc(): env(NULL), conn(NULL)
|
||
{
|
||
string username, password, connect;
|
||
getIniParam("Username", username);
|
||
getIniParam("Password", password);
|
||
getIniParam("Connect", connect);
|
||
try
|
||
{
|
||
env = Environment::createEnvironment(Environment::DEFAULT);
|
||
conn = env->createConnection(username, password, connect);
|
||
}
|
||
catch (...)
|
||
{
|
||
conn = NULL;
|
||
|
||
char message[1024];
|
||
sprintf(message, "Impossibile connettersi a: %s", connect);
|
||
writeLog(message);
|
||
}
|
||
}
|
||
|
||
campo2pmc::~campo2pmc()
|
||
{
|
||
if (env != NULL)
|
||
{
|
||
if (conn != NULL)
|
||
env->terminateConnection(conn);
|
||
Environment::terminateEnvironment(env);
|
||
}
|
||
}
|
||
|
||
void campo2pmc::writeLog(const char* message)
|
||
{
|
||
ofstream logfile("crpa.log", ios::app);
|
||
|
||
char date[128];
|
||
char time[128];
|
||
_strtime(time);
|
||
_strdate(date);
|
||
logfile << date << ' ' << time << ' ' << message << endl;
|
||
}
|
||
|
||
void campo2pmc::writeResult(const char* resultfile, int error)
|
||
{
|
||
char stringerr[16];
|
||
sprintf(stringerr, "%d", error);
|
||
::WritePrivateProfileString("Transaction", "Error", stringerr, resultfile);
|
||
::WritePrivateProfileString("Transaction", "Result", error ? "ERROR": "OK", resultfile);
|
||
}
|
||
|
||
bool campo2pmc::getIniParam(const char* name, string& value)
|
||
{
|
||
char val[256];
|
||
bool ok = ::GetPrivateProfileString("Oracle", name, "", val,
|
||
sizeof(val), "./crpa.ini") != 0;
|
||
if (ok) value = val;
|
||
return ok;
|
||
}
|
||
|
||
|
||
int campo2pmc::executeCommand(const char* command)
|
||
{
|
||
int err = 0;
|
||
if (ok())
|
||
{
|
||
Statement* stmt = conn->createStatement(command);
|
||
if (stmt != NULL)
|
||
{
|
||
try
|
||
{
|
||
stmt->executeUpdate();
|
||
}
|
||
catch(SQLException ex)
|
||
{
|
||
err = ex.getErrorCode();
|
||
OCCI_STD_NAMESPACE::string mess = ex.getMessage();
|
||
char message[1024];
|
||
sprintf(message, "Errore %d: %s nel comando %s", err, mess.c_str(), command);
|
||
writeLog(message);
|
||
}
|
||
conn->terminateStatement(stmt);
|
||
}
|
||
else
|
||
{
|
||
char message[1024];
|
||
sprintf(message, "Statement non valido: %s", command);
|
||
writeLog(message);
|
||
err = -2;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
writeLog("Connessione non valida");
|
||
err = -1;
|
||
}
|
||
return err;
|
||
}
|
||
|
||
int campo2pmc::readCommands(const char* filename)
|
||
{
|
||
int err = 0;
|
||
|
||
if (ok())
|
||
{
|
||
ifstream fin(filename);
|
||
while ((!fin.eof()) && (err==0))
|
||
{
|
||
char line[4096];
|
||
fin.getline(line, sizeof(line), '<EFBFBD>');
|
||
if (!fin.eof())
|
||
{
|
||
strcat(line,"\n");
|
||
err = executeCommand(line);
|
||
}
|
||
}
|
||
if (err != 0)
|
||
conn->rollback();
|
||
}
|
||
else
|
||
err = -3;
|
||
return err;
|
||
}
|
||
|
||
|
||
int main(int argc, char* argv[])
|
||
{
|
||
const char* commandfile = "crpa.sql";
|
||
const char* resultfile = "./crpasql.ini";
|
||
if (argc > 1)
|
||
commandfile = argv[1];
|
||
if (argc > 2)
|
||
resultfile = argv[2];
|
||
campo2pmc passa;
|
||
int err = -4;
|
||
if (passa.ok())
|
||
{
|
||
err = passa.readCommands(commandfile);
|
||
passa.writeResult(resultfile, err);
|
||
}
|
||
return err;
|
||
} |