/* * new { * Table = "Customer", * Where = new { * Name = "Monkey", * And = new { * LastName = "Tail" * }, * Or = new { * LastName = "Guy" * } * } * } * */ private QueryFilter ParseWhere(string tableName, dynamic where) { Type type = where.GetType(); QueryFilter filter = new QueryFilter(); foreach (PropertyInfo prop in type.GetProperties()) { if (TablePropertyNames.Contains(prop.Name)) { continue; } if (prop.Name.Equals("And")) { object and = prop.GetValue(where); filter = filter.IsEmpty ? ParseWhere(tableName, and) : filter && ParseWhere(tableName, and); } else if (prop.Name.Equals("Or")) { object or = prop.GetValue(where); filter = filter.IsEmpty ? ParseWhere(tableName, or) : filter || ParseWhere(tableName, or); } else if (prop.Name.Equals("Where")) { filter = filter.IsEmpty ? ParseWhere(tableName, prop.GetValue(where)) : filter && ParseWhere(tableName, where); } else { // TODO: enable multiple operators like QiQuery, contains, doesn't contain starts with etc string columnName = NameMap.GetColumnName(tableName, prop.Name); QueryFilter queryFilter = Bam.Net.Data.Query.Where(columnName) == prop.GetValue(where); filter = filter.IsEmpty ? queryFilter : filter && queryFilter; } } return(filter); }
private List <AssignValue> GetValueAssignments(dynamic value, string tableName) { Type type = value.GetType(); List <AssignValue> assignValues = new List <AssignValue>(); foreach (PropertyInfo prop in type.GetProperties()) { if (KeywordProperties.Contains(prop.Name)) { continue; } assignValues.Add(new AssignValue(NameMap.GetColumnName(tableName, prop.Name), prop.GetValue(value))); } return(assignValues); }
private SqlStringBuilder WriteRetrieve(string table, SqlStringBuilder sql, dynamic querySpec) { string columns = ReflectionExtensions.Property <string>(querySpec, "Columns", false) ?? "*"; string[] columnNames = new string[] { columns }; if (!columns.Equals("*")) { columnNames = columns.DelimitSplit(",").Select(s => NameMap.GetColumnName(table, s)).ToArray(); } dynamic whereSpec = ReflectionExtensions.Property(querySpec, "Where") ?? ReflectionExtensions.Property(querySpec, "where"); sql.Select(table); if (whereSpec != null) { sql.Where(ParseWhere(table, whereSpec)); } return(sql); }