public IQueryBuilder QueryIds <T>(List <T> rs) where T : IDataObject { var type = rs.FirstOrDefault()?.GetType() ?? typeof(T); var id = FiTechBDadosExtensions.IdColumnOf[type]; var rid = FiTechBDadosExtensions.RidColumnOf[type]; var ridType = ReflectionTool.GetTypeOf(ReflectionTool.FieldsAndPropertiesOf(type).FirstOrDefault(x => x.GetCustomAttribute <ReliableIdAttribute>() != null)); return(Qb.Fmt($"SELECT {id} AS Id, {rid} AS RID FROM {type.Name} WHERE") + Qb.In(rid, rs, i => Convert.ChangeType(i.RID, ridType))); }
/// <summary> /// deprecated /// this is responsibility of the rdbms query generator /// </summary> /// <param name="field"></param> /// <param name="info"></param> /// <returns></returns> public String GetColumnDefinition(MemberInfo field, FieldAttribute info = null) { if (info == null) { info = field.GetCustomAttribute <FieldAttribute>(); } if (info == null) { return("VARCHAR(128)"); } var typeOfField = ReflectionTool.GetTypeOf(field); var nome = field.Name; String tipo = GetDatabaseTypeWithLength(field, info); var options = ""; if (info.Options != null && info.Options.Length > 0) { options = info.Options; } else { if (!info.AllowNull) { options += " NOT NULL"; } else if (Nullable.GetUnderlyingType(typeOfField) == null && typeOfField.IsValueType && !info.AllowNull) { options += " NOT NULL"; } if (info.Unique) { options += " UNIQUE"; } if ((info.AllowNull && info.DefaultValue == null)) { options += " DEFAULT NULL"; } else if (info.DefaultValue != null) { options += $" DEFAULT {ConvertDefaultOption(info.DefaultValue, typeOfField)}"; } foreach (var att in field.GetCustomAttributes()) { if (att is PrimaryKeyAttribute) { options = " PRIMARY KEY"; tipo = "SERIAL"; } } } return($"{nome} {tipo} {options}"); }
private String GetPrefixOfExpression(Expression expression) { if (expression == null) { return(""); } if (expression is UnaryExpression u) { return(GetPrefixOfExpression(u.Operand)); } if (expression is ParameterExpression p) { return(prefixer.GetAliasFor("root", p.Type.Name, String.Empty)); } var exp = (expression as MemberExpression).Expression; var agT = exp.Type; Expression subexp = expression; var thisAlias = "tba"; // prefixer.GetAliasFor("tba", agT.Name, String.Empty); while (subexp.NodeType == ExpressionType.MemberAccess) { if (subexp is MemberExpression smex) { var mt = ReflectionTool.GetTypeOf(smex.Member); var smexMember = (smex.Expression as MemberExpression)?.Member; var f1 = smex.Member.GetCustomAttribute <AggregateFieldAttribute>(); var f2 = smexMember?.GetCustomAttribute <AggregateObjectAttribute>(); var f3 = smex.Member.GetCustomAttribute <AggregateFarFieldAttribute>(); var f4 = smexMember?.GetCustomAttribute <AggregateListAttribute>(); if (f1 != null) { thisAlias = prefixer.GetAliasFor(thisAlias, f1.RemoteObjectType.Name, f1.ObjectKey); } else if (f2 != null) { thisAlias = prefixer.GetAliasFor(thisAlias, ReflectionTool.GetTypeOf(smexMember)?.Name, f2.ObjectKey); } else if (f3 != null) { thisAlias = prefixer.GetAliasFor(thisAlias, f3.ImediateType.Name, f3.ImediateKey); thisAlias = prefixer.GetAliasFor(thisAlias, f3.FarType.Name, f3.FarKey); } else if (f4 != null) { thisAlias = prefixer.GetAliasFor(thisAlias, f4.RemoteObjectType.Name, f4.RemoteField); } subexp = (subexp as MemberExpression).Expression; } } return(thisAlias); }
public IQueryBuilder GenerateUpdateQuery(IDataObject tabelaInput) { var type = tabelaInput.GetType(); var rid = FiTechBDadosExtensions.RidColumnOf[type]; var ridType = ReflectionTool.GetTypeOf(ReflectionTool.FieldsAndPropertiesOf(type).FirstOrDefault(x => x.GetCustomAttribute <ReliableIdAttribute>() != null)); QueryBuilder Query = new QbFmt(String.Format("UPDATE {0} ", tabelaInput.GetType().Name)); Query.Append("SET"); Query.Append(GenerateUpdateValueParams(tabelaInput, false)); Query.Append($" WHERE {rid} = @rid;", Convert.ChangeType(tabelaInput.RID, ridType)); return(Query); }
public static void ValuesFromDictionary(this Object me, Dictionary <string, object> input) { if (me == null) { throw new NullReferenceException("Figlotech FromDictionary Extension method called on a null value, this is a natural NullReferenceException"); } var refl = me.AsReflectable(); foreach (var a in ReflectionTool.FieldsAndPropertiesOf(me.GetType())) { if (ReflectionTool.GetTypeOf(a).IsPublic&& input.ContainsKey(a.Name)) { refl[a.Name] = input[a.Name]; } } }
public IQueryBuilder GenerateMultiUpdate <T>(List <T> inputRecordset) where T : IDataObject { // -- List <T> workingSet = new List <T>(); var rid = FiTechBDadosExtensions.RidColumnOf[typeof(T)]; workingSet.AddRange(inputRecordset.Where((record) => record.IsPersisted)); if (workingSet.Count < 1) { return(null); } QueryBuilder Query = new QueryBuilder(); Query.Append($"UPDATE {typeof(T).Name} "); Query.Append("SET "); // -- var members = GetMembers(typeof(T)); members.RemoveAll(m => m.GetCustomAttribute <PrimaryKeyAttribute>() != null); int x = 0; for (var i = 0; i < members.Count; i++) { var memberType = ReflectionTool.GetTypeOf(members[i]); Query.Append($"{members[i].Name}=(CASE "); foreach (var a in inputRecordset) { Query.Append($"WHEN {rid}=@_mu{x++} THEN @_mu{x++}", Convert.ChangeType(a.RID, FiTechBDadosExtensions.RidFieldType[a.GetType()]), ReflectionTool.GetMemberValue(members[i], a)); } Query.Append($"ELSE {members[i].Name} END)"); if (i < members.Count - 1) { Query.Append(","); } } Query.Append($"WHERE {rid} IN (") .Append(Fi.Tech.ListRids(workingSet)) .Append(");"); // -- return(Query); }
/// <summary> /// <para> /// This function scans input object and resolves all fields and properties /// that are Interface type with null value and resolves them. The scanner considers /// EVERY public interface field or property that is null as a "non-satisfied" dependency /// and tries to resolve it. /// </para> /// <para> /// It's shitty, I think the programmer community will hate on me for this, but I don't care /// I'm lazy, that's what I am. /// </para> /// </summary> /// <param name="input">The object to be scanned and have its dependencies resolved</param> public void SmartResolve(object input, bool ignoreErrors = false) { ObjectReflector rflx = new ObjectReflector(input); var t = input.GetType(); var members = ReflectionTool.FieldsAndPropertiesOf(t); foreach (var member in members) { var type = ReflectionTool.GetTypeOf(member); if (type.IsInterface) { var resolution = Resolve(type, ignoreErrors); if (resolution != null) { rflx[member] = resolution; } } } }
public static Dictionary <string, object> ValuesToDictionary(this Object me) { if (me == null) { throw new NullReferenceException("Figlotech ToDictionary Extension method called on a null value, this is a natural NullReferenceException"); } var retv = new Dictionary <string, object>(); var refl = me.AsReflectable(); foreach (var a in ReflectionTool.FieldsAndPropertiesOf(me.GetType())) { if (ReflectionTool.GetTypeOf(a).IsPublic) { retv[a.Name] = ReflectionTool.GetMemberValue(a, me); } } return(retv); }
void CascadingDoForFields <T>(Action <T> process, List <Type> prevList = null) { if (prevList == null) { prevList = new List <Type>(); } // prevList is a resource I'm using to break the f**k out of // looping references. // Idk if its the best way to, but it works. if (prevList.Contains(this.GetType())) { return; } prevList.Add(this.GetType()); if (!typeof(T).IsInterface) { throw new BDadosException("CascadingDoForFields expects T to be an Interface type."); } var myValues = new List <MemberInfo>(); myValues.AddRange(this.GetType().GetFields()); myValues.AddRange(this.GetType().GetProperties()); foreach (var field in myValues) { var workingValue = ReflectionTool.GetMemberValue(field, this); if (workingValue is T) { CascadingDoForFields <T>(process, prevList); } if (!ReflectionTool.GetTypeOf(field).GetInterfaces().Contains(typeof(T))) { continue; } prevList.Add(ReflectionTool.GetTypeOf(field)); try { T workObject = ((T)workingValue); process(workObject); } catch (Exception) { } } }
public void Apply(T element, IEnumerable <T> elements) { var value = Enumerable.Sum <T>(elements, (e) => (decimal)ReflectionTool.GetMemberValue(member, e)); ReflectionTool.SetMemberValue(member, element, Convert.ChangeType(value, ReflectionTool.GetTypeOf(member))); }
public IQueryBuilder GenerateMultiUpdate <T>(List <T> inputRecordset) where T : IDataObject { // -- var t = inputRecordset?.FirstOrDefault()?.GetType(); if (t == null) { return(Qb.Fmt("SELECT 1")); } List <T> workingSet = new List <T>(); var rid = FiTechBDadosExtensions.RidColumnOf[t]; var upd = FiTechBDadosExtensions.UpdateColumnOf[t]; workingSet.AddRange(inputRecordset.Where((record) => record.IsPersisted)); if (workingSet.Count < 1) { return(null); } QueryBuilder Query = new QueryBuilder(); Query.Append($"UPDATE {t.Name} "); Query.Append("SET \r\n"); // -- var members = GetMembers(t); members.RemoveAll( m => m.GetCustomAttribute <PrimaryKeyAttribute>() != null || m.GetCustomAttribute <ReliableIdAttribute>() != null ); int x = 0; int ggid = ++gid; Query.PrepareForQueryLength(inputRecordset.Count * 512); for (var i = 0; i < members.Count; i++) { var memberType = ReflectionTool.GetTypeOf(members[i]); Query.Append($"\t{members[i].Name}=(CASE "); for (int ridx = 0; ridx < inputRecordset.Count; ridx++) { Query.Append($"WHEN {rid}=@r_{ridx}", inputRecordset[ridx].RID); if (inputRecordset[ridx].IsReceivedFromSync) { Query.Append($"AND {upd}<@u_{ridx}", inputRecordset[ridx].UpdatedTime); } Query.Append($"THEN @{ggid}_{++x}", ReflectionTool.GetMemberValue(members[i], inputRecordset[ridx])); } Query.Append($"ELSE {members[i].Name} END)"); if (i < members.Count - 1) { Query.Append(",\r\n"); } } Query.Append($"WHERE {rid} IN (") .Append(Fi.Tech.ListRids(workingSet)) .Append(");"); // -- return(Query); }
public String GetDatabaseType(MemberInfo field, FieldAttribute info = null) { if (info == null) { foreach (var att in field.GetCustomAttributes()) { if (att is FieldAttribute) { info = (FieldAttribute)att; break; } } } if (info == null) { return("VARCHAR"); } var typeOfField = ReflectionTool.GetTypeOf(field); string tipoDados; if (Nullable.GetUnderlyingType(typeOfField) != null) { tipoDados = Nullable.GetUnderlyingType(typeOfField).Name; } else { tipoDados = typeOfField.Name; } if (typeOfField.IsEnum) { return("INT"); } String type = "VARCHAR"; if (info.Type != null && info.Type.Length > 0) { type = info.Type; } else { switch (tipoDados.ToLower()) { case "string": type = $"VARCHAR"; break; case "short": case "int16": type = $"SMALLINT"; break; case "ushort": case "uint16": type = $"SMALLINT"; break; case "int": case "int32": type = $"INT"; break; case "uint": case "uint32": type = $"INT"; break; case "long": case "int64": type = $"BIGINT"; break; case "ulong": case "uint64": type = $"BIGINT"; break; case "bool": case "boolean": type = $"TINYINT"; break; case "float": case "double": case "single": case "decimal": type = $"DECIMAL"; break; case "byte[]": type = $"BLOB"; break; case "datetime": type = $"DATETIME"; break; } } return(type); }