StringBuilder CreateSqlText(DataRecordTypePlan typePlan) { CommandParams pars = Pars; string[] valueKeys = pars.GetAttachedValueKeys(); var stBuilder = new StringBuilder(); stBuilder.Append("select "); int j = typePlan.fields.Count; for (int i = 0; i < j; ++i) { DataFieldPlan fieldPlan = typePlan.fields[i]; stBuilder.Append(fieldPlan.name); if (i < j - 1) { stBuilder.Append(','); } } stBuilder.Append(" from "); stBuilder.Append(TargetTableName); if (_whereClause != null) { stBuilder.Append(" where "); stBuilder.Append(_whereClause); } if (Limit != null) { stBuilder.Append(" limit " + Limit); } return(stBuilder); }
static DataRecordTypePlan CreateTypePlan(Type t) { if (t.IsPrimitive) { return(null); } //----------------- //check public ctor ConstructorInfo[] allCtors = t.GetConstructors(BindingFlags.Instance | BindingFlags.Public); //type layout check //1. find default ctor int ctorCount = allCtors.Length; if (ctorCount == 0) { return(null); } ConstructorInfo defaultParameterLessCtor = null; for (int i = 0; i < ctorCount; ++i) { ConstructorInfo ctor = allCtors[i]; ParameterInfo[] ctorParams = ctor.GetParameters(); if (ctorParams.Length == 0) { defaultParameterLessCtor = ctor; break; } else { } } //------------------------------------------------- //note, in this version we have some restriction //1. must be class //2. must be sealed //3. have 1 ctor if (!t.IsSealed && allCtors.Length > 0) { return(null); } if (t.IsAutoLayout) { if (defaultParameterLessCtor != null) { //have parameter less ctor //then //more restrictions //1. all public fields, no properties //2. all public properties, no public fields //TODO: //impl: PropertyInfo[] allProps = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); FieldInfo[] allFields = t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); if (allProps.Length > 0 && allFields.Length == 0) { var typePlan = new DataRecordTypePlan(); int j = allProps.Length; for (int i = 0; i < j; ++i) { PropertyInfo propInfo = allProps[i]; if (propInfo.GetMethod == null || propInfo.SetMethod == null) { return(null); } var fieldPlan = new DataFieldPlan(propInfo); typePlan.fields.Add(fieldPlan); } typePlan.planKind = TypePlanKind.AllProps; return(typePlan); } else if (allFields.Length > 0) { //not readonly field var typePlan = new DataRecordTypePlan(); int j = allFields.Length; for (int i = 0; i < j; ++i) { FieldInfo fieldInfo = allFields[i]; var fieldPlan = new DataFieldPlan(fieldInfo); typePlan.fields.Add(fieldPlan); } typePlan.planKind = TypePlanKind.AllFields; return(typePlan); } else { return(null); } } else { //guess that type is anonymous type //no default parameter less ctro //only 1 exeption, //for anonymous type //------------------------------------------------- //get public property get,set only //single layer only PropertyInfo[] allProps = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); //all prop has no set method int j = allProps.Length; for (int i = 0; i < j; ++i) { PropertyInfo propInfo = allProps[i]; if (propInfo.SetMethod != null) { //not anonyomus type return(null); } } ParameterInfo[] ctorParams = allCtors[0].GetParameters(); if (ctorParams.Length != allProps.Length) { return(null); //not anonyomus type } //--------------------------------------------------------- //guess that this is anonymous type //since type is auto layout //so we use field order according to its ctor parameters var typePlan = new DataRecordTypePlan(); for (int i = 0; i < j; ++i) { ParameterInfo pinfo = ctorParams[i]; typePlan.fields.Add(new DataFieldPlan(pinfo)); } typePlan.planKind = TypePlanKind.MaybeAnonymousType; return(typePlan); } } else if (t.IsLayoutSequential) { //assign by fieldname *** if (defaultParameterLessCtor == null) { //TODO: impl here return(null); } else { //have parameter less ctor //then //more restrictions //1. all public fields, no properties //2. all public properties, no public fields //TODO: //impl: PropertyInfo[] allProps = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); FieldInfo[] allFields = t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); if (allProps.Length > 0 && allFields.Length == 0) { var typePlan = new DataRecordTypePlan(); int j = allProps.Length; for (int i = 0; i < j; ++i) { PropertyInfo propInfo = allProps[i]; if (propInfo.GetMethod == null || propInfo.SetMethod == null) { return(null); } var fieldPlan = new DataFieldPlan(propInfo); typePlan.fields.Add(fieldPlan); } typePlan.planKind = TypePlanKind.AllProps; return(typePlan); } else if (allFields.Length > 0) { //not readonly field var typePlan = new DataRecordTypePlan(); int j = allFields.Length; for (int i = 0; i < j; ++i) { FieldInfo fieldInfo = allFields[i]; var fieldPlan = new DataFieldPlan(fieldInfo); typePlan.fields.Add(fieldPlan); } typePlan.planKind = TypePlanKind.AllFields; return(typePlan); } else { return(null); } } } else { throw new Exception("not supported layout"); } }