Пример #1
0
        // Returns a list of objects of the specified type, based on the specified criteria.
        public List <DatabaseTable> Get(Type tableType, ICriteria criteria)
        {
            verifyTable(tableType);

            // if this is a request for all object of this type, if we already have done this
            // type of request, just return the cached objects. This assumes no one else is changing
            // the DB.
            if (criteria == null)
            {
                if (doneFullRetrieve.ContainsKey(tableType))
                {
                    return(new List <DatabaseTable>(cache.GetAll(tableType)));
                }

                doneFullRetrieve[tableType] = true;
            }

            lock (lockObject) {
                List <DatabaseTable> rtn = new List <DatabaseTable>();

                try {
                    // build and execute the query
                    string query = getSelectQuery(tableType);
                    if (criteria != null)
                    {
                        query += criteria.GetWhereClause();
                    }

                    SQLiteResultSet resultSet = dbClient.Execute(query);

                    // store each one
                    foreach (SQLiteResultSet.Row row in resultSet.Rows)
                    {
                        // create the new entry
                        DatabaseTable newRecord = (DatabaseTable)tableType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                        newRecord.DBManager = this;
                        newRecord.LoadByRow(row);

                        // if it is already cached, just use the cached object
                        if (cache.Get(tableType, (int)newRecord.ID) != null)
                        {
                            rtn.Add(cache.Get(tableType, (int)newRecord.ID));
                        }

                        // otherwise use the new record and cache it
                        else
                        {
                            newRecord = cache.Add(newRecord);
                            getAllRelationData(newRecord);
                            rtn.Add(newRecord);
                        }
                    }
                }
                catch (SQLiteException e) {
                    logger.ErrorException("Error retrieving with criteria from " + tableType.Name + " table.", e);
                }

                return(rtn);
            }
        }