void Oracle8Query::rollback(const XID& xid) { Oracle8QueryEndTransactionGuard endTransactionGuard(*this,xid); OCITransSet::TransRecord* tr; OCITransSet::State state; try { tr = OCITransSet::attach(*handles_p_,xid,0,false); }catch(const OCIException& ex){ fillDBError(); throw CosTransactions::HeuristicHazard(); } { GradSoft::ReadLocker rl(tr->rwlock); state = tr->state; } if (state==OCITransSet::commited) throw CosTransactions::HeuristicCommit(); if (state==OCITransSet::commited_with_error) throw CosTransactions::HeuristicHazard(); if (state==OCITransSet::rollbacked) return; if (state==OCITransSet::forgotted) return; OCISvcCtx* ociSvcCtx_p = handles_p_->getSvcCtx_p(); sword res = OCITransRollback(ociSvcCtx_p,ociError_p(),OCI_DEFAULT); if (res!=OCI_SUCCESS) { fillDBError(); } state=OCITransSet::rollbacked; { GradSoft::WriteLocker wl(tr->rwlock); tr->state = state; } }
static VALUE complex_logoff_execute(void *arg) { complex_logoff_arg_t *cla = (complex_logoff_arg_t *)arg; OCIError *errhp = oci8_errhp; sword rv = OCI_SUCCESS; OCITransRollback(cla->svchp, errhp, OCI_DEFAULT); if (cla->state & OCI8_STATE_SESSION_BEGIN_WAS_CALLED) { rv = OCISessionEnd(cla->svchp, oci8_errhp, cla->usrhp, OCI_DEFAULT); cla->state &= ~OCI8_STATE_SESSION_BEGIN_WAS_CALLED; } if (cla->state & OCI8_STATE_SERVER_ATTACH_WAS_CALLED) { rv = OCIServerDetach(cla->srvhp, oci8_errhp, OCI_DEFAULT); cla->state &= ~OCI8_STATE_SERVER_ATTACH_WAS_CALLED; } if (cla->usrhp != NULL) { OCIHandleFree(cla->usrhp, OCI_HTYPE_SESSION); } if (cla->srvhp != NULL) { OCIHandleFree(cla->srvhp, OCI_HTYPE_SERVER); } if (cla->svchp != NULL) { OCIHandleFree(cla->svchp, OCI_HTYPE_SVCCTX); } free(cla); return (VALUE)rv; }
void oracle_session_rollback (GSQLSession *session) { GSQL_TRACE_FUNC; sword ret; gchar *mess; GSQLWorkspace *workspace; GSQLEOracleSession * o_session; o_session = (GSQLEOracleSession *) session->spec; workspace = gsql_session_get_workspace (session); ret = OCITransRollback(o_session->svchp, o_session->errhp, OCI_DEFAULT); if (ret != OCI_SUCCESS) { mess = oracle_get_error_string (o_session->errhp); gsql_message_add (workspace, GSQL_MESSAGE_ERROR, mess); g_free (mess); } else gsql_message_add (workspace, GSQL_MESSAGE_NOTICE, N_("Transaction rolled back")); }
int COracleDB::oracle_rollback( void ) { ORACLE_HANDLE* handle = ( ORACLE_HANDLE* ) _handle; int status = 0; int errcode = 0; time_t t1 = time( NULL ); while ( ( status = OCITransRollback( handle->svchp, handle->errhp, 0 ) ) == OCI_STILL_EXECUTING ) { if ( time( NULL ) - t1 > 60 ) { // timeout errcode = DB_ERR_TIMEOUT; break; } my_usleep(); } if ( errcode != DB_ERR_TIMEOUT && status ) { errcode = checkerr( handle->errhp, status, _errinfo ); if ( errcode == 1012 || errcode == 12705 || errcode == 3113 || errcode == 3114 || errcode == 12541 || errcode == 12547 ) { errcode = DB_ERR_NOCONNECTION; } else { errcode = DB_ERR_FAILED; } } return errcode; }
static int yada_oracle_rollback(yada_t *_yada, int flags) { char errbuf[1024]; sb4 errcode; /* check database connection */ if(!_yada->_mod->env) { _yada_set_err(_yada, 0, "database unavailable"); return(-1); } if(OCITransRollback(_yada->_mod->ctx, _yada->_mod->err, OCI_DEFAULT) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(-1); } if(_yada->_mod->flags & ORAF_INTRX) _yada->_mod->flags ^= ORAF_INTRX; return(0); }
void DataBase::_DB_EndTrans(int flag) { if(0 == flag) OCITransCommit(DBSession->svchp, DBSession->errhp, (ub4) 0); else OCITransRollback(DBSession->svchp, DBSession->errhp, (ub4) 0); return; }
/* ** Rollback the current transaction. */ static int conn_rollback (lua_State *L) { conn_data *conn = getconnection (L); ASSERT (L, OCITransRollback (conn->svchp, conn->errhp, OCI_DEFAULT), conn->errhp); /* if (conn->auto_commit == 0) sql_begin(conn); */ return 0; }
static VALUE simple_logoff_execute(void *arg) { simple_logoff_arg_t *sla = (simple_logoff_arg_t *)arg; OCIError *errhp = oci8_errhp; sword rv; OCITransRollback(sla->svchp, errhp, OCI_DEFAULT); rv = OCILogoff(sla->svchp, errhp); free(sla); return (VALUE)rv; }
/* ** Set "auto commit" property of the connection. ** If 'true', then rollback current transaction. ** If 'false', then start a new transaction. */ static int conn_setautocommit (lua_State *L) { conn_data *conn = getconnection (L); if (lua_toboolean (L, 2)) { conn->auto_commit = 1; /* Undo active transaction. */ ASSERT (L, OCITransRollback (conn->svchp, conn->errhp, OCI_DEFAULT), conn->errhp); } else { conn->auto_commit = 0; /* sql_begin(conn);*/ } lua_pushboolean(L, 1); return 1; }
/* =begin --- OCISvcCtx#rollback([flags]) rollback the transaction. :flags ((|OCI_DEFAULT|)) only valid. Default value is ((|OCI_DEFAULT|)). correspond native OCI function: ((|OCITransRollback|)) =end */ static VALUE oci8_trans_rollback(int argc, VALUE *argv, VALUE self) { VALUE vflags; oci8_handle_t *h; ub4 flags; sword rv; rb_scan_args(argc, argv, "01", &vflags); Get_Handle(self, h); /* 0 */ Get_Int_With_Default(argc, 1, vflags, flags, OCI_DEFAULT); /* 1 */ rv = OCITransRollback(h->hp, h->errhp, flags); if (rv != OCI_SUCCESS) { oci8_raise(h->errhp, rv, NULL); } return self; }
/****************************************************************************** * * * Function: zbx_db_rollback * * * * Purpose: rollback transaction * * * * Author: Eugene Grigorjev * * * * Comments: do nothing if DB does not support transactions * * * ******************************************************************************/ int zbx_db_rollback() { int rc = ZBX_DB_OK, last_txn_error; if (0 == txn_level) { zabbix_log(LOG_LEVEL_CRIT, "ERROR: rollback without transaction." " Please report it to Job Arranger Team."); assert(0); } last_txn_error = txn_error; /* allow rollback of failed transaction */ txn_error = 0; #if defined(HAVE_IBM_DB2) if (SUCCEED != zbx_ibm_db2_success(SQLEndTran(SQL_HANDLE_DBC, ibm_db2.hdbc, SQL_ROLLBACK))) rc = ZBX_DB_DOWN; if (SUCCEED != zbx_ibm_db2_success(SQLSetConnectAttr(ibm_db2.hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS))) rc = ZBX_DB_DOWN; if (ZBX_DB_OK != rc) { zbx_ibm_db2_log_errors(SQL_HANDLE_DBC, ibm_db2.hdbc); rc = (SQL_CD_TRUE == IBM_DB2server_status() ? ZBX_DB_FAIL : ZBX_DB_DOWN); } #elif defined(HAVE_MYSQL) || defined(HAVE_POSTGRESQL) rc = zbx_db_execute("%s", "rollback;"); #elif defined(HAVE_ORACLE) OCITransRollback(oracle.svchp, oracle.errhp, OCI_DEFAULT); #elif defined(HAVE_SQLITE3) rc = zbx_db_execute("%s", "rollback;"); php_sem_release(&sqlite_access); #endif if (ZBX_DB_DOWN != rc) /* ZBX_DB_FAIL or ZBX_DB_OK or number of changes */ txn_level--; else txn_error = last_txn_error; /* in case of DB down we will repeat this operation */ return rc; }
/* Execute a SQL command which has no resulting table. Examples include insert, delete, update. */ int cllExecSqlNoResult( icatSessionStruct *icss, const char *sqlInput ) { int stat, stat2, stat3; OCIEnv *p_env; OCISvcCtx *p_svc; static OCIStmt *p_statement; char sql[MAX_SQL_SIZE]; ub4 rows_affected; ub4 *pUb4; stat = convertSqlToOra( ( char* )sqlInput, sql ); if ( stat != 0 ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: SQL too long" ); return CAT_OCI_ERROR; } p_svc = ( OCISvcCtx * )icss->connectPtr; p_env = ( OCIEnv * )icss->environPtr; if ( strcmp( sql, "commit" ) == 0 ) { rodsLogSql( sql ); stat = OCITransCommit( p_svc, p_err, ( ub4 ) OCI_DEFAULT ); stat2 = logExecuteStatus( stat, sql, "cllExecSqlNoResult" ); if ( stat != OCI_SUCCESS ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: OCITransCommit failed: %d", stat ); logOraError( LOG_ERROR, p_err, stat ); return CAT_OCI_ERROR; } return 0; } if ( strcmp( sql, "rollback" ) == 0 ) { rodsLogSql( sql ); stat = OCITransRollback( p_svc, p_err, ( ub4 ) OCI_DEFAULT ); stat2 = logExecuteStatus( stat, sql, "cllExecSqlNoResult" ); if ( stat != OCI_SUCCESS ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: OCITransRollback failed: %d", stat ); logOraError( LOG_ERROR, p_err, stat ); return CAT_OCI_ERROR; } return 0; } /* Allocate SQL statement */ stat = OCIHandleAlloc( ( dvoid * ) p_env, ( dvoid ** ) &p_statement, OCI_HTYPE_STMT, ( size_t ) 0, ( dvoid ** ) 0 ); if ( stat != OCI_SUCCESS ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: OCIHandleAlloc failed: %d", stat ); logOraError( LOG_ERROR, p_err, stat ); return CAT_OCI_ERROR; } /* Prepare SQL statement */ stat = OCIStmtPrepare( p_statement, p_err, ( OraText * )sql, ( ub4 ) strlen( sql ), ( ub4 ) OCI_NTV_SYNTAX, ( ub4 ) OCI_DEFAULT ); if ( stat != OCI_SUCCESS ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: OCIStmtPrepare failed: %d", stat ); rodsLog( LOG_ERROR, sql ); logOraError( LOG_ERROR, p_err, stat ); return CAT_OCI_ERROR; } if ( bindTheVariables( p_statement, sql ) != 0 ) { logTheBindVariables( LOG_ERROR ); return CAT_OCI_ERROR; } logTheBindVariables( 0 ); rodsLogSql( sql ); /* Execute statement */ stat = OCIStmtExecute( p_svc, p_statement, p_err, ( ub4 ) 1, ( ub4 ) 0, ( CONST OCISnapshot * ) NULL, ( OCISnapshot * ) NULL, OCI_DEFAULT ); stat2 = logExecuteStatus( stat, sql, "cllExecSqlNoResult" ); if ( stat == OCI_NO_DATA ) { /* Don't think this ever happens, but... */ return CAT_SUCCESS_BUT_WITH_NO_INFO; } /* malloc it so that it's aligned properly, else can get bus errors when doing 64-bit addressing */ pUb4 = ( ub4* ) malloc( sizeof( rows_affected ) ); *pUb4 = 0; rows_affected = 0; if ( stat == OCI_ERROR ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: OCIStmtExecute failed: %d", stat ); logOraError( LOG_ERROR, p_err, stat ); free( pUb4 ); if ( stat2 == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME ) { return stat2; } return CAT_OCI_ERROR; } stat3 = OCIAttrGet( ( dvoid * )p_statement, OCI_HTYPE_STMT, pUb4, 0, OCI_ATTR_ROW_COUNT, ( OCIError * ) p_err ); rows_affected = *pUb4; free( pUb4 ); stat = OCIHandleFree( ( dvoid * )p_statement, OCI_HTYPE_STMT ); /* free the statement */ if ( stat3 != OCI_SUCCESS ) { rodsLog( LOG_ERROR, "cllExecSqlNoResult: OCIAttrGet failed: %d", stat ); return stat3; } /* rodsLog(LOG_NOTICE, "cllExecSqlNoResult: OCIAttrGet, rows_affected: %d", rows_affected); */ if ( rows_affected == 0 ) { return CAT_SUCCESS_BUT_WITH_NO_INFO; } return stat2; }
void *thread_process() { int thisret,size,i,fields,j,bufout=0,counter=0; int fetch_resu; long int datalen,buflen=0; int ociposi=-1; char buf[_buflen],tmp[_buflen],*realdata ; mytext *sqldata; int realdatalen=0; ub1 *bufp; int bufpint=1024; dvoid *hdlptr = (dvoid *) 0,*tmphdlptr = (dvoid *) 0; ub1 in_out = 0; sb2 indptr = 0; ub1 piece = OCI_FIRST_PIECE; ub4 hdltype = OCI_HTYPE_DEFINE, iter = 0, idx = 0; int poid; pthread_detach(pthread_self()); sqldata=(mytext *)malloc(sizeof(mytext)); sqldata->next=0; sqldata->last=0; sqldata->size=0; bufp=(ub1 *)malloc(sizeof(ub1)*_clobmax);//**bufp must set to _clobmax realdata=(char *)malloc(_buflen); realdatalen=_buflen; if((ociposi=getsession())==-1){ printf("pool is busy!!\n"); buf[16]=0; exit(1); } poci[ociposi].threadid=pthread_self(); poci[ociposi].id=ociposi; printf("thread id=%d , ociposi=%d \n",pthread_self(),ociposi); memset(buf,0,_buflen); while(1){ pthread_mutex_lock(&mylock); thisret=accept(sock,NULL,NULL); pthread_mutex_unlock(&mylock); poci[ociposi].busy=1; bufout=0; conn_times++; if(conn_times>=1000000){conn_times1++;conn_times=0;} if(conn_times1>=1000000)conn_times1=0; printf("connections %d millions %d socket=%d ociposi=%d\n",conn_times1,conn_times,thisret,ociposi); poci[ociposi].datetime=datetime->tm_min*60+datetime->tm_sec; poci[ociposi].transfer=0; //data_buffer(sqldata,"",2,0); while(sqldata->next){ sqldata=sqldata->next; } sqldata->size=0; while(sqldata->last){ sqldata=sqldata->last; sqldata->size=0; } poci[ociposi].sock=thisret; while(size=recv(thisret,buf,_buflen,0)){ buflen=size; buf[buflen]=0; poci[ociposi].transfer=1; //printf("--buflen=%d \n",buflen); if(buflen>2){ } else{ bufout++; if(bufout>=5 || buflen<=0)break; } size=buflen; if(poci[ociposi].sock==0)continue; poci[ociposi].datetime=datetime->tm_min*60+datetime->tm_sec; data_buffer(sqldata,buf,1,buflen); //end j=0; if(size>9) for(i=size-9;i<size;i++){ tmp[j]=buf[i]; j++; } tmp[j]=0; if((strncmp(buf,_end,strlen(_end))==0)||(strncmp(tmp,_end,strlen(_end))==0)){ break; } //set commit off if(strncmp(buf,_commitoff,strlen(_commitoff))==0){ poci[ociposi].commitmode=1; memset(buf,0,sizeof(buf)); send(thisret,_autocommitoff,strlen(_autocommitoff),0); continue; } //_commit if(strncmp(buf,_commit,strlen(_commit))==0){ OCITransCommit(poci[ociposi].svchp,poci[ociposi].errhp,OCI_DEFAULT ); memset(buf,0,sizeof(buf)); continue; } //set commit on if(strncmp(buf,_commiton,strlen(_commiton))==0){ poci[ociposi].commitmode=0; memset(buf,0,sizeof(buf)); send(thisret,_autocommiton,strlen(_autocommiton),0); continue; } //rollback if(strncmp(buf,_rollback,strlen(_rollback))==0){ OCITransRollback(poci[ociposi].svchp,poci[ociposi].errhp,OCI_DEFAULT) ; poci[ociposi].commitmode=1; memset(buf,0,sizeof(buf)); continue; } //_serverdown if(strncmp(buf,_serverdown,strlen(_serverdown))==0){ OCITerminate(OCI_DEFAULT); exit(1); } if(sqldata->size+1>=_mytextlen) while(sqldata->next){ if(sqldata->size+1>=_mytextlen) sqldata=sqldata->next; else break; } //end input size=sqldata->size; datalen=size-2; j=0; if(size>15) for(i=size-15;i<size;i++){ tmp[j]=sqldata->mydata[i]; j++; } tmp[j]=0; if((strncmp(tmp,_inputend,strlen(_inputend))==0)){ datalen=data_buffer(sqldata,buf,3,0); if(datalen+1>realdatalen){ printf("free\n"); free(realdata); printf("malloc\n"); realdata=(char *)malloc(datalen+1); realdatalen=datalen+1; } data_buffer(sqldata,realdata,4,0); size=realdatalen; realdata[size-17]=0; OCIHandleAlloc(poci[ociposi].envhp,(dvoid*)&poci[ociposi].stmthp,OCI_HTYPE_STMT,0,0); printf("oracle_query \n"); fields=oracle_query(&poci[ociposi],realdata,datalen-17,tmp); printf("oracle_query ok fields=%d \n",fields); if(fields<=0)break; //printf("start fetch\n"); i=0; counter=0; if(fields>0){ piece = OCI_FIRST_PIECE ; bufp[0]=0; fetch_data(&poci[ociposi]); counter++;printf("counter=%d \n",counter); send_process(_output_begin,strlen(_output_begin),&poci[ociposi]); while (poci[ociposi].status != OCI_NO_DATA){ //if(OCI_NEED_DATA==poci[ociposi].status){ //bufpint=4096; //poci[ociposi].status = OCIStmtGetPieceInfo(poci[ociposi].stmthp, poci[ociposi].errhp, &hdlptr, &hdltype,&in_out, &iter, &idx, &piece); //poci[ociposi].status = OCIStmtSetPieceInfo(hdlptr, hdltype, poci[ociposi].errhp,(dvoid *) bufp, &bufpint, piece,(CONST dvoid *) &indptr, (ub2 *) 0); //} //printf("data=%s\n",poci[ociposi].data[0]); //printf("data=%s\n",poci[ociposi].data[1]); //printf("data=%s\n",poci[ociposi].data[2]); fetch_data(&poci[ociposi]); if(poci[ociposi].status<0)break; counter++;printf("OCI_NO_DATA=%d , poci[ociposi].status=%d , counter=%d fetch_resu=%d \n",OCI_NO_DATA,poci[ociposi].status,counter); bufp[bufpint]=0; for (i=0;i<fields;i++){ send_process(poci[ociposi].data[i],strlen(poci[ociposi].data[i]),&poci[ociposi]); if(i+1<fields){ send_process(_output_column,strlen(_output_column),&poci[ociposi]); } } send_process(_output_row,strlen(_output_row),&poci[ociposi]); /* if(poci[ociposi].status==OCI_NEED_DATA){ if(tmphdlptr!=hdlptr){ i++; if(i>1) if((i-1)%fields==0){ send_process(_output_row,strlen(_output_row),&poci[ociposi]); }else{ send_process(_output_column,strlen(_output_column),&poci[ociposi]); } tmphdlptr=hdlptr; } send_process(bufp,bufpint,&poci[ociposi]); }*/ } if(i>1){ send_process(_output_column,strlen(_output_column),&poci[ociposi]); send_process(bufp,bufpint,&poci[ociposi]); send_process(_output_row,strlen(_output_row),&poci[ociposi]); } send_process(_output_end,strlen(_output_end),&poci[ociposi]); }else{ if((fields<0) && (strlen(poci[ociposi].errbuf)>0)){ send_process(_output_beginerr,strlen(_output_beginerr),&poci[ociposi]); send_process(poci[ociposi].errbuf,512,&poci[ociposi]); send_process(_output_end,strlen(_output_end),&poci[ociposi]); }else{ send_process(_ok,strlen(_ok),&poci[ociposi]); } } OCIHandleFree(poci[ociposi].stmthp,OCI_HTYPE_STMT); //printf("fetch ok\n"); send_process("",-1,&poci[ociposi]); //printf("data send ok\n"); datalen=0; //data_buffer(sqldata,"",2,0); while(sqldata->next){ sqldata=sqldata->next; } sqldata->size=0; while(sqldata->last){ sqldata=sqldata->last; sqldata->size=0; } //printf("query complete\n"); } poci[ociposi].transfer=0; } close(thisret); poci[ociposi].busy=0; if(fields==-99){ poci[ociposi].id=-1; OCILogoff(poci[ociposi].svchp,poci[ociposi].errhp); OCIHandleFree(poci[ociposi].envhp,OCI_HTYPE_ENV); pthread_create(&ph[ociposi], NULL, &thread_process,NULL); printf("exit program \n"); break; } } }
int OracleConnection_rollback(T C) { assert(C); C->lastError = OCITransRollback(C->svc, C->err, OCI_DEFAULT); return C->lastError == OCI_SUCCESS; }
// Это просто роллбак... int ora_rollback(database *db) { t_ora *o = db->h; if (!o) return ora_error(db); if (!ora_ok(OCITransRollback(o->svchp,o->errhp, 0))) return ora_error(db); return 1; }
JP_INTERNAL(int) JP_Rollback(TJConnector &conn) { conn.result = OCITransRollback(conn.ociSvcCtx, conn.ociError, OCI_DEFAULT); return _Result(conn); }
/* * Add a new message to the database. * session - session name * sender_uid - * rcpts - * content - * recv_time - time of arrival * quiet - print error messages? * Returns 0 on success */ int oralog_db_new_msg(char *session, char *sender_uid, char **rcpts, char *content, time_t recv_time, int quiet) { int i; /* There will be at least 2 INSERTs - 1 into 'messages' table and 1 (at least) into the 'recipients' table. */ text *sqlstmt_msg = (text *)"INSERT INTO messages VALUES(messages_seq.nextval, :session_uid, :sender_uid, :content, :recv_time) RETURNING id INTO :msg_id"; text *sqlstmt_rcp = (text *)"INSERT INTO recipients VALUES(recipients_seq.nextval, :recipient_uid, :msg_id)"; /* Statement & bind handles */ OCIStmt *hp_stmt_msg = NULL; OCIStmt *hp_stmt_rcp = NULL; OCIBind *bind_msg[5] = { NULL }; OCIBind *bind_rcp[2] = { NULL }; /* Control vars */ sword retstat = 0; int errors = 0; int print_errors = (quiet) ? 0 : 1; OCIError *hp_error = NULL; /* Date handling */ uword invalid = 0; OCIDate oci_recvtime; struct tm *tm_recvtime = NULL; /* ID of inserted message will be put here */ OCINumber msg_id; int init_val = 0; /* this variable is needed for initialization only */ pthread_mutex_lock(&oralog_oper_lock); if (!oralog_is_connected()) { debug("[logsoracle] can't log msg - not connected\n"); pthread_mutex_unlock(&oralog_oper_lock); return 1; } check_string_len(session); check_string_len(sender_uid); check_string_len(content); /* Create local handles (OCIBind's are created automaticly) */ OCIHandleAlloc( (dvoid *)hp_env, (dvoid **)&hp_stmt_msg, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0); OCIHandleAlloc( (dvoid *)hp_env, (dvoid **)&hp_stmt_rcp, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0); OCIHandleAlloc( (dvoid *)hp_env, (dvoid **)&hp_error, OCI_HTYPE_ERROR, 0, (dvoid **) 0); /* Create DATE in Oracle format */ tm_recvtime = localtime(&recv_time); memset(&oci_recvtime, 0, sizeof(OCIDate)); OCIDateSetTime( &oci_recvtime, (ub1)tm_recvtime->tm_hour, (ub1)tm_recvtime->tm_min, (ub1)tm_recvtime->tm_sec ); OCIDateSetDate( &oci_recvtime, (sb2)tm_recvtime->tm_year+1900, (ub1)tm_recvtime->tm_mon+1, (ub1)tm_recvtime->tm_mday ); /* check if provided 'recv_time' was mapped correctly */ retstat = OCIDateCheck(hp_error, &oci_recvtime, &invalid); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* prepare SQL statements */ debug("[logsoracle] preparing new messages insert statement\n"); retstat = OCIStmtPrepare(hp_stmt_msg, hp_error, (text *)sqlstmt_msg, (ub4)ora_strlen((char *)sqlstmt_msg), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; debug("[logsoracle] preparing new recipients insert statement\n"); retstat = OCIStmtPrepare(hp_stmt_rcp, hp_error, (text *)sqlstmt_rcp, (ub4)ora_strlen((char *)sqlstmt_rcp), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* bind the placeholders */ debug("[logsoracle] binding messages insert..\n"); retstat = OCIBindByName(hp_stmt_msg, &bind_msg[0], hp_error, (text *) ":session_uid", -1, (dvoid *)session, ora_strlen(session)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt_msg, &bind_msg[1], hp_error, (text *) ":sender_uid", -1, (dvoid *)sender_uid, ora_strlen(sender_uid)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt_msg, &bind_msg[2], hp_error, (text *) ":content", -1, (dvoid *)content, ora_strlen(content)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt_msg, &bind_msg[3], hp_error, (text *) ":recv_time", -1, (dvoid *) &oci_recvtime, (sword) sizeof(OCIDate), SQLT_ODT, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* Create a new OCINumber - will be under msg_id */ retstat = OCINumberFromInt(hp_error, &init_val, sizeof(init_val), OCI_NUMBER_SIGNED, &msg_id); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* bind the OCINumber */ retstat = OCIBindByName(hp_stmt_msg, &bind_msg[4], hp_error, (text *) ":msg_id", -1, (dvoid *) &msg_id, (sword) sizeof(msg_id), SQLT_VNU, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* Statement ready - execute */ debug("[logsoracle] executing insert on messages\n"); retstat = OCIStmtExecute(hp_service, hp_stmt_msg, hp_error, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* You can check recieved message ID like this: retstat = OCINumberToInt(hp_error, &msg_id, sizeof(int), OCI_NUMBER_SIGNED, &tmpval); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; debug("[logsoracle] recieved message id: %d\n", tmpval); */ /* Insert into recipients table */ if(rcpts) { for(i=0; rcpts[i] != NULL; i++) { check_string_len(rcpts[i]); debug("[logsoracle] binding recipients\n"); retstat = OCIBindByName(hp_stmt_rcp, &bind_rcp[0], hp_error, (text *) ":recipient_uid", -1, (dvoid *)rcpts[i], ora_strlen(rcpts[i])+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt_rcp, &bind_rcp[1], hp_error, (text *) ":msg_id", -1, (dvoid *) &msg_id, (sword) sizeof(msg_id), SQLT_VNU, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; debug("[logsoracle] executing insert on recipients\n"); retstat = OCIStmtExecute(hp_service, hp_stmt_rcp, hp_error, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; } } /* Commit transaction */ if(!errors) { debug("[logsoracle] commit\n"); OCITransCommit(hp_service, hp_error, (ub4) 0); } else { debug("[logsoracle] errors present - aborting transaction\n"); OCITransRollback(hp_service, hp_error, (ub4) OCI_DEFAULT); } /* Cleanup (bind handles should be removed as a part of statement) */ if(hp_stmt_msg) OCIHandleFree(hp_stmt_msg, OCI_HTYPE_STMT); if(hp_stmt_rcp) OCIHandleFree(hp_stmt_rcp, OCI_HTYPE_STMT); if(hp_error) OCIHandleFree(hp_error, OCI_HTYPE_ERROR); pthread_mutex_unlock(&oralog_oper_lock); return 0; }
/* * Add a new status change to the database * Params: * session - user session name * uid - uid of person changing status * status - type of new status (away|back|...) * descr - new description * change_time - time of status change (no. of seconds since 1st Jan 1970) * quiet - print error messages or not * Returns 0 on success. */ int oralog_db_new_status(char *session, char *uid, char *status, char *descr, time_t change_time, int quiet) { OCIStmt *hp_stmt = NULL; /* SQL statement handle */ OCIBind *bind[5] = { NULL }; /* Needed for C-var to SQL-var binding */ sword retstat = 0; /* Return value of function */ int errors = 0; /* Error counter */ uword invalid = 0; /* for DateCheck() */ OCIDate oci_date; /* for 'change_time' - oracle internal representation */ struct tm *tm_chtime = NULL; /* for 'change_time' - standard C representation */ int print_errors = (quiet) ? 0 : 1; /* if nonzero then print errors into status window */ OCIError *hp_error = NULL; /* oci error handle */ /* SQL statement to execute */ text *sqlstmt = (text *) "INSERT INTO status_changes VALUES( status_changes_seq.nextval, :session_uid, :changes_uid, :status, :descr, :change_time)"; pthread_mutex_lock(&oralog_oper_lock); if (!oralog_is_connected()) { debug("[logsoracle] can't log status - not connected\n"); pthread_mutex_unlock(&oralog_oper_lock); return 1; } check_string_len(session); check_string_len(uid); check_string_len(status); check_string_len(descr); /* Create local handles (OCIBind's are created automaticly) */ OCIHandleAlloc( (dvoid *)hp_env, (dvoid **)&hp_stmt, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0); OCIHandleAlloc( (dvoid *)hp_env, (dvoid **)&hp_error, OCI_HTYPE_ERROR, 0, (dvoid **) 0); /* Create DATE */ tm_chtime = localtime(&change_time); memset(&oci_date, 0, sizeof(OCIDate)); OCIDateSetTime( &oci_date, (ub1)tm_chtime->tm_hour, (ub1)tm_chtime->tm_min, (ub1)tm_chtime->tm_sec ); OCIDateSetDate( &oci_date, (sb2)tm_chtime->tm_year+1900, (ub1)tm_chtime->tm_mon+1, (ub1)tm_chtime->tm_mday ); /* check if provided 'change_time' was mapped correctly */ retstat = OCIDateCheck(hp_error, &oci_date, &invalid); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* prepare SQL statement */ debug("[logsoracle] preparing new status statement\n"); retstat = OCIStmtPrepare(hp_stmt, hp_error, (text *)sqlstmt, (ub4)ora_strlen((char *)sqlstmt), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* bind variables */ debug("[logsoracle] binding..\n"); retstat = OCIBindByName(hp_stmt, &bind[0], hp_error, (text *) ":session_uid", -1, (dvoid *)session, ora_strlen(session)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt, &bind[1], hp_error, (text *) ":changes_uid", -1, (dvoid *) uid, ora_strlen(uid)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt, &bind[2], hp_error, (text *) ":status", -1, (dvoid *) status, ora_strlen(status)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; retstat = OCIBindByName(hp_stmt, &bind[3], hp_error, (text *) ":descr", -1, (dvoid *) descr, ora_strlen(descr)+1, SQLT_STR, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* Oracle type DATE can be easily bind by using OCIDate struct and SQLT_ODT datatype */ retstat = OCIBindByName(hp_stmt, &bind[4], hp_error, (text *) ":change_time", -1, (dvoid *) &oci_date, (sword) sizeof(OCIDate), SQLT_ODT, (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* Statement ready - execute */ debug("[logsoracle] executing\n"); retstat = OCIStmtExecute(hp_service, hp_stmt, hp_error, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT); if(oralog_is_error(hp_error, retstat, print_errors)) errors++; /* Commit transaction */ if(!errors) { debug("[logsoracle] commit\n"); OCITransCommit(hp_service, hp_error, (ub4) 0); } else { debug("[logsoracle] errors present - aborting transaction\n"); OCITransRollback(hp_service, hp_error, (ub4) OCI_DEFAULT); } /* Cleanup (bind handles should be removed as a part of statement) */ if(hp_stmt) OCIHandleFree(hp_stmt, OCI_HTYPE_STMT); if(hp_error) OCIHandleFree(hp_error, OCI_HTYPE_ERROR); pthread_mutex_unlock(&oralog_oper_lock); return 0; }