. */ /** * @brief API to manage batchs * * @file * @author Laurent Giovannoni * @date $date$ * @version $Revision$ * @ingroup core */ /** * Execute a sql query * * @param object $dbConn connection object to the database * @param string $queryTxt path of the file to include * @param boolean $transaction for rollback if error * @return true if ok, exit if ko and rollback if necessary */ function Bt_doQuery($dbConn, $queryTxt, $transaction=false) { $res = $dbConn->query($queryTxt, true); if (!$res) { if ($transaction) { $GLOBALS['logger']->write('ROLLBACK', 'INFO'); $dbConn->query('ROLLBACK', true); } Bt_exitBatch( 104, 'SQL Query error:' . $queryTxt ); } $GLOBALS['logger']->write('SQL query:' . $queryTxt, 'DEBUG'); return true; } /** * Exit the batch with a return code, message in the log and * in the database if necessary * * @param int $returnCode code to exit (if > O error) * @param string $message message to the log and the DB * @return nothing, exit the program */ function Bt_exitBatch($returnCode, $message='') { if (file_exists($GLOBALS['lckFile'])) { unlink($GLOBALS['lckFile']); } if ($returnCode > 0) { $GLOBALS['totalProcessedResources']--; if ($GLOBALS['totalProcessedResources'] == -1) { $GLOBALS['totalProcessedResources'] = 0; } if($returnCode < 100) { if (file_exists($GLOBALS['errorLckFile'])) { unlink($GLOBALS['errorLckFile']); } $semaphore = fopen($GLOBALS['errorLckFile'], "a"); fwrite($semaphore, '1'); fclose($semaphore); } $GLOBALS['logger']->write($message, 'ERROR', $returnCode); Bt_logInDataBase($GLOBALS['totalProcessedResources'], 1, 'return code:' . $returnCode . ', ' . $message); } elseif ($message <> '') { $GLOBALS['logger']->write($message, 'INFO', $returnCode); Bt_logInDataBase($GLOBALS['totalProcessedResources'], 0, 'return code:' . $returnCode . ', ' . $message); } exit($returnCode); } /** * Insert in the database the report of the batch * @param long $totalProcessed total of resources processed in the batch * @param long $totalErrors total of errors in the batch * @param string $info message in db */ function Bt_logInDataBase($totalProcessed=0, $totalErrors=0, $info='') { $query = "insert into history_batch (module_name, batch_id, event_date, " . "total_processed, total_errors, info) values('" . $GLOBALS['batchName'] . "', " . $GLOBALS['wb'] . ", " . $GLOBALS['db']->current_datetime() . ", " . $totalProcessed . ", " . $totalErrors . ", '" . $GLOBALS['func']->protect_string_db(substr(str_replace('\\', '\\\\', str_replace("'", "`", $info)), 0, 999)) . "')"; //. $GLOBALS['func']->protect_string_db(substr($info, 0, 999)) . "')"; /*$dbLog = new dbquery(); $dbLog->connect(); $dbLog->query($query);*/ //Bt_doQuery($GLOBALS['db'], $query); } /** * Get the batch if of the batch * * @return nothing */ function Bt_getWorkBatch() { $req = "select param_value_int from parameters where id = " . "'". $GLOBALS['batchName'] . "_id'"; $GLOBALS['db']->query($req); while ($reqResult = $GLOBALS['db']->fetch_array()) { $GLOBALS['wb'] = $reqResult[0] + 1; } if ($GLOBALS['wb'] == '') { $req = "insert into parameters(id, param_value_int) values " . "('" . $GLOBALS['batchName'] . "_id', 1)"; $GLOBALS['db']->query($req); $GLOBALS['wb'] = 1; } } /** * Update the database with the new batch id of the batch * * @return nothing */ function Bt_updateWorkBatch() { $req = "update parameters set param_value_int = " . $GLOBALS['wb'] . " " . "where id = '" . $GLOBALS['batchName'] . "_id'"; $GLOBALS['db']->query($req); } /** * Include the file requested if exists * * @param string $file path of the file to include * @return nothing */ function Bt_myInclude($file) { if (file_exists($file)) { include_once ($file); } else { throw new IncludeFileError($file); } }