public IQueryBuilder GetIdFromRid <T>(object Rid) where T : IDataObject, new() { var id = ReflectionTool.FieldsAndPropertiesOf(typeof(T)).FirstOrDefault(f => f.GetCustomAttribute <PrimaryKeyAttribute>() != null); var rid = ReflectionTool.FieldsAndPropertiesOf(typeof(T)).FirstOrDefault(f => f.GetCustomAttribute <ReliableIdAttribute>() != null); return(new QueryBuilder().Append($"SELECT {id.Name} FROM {typeof(T).Name} WHERE {rid.Name}=@???", Rid)); }
public static IEnumerable <Type> ExplodeDependencyTree(Type t, int level = 0, List <Type> dependedTypes = null) { var members = ReflectionTool.FieldsAndPropertiesOf(t); dependedTypes = dependedTypes ?? new List <Type>(); //Console.WriteLine($"EDT | {new String(' ', level)} -> {t.Name}"); foreach (var m in members) { var fk = m.GetCustomAttribute <ForeignKeyAttribute>(); if (fk != null) { if (!dependedTypes.Contains(fk.RefType)) { dependedTypes.Add(fk.RefType); yield return(fk.RefType); foreach (var innerDep in ExplodeDependencyTree(fk.RefType, level++, dependedTypes)) { if (!dependedTypes.Contains(innerDep)) { yield return(innerDep); } } } } } }
public IQueryBuilder QueryIds <T>(List <T> rs) where T : IDataObject { var id = ReflectionTool.FieldsAndPropertiesOf(typeof(T)).FirstOrDefault(f => f.GetCustomAttribute <PrimaryKeyAttribute>() != null); var rid = ReflectionTool.FieldsAndPropertiesOf(typeof(T)).FirstOrDefault(f => f.GetCustomAttribute <ReliableIdAttribute>() != null); return(Qb.Fmt($"SELECT {id.Name}, {rid.Name} FROM {typeof(T).Name} WHERE") + Qb.In(rid.Name, rs, i => i.RID)); }
public IQueryBuilder GetCreationCommand(Type t) { String objectName = t.Name; var members = ReflectionTool .FieldsAndPropertiesOf(t).Where((m) => m?.GetCustomAttribute <FieldAttribute>() != null) .ToArray(); if (objectName == null || objectName.Length == 0) { return(null); } QueryBuilder CreateTable = new QbFmt($"CREATE TABLE IF NOT EXISTS {objectName} (\n"); for (int i = 0; i < members.Length; i++) { var info = members[i].GetCustomAttribute <FieldAttribute>(); CreateTable.Append(GetColumnDefinition(members[i], info)); CreateTable.Append(" "); CreateTable.Append(info.Options ?? ""); if (i != members.Length - 1) { CreateTable.Append(", \n"); } } CreateTable.Append(" );"); return(CreateTable); }
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))); }
public static String GetIdColumn(this Fi _selfie, Type type) { var fields = ReflectionTool.FieldsAndPropertiesOf(type); var retv = fields .Where((f) => f.GetCustomAttribute <PrimaryKeyAttribute>() != null) .FirstOrDefault() ?.Name ?? "Id"; return(retv); }
// private static Object _readLock = new Object(); // private static int _generalId = 0; // public static ILogger ApiLogger; // public static bool EnableStdoutLogs { get; set; } = false; public static String GetUpdateColumn(this Fi _selfie, Type type) { var fields = ReflectionTool.FieldsAndPropertiesOf(type); var retv = fields .Where((f) => f.GetCustomAttribute <UpdateTimeStampAttribute>() != null) .FirstOrDefault() ?.Name ?? "UpdatedTime"; return(retv); }
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 async Task WriteObjectsToCsv <T>(IEnumerable <T> objs, Stream stream, bool printHeaders = true) { var headers = ReflectionTool.FieldsAndPropertiesOf(typeof(T)).Select(x => x.Name).ToArray(); if (printHeaders) { WriteHeadersToStream(headers, stream); } var li = objs.ToList(); await Fi.Tech.ParallelFlow <T>((ch) => { ch.ReturnRange(li); }).Then(Math.Max(Environment.ProcessorCount - 1, 1), async(c) => { return(ObjectToRow(headers, new ObjectReflector(c))); }).Then(1, async(data) => { WriteDataToStream(data, stream); }); }
/// <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; } } } }
private List <MemberInfo> GetMembers(Type t) { List <MemberInfo> lifi = new List <MemberInfo>(); var members = ReflectionTool.FieldsAndPropertiesOf(t); foreach (var fi in members .Where((a) => a.GetCustomAttribute(typeof(FieldAttribute)) != null) .ToArray()) { foreach (var at in fi.CustomAttributes) { if (at.AttributeType == typeof(FieldAttribute)) { lifi.Add(fi); } } } return(lifi); }
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); }
public IQueryBuilder GenerateJoinQuery(JoinDefinition inputJoin, IQueryBuilder conditions, int?skip = null, int?limit = null, MemberInfo orderingMember = null, OrderingType otype = OrderingType.Asc, IQueryBuilder rootConditions = null) { if (rootConditions == null) { rootConditions = new QbFmt("true"); } if (inputJoin.Joins.Count < 1) { throw new BDadosException("This join needs 1 or more tables."); } List <Type> tables = (from a in inputJoin.Joins select a.ValueObject).ToList(); List <String> tableNames = (from a in inputJoin.Joins select a.ValueObject.Name).ToList(); List <String> prefixes = (from a in inputJoin.Joins select a.Prefix).ToList(); List <String> aliases = (from a in inputJoin.Joins select a.Alias).ToList(); List <String> onclauses = (from a in inputJoin.Joins select a.Args).ToList(); List <List <String> > columns = (from a in inputJoin.Joins select a.Columns).ToList(); List <JoinType> joinTypes = (from a in inputJoin.Joins select a.Type).ToList(); QueryBuilder Query = new QbFmt("SELECT sub.*\n"); Query.Append($"\t FROM (SELECT\n"); for (int i = 0; i < tables.Count; i++) { Query.Append($"\t\t-- Table {tableNames[i]}\n"); var fields = ReflectionTool.FieldsAndPropertiesOf( tables[i]) .Where((a) => a.GetCustomAttribute(typeof(FieldAttribute)) != null) .ToArray(); if (!columns[i].Contains("RID")) { columns[i].Add("RID"); } var nonexcl = columns[i]; for (int j = 0; j < nonexcl.Count; j++) { Query.Append($"\t\t{prefixes[i]}.{nonexcl[j]} AS {prefixes[i]}_{nonexcl[j]}"); if (true || j < nonexcl.Count - 1 || i < tables.Count - 1) { Query.Append(","); } Query.Append("\n"); } Query.Append("\n"); } Query.Append($"\t\t1 FROM (SELECT * FROM {tableNames[0]}"); if (rootConditions != null) { Query.Append("WHERE "); Query.Append(rootConditions); } if (orderingMember != null) { Query.Append($"ORDER BY {orderingMember.Name} {otype.ToString().ToUpper()}"); } if (limit != null) { Query.Append($"LIMIT"); if (skip != null) { Query.Append($"{skip},"); } Query.Append($"{limit}"); } Query.Append($""); Query.Append($") AS {prefixes[0]}\n"); for (int i = 1; i < tables.Count; i++) { Query.Append($"\t\t{"LEFT"} JOIN {tableNames[i]} AS {prefixes[i]} ON {onclauses[i]}\n"); } if (conditions != null && !conditions.IsEmpty) { Query.Append("\tWHERE"); Query.Append(conditions); } if (orderingMember != null) { Query.Append($"ORDER BY {prefixes[0]}.{orderingMember.Name} {otype.ToString().ToUpper()}"); } if (limit != null) { Query.Append($"LIMIT"); if ((skip ?? 0) > 0) { Query.Append($"{skip}, "); } Query.Append($"{limit}"); } Query.Append(") AS sub\n"); return(Query); }