context: namespace iTrading.Db
  {
      using System;
      using System.Collections;
      using System.Data;
      using System.Diagnostics;
      using iTrading.Core.Kernel;
  
      internal class Symbol
      {
          private static IDbCommand deleteBrokerSymbol = null;
          private static IDbCommand deleteDividends = null;
          private static IDbCommand deleteExchangeSymbol = null;
          private static IDbCommand deleteQuotesDaily = null;
          private static IDbCommand deleteQuotesIntraday = null;
          private static IDbCommand deleteSplits = null;
          private static IDbCommand deleteSymbol = null;
          private static IDbCommand insertBrokerSymbol = null;
          private static IDbCommand insertDividends = null;
          private static IDbCommand insertExchangeSymbol = null;
          private static IDbCommand insertSplits = null;
          private static IDbCommand insertSymbol = null;
          private static IDbCommand selectBrokerSymbol = null;
          private static IDbCommand selectDividends = null;
          private static IDbCommand selectExchangeSymbol = null;
          private static IDbCommand selectSplits = null;
          private static IDbCommand selectSymbolId = null;
          private static IDbCommand selectSymbolId2 = null;
          private static IDbCommand updateBrokerSymbol = null;
          private static IDbCommand updateSymbol = null;
  
          internal static bool Delete(iTrading.Core.Kernel.Symbol symbol)
          {
              int symbolId = GetSymbolId(symbol);
              if (symbolId >= 0)
              {
                  if (iTrading.Db.Execution.HasExecutions(symbol))
                  {
                      if (symbol.Connection != null)
                      {
                          symbol.Connection.ProcessEventArgs(new ITradingErrorEventArgs(symbol.Connection, ErrorCode.UnableToDeleteSymbol, "", "Symbol can't be deleted, since there are executions stored in the repository for this symbol"));
                      }
                      return false;
                  }
                  if (iTrading.Db.Order.HasOrders(symbol))
                  {
                      if (symbol.Connection != null)
                      {
                          symbol.Connection.ProcessEventArgs(new ITradingErrorEventArgs(symbol.Connection, ErrorCode.UnableToDeleteSymbol, "", "Symbol can't be deleted, since there are orders stored in the repository for this symbol"));
                      }
                      return false;
                  }
                  if (deleteBrokerSymbol == null)
                  {
                      deleteBrokerSymbol = iTrading.Db.Db.NewCommand("delete from tm_brokersymbols where symbol = @symbol", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteBrokerSymbol, "@symbol", "Integer");
                      deleteBrokerSymbol.Prepare();
                  }
                  deleteBrokerSymbol.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteBrokerSymbol, "@symbol").Value = symbolId;
                  deleteBrokerSymbol.ExecuteNonQuery();
                  if (deleteDividends == null)
                  {
                      deleteDividends = iTrading.Db.Db.NewCommand("delete from tm_dividends where symbol = @symbol", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteDividends, "@symbol", "Integer");
                      deleteDividends.Prepare();
                  }
                  deleteDividends.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteDividends, "@symbol").Value = symbolId;
                  deleteDividends.ExecuteNonQuery();
                  if (deleteExchangeSymbol == null)
                  {
                      deleteExchangeSymbol = iTrading.Db.Db.NewCommand("delete from tm_exchangesymbols where symbol = @symbol", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteExchangeSymbol, "@symbol", "Integer");
                      deleteExchangeSymbol.Prepare();
                  }
                  deleteExchangeSymbol.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteExchangeSymbol, "@symbol").Value = symbolId;
                  deleteExchangeSymbol.ExecuteNonQuery();
                  if (deleteQuotesDaily == null)
                  {
                      deleteQuotesDaily = iTrading.Db.Db.NewCommand("delete from tm_quotesdaily where symbol = @symbol", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteQuotesDaily, "@symbol", "Integer");
                      deleteQuotesDaily.Prepare();
                  }
                  deleteQuotesDaily.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteQuotesDaily, "@symbol").Value = symbolId;
                  deleteQuotesDaily.ExecuteNonQuery();
                  if (deleteQuotesIntraday == null)
                  {
                      deleteQuotesIntraday = iTrading.Db.Db.NewCommand("delete from tm_quotesintraday where symbol = @symbol", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteQuotesIntraday, "@symbol", "Integer");
                      deleteQuotesIntraday.Prepare();
                  }
                  deleteQuotesIntraday.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteQuotesIntraday, "@symbol").Value = symbolId;
                  deleteQuotesIntraday.ExecuteNonQuery();
                  if (deleteSplits == null)
                  {
                      deleteSplits = iTrading.Db.Db.NewCommand("delete from tm_splits where symbol = @symbol", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteSplits, "@symbol", "Integer");
                      deleteSplits.Prepare();
                  }
                  deleteSplits.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteSplits, "@symbol").Value = symbolId;
                  deleteSplits.ExecuteNonQuery();
                  if (deleteSymbol == null)
                  {
                      deleteSymbol = iTrading.Db.Db.NewCommand("delete from tm_symbols where name = @name and type = @type and strikeprice = @strikeprice and optionright = @optionright", Adapter.iDbTransaction);
                      iTrading.Db.Db.AddParameter(deleteSymbol, "@name", "VarChar", 50);
                      iTrading.Db.Db.AddParameter(deleteSymbol, "@type", "Integer");
                      iTrading.Db.Db.AddParameter(deleteSymbol, "@strikeprice", "Double");
                      iTrading.Db.Db.AddParameter(deleteSymbol, "@optionright", "Integer");
                      deleteSymbol.Prepare();
                  }
                  deleteSymbol.Transaction = Adapter.iDbTransaction;
                  iTrading.Db.Db.GetParameter(deleteSymbol, "@name").Value = symbol.Name;
                  iTrading.Db.Db.GetParameter(deleteSymbol, "@type").Value = symbol.SymbolType.Id;
                  iTrading.Db.Db.GetParameter(deleteSymbol, "@strikeprice").Value = symbol.StrikePrice;
                  iTrading.Db.Db.GetParameter(deleteSymbol, "@optionright").Value = symbol.Right.Id;
                  deleteSymbol.ExecuteNonQuery();
              }
              return true;
          }
  
          private static string GetBrokerName(ProviderTypeId broker, int symbolId)
          {
              if (selectBrokerSymbol == null)
              {
                  selectBrokerSymbol = iTrading.Db.Db.NewCommand("select brokername from tm_brokersymbols where symbol = @symbol and broker = @broker", Adapter.iDbTransaction);
                  iTrading.Db.Db.AddParameter(selectBrokerSymbol, "@symbol", "Integer");
                  iTrading.Db.Db.AddParameter(selectBrokerSymbol, "@broker", "Integer");
                  selectBrokerSymbol.Prepare();
              }
              selectBrokerSymbol.Transaction = Adapter.iDbTransaction;
              iTrading.Db.Db.GetParameter(selectBrokerSymbol, "@symbol").Value = symbolId;
              iTrading.Db.Db.GetParameter(selectBrokerSymbol, "@broker").Value = broker;
              IDataReader reader = selectBrokerSymbol.ExecuteReader();
              string str = "";
              if (reader.Read())
              {
                  str = (string) reader["brokername"];
              }
              reader.Close();
              return str;
          }
  
          private static DividendDictionary GetDividends(int symbolId)
          {
              if (selectDividends == null)
              {
                  selectDividends = iTrading.Db.Db.NewCommand("select dividenddate, dividend from tm_dividends where symbol = @symbol order by dividenddate", Adapter.iDbTransaction);
                  iTrading.Db.Db.AddParameter(selectDividends, "@symbol", "Integer");
                  selectDividends.Prepare();
              }
              selectDividends.Transaction = Adapter.iDbTransaction;
              iTrading.Db.Db.GetParameter(selectDividends, "@symbol").Value = symbolId;
              IDataReader reader = selectDividends.ExecuteReader();
              DividendDictionary dictionary = new DividendDictionary();
              while (reader.Read())
              {
                  dictionary.Add((DateTime) reader["dividenddate"], (double) reader["dividend"]);
              }
              reader.Close();
              return dictionary;
          }
  
          internal static int GetExchangeSymbolId(iTrading.Core.Kernel.Symbol symbol)
          {
              if (Globals.TraceSwitch.DataBase)
              {
                  Trace.WriteLine("(Db) Db.Symbol.GetSymbolId '" + symbol.FullName + "'");
              }
              if (selectSymbolId == null)
              {
                  selectSymbolId = iTrading.Db.Db.NewCommand("select tm_exchangesymbols.id from tm_symbols, tm_exchangesymbols where name = @name and type = @type and strikeprice = @strikeprice and optionright = @optionright and tm_symbols.id = tm_exchangesymbols.symbol and exchange = @exchange", Adapter.iDbTransaction);
                  iTrading.Db.Db.AddParameter(selectSymbolId, "@name", "VarChar", 50);
                  iTrading.Db.Db.AddParameter(selectSymbolId, "@type", "Integer");
                  iTrading.Db.Db.AddParameter(selectSymbolId, "@strikeprice", "Double");
                  iTrading.Db.Db.AddParameter(selectSymbolId, "@optionright", "Integer");
                  iTrading.Db.Db.AddParameter(selectSymbolId, "@exchange", "Integer");
                  selectSymbolId.Prepare();
              }
              selectSymbolId.Transaction = Adapter.iDbTransaction;
              iTrading.Db.Db.GetParameter(selectSymbolId, "@name").Value = symbol.Name;
              iTrading.Db.Db.GetParameter(selectSymbolId, "@type").Value = symbol.SymbolType.Id;
              iTrading.Db.Db.GetParameter(selectSymbolId, "@strikeprice").Value = symbol.StrikePrice;
              iTrading.Db.Db.GetParameter(selectSymbolId, "@optionright").Value = symbol.Right.Id;
              iTrading.Db.Db.GetParameter(selectSymbolId, "@exchange").Value = symbol.Exchange.Id;
              IDataReader reader = selectSymbolId.ExecuteReader();
              int num = -1;
              if (reader.Read())
              {
                  num = (int) reader["id"];
                  reader.Close();
                  return num;
              }
              reader.Close();
              Update(symbol);
              reader = selectSymbolId.ExecuteReader();
              reader.Read();
              num = (int) reader["id"];
              reader.Close();
              return num;
          }
  
          private static SplitDictionary GetSplits(int symbolId)
          {
              if (selectSplits == null)
              {
                  selectSplits = iTrading.Db.Db.NewCommand("select splitdate, splitfactor from tm_splits where symbol = @symbol order by splitdate", Adapter.iDbTransaction);
                  iTrading.Db.Db.AddParameter(selectSplits, "@symbol", "Integer");
                  selectSplits.Prepare();
              }
              selectSplits.Transaction = Adapter.iDbTransaction;
              iTrading.Db.Db.GetParameter(selectSplits, "@symbol").Value = symbolId;
              IDataReader reader = selectSplits.ExecuteReader();
              SplitDictionary dictionary = new SplitDictionary();
              while (reader.Read())
              {
                  dictionary.Add((DateTime) reader["splitdate"], (double) reader["splitfactor"]);
              }
              reader.Close();
              return dictionary;
          }
  
          internal static int GetSymbolId(iTrading.Core.Kernel.Symbol symbol)
          {
              if (selectSymbolId2 == null)
              {
                  selectSymbolId2 = iTrading.Db.Db.NewCommand("select id from tm_symbols where name = @name and type = @type and strikeprice = @strikeprice and optionright = @optionright", Adapter.iDbTransaction);
                  iTrading.Db.Db.AddParameter(selectSymbolId2, "@name", "VarChar", 50);
                  iTrading.Db.Db.AddParameter(selectSymbolId2, "@type", "Integer");
                  iTrading.Db.Db.AddParameter(selectSymbolId2, "@strikeprice", "Double");
                  iTrading.Db.Db.AddParameter(selectSymbolId2, "@optionright", "Integer");
                  selectSymbolId2.Prepare();
              }
              selectSymbolId2.Transaction = Adapter.iDbTransaction;
              iTrading.Db.Db.GetParameter(selectSymbolId2, "@name").Value = symbol.Name;
              iTrading.Db.Db.GetParameter(selectSymbolId2, "@type").Value = symbol.SymbolType.Id;
              iTrading.Db.Db.GetParameter(selectSymbolId2, "@strikeprice").Value = symbol.StrikePrice;
              iTrading.Db.Db.GetParameter(selectSymbolId2, "@optionright").Value = symbol.Right.Id;
              IDataReader reader = selectSymbolId2.ExecuteReader();
  
gt:             int num = -1;