Esempio n. 1
0
        /*
         * private static bool DeleteMatches(IDbConnection conn, IDbTransaction transaction)
         * {
         *  LineObjectCollection<MatchLn> locMatches = LineSr.Instance.ObjectsToRemove.GetLineObjectCollection<MatchLn>();
         *
         *  if (locMatches != null && locMatches.Count > 0)
         *  {
         *      IdentityList il = new IdentityList();
         *
         *      foreach (MatchLn mtch in locMatches.Values)
         *      {
         *          il.AddUnique(mtch.MatchId);
         *      }
         *
         *      string sMatchIds = il.FormatIds();
         *
         *      DataCopy.ExecuteScalar(conn, transaction, DELETE_MATCH_ODDS_QUERY, sMatchIds);
         *      DataCopy.ExecuteScalar(conn, transaction, DELETE_MATCH_BETDOMAIN_QUERY, sMatchIds);
         *      DataCopy.ExecuteScalar(conn, transaction, DELETE_MATCH_TO_GROUP_QUERY, sMatchIds);
         *      DataCopy.ExecuteScalar(conn, transaction, DELETE_LIVE_MATCH_INFO_QUERY, sMatchIds);
         *      DataCopy.ExecuteScalar(conn, transaction, DELETE_MATCHES_QUERY, sMatchIds);
         *
         *      return true;
         *  }
         *
         *  return false;
         * }
         */

        public static eFileSyncResult UpdateDatabase(IDbConnection conn, eUpdateType eut, string sProviderDescription, UpdateStatistics us)
        {
            eFileSyncResult fsr = eFileSyncResult.Failed;

            DateTime dtStart      = DateTime.Now;
            string   sErrorString = string.Empty;

            DataCopy dc = null;

            string sUpdateDescription = eut.ToString();

            CheckTime ct = new CheckTime("UpdateAll({0}) Entered", sUpdateDescription);

            DictionaryOfLineObjectCollectionLight dlocToModify = LineSr.Instance.NewOrChangedObjects;
            DictionaryOfLineObjectCollectionLight dlocToDelete = LineSr.Instance.ObjectsToRemove;

            try
            {
                using (IDbTransaction transaction = conn.BeginTransaction())
                {
                    dc = new DataCopy(conn, transaction, sUpdateDescription);

                    try
                    {
                        UpdatesLn updatesLn = new UpdatesLn(eut, string.Format("Saving {0} and Deleting {1} line tables", dlocToModify.Count, dlocToDelete.Count), sProviderDescription);

                        updatesLn.Save(conn, transaction);
                        Debug.Assert(updatesLn.UpdateId > 0);

                        // Insert or Update objects
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <TaggedStringLn>(), TaggedStringLn.TableSpec, updatesLn);

                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <TimeTypeLn>(), TimeTypeLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <ScoreTypeLn>(), ScoreTypeLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <BetTypeLn>(), BetTypeLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <BetDomainTypeLn>(), BetDomainTypeLn.TableSpec, updatesLn);

                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <GroupLn>(), GroupLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <CompetitorLn>(), CompetitorLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <MatchLn>(), MatchLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <CompetitorToOutrightLn>(), CompetitorToOutrightLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <LiveMatchInfoLn>(), LiveMatchInfoLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <MatchResultLn>(), MatchResultLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <MatchToGroupLn>(), MatchToGroupLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <BetDomainLn>(), BetDomainLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <ResourceRepositoryLn>(), ResourceRepositoryLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <ResourceAssignmentLn>(), ResourceAssignmentLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <CompetitorInfosLn>(), CompetitorInfosLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <MatchInfosLn>(), MatchInfosLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <TournamentInfosLn>(), TournamentInfosLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <OddLn>(), OddLn.TableSpec, updatesLn);
                        //dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection<LiabilityLn>(), LiabilityLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <LanguageLn>(), LanguageLn.TableSpec, updatesLn);
                        dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection <MultistringGroupLn>(), MultistringGroupLn.TableSpec, updatesLn);
                        //dc.InsertOrUpdate(dlocToModify.GetLineObjectCollection<TournamentMatchLocksLn>(), TournamentMatchLocksLn.TableSpec, updatesLn);

                        // Delete Objects
                        dc.DeleteObjects(dlocToDelete.GetLineObjectCollection <OddLn>(), OddLn.TableSpec);
                        dc.DeleteObjects(dlocToDelete.GetLineObjectCollection <BetDomainLn>(), BetDomainLn.TableSpec);
                        dc.DeleteObjects(dlocToDelete.GetLineObjectCollection <MatchToGroupLn>(), MatchToGroupLn.TableSpec);
                        dc.DeleteObjects(dlocToDelete.GetLineObjectCollection <LiveMatchInfoLn>(), LiveMatchInfoLn.TableSpec);
                        dc.DeleteObjects(dlocToDelete.GetLineObjectCollection <CompetitorToOutrightLn>(), CompetitorToOutrightLn.TableSpec);
                        dc.DeleteObjects(dlocToDelete.GetLineObjectCollection <MatchLn>(), MatchLn.TableSpec);

                        if (dc.Statistics.IsInsrtedOrUpdatedOrDeleted)
                        {
                            // DK: If You like to test data import then uncomment line bellow:
                            // throw new SystemException("Developer Exception: Always rollback transaction for test purposes");

                            transaction.Commit();
                            fsr = eFileSyncResult.Succeeded;
                            ct.AddEvent("Commited");
                        }
                        else
                        {
                            transaction.Rollback();
                            fsr = eFileSyncResult.Skipped;
                            ct.AddEvent("Rolled Back");
                        }

                        if (us != null)
                        {
                            us.Append(dc.Statistics);
                        }
                    }
                    catch (Exception excp)
                    {
                        transaction.Rollback();
                        m_logger.Excp(excp, "UpdateAllWithSqlBulkCopy General Transaction Exception");
                    }
                    finally
                    {
                        DateTime dtEnd = DateTime.Now;

                        if (fsr != eFileSyncResult.Skipped)
                        {
                            // DK: Let's write to log ONLY Succeeded or Failed file syncrhonization info.

                            string sInfo = @"
{7} UpdateAllWithSqlBulkCopy({0}) {1}

{2}


{3}

Start: {4};  
  End: {5};  
 Time: {6};
";

                            m_logger.InfoFormat(
                                sInfo,                // Format
                                sProviderDescription, // Provider Description
                                fsr,                  // Result
                                sErrorString,         // Errors if any
                                dc.Statistics,        // Statistic
                                dtStart,
                                dtEnd,
                                dtEnd - dtStart,     // Time
                                sUpdateDescription); // LiveBet or PreMatch
                        }
                        else
                        {
                            m_logger.InfoFormat("{0} UpdateAllWithSqlBulkCopy Skipped. Time: {1}", sUpdateDescription, dtEnd - dtStart);
                        }

                        if (fsr == eFileSyncResult.Succeeded)
                        {
                        }

                        ct.AddEvent("UpdateAll({0}) completed", sUpdateDescription);
                        ct.Info(m_logger);
                    }
                }
                //                }
            }
            catch (Exception excp)
            {
                sErrorString += ExcpHelper.FormatException(excp, "UpdateAllWithSqlBulkCopy General Exception");
                m_logger.Error(sErrorString, excp);
            }

            return(fsr);
        }
