Ejemplo n.º 1
0
 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?
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Удаление записи
  * @param $model_obj
  * @return \PDOStatement
  */
 public static function deleteModelObj($model_obj)
 {
     self::exceptionIfObjectIsIncompatibleWithActiveRecord($model_obj);
     $model_class_name = get_class($model_obj);
     $db_id = $model_class_name::DB_ID;
     $db_table_name = $model_class_name::DB_TABLE_NAME;
     $db_id_field_name = self::getIdFieldName($model_obj);
     $reflect = new \ReflectionClass($model_obj);
     $property_obj = $reflect->getProperty($db_id_field_name);
     $property_obj->setAccessible(true);
     $model_id_value = $property_obj->getValue($model_obj);
     if ($model_id_value == '') {
         throw new \Exception('Deleting not saved object');
     }
     $result = \OLOG\DB\DBWrapper::query($db_id, 'DELETE FROM ' . $db_table_name . ' where ' . $db_id_field_name . ' = ?', array($model_id_value));
     //\OLOG\Logger\Logger::logObjectEvent($model_obj, 'DELETE');
     return $result;
 }