using System.Text;
using MySql.Data.MySqlClient;
using MarketData.MarketDataModel;
using MarketData.Utils;

namespace MarketData.DataAccess
{
  public class IncomeStatementDA
  {
// ********************************************* I N C O M E   S T A T E M E N T **************************************
    public static bool CheckIncomeStatementModifiedOn(String symbol, DateTime modified,IncomeStatement.PeriodType periodType)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      DateTime? maxDate = DateTime.Parse("01-01-0001");

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select count(*) from incomestatement");
        sb.Append(" where symbol='").Append(symbol).Append("'").Append(" ");
        sb.Append(" and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append("").Append(" ");
        sb.Append("and modified='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
        strQuery = sb.ToString(); ;
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        sqlDataReader.Read();
        int recordCount = sqlDataReader.GetInt32(0);
        return 0 == recordCount ? false : true;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return false;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static List<DateTime> GetIncomeStatementDates(String symbol,IncomeStatement.PeriodType periodType)
    {
      List<DateTime> incomeStatementDates = new List<DateTime>();
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select asof from incomestatement where symbol='").Append(symbol).Append("' ");
        sb.Append("and period='").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append("'");
        sb.Append(" order by 1 asc;");
        strQuery = sb.ToString(); ;
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          DateTime asof = sqlDataReader.GetDateTime(0);
          incomeStatementDates.Add(asof);
        }
        return incomeStatementDates;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return incomeStatementDates;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DateTime? GetLatestIncomeStatementDate(String symbol,IncomeStatement.PeriodType periodType)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      DateTime? maxDate = DateTime.Parse("01-01-0001");

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select max(asof) from incomestatement");
        sb.Append(" where symbol='").Append(symbol).Append("' ");
        sb.Append(" and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1);
        strQuery = sb.ToString(); ;
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if (sqlDataReader.Read())
        {
          if (sqlDataReader.IsDBNull(0)) maxDate = null;
          else maxDate = sqlDataReader.GetDateTime(0);
        }
        return maxDate;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return maxDate;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static IncomeStatement GetIncomeStatement(String symbol,IncomeStatement.PeriodType periodType)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        DateTime? latestDate = GetLatestIncomeStatementDate(symbol,periodType);
        if(null==latestDate)return null;
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,sga,net_income_applicable_to_common_shares,ebit,operating_expenses,research_and_development,interest_expense,period,modified from incomestatement where symbol=");
        sb.Append("'").Append(symbol).Append("'").Append(" ");
        sb.Append("and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append(" ");
        sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(latestDate.Value)).Append("'").Append(";");
        strQuery = sb.ToString(); ;
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if (!sqlDataReader.Read()) return null;
        IncomeStatement incomeStatement = new IncomeStatement();
        incomeStatement.Symbol = sqlDataReader.GetString(0);
        incomeStatement.AsOf = sqlDataReader.GetDateTime(1);
        if (!sqlDataReader.IsDBNull(2)) incomeStatement.TotalRevenue = sqlDataReader.GetDouble(2);
        if (!sqlDataReader.IsDBNull(3)) incomeStatement.CostOfRevenue = sqlDataReader.GetDouble(3);
        if (!sqlDataReader.IsDBNull(4)) incomeStatement.IncomeTaxExpense = sqlDataReader.GetDouble(4);
        if (!sqlDataReader.IsDBNull(5)) incomeStatement.GrossProfit = sqlDataReader.GetDouble(5);
        if (!sqlDataReader.IsDBNull(6)) incomeStatement.NetIncome = sqlDataReader.GetDouble(6);
        if (!sqlDataReader.IsDBNull(7)) incomeStatement.SGA = sqlDataReader.GetDouble(7);
        if (!sqlDataReader.IsDBNull(8)) incomeStatement.NetIncomeApplicableToCommonShares = sqlDataReader.GetDouble(8);
        if (!sqlDataReader.IsDBNull(9)) incomeStatement.EBIT = sqlDataReader.GetDouble(9);
        if (!sqlDataReader.IsDBNull(10)) incomeStatement.OperatingExpenses = sqlDataReader.GetDouble(10);
        if (!sqlDataReader.IsDBNull(11)) incomeStatement.ResearchAndDevelopment = sqlDataReader.GetDouble(11);
        if (!sqlDataReader.IsDBNull(12)) incomeStatement.InterestExpense = sqlDataReader.GetDouble(12);
        if (!sqlDataReader.IsDBNull(13)) incomeStatement.Period = sqlDataReader.GetInt32(13)==0?IncomeStatement.PeriodType.Annual:IncomeStatement.PeriodType.Quarterly;
        if (!sqlDataReader.IsDBNull(14)) incomeStatement.Modified = sqlDataReader.GetDateTime(14);
        return incomeStatement;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static TimeSeriesCollection GetRevenue(String symbol,IncomeStatement.PeriodType period)
    {
      Profiler profiler = new Profiler();
      TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select symbol,asof,total_revenue from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
        sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append(" order by asof desc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
          timeSeriesElement.Symbol = sqlDataReader.GetString(0);
          timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
          timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly)?TimeSeriesElement.ElementType.QuarterlyRevenue:TimeSeriesElement.ElementType.Revenue;
          if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
          else continue;
          timeSeriesCollection.Add(timeSeriesElement);
        }
        return timeSeriesCollection;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
// Profit Margin is calculated as a percentage.
    public static TimeSeriesCollection GetProfitMargin(String symbol,IncomeStatement.PeriodType period=IncomeStatement.PeriodType.Annual)
    {
      TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select symbol,asof,total_revenue,gross_profit from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
        sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append(" order by asof desc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
          timeSeriesElement.Symbol = sqlDataReader.GetString(0);
          timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
          timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly)?TimeSeriesElement.ElementType.QuarterlyRevenue:TimeSeriesElement.ElementType.Revenue;

          if(sqlDataReader.IsDBNull(2))continue;
          double total_revenue=sqlDataReader.GetDouble(2);
          if(sqlDataReader.IsDBNull(3))continue;
          double gross_profit=sqlDataReader.GetDouble(3);
          if(0==gross_profit)continue;
          timeSeriesElement.Value=(gross_profit/total_revenue)*100.00;
          timeSeriesCollection.Add(timeSeriesElement);
        }
        return timeSeriesCollection;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    // Profit Margin is calculated as a percentage.
    public static TimeSeriesCollection GetProfitMarginMaxAsOf(String symbol,DateTime maxDate, IncomeStatement.PeriodType period = IncomeStatement.PeriodType.Annual)
    {
      TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select symbol,asof,total_revenue,gross_profit from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
        sb.Append(" and asof<='").Append(SqlUtils.SqlDate(maxDate)).Append("' ");
        sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" order by asof desc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
          timeSeriesElement.Symbol = sqlDataReader.GetString(0);
          timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
          timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly) ? TimeSeriesElement.ElementType.QuarterlyRevenue : TimeSeriesElement.ElementType.Revenue;

          if (sqlDataReader.IsDBNull(2)) continue;
          double total_revenue = sqlDataReader.GetDouble(2);
          if (sqlDataReader.IsDBNull(3)) continue;
          double gross_profit = sqlDataReader.GetDouble(3);
          if (0 == gross_profit) continue;
          timeSeriesElement.Value = (gross_profit / total_revenue) * 100.00;
          timeSeriesCollection.Add(timeSeriesElement);
        }
        return timeSeriesCollection;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG, exception);
        return null;
      }
      finally
      {
        if (null != sqlCommand) sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static IncomeStatement GetIncomeStatement(String symbol,DateTime asof,IncomeStatement.PeriodType periodType)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,net_income_applicable_to_common_shares,sga,ebit,operating_expenses,research_and_development,interest_expense,period,modified from incomestatement where symbol=");
        sb.Append("'").Append(symbol).Append("'").Append(" ");
        sb.Append("and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
        sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'").Append(";");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if (!sqlDataReader.Read()) return null;
        IncomeStatement incomeStatement = new IncomeStatement();
        incomeStatement.Symbol = sqlDataReader.GetString(0);
        incomeStatement.AsOf = sqlDataReader.GetDateTime(1);
        if (!sqlDataReader.IsDBNull(2)) incomeStatement.TotalRevenue = sqlDataReader.GetDouble(2);
        if (!sqlDataReader.IsDBNull(3)) incomeStatement.CostOfRevenue = sqlDataReader.GetDouble(3);
        if (!sqlDataReader.IsDBNull(4)) incomeStatement.IncomeTaxExpense = sqlDataReader.GetDouble(4);
        if (!sqlDataReader.IsDBNull(5)) incomeStatement.GrossProfit = sqlDataReader.GetDouble(5);
        if (!sqlDataReader.IsDBNull(6)) incomeStatement.NetIncome = sqlDataReader.GetDouble(6);
        if (!sqlDataReader.IsDBNull(7)) incomeStatement.SGA = sqlDataReader.GetDouble(7);
        if (!sqlDataReader.IsDBNull(8)) incomeStatement.NetIncomeApplicableToCommonShares = sqlDataReader.GetDouble(8);
        if (!sqlDataReader.IsDBNull(9)) incomeStatement.EBIT = sqlDataReader.GetDouble(9);
        if (!sqlDataReader.IsDBNull(10)) incomeStatement.OperatingExpenses = sqlDataReader.GetDouble(10);
        if (!sqlDataReader.IsDBNull(11)) incomeStatement.ResearchAndDevelopment = sqlDataReader.GetDouble(11);
        if (!sqlDataReader.IsDBNull(12)) incomeStatement.InterestExpense = sqlDataReader.GetDouble(12);
        if (!sqlDataReader.IsDBNull(13)) incomeStatement.Period = 0 == sqlDataReader.GetInt32(13) ? IncomeStatement.PeriodType.Annual : IncomeStatement.PeriodType.Quarterly;
        if (!sqlDataReader.IsDBNull(14)) incomeStatement.Modified = sqlDataReader.GetDateTime(14);
        return incomeStatement;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
        if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static IncomeStatement GetIncomeStatementMaxAsOf(String symbol,DateTime asof,IncomeStatement.PeriodType periodType)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,net_income_applicable_to_common_shares,sga,ebit,operating_expenses,research_and_development,interest_expense,period,modified from incomestatement where symbol=");
        sb.Append("'").Append(symbol).Append("'").Append(" ");
        sb.Append("and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
        sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'");
        sb.Append(" order by asof desc limit 1;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if (!sqlDataReader.Read()) return null;
        IncomeStatement incomeStatement = new IncomeStatement();
        incomeStatement.Symbol = sqlDataReader.GetString(0);
        incomeStatement.AsOf = sqlDataReader.GetDateTime(1);
        if (!sqlDataReader.IsDBNull(2)) incomeStatement.TotalRevenue = sqlDataReader.GetDouble(2);
        if (!sqlDataReader.IsDBNull(3)) incomeStatement.CostOfRevenue = sqlDataReader.GetDouble(3);
        if (!sqlDataReader.IsDBNull(4)) incomeStatement.IncomeTaxExpense = sqlDataReader.GetDouble(4);
        if (!sqlDataReader.IsDBNull(5)) incomeStatement.GrossProfit = sqlDataReader.GetDouble(5);
        if (!sqlDataReader.IsDBNull(6)) incomeStatement.NetIncome = sqlDataReader.GetDouble(6);
        if (!sqlDataReader.IsDBNull(7)) incomeStatement.SGA = sqlDataReader.GetDouble(7);
        if (!sqlDataReader.IsDBNull(8)) incomeStatement.NetIncomeApplicableToCommonShares = sqlDataReader.GetDouble(8);
        if (!sqlDataReader.IsDBNull(9)) incomeStatement.EBIT = sqlDataReader.GetDouble(9);
        if (!sqlDataReader.IsDBNull(10)) incomeStatement.OperatingExpenses = sqlDataReader.GetDouble(10);
        if (!sqlDataReader.IsDBNull(11)) incomeStatement.ResearchAndDevelopment = sqlDataReader.GetDouble(11);
        if (!sqlDataReader.IsDBNull(12)) incomeStatement.InterestExpense = sqlDataReader.GetDouble(12);
        if (!sqlDataReader.IsDBNull(13)) incomeStatement.Period = 0 == sqlDataReader.GetInt32(13) ? IncomeStatement.PeriodType.Annual : IncomeStatement.PeriodType.Quarterly;
        if (!sqlDataReader.IsDBNull(14)) incomeStatement.Modified = sqlDataReader.GetDateTime(14);
        return incomeStatement;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
        if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static bool InsertIncomeStatements(List<IncomeStatement> incomeStatements)
    {
      MySqlConnection sqlConnection = null;
      MySqlTransaction sqlTransaction = null;
      String strQuery = null;

      try
      {
        DateTime modified = DateTime.Now;
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        DeleteIncomeStatements(incomeStatements, sqlConnection, sqlTransaction);
        for (int index = 0; index < incomeStatements.Count; index++)
        {
          IncomeStatement incomeStatement = incomeStatements[index];
          StringBuilder sb = new StringBuilder();
          sb.Append("insert into incomestatement (symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,sga,net_income_applicable_to_common_shares,ebit,operating_expenses,research_and_development,interest_expense,period,modified) ");
          sb.Append("values(");
          sb.Append("'").Append(incomeStatement.Symbol).Append("'").Append(",");
          sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(incomeStatement.AsOf)).Append("'").Append(",");
          if (!Double.IsNaN(incomeStatement.TotalRevenue)) sb.Append(incomeStatement.TotalRevenue).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.CostOfRevenue)) sb.Append(incomeStatement.CostOfRevenue).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.IncomeTaxExpense)) sb.Append(incomeStatement.IncomeTaxExpense).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.GrossProfit)) sb.Append(incomeStatement.GrossProfit).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.NetIncome)) sb.Append(incomeStatement.NetIncome).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.SGA)) sb.Append(incomeStatement.SGA).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.NetIncomeApplicableToCommonShares))sb.Append(incomeStatement.NetIncomeApplicableToCommonShares).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.EBIT)) sb.Append(incomeStatement.EBIT).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.OperatingExpenses)) sb.Append(incomeStatement.OperatingExpenses).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.ResearchAndDevelopment)) sb.Append(incomeStatement.ResearchAndDevelopment).Append(",");
          else sb.Append("null").Append(",");
          if (!Double.IsNaN(incomeStatement.InterestExpense)) sb.Append(incomeStatement.InterestExpense).Append(",");
          else sb.Append("null").Append(",");
          sb.Append(IncomeStatement.PeriodType.Annual.Equals(incomeStatement.Period)?0:1).Append(",");
          sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
          sb.Append(")");
          strQuery = sb.ToString();
          MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
          sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
          sqlCommand.ExecuteNonQuery();
					sqlCommand.Dispose();
        }
        sqlTransaction.Commit();
        return true;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
        return false;
      }
      finally
      {
        if (null != sqlTransaction) sqlTransaction.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    private static bool DeleteIncomeStatements(List<IncomeStatement> incomeStatements, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
    {
      for (int index = 0; index < incomeStatements.Count; index++)
      {
        DeleteIncomeStatement(incomeStatements[index], sqlConnection, sqlTransaction);
      }
      return true;
    }
    private static bool DeleteIncomeStatement(IncomeStatement incomeStatement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
    {
      StringBuilder sb = new StringBuilder();
      String strQuery = null;

      try
      {
        sb.Append("delete from incomestatement where ");
        sb.Append("symbol='").Append(incomeStatement.Symbol).Append("'");
        sb.Append(" and ");
        sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(incomeStatement.AsOf)).Append("'");
        sb.Append(" and period=").Append(incomeStatement.Period.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1);
        strQuery = sb.ToString();
        MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlCommand.ExecuteNonQuery();
				sqlCommand.Dispose();
        return true;
      }
      catch (Exception exception)
      {
        sqlTransaction.Rollback();
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        if (null != strQuery) MDTrace.WriteLine(LogLevel.DEBUG,"Query was " + strQuery);
        return false;
      }
      finally
      {
      }
    }
  }
}
