/// <summary> /// This will expand foreign key references of the passed entity, loading foreign key /// references into a list (for collections) or an instance (for non-collections). /// </summary> /// <param name="entity"></param> /// <returns></returns> public static object ExpandForeignRefs(ZenDbContext dbContext, object entity) { // If this entity has already had its references expanded, do not process again. var expandedRef = entity as IIsExpanded; if (expandedRef != null) { if (expandedRef.IsExpanded) { return(entity); } else { expandedRef.IsExpanded = true; } } ForeignKeyAttribute attribute; object tempList; object[] results; MethodInfo method; PropertyInfo idProp = entity.GetType().GetProperty("_id", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance); foreach (var prop in entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty)) { attribute = prop.GetCustomAttribute <ForeignKeyAttribute>(); if (attribute != null) { // This is a list of related entities. if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)) { // Find the related entities, using the foreign entity field name from the attribute // and the _id field from the current entity. results = Find(dbContext, dbContext.GetEntityNameFromModelType(prop.PropertyType.GenericTypeArguments[0]), attribute.Name, idProp.GetValue(entity).ToString()); if (results.Length != 0) { tempList = prop.GetValue(entity); method = tempList.GetType().GetMethod("Add"); foreach (var result in results) { method.Invoke(tempList, new object[] { result }); } } } else // This is a single reference to a foreign entity. { var tableName = dbContext.GetEntityNameFromModelType(prop.PropertyType); // Find the related entity, using the value of the property specified in the attribute (from the current entity) // and the _id field of the related entity. results = Find(dbContext, tableName, "_id", entity.GetType().GetProperty(attribute.Name, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public).GetValue(entity).ToString()); if (results.Length != 0) { prop.SetValue(entity, results[0]); } } } } return(entity); }
static void Main(string[] args) { var dbSet = new ZenDbContext(); var commands = GetCommands(); string userCommand; string parameters; int pos; Console.Write("Welcome to Zendesk Search\r\nType 'quit' to exit at any time, Type 'help' to get a list of available commands, Press 'Enter' to continue\r\n"); while (true) { try { // Display current menu options each time through the loop. Console.Write("Zendesk Search>"); userCommand = Console.ReadLine(); pos = userCommand.IndexOf(' '); if (pos != -1) { parameters = userCommand.Substring(pos + 1); userCommand = userCommand.Substring(0, pos); } else { parameters = string.Empty; } // quit and 0 are hard coded always available options. if (userCommand.Equals("quit")) { return; } else if (userCommand.Equals("help")) { Console.WriteLine("Available commands:"); foreach (var item in commands) { Console.WriteLine(item.Key + "\t" + item.Value.HelpText); } } else if (commands.ContainsKey(userCommand)) { commands[userCommand].Execute(dbSet, parameters); } else { Console.WriteLine(string.Format("Unknown command '{0}'", userCommand)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public override void Execute(ZenDbContext dbSet, string parameters) { var builder = new StringBuilder(); foreach (var table in dbSet.Entities) { builder.AppendLine(table.Key); } Console.WriteLine("-------------------------------------"); Console.WriteLine("Searchable tables"); Console.WriteLine(builder.ToString()); }
public override void Execute(ZenDbContext dbContext, string parameters) { var queryOptions = new QueryOptions(parameters); var result = SearchEngine.Find(dbContext, queryOptions.Table, queryOptions.SearchField, queryOptions.SearchOperator, queryOptions.SearchValue); var settings = new JsonSerializerSettings() { // Only include the specified fields in the output. ContractResolver = new FieldsContractResolver(queryOptions.Table, queryOptions.Fields) }; Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented, settings)); Console.WriteLine(string.Format("Found {0} results searching {1} for {2}", result.Length, queryOptions.Table, queryOptions.Where)); }
public override void Execute(ZenDbContext dbSet, string parameters) { parameters = parameters.Trim(); foreach (var table in dbSet.Entities) { if (string.IsNullOrWhiteSpace(parameters) || table.Key.Equals(parameters, StringComparison.CurrentCultureIgnoreCase)) { Console.WriteLine("-------------------------------------"); Console.WriteLine(string.Format("Search {0} with", table.Key)); Console.WriteLine(dbSet.DisplayFieldsForTable(table.Value.EntityType)); } } }
public static object[] Find(ZenDbContext dbContext, string tableName, IEnumerable entities, string fieldName, string op, object textToFind) { PropertyInfo prop = null; object value; object valueToFind = textToFind; var foundEntities = new List <object>(); bool isMatch; // If no field specified, return all items. if (string.IsNullOrWhiteSpace(fieldName)) { foreach (var entity in entities) { ExpandForeignRefs(dbContext, entity); foundEntities.Add(entity); } } else if (fieldName.Equals("_id", StringComparison.CurrentCultureIgnoreCase) && op.Equals("=")) { var singleResult = FindByKey(dbContext, tableName, textToFind); if (singleResult != null) { ExpandForeignRefs(dbContext, singleResult); foundEntities.Add(singleResult); } } else { foreach (var entity in entities) { if (prop == null) { prop = entity.GetType().GetProperty(fieldName, BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (prop == null) { throw new Exception(string.Format("Unknown field name '{0}' in data model class '{1}'", fieldName, entity.GetType().Name)); } try { if (prop.PropertyType.Equals(typeof(bool))) { if (!textToFind.GetType().Equals(typeof(bool))) { valueToFind = Convert.ToBoolean(textToFind); } } else if (prop.PropertyType.Equals(typeof(int))) { if (!textToFind.GetType().Equals(typeof(int))) { valueToFind = Convert.ToInt32(textToFind); } } else if (prop.PropertyType.Equals(typeof(DateTime))) { if (!textToFind.GetType().Equals(typeof(DateTime))) { valueToFind = Convert.ToDateTime(textToFind); } } } catch { throw new Exception(string.Format("Error converting {0} to type {1}", textToFind, prop.PropertyType.Name)); } } value = prop.GetValue(entity); if (value != null) { isMatch = false; if (op.Equals("=") && value.Equals(valueToFind)) { isMatch = true; } else if (op.Equals(">")) { // Casting is slow. Have to figure out a way to do this without casting. if (prop.PropertyType.Equals(typeof(int)) && (int)value > (int)valueToFind) { isMatch = true; } else if (prop.PropertyType.Equals(typeof(DateTime)) && (DateTime)value > (DateTime)valueToFind) { isMatch = true; } } else if (op.Equals("<")) { // Casting is slow. Have to figure out a way to do this without casting. if (prop.PropertyType.Equals(typeof(int)) && (int)value < (int)valueToFind) { isMatch = true; } else if (prop.PropertyType.Equals(typeof(DateTime)) && (DateTime)value < (DateTime)valueToFind) { isMatch = true; } } if (isMatch) { ExpandForeignRefs(dbContext, entity); foundEntities.Add(entity); } } } } return(foundEntities.ToArray()); }
public static object[] Find(ZenDbContext dbContext, string tableName, IEnumerable entities, string fieldName, object textToFind) { return(Find(dbContext, tableName, entities, fieldName, "=", textToFind)); }
public static object FindByKey(ZenDbContext dbContext, string tableName, object textToFind) { var entityDef = dbContext.GetEntityDefinition(tableName); return(entityDef.FindByKey(textToFind)); }
public static object[] Find(ZenDbContext dbContext, string tableName, string fieldName, string op, object textToFind) { return(Find(dbContext, tableName, dbContext.GetListFromTableName(tableName), fieldName, op, textToFind)); }
public virtual void Execute(ZenDbContext dbSet, string parameters) { }