#include <iostream>
using namespace std;
#include <stdio.h>
#define OTL_STREAM_READ_ITERATOR_ON
#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#include <otlv4.h> // include the OTL 4.0 header file
#include "db.h"
#include "wdlib/wdlib.h"
#include "pthread.h"
otl_connect db; // connect object
extern pthread_mutex_t db_mutex;
int ConnectDB(char username[], char password[], char dbname[])
{
otl_connect::otl_initialize(); // initialize OCI environment
try{
char connstr[50] = {0};
sprintf(connstr, "%s/%s@%s", username, password, dbname);
db.rlogon(connstr); // connect to Oracle
info(0, "connect db successfully!/n");
} catch(otl_exception& p){ // intercept OTL exceptions
error(0, "%s", p.msg);
return -1;
}
return 0;
}
void DisconnectDB()
{
db.logoff(); // disconnect from Oracle
}
int EP_Command_Insert(int commType, unsigned char main_type[10], unsigned char secu_para[10], unsigned char load_len[10], unsigned char sub_type[10], unsigned char ep_id[20], unsigned char object_id[10], unsigned char action[10], unsigned char reserve[20], unsigned char random[10], unsigned char mac[10], unsigned char ep_command[128], int commStatus)
{
//#ifdef ORACLE_DB
pthread_mutex_lock(&db_mutex);
otl_stream o(1, // buffer size
"insert into ep_command_log(command_type, main_type, secu_para, load_len, sub_type, ep_id, object_id, action, reserve, random, mac, ep_command, command_status) values(:f11<int>, :f12<char[10]>, :f13<char[10]>, :f14<char[10]>, :f15<char[10]>, :f16<char[20]>, :f17<char[10]>, :f18<char[10]>, :f19<char[20]>, :f20<char[10]>, :f21<char[10]>, :f22<char[128]>, :f23<int>)",
// SQL statement
db // connect object
);
o<<commType<<main_type<<secu_para<<load_len<<sub_type<<ep_id<<object_id<<action<<reserve<<random<<mac<<ep_command<<commStatus;
pthread_mutex_unlock(&db_mutex);
return 0;
//#endif
}
int EP_Command_Update(unsigned char ep_command[128], int commStatus )
{
//#ifdef ORACLE_DB
pthread_mutex_lock(&db_mutex);
otl_stream o(1, // buffer size
"update ep_command_log set command_status=:f11<int> where ep_command =:f12<char[128]>",
// SQL statement
db // connect object
);
o<<commStatus<<ep_command;
pthread_mutex_unlock(&db_mutex);
return 0;
//#endif
}
int UpdateChannelStatus(int channelID, int max_streams, int max_comp_time, int status)
{
//#ifdef ORACLE_DB
pthread_mutex_lock(&db_mutex);
otl_stream o(1, // buffer size
"update channel_info set channel_status=:f11<int>, max_streams=:f12<int>, max_comp_time=:f13<int> where channel_ID=:f14<int>", // SQL statement
db // connect object
);
o<<status<<max_streams<<max_comp_time<<channelID;
pthread_mutex_unlock(&db_mutex);
return 0;
//#endif
}
int UpdateStreamStatus(int channelID, int streamID, int status)
{
//#ifdef ORACLE_DB
pthread_mutex_lock(&db_mutex);
otl_stream o(1, // buffer size
"update channel_stream_info set stream_status=:f11<int>, sent_num=0 where channel_ID=:f12<int> and stream_ID=:f13<int>", // SQL statement
db // connect object
);
o<<status<<channelID<<streamID;
pthread_mutex_unlock(&db_mutex);
return 0;
//#endif
}
int CloseStreamByChannelID(int channelID)
{
//#ifdef ORACLE_DB
pthread_mutex_lock(&db_mutex);
otl_stream o(1, // buffer size
"update channel_stream_info set stream_status=1, sent_num=0 where channel_ID=:f11<int>", // SQL statement
db // connect object
);
o<<channelID;
pthread_mutex_unlock(&db_mutex);
return 0;
//#endif
}
int UpdateStreamSentNum(int channelID, int streamID, int sent_num)
{
//#ifdef ORACLE_DB
pthread_mutex_lock(&db_mutex);
otl_stream o(1, // buffer size
"update channel_stream_info set sent_num=:f11<int> where channel_ID=:f12<int> and stream_ID=:f13<int>", // SQL statement
db // connect object
);
o<<sent_num<<channelID<<streamID;
pthread_mutex_unlock(&db_mutex);
return 0;
//#endif
}
int QueryStreamStatus(int streamid, int *priority, int *ep_msg_type, int *repeat_num, char valid_time[5])
{
//#ifdef ORACLE_DB
otl_stream o(1, // buffer size
"select priority, ep_message_type, repeat_number, valid_time from stream_type_info where stream_ID=:f11<int>", // SELECT statement
db // connect object
);
// create select stream
otl_stream_read_iterator<otl_stream, otl_exception, otl_lob_stream> rs;
rs.attach(o); // attach the iterator "rs" to the stream "o".
o<<streamid;
while(rs.next_row()){ // while not end-of-data
rs.get(1, *priority);
rs.get(2, *ep_msg_type);
rs.get(3, *repeat_num);
rs.get(4, valid_time);
}
rs.detach(); // detach the itertor from the stream
return 0;
//#endif
}