Salome HOME
[EDF17719] : When comparison fails do not loose the content of data
[modules/yacs.git] / src / SALOMESDS / SALOMESDS_Transaction.cxx
index 29d9a3ed88a22bbfc99ef98ff0178ac6b2662831..cff92a346264c47ffee6b94cc441ec79c25f9cae 100644 (file)
@@ -87,20 +87,53 @@ void TransactionRdExtVarCreate::perform()
   _dsct->createRdExtVarInternal(_var_name,data2);
 }
 
-TransactionRdExtVarFreeStyleCreate::TransactionRdExtVarFreeStyleCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue, const SALOME::ByteVec& sha1):TransactionRdExtVarCreate(dsct,varName,constValue)
+TransactionRdExtVarFreeStyleCreate::TransactionRdExtVarFreeStyleCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue, const char *compareFuncContent):_cmp_func_content(compareFuncContent),_cmp_func(nullptr),TransactionRdExtVarCreate(dsct,varName,constValue)
 {
-  FromByteSeqToVB(sha1,_sha1);
+  constexpr char EXPECTED_COMPARE_FUNC_NAME[]="comptchev";
+  SALOME::AutoPyRef context(PyDict_New());
+  SALOME::AutoPyRef res(PyRun_String(compareFuncContent,Py_file_input,_dsct->getGlobals(),context));
+  if(res.isNull())
+    {
+      std::ostringstream oss; oss << "TransactionRdExtVarFreeStyleCreate ctor : Fail to parse and evaluate \"" << compareFuncContent << "\" as python function !";
+      throw Exception(oss.str());
+    }
+  PyObject *func(PyDict_GetItemString(context,EXPECTED_COMPARE_FUNC_NAME));//borrowed
+  if(!func)
+    {
+      std::ostringstream oss; oss << "TransactionRdExtVarFreeStyleCreate ctor : Parsing of func is OK but not func called \"" << EXPECTED_COMPARE_FUNC_NAME << "\" is the given parsed string !";
+      throw Exception(oss.str());
+    }
+  _cmp_func = func; Py_XINCREF(func);
+  if(PyDict_DelItemString(context,EXPECTED_COMPARE_FUNC_NAME)!=0)
+    {
+      std::ostringstream oss; oss << "TransactionRdExtVarFreeStyleCreate ctor : Internal error during suppression of \"" << EXPECTED_COMPARE_FUNC_NAME << "\" key that exepect to be present ! Smells bad !";
+      throw Exception(oss.str());
+    }
 }
 
 void TransactionRdExtVarFreeStyleCreate::prepareRollBackInCaseOfFailure()
 {//nothing it is not a bug
 }
 
+void TransactionRdExtVarFreeStyleCreate::rollBack()
+{// nothing to be done here. Fress style means I don t care. Do not remove var.
+  if(!_null_rollback)
+    TransactionRdExtVarCreate::rollBack();
+}
+
 void TransactionRdExtVarFreeStyleCreate::perform()
 {
   SALOME::ByteVec data2;
   FromVBToByteSeq(_data,data2);
-  _dsct->createRdExtVarFreeStyleInternal(_var_name,data2,std::move(_sha1));
+  try
+    {
+      _dsct->createRdExtVarFreeStyleInternal(_var_name,data2,std::move(_cmp_func_content),std::move(_cmp_func));
+    }
+  catch(NotSameException& e)
+    {
+      _null_rollback = true;
+      throw e;
+    }
 }
 
 void TransactionRdExtInitVarCreate::perform()