/* * Join clause * Get join clause */ public void GetJoinClauses <T>(T obj, JoinClauses join) { object cls = null; /* * var properties = obj.GetType().GetProperties() * .Where(prop => prop.IsDefined(typeof(DBAttributes), false)); */ foreach (PropertyInfo prop in obj.GetType().GetProperties()) { foreach (DBAttributes attr in prop.GetCustomAttributes <DBAttributes>(false).Where(el => el.JoinClass != null)) { cls = Activator.CreateInstance( Type.GetType( obj.GetType().Namespace + "." + attr.JoinClass) ); join.AddJoin(GetTableName(cls), GetAlias(cls), attr.JoinMethod); join.AddComparison(GetTableName(cls), GetAlias(obj), prop.Name, GetAlias(cls), prop.Name, attr.Comparison); } } if (cls != null) { GetJoinClauses(cls, join); } }
//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()); }