예제 #1
0
 private static void Call(DBManager db, AssortmentProcedure proc)
 {
     try
     {
         var parameters = db.CallProcedure(proc);
     }
     catch (AssortmentException e)
     {
         Console.Error.WriteLine("Log Write Error: " + proc.Name + "; Message: " + e.Message);
     }
 }
예제 #2
0
        /// <summary>
        /// Calls Database stored procedures
        /// </summary>
        /// <exception cref="CancelException"></exception>
        /// <exception cref="AssortmentDBException"></exception>
        /// <exception cref="ConnectionException"></exception>
        /// <returns >Dictionary of output parameters</returns>
        /// <param name="proc">Calling procedure</param>
        public Dictionary<string, object> CallProcedure(AssortmentProcedure proc)
        {
            #region Preparation

            var result = new Dictionary<string, object>();
            Command = Connection.CreateCommand();
            Command.CommandText = proc.Mode ? proc.SecondName : proc.Name;
            Command.CommandType = CommandType.StoredProcedure;
            Command.CommandTimeout = proc.Timeout;
            if (proc.Transaction) Command.Transaction = TransactionCreate();
            OracleDataReader dr = null;
            Command.Parameters.AddRange(proc.Parameters.Values.ToArray());

            #endregion

            #region Executing

            InProcess = true;
            try
            {
                if (proc.IsRefCursor) dr = Command.ExecuteReader();
                else Command.ExecuteNonQuery();
            }
            catch (OracleException e)
            {
                if (e.Number == 3114) throw new ConnectionException(e.Message);
                if (proc.Transaction)
                {
                    if (e.Number == 1013)
                    {
                        Rollback();
                        throw new CancelException("Отмена операции");
                    }
                    Rollback(e.Procedure + ": " + e.Message);
                }
                throw new AssortmentDBException(e.Message);
            }
            finally
            {
                InProcess = false;
            }

            #endregion

            #region Post processing

            if (proc.Parameters.ContainsKey("o_error_message"))
            {
                if (Command.Parameters["o_error_message"] != null)
                {
                    var error = (OracleString)Command.Parameters["o_error_message"].Value;
                    if (!error.IsNull)
                    {
                        if (proc.Transaction) Rollback(error.Value);
                        throw new AssortmentDBException(error.Value);
                    }
                }
            }

            if (proc.Transaction) Commit();

            foreach (var param in proc.Parameters)
            {
                if (param.Value.Direction != ParameterDirection.Output) continue;
                if (param.Value.OracleDbType == OracleDbType.RefCursor)
                {
                    var valueList = new List<Dictionary<string, object>>();
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            var rowDictionary = new Dictionary<string, object>();
                            for (var i = 0; i < dr.FieldCount; i++)
                            {
                                rowDictionary.Add(dr.GetName(i), dr[i]);
                            }
                            valueList.Add(rowDictionary);
                        }
                        dr.Close();
                    }
                    result.Add(param.Key, valueList);
                }
                else
                {
                    if (Command.Parameters[param.Key] != null)
                        result.Add(param.Key, Command.Parameters[param.Key].Value);
                }
            }

            #endregion
            Command.Dispose();
            return result;
        }