public IDbQuery <TModel> Select(Func <TModel, object> expression) { if (expression == null) { throw new ArgumentNullException("expression"); } var model = expression(new TModel()); var properties = ReflectionHelper.AllProperty(model.GetType(), ReflectionHelper.DefBindingFlags | BindingFlags.DeclaredOnly); if (properties.Count == 0) { throw new NotImplementedException(); } var modelType = typeof(TModel); var tableName = modelType.Name; var tableAliasName = Resolver.ResolveAliasName <TModel>(modelType); Builder.Dispose(); Builder.Add("SELECT"); Builder.Add(" "); var index = 0; foreach (var property in properties) { var aliasName = Resolver.ResolveAliasName <TModel>(property.Member); if (tableName != tableAliasName) { Builder.Add(tableAliasName).Add("."); } if (aliasName != property.Name) { Builder.Add(" AS [").AddColumn(aliasName).Add("]"); } else { Builder.AddColumn(property.Name); } if (index != properties.Count - 1) { Builder.Add(","); } index++; } Builder.Add(" "); Builder.Add("FORM "); Builder.Add(tableName); if (tableName != tableAliasName) { Builder.Add(" AS ").Add(tableAliasName); } return(this); }
/// <summary> /// Attempts to parse a schema specification. /// </summary> /// <param name="spec">The schema specification string.</param> /// <returns>A Schema, or null if it failed to parse.</returns> public static Schema?TryParse(string spec) { var map = ColumnTypeMap.Value; var colSpecs = NewLineRegex.Replace(spec, "").Split(','); var builder = new Builder(); foreach (var colSpec in colSpecs) { var match = ColSpecRegex.Match(colSpec); if (match.Success) { var name = match.Groups["Name"].Value; var typeName = match.Groups["Type"].Value; var allowNull = match.Groups["AllowNull"].Success; if (map.TryGetValue(typeName, out var type)) { builder.AddColumn(name, type, allowNull); continue; } } return(null); } return(builder.Build()); }