/* * Select clause * Get all property name (as column name) with attribute Showable is true */ public void GetSelectClauses <T>(T obj, SelectClauses select) { if (obj == null || select == null) { throw new ArgumentNullException("Null parameter."); } foreach (PropertyInfo prop in obj.GetType().GetProperties()) { foreach (DBAttributes attr in prop.GetCustomAttributes <DBAttributes>(false).Where(el => el.Showable || el.JoinClass != null)) { if (attr.Showable) { select.Add(GetAlias(obj), prop.Name); } else if (attr.JoinClass != null) { if (!_alias.ContainsKey(attr.JoinClass)) { GetSelectClauses(Activator.CreateInstance( Type.GetType( obj.GetType().Namespace + "." + attr.JoinClass) ), select); } } } } }
//demo of sqlbuilder static void Main(string[] args) { P p = new P(); DBBase db = new DBBase(); QueryClauses q = new QueryClauses(); OrderByClauses o = new OrderByClauses(); DBBase.GetQueryClauses("q0=age,eq,&q1=sex,eq,1&q2=age,lt,30&q3=sex,in,1,2,3,4&q4=id,eq,a001&orderby=sex,asc,age,desc", q, o); SelectClauses s = new SelectClauses(); db.GetSelectClauses(p, s); JoinClauses j = new JoinClauses(); db.GetJoinClauses(p, j); WhereClauses w = new WhereClauses(); db.GetWhereClauses(p, q, w); db.GetOrderByClauses(p, o); SQLClause sql = new SQLClause(s, db.GetFromClause(p), j, w, o); Trace.WriteLine(sql.ToString()); }