public static function process_db($db_id) { // checking DB connectivity $db_obj = null; try { $db_obj = \OLOG\DB\DBFactory::getDB($db_id); } catch (\Exception $e) { echo $e->getMessage() . "\n\n"; } if (!$db_obj) { echo CliUtil::delimiter(); echo "Can't connect to database " . $db_id . "\n"; echo "Probable problems:\n"; echo "- misconfiguration. App config for database:\n"; //echo var_export(DBFactory::getConfigArr($db_id)) . "\n"; // TODO: fix echo "- database server not accessible\n"; echo "- database not created. It must be created manually.\n"; exit; } $executed_queries_sql_arr = []; try { $executed_queries_sql_arr = \OLOG\DB\DBWrapper::readColumn($db_id, 'select sql_query from ' . self::EXECUTED_QUERIES_TABLE_NAME); } catch (\Exception $e) { echo CliUtil::delimiter(); echo "Can not load the executed queries list from " . self::EXECUTED_QUERIES_TABLE_NAME . " table:\n"; echo $e->getMessage() . "\n\n"; echo "Probably the " . self::EXECUTED_QUERIES_TABLE_NAME . " table was not created. Choose:\n"; echo "\tENTER to create table and proceed\n"; // TODO: constants echo "\tany other key to exit\n"; $command_str = CliUtil::readStdinAnswer(); // TODO: switch if ($command_str == '') { // TODO: constants \OLOG\DB\DBWrapper::query($db_id, 'create table ' . self::EXECUTED_QUERIES_TABLE_NAME . ' (id int not null auto_increment primary key, created_at_ts int not null, sql_query text) engine InnoDB default charset utf8'); } else { exit; } } $sql_arr = self::loadSqlArrForDB($db_id); foreach ($sql_arr as $sql) { if (!in_array($sql, $executed_queries_sql_arr)) { echo CliUtil::delimiter(); echo $sql . "\n"; // TODO: constants echo "\n"; echo "\t" . self::COMMAND_SKIP_QUERY . ": skip query now, do not mark as executed\n"; echo "\t" . self::COMMAND_IGNORE_QUERY . ": ignore query - mark as executed, but do not execute (you can execute one manually)\n"; echo "\tENTER execute query\n"; $command_str = CliUtil::readStdinAnswer(); switch ($command_str) { case '': \OLOG\DB\DBWrapper::query($db_id, $sql); \OLOG\DB\DBWrapper::query($db_id, 'insert into ' . self::EXECUTED_QUERIES_TABLE_NAME . ' (created_at_ts, sql_query) values (?, ?)', array(time(), $sql)); echo "Query executed.\n"; break; case self::COMMAND_IGNORE_QUERY: \OLOG\DB\DBWrapper::query($db_id, 'insert into ' . self::EXECUTED_QUERIES_TABLE_NAME . ' (created_at_ts, sql_query) values (?, ?)', array(time(), $sql)); echo "Query marked as executed without execution.\n"; break; case self::COMMAND_SKIP_QUERY: echo "Query skipped.\n"; break; default: //echo "Unknown command.\n"; throw new \Exception('unknown command'); break; // TODO: repeat entry? } } } }
public static function rollBackTransaction($db_name) { $db_obj = \OLOG\DB\DBFactory::getDB($db_name); if (!$db_obj) { throw new \Exception('getDB failed'); } $db_obj->rollBack(); }