/// <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; }
/// <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; }