示例#1
0
 /// <summary>
 /// Build "order by" using the specified generic field name.
 /// </summary>
 /// <param name="sortField">official field name (not the column name)</param>
 /// <param name="entity"></param>
 /// <param name="descending">true for descending sort, false for ascending sort</param>
 /// <returns>empty string if sortField not defined for this station entity.</returns>
 public static string AddSortOrder(string sortField, StationEntity entity,
     bool descending)
 {
     string orderBy = "";
     StationField sf = entity.GetField(sortField);
     if (sf != null)
     {
         orderBy += " order by " + sf.Location + " ";
         if (descending) orderBy += "desc ";
     }
     return orderBy;
 }
示例#2
0
        /// <summary>
        /// Helper methods to build SQL queries.
        /// Not used recently since using Entity Framework instead.
        /// </summary>
        /// <summary>
        /// Build database SQL query clause for multiple criteria.
        /// TODO could add a "joiner" operator to use between multiple clauses (OR, AND)
        /// </summary>
        /// <param name="prefix">operator to precede clause, e.g. "AND"</param>
        /// <param name="crit">set of Criterion (in a Collection)</param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string BuildQueryClause(string prefix, ICollection<Criterion> crit, StationEntity entity)
        {
            string clause = "";
            foreach (Criterion c in crit)
            {
                // ignore "TYPE" criteria when building query
                if (c.FieldName == (Field.EntityType)) continue;

                // if we have already added a clause, use "AND" to join the next clause
                if (clause.Length > 0) prefix = "AND";
                // get the implementation details for this field in this station
                StationField sf = entity.GetField(c.FieldName);

                // TODO should we just return an empty string instead?
                //     risk is returning too many results
                //     this could be configurable if we make this a separate instantiable class
                if (sf == null) throw new StationUnsupportedCriterionException(
                    "Station doesn't support field " + c.FieldName);

                string type = sf.Type;
                // TODO for now, we assume types are generic types.
                //    when we want to extend with custom types, we will need another
                //    mechanism to handle building queries for custom types.
                if (type == (Field.TextType))
                    clause += AddSqlTextClause(prefix, sf, c);
                else if (type == (Field.NumberType))
                    clause += AddSqlNumberClause(prefix, sf, c);
                else if (type == (Field.DateType))
                    clause += AddSqlDateClause(prefix, sf, c);
                // TODO implement clause builders for other data types : BOOLEAN
                else throw new Exception("Field type not yet implemented: " + type);
            }
            return clause;
        }
示例#3
0
        protected virtual List<StationEntity> LoadStationEntities(ConfigData configData)
        {
            var entitiesList = new List<StationEntity>();
            var entityConfigs = configData.GetConfigSections(Constants.StationEntity);
            foreach (var entityConfig in entityConfigs)
            {
                var entityName = entityConfig.RequiredValue(Constants.StationEntityName);
                var entity = new StationEntity(entityName);
                var entityLocation = entityConfig.Value(Constants.StationEntityLocation);
                entity.Location = (entityLocation);
                var fieldConfigs = entityConfig.GetConfigSections(Constants.StationField);
                foreach (var fieldConfig in fieldConfigs)
                {
                    var field = new StationField(fieldConfig.RequiredValue(Constants.StationFieldName));
                    field.Type = (fieldConfig.RequiredValue(Constants.StationFieldType));

                    // if location is empty, just use field name as default location
                    var location = fieldConfig.Value(Constants.StationFieldLocation);
                    if (location == null) location = field.Name;
                    field.Location = (location);

                    field.Level = (fieldConfig.IntValue(Constants.StationFieldLevel));
                    // TODO set other field properties -  referredEntity, referredEntityField
                    entity.AddField(field);
                }
                var sortField = entityConfig.Value(Constants.StationEntitySortField);
                if (sortField != null)
                {
                    entity.SortField = (sortField);
                    var sortOrder = entityConfig.Value(Constants.StationEntitySortOrder);
                    if (sortOrder != null && sortOrder.ToUpper().StartsWith("DESC"))
                    {
                        entity.DescendingSort = (true);
                    }
                }
                entitiesList.Add(entity);
            }
            return entitiesList;
        }