コード例 #1
0
        public bool Update <TEntity> (IID entity) where TEntity : class
        {
            if (entity == null)
            {
                return(false);
            }

            if (entity.ID == 0)
            {
                return(Add <TEntity> (entity));
            }

            using (CSDB context = new CSDB(Connection))
            {
                TEntity origonal = GetSingle <TEntity>(entity.ID, context);

                MakeObjectsEqual(origonal, entity);

                context.SubmitChanges( );

                cache.AddToCache(typeof(TEntity), entity);

                return(true);
            }
        }
コード例 #2
0
        public bool Delete <TEntity> (IID entity) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                Type           baseType = typeof(TEntity);
                TableAttribute ta       = null;

                while (true)
                {
                    ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                    if (ta != null)
                    {
                        break;
                    }

                    baseType = baseType.BaseType;

                    if (baseType == null)
                    {
                        break;
                    }
                }

                if (ta == null)
                {
                    throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
                }


                using (CSDB context = new CSDB(Connection))
                {
#if (DEBUG)
                    TextWriter log = new StringWriter();
                    context.Log = log;
#endif
                    TEntity t = GetSingle <TEntity> (entity.ID, context);

                    context.GetTable(baseType).DeleteOnSubmit(t);
                    context.SubmitChanges( );
#if (UseCache)
                    cache.DeleteItemFromCache(baseType, ((IID)t).ID);
#endif
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error deleting entity from database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #3
0
        internal static void InsertSchoolTeam(SchoolTeams ST, SQLiteConnection Connection)
        {
            using (CSDB_BASE context = new CSDB(Connection))

            {
                string cmd = string.Format(@"INSERT INTO SchoolTeams (ID, School_ID, Team_ID) VALUES ({0}, {1}, {2})",
                                           CSDB_BASE.getNextIdentifier(typeof(SchoolTeams), context), ST.School.ID, ST.Team.ID);

                object[] obj = context.ExecuteQuery(cmd).FirstOrDefault();
            }
        }
コード例 #4
0
        private bool addRange <TEntity> (TEntity[] entities) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                ArrayList newEntities = new ArrayList(entities.Count());
                TEntity   newEntity;

                // determine type here
                var type = typeof(TEntity);   //.GetType(); // entities.GetType();

                foreach (IID entity in entities)
                {
                    // create an object of the type
                    newEntity = (TEntity)Activator.CreateInstance(type);

                    MakeObjectsEqual(newEntity, entity);

                    newEntities.Add(newEntity);
                }


                using (CSDB context = new CSDB(Connection))
                {
                    TEntity[] array = (TEntity[])newEntities.ToArray();
                    context.GetTable <TEntity> ( ).InsertAllOnSubmit(array);
                    context.SubmitChanges( );
                }

                for (int i = 0; i < entities.Count( ); i++)
                {
                    ((IID)entities[i]).ID            = ((IID)newEntities[i]).ID;
                    ((IID)entities[i]).Discriminator = ((IID)newEntities[i]).Discriminator;
                    cache.AddToCache(type, (IID)entities[i]);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error inserting range into database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #5
0
        public bool DeleteRange <TEntity> (TEntity[] entities) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                using (CSDB context = new CSDB(Connection))
                {
                    // retrieve entities from context

                    HashSet <TEntity> ToBeDeleted = new HashSet <TEntity>();

                    foreach (IID entity in entities)
                    {
                        TEntity t = GetSingle <TEntity> (entity.ID, context);
                        if (t != null)
                        {
                            ToBeDeleted.Add(t);
                        }
                    }
                    if (ToBeDeleted.Count == 0)
                    {
                        return(false);
                    }

                    context.GetTable <TEntity> ( ).DeleteAllOnSubmit(ToBeDeleted);
                    context.SubmitChanges( );

                    foreach (IID entity in entities)
                    {
                        cache.DeleteItemFromCache(typeof(TEntity), ((IID)entity).ID);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error deleting range from database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #6
0
 /// <summary>
 /// Absolutely must ask for a return value or it wont be fired.
 /// </summary>
 /// <param name="con"></param>
 /// <param name="query"></param>
 /// <returns></returns>
 public static IEnumerable <object[]> ExecuteQuery(
     this DbConnection con, string query)
 {
     using (CSDB_BASE context = new CSDB(con))
     {
         using (DbCommand cmd = con.CreateCommand( ))
         {
             cmd.CommandText = query;
             con.Open( );
             using (DbDataReader rdr =
                        cmd.ExecuteReader(CommandBehavior.CloseConnection))
             {
                 while (rdr.Read( ))
                 {
                     object[] res = new object[rdr.FieldCount];
                     rdr.GetValues(res);
                     yield return(res);
                 }
             }
         }
     }
 }
コード例 #7
0
 public static int CleanUpCustomData(CSDB Context)
 {
     return(Context.ExecuteCommand("DELETE FROM[CustomDataValues] WHERE ( [Championship_ID] IS NULL ) AND ( [Competitor_ID] IS NULL ) AND ( [Event_ID] IS NULL )"));
 }
コード例 #8
0
        public static int DeleteAthleteTeamChamptionships(int Championship_ID, int Athlete_ID, CSDB Context)
        {
            string cmd = string.Format("DELETE FROM [AthleteTeamChamptionships] WHERE [Championship_ID] = {0} AND [Athlete_ID] = {1}", Championship_ID, Athlete_ID);

            return(Context.ExecuteCommand(cmd));
        }
コード例 #9
0
        internal static int DeleteSchoolTeam(int sch, int team, CSDB Context)
        {
            string cmd = string.Format("DELETE FROM [SchoolTeams] WHERE [School_ID] = {0} AND [Team_ID] = {1}", sch, team);

            return(Context.ExecuteCommand(cmd));
        }
コード例 #10
0
        private bool add <TEntity> (TEntity entity) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                TEntity newEntity;

                // determine type here
                var type = entity.GetType();

                Type           baseType = typeof(TEntity);
                TableAttribute ta       = null;

                while (true)
                {
                    ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                    if (ta != null)
                    {
                        break;
                    }

                    baseType = baseType.BaseType;

                    if (baseType == null)
                    {
                        break;
                    }
                }

                if (ta == null)
                {
                    throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
                }


                // create an object of the type
                newEntity = (TEntity)Activator.CreateInstance(type);

                MakeObjectsEqual(newEntity, entity);

                using (CSDB context = new CSDB(Connection))
                {
                    context.GetTable <TEntity> ( ).InsertOnSubmit(newEntity);
                    context.SubmitChanges( );
                }

                ((IID)entity).ID            = ((IID)newEntity).ID;
                ((IID)entity).Discriminator = ((IID)newEntity).Discriminator;

                cache.AddToCache(baseType, (IID)entity);

                return(true);
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error inserting into database of type " + typeof(TEntity).ToString());
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #11
0
        public TEntity GetID <TEntity> (int?ID) where TEntity : class
        {
            if (ID == null)
            {
                return(null);
            }

            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }


            Type           baseType = typeof(TEntity);
            TableAttribute ta       = null;

            while (true)
            {
                ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                if (ta != null)
                {
                    break;
                }

                baseType = baseType.BaseType;

                if (baseType == null)
                {
                    break;
                }
            }

            if (ta == null)
            {
                throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
            }
#if (UseCache)
            // Try to get the data from the cache first
            bool    deleted;
            TEntity cacheObj = (TEntity)cache.GetFromCache(baseType, ID.Value, out deleted);

            if (deleted)
            {
                return(null);
            }

            if (cacheObj != null)
            {
                CacheReadCounter++;
                return(cacheObj);
            }
#endif
            // could not find this item in the cache or it needs updating.

            using (CSDB context = new CSDB(Connection))
            {
                TEntity temp = context.GetTable(baseType).OfType <TEntity>().Where(t => ((IID)t).ID == ID).ToList( ).FirstOrDefault();

                if (temp != null)
                {
                    if (typeof(IID).IsAssignableFrom(typeof(TEntity)))
                    {
                        ((IID)temp).DState = getDataState( );
#if (UseCache)
                        cache.AddToCache(baseType, (IID)temp);
#endif
                    }
                }

                DBReadCounter++;
                return(temp);
            }
        }
コード例 #12
0
        public IList <TEntity> GetAll <TEntity> ( ) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            // try to get the Table (base) type instead of T

            Type           baseType = typeof(TEntity);
            TableAttribute ta       = null;

            while (true)
            {
                ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                if (ta != null)
                {
                    break;
                }

                baseType = baseType.BaseType;

                if (baseType == null)
                {
                    break;
                }
            }

            if (ta == null)
            {
                throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
            }

#if (UseCache)
            // Try to get the data from the cache first
            var cacheTable = cache.GetTableFromCache(baseType);

            if (cacheTable != null)
            {
                CacheReadCounter++;

                // Casting list makes sure that only valid items are returned.
                // For example if TEntity is Athlete then we need to remove all
                // objects of type person.
                List <TEntity> castingList = new List <TEntity>();

                foreach (var i in cacheTable)
                {
                    if (i is TEntity)
                    {
                        castingList.Add((TEntity)i);
                    }
                }

                return(castingList.Cast <TEntity> ( ).ToList( ));
            }

            // could not find this item in the cache or it needs updating.
#endif
            using (CSDB context = new CSDB(Connection))
            {
                context.DeferredLoadingEnabled = false;


                IList <TEntity> temp = context.GetTable(baseType).OfType <TEntity>().ToList( );

                if (typeof(IID).IsAssignableFrom(typeof(TEntity)))
                {
                    foreach (TEntity ent in temp)
                    {
                        ((IID)ent).DState = getDataState( );
                    }

#if (UseCache)
                    cache.AddTableToCache(baseType, temp.Cast <IID>().ToList());
#endif
                }

                DBReadCounter++;
                return(temp);
            }
        }