当调用函数因约束FK而出错时,Ggre PostgresException返回null

时间:2018-12-23 19:46:42

标签: npgsql

我正在使用Google Cloud Platform托管我的数据库postgres 9.6。

我已发送数据以强制FK中的错误进行更新,在filas = cmd.ExecuteNonQuery()中,结果是0行,正如我希望的那样。但是catch (PostgresException msg_exception)无法识别异常,并且在尝试使用PostgresNotice msg_notice = new PostgresNotice()NULL也会返回msg_notice

using(var conn = new NpgsqlConnection(connString)) {
 conn.Open();
 conn.TypeMapper.MapComposite < DTO_Usuario > ("t_usuario");

 using(var transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)) {
  try {
   using(NpgsqlCommand cmd = new NpgsqlCommand(@ "SELECT ent_usuario_update(@p)", conn)) {
    cmd.Parameters.Add(new NpgsqlParameter {
     ParameterName = "p",
      Value = row_usuario
    });

    PostgresNotice msg_notice = new PostgresNotice();
    try {
     filas = cmd.ExecuteNonQuery();

     transaction.Commit();

     if (filas > 0) {
      row_usuario.Nom_usuario = msg_notice.MessageText;
     } else {
      row_usuario.Nom_usuario = msg_notice.MessageText;
     }

    } catch (PostgresException msg_exception) {
     transaction.Rollback();
     row_usuario.Des_cargo = msg_exception.SqlState;

    }
    return Ok(row_usuario);
   }
  } catch (NpgsqlException) {
   transaction.Rollback();
   return NoContent();
  }
 }
}

我在Postgres中的功能:

CREATE OR REPLACE FUNCTION public.ent_usuario_update(
p_usuario t_usuario)
RETURNS void

update usuario
   set des_cargo=p_usuario.des_cargo,
       nom_usuario = p_usuario.nom_usuario,
       id_organizacion = p_usuario.id_organizacion
 where id_instancia = p_usuario.id_instancia
   and id_mandante = p_usuario.id_mandante
   and id_usuario=p_usuario.id_usuario;

GET DIAGNOSTICS filas = ROW_COUNT;

IF filas == 0 THEN
    RAISE INFO 'No se actualizo usuario % en instancia %', p_usuario.id_usuario,  p_usuario.id_instancia
    using message = 'No actualizo';
ELSE
    RAISE INFO 'Actualización Realizada'
    using message = 'No actualizo';
END IF;

RETURN;

END;

1 个答案:

答案 0 :(得分:0)

PostgreSQL通知不会引发异常,因为它们不是错误-因此try / catch不会产生任何结果。也不可能简单地实例化/** * @brief This function configures the source of the time base. * The time source is configured to have 1ms time base with a dedicated * Tick interrupt priority. * @note This function is called automatically at the beginning of program after * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig(). * @note In the default implementation, SysTick timer is the source of time base. * It is used to generate interrupts at regular time intervals. * Care must be taken if HAL_Delay() is called from a peripheral ISR process, * The SysTick interrupt must have higher priority (numerically lower) * than the peripheral interrupt. Otherwise the caller ISR process will be blocked. * The function is declared as __weak to be overwritten in case of other * implementation in user file. * @param TickPriority Tick interrupt priority. * @retval HAL status */ __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { /* Configure the SysTick to have interrupt in 1ms time basis*/ if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) > 0U) { return HAL_ERROR; } /* Configure the SysTick IRQ priority */ if (TickPriority < (1UL << __NVIC_PRIO_BITS)) { HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U); uwTickPrio = TickPriority; } else { return HAL_ERROR; } /* Return function status */ return HAL_OK; } 并期望它在执行命令时自动填充。

要接收PostgreSQL通知,只需在连接上订阅PostgresNotice事件:

Notice