Esempio n. 2
0
        public void InsertOrUpdate <T>(LineObjectCollection <T> locLineObjects, TableSpecification ts, UpdatesLn updatesLn) where T : ILineObject <T>
        {
            CheckTime ct = new CheckTime(false, "InsertOrUpdate for '{0}' entered", ts.TableName);

            List <object> lInserted = new List <object>();
            List <object> lUpdated  = new List <object>();

            m_diInserted.Add(ts.TableName, lInserted);
            m_diUpdated.Add(ts.TableName, lUpdated);

            if (locLineObjects == null)
            {
                return;
            }

            UpdateStatistic us = m_uss.EnsureStatistic(ts.TableName);

            string sInfo = string.Format("{0} table [{1}] {2};  ", m_sLiveBet, ts.TableName, locLineObjects);

#if DEBUG
            int iInsertCount = 0;
            int iUpdateCount = 0;
#endif

            try
            {
                ct.AddEvent("Empty DataTables created.");

                foreach (string sKey in locLineObjects.Keys)
                {
                    using (DataCopyTables dct = DataCopyTables.GetDataCopyTables(m_conn, m_transaction, ts.TableName))
                    {
                        T obj = locLineObjects[sKey];

                        obj.UpdateId = updatesLn.UpdateId;

                        if (obj.IsNew)
                        {
                            DataRow drNew = obj.CreateDataRow(dct.InsertDataTable);
                            dct.InsertDataTable.Rows.Add(drNew);
                            lInserted.Add(obj);
                        }
                        else
                        {
                            DataRow drNew = obj.CreateDataRow(dct.UpdateDataTable);
                            dct.UpdateDataTable.Rows.Add(drNew);
                            lUpdated.Add(obj);
                        }

#if DEBUG
                        iInsertCount = dct.InsertDataTable.Rows.Count;
                        iUpdateCount = dct.UpdateDataTable.Rows.Count;
#endif


                        if (dct.InsertDataTable.Rows.Count > 0)
                        {
                            using (IDbCommand cmdInsert = GenerateInsertCommand(m_conn, m_transaction, dct.InsertDataTable, ts))
                            {
                                using (IDisposable dsp = SqlObjectFactory.CreateDbAdapter(null) as IDisposable)
                                {
                                    IDbDataAdapter daInsert = dsp as IDbDataAdapter;
                                    Debug.Assert(daInsert != null);

                                    daInsert.InsertCommand = cmdInsert;

                                    dct.InsertDataTable.AcceptChanges();

                                    foreach (DataRow dr in dct.InsertDataTable.Rows)
                                    {
                                        dr.SetAdded();
                                    }

                                    using (DataSet ds = new DataSet())
                                    {
                                        ds.Tables.Add(dct.InsertDataTable);
                                        daInsert.Update(ds);
                                    }
                                }
                            }

                            us.InsertCount = dct.InsertDataTable.Rows.Count;
                            ct.AddEvent("Insert completed ({0})", dct.InsertDataTable.Rows.Count);
                        }

                        if (dct.UpdateDataTable.Rows.Count > 0)
                        {
                            using (IDbCommand cmdUpdate = GenerateUpdateCommand(m_conn, m_transaction, dct.UpdateDataTable, ts))
                            {
                                using (IDisposable dsp = SqlObjectFactory.CreateDbAdapter(null) as IDisposable)
                                {
                                    IDbDataAdapter daUpdate = dsp as IDbDataAdapter;
                                    Debug.Assert(daUpdate != null);

                                    daUpdate.UpdateCommand = cmdUpdate;

                                    dct.UpdateDataTable.AcceptChanges();

                                    foreach (DataRow dr in dct.UpdateDataTable.Rows)
                                    {
                                        dr.SetModified();
                                    }

                                    using (DataSet ds = new DataSet())
                                    {
                                        ds.Tables.Add(dct.UpdateDataTable);
                                        daUpdate.Update(ds);
                                    }
                                }
                            }

                            us.UpdateCount = dct.UpdateDataTable.Rows.Count;
                            ct.AddEvent("Update completed ({0})", dct.UpdateDataTable.Rows.Count);
                        }
                        ct.AddEvent("Insert/Update filled up (I.Cnt={0}; U.Cnt={1})", dct.InsertDataTable.Rows.Count, dct.UpdateDataTable.Rows.Count);
                    }
                }



                //Debug.Assert(us.Count == arrObjects.Length);

                //m_elInfo.AddFormat("{0} Result: Succeeded;  Inserted: {1};  Updated: {2};  Skipped; {3}", sInfo, us.InsertCount, us.UpdateCount, us.SkipCount);
            }
            catch (Exception excp)
            {
                m_elInfo.AddFormat("{0} Result: FAILED; Inserted: {1};  Updated: {2};", sInfo, us.InsertCount, us.UpdateCount);

#if DEBUG
                if (typeof(T) == typeof(TaggedStringLn))
                {
                    FindDuplucates(locLineObjects);
                }

                int    iCount      = 0;
                string sObjectList = string.Format("ERROR objects (Count={0})\r\n", locLineObjects.Count);

                foreach (T obj in locLineObjects.Values)
                {
                    sObjectList += obj.ToString() + "\r\n";

                    if (++iCount > MAX_ERROR_LIST_COUNT)
                    {
                        sObjectList += string.Format("And More {0} objects not listed", locLineObjects.Count - iCount);
                        break;
                    }
                }


                m_logger.Error(sObjectList, excp);
#endif
                ExcpHelper.ThrowUp(excp, "ERROR InsertOrUpdate() for {0}", locLineObjects);
            }
            finally
            {
                ct.AddEvent("InsertOrUpdate for '{0}' completed", ts.TableName);
                ct.Info(m_logger);
            }
        }