public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var parameters = BuildHelper.GetParameters(option, mi); var tableName = table ?? (type != null ? BuildHelper.GetTableNameOfType(option, type) : null) ?? BuildHelper.GetTableName(option, mi); if (String.IsNullOrEmpty(tableName)) { throw new BuilderException($"Table name resolve failed. type=[{mi.DeclaringType.FullName}], method=[{mi.Name}]"); } var sql = new StringBuilder(); sql.Append("INSERT INTO "); sql.Append(tableName); sql.Append(" ("); BuildHelper.AddInsertColumns(sql, mi, parameters); sql.Append(") VALUES ("); BuildHelper.AddInsertValues(sql, mi, parameters); sql.Append(")"); var tokenizer = new SqlTokenizer(sql.ToString()); var builder = new NodeBuilder(tokenizer.Tokenize()); return(builder.Build()); }
//-------------------------------------------------------------------------------- // Parameter //-------------------------------------------------------------------------------- public static IList <BuildParameterInfo> GetParameters(IGeneratorOption option, MethodInfo mi) { var naming = option.GetValue("FieldNaming"); var parameters = new List <BuildParameterInfo>(); foreach (var pmi in mi.GetParameters().Where(ParameterHelper.IsSqlParameter)) { if (ParameterHelper.IsNestedParameter(pmi)) { parameters.AddRange(pmi.ParameterType.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(x => x.GetCustomAttribute <IgnoreAttribute>() == null) .Select(pi => new BuildParameterInfo( pmi, pi, $"{pmi.Name}.{pi.Name}", pi.GetCustomAttribute <NameAttribute>()?.Name ?? NormalizeName(pi.Name, naming)))); } else { parameters.Add(new BuildParameterInfo( pmi, null, pmi.Name, pmi.GetCustomAttribute <NameAttribute>()?.Name ?? NormalizeName(pmi.Name, naming))); } } return(parameters); }
public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var parameters = BuildHelper.GetParameters(option, mi); var keys = BuildHelper.GetKeyParameters(parameters); var tableName = table ?? (type != null ? BuildHelper.GetTableNameOfType(option, type) : null) ?? BuildHelper.GetTableName(option, mi); var conditions = keys.Count > 0 ? keys : parameters; if (String.IsNullOrEmpty(tableName)) { throw new BuilderException($"Table name resolve failed. type=[{mi.DeclaringType.FullName}], method=[{mi.Name}]"); } if (!Force && (conditions.Count == 0)) { throw new BuilderException($"Delete all requires force option. type=[{mi.DeclaringType.FullName}], method=[{mi.Name}]"); } var sql = new StringBuilder(); sql.Append("DELETE FROM "); sql.Append(tableName); BuildHelper.AddCondition(sql, conditions); var tokenizer = new SqlTokenizer(sql.ToString()); var builder = new NodeBuilder(tokenizer.Tokenize()); return(builder.Build()); }
public TestFactoryBuilder ConfigureOptions(Action <Dictionary <string, string> > action) { var map = new Dictionary <string, string>(); action(map); option = new TestGeneratorOption(map); return(this); }
public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var tokenizer = new SqlTokenizer(sql); var tokens = tokenizer.Tokenize(); var builder = new NodeBuilder(tokens); return(builder.Build()); }
public static string GetTableNameOfType(IGeneratorOption option, Type type) { var suffix = option.GetValueAsStringArray("EntityClassSuffix"); var match = suffix.FirstOrDefault(x => type.Name.EndsWith(x)); return(match == null ? type.Name : type.Name.Substring(0, type.Name.Length - match.Length)); }
public static string[] GetValueAsStringArray(this IGeneratorOption option, string key) { var value = option.GetValue(key); if (String.IsNullOrEmpty(value)) { return(Array.Empty <string>()); } return(value.Split(',').Select(x => x.Trim()).ToArray()); }
public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var nodes = new List <INode> { new SqlNode(procedure) }; nodes.AddRange(BuildHelper.GetParameters(option, mi).Select(x => new ParameterNode(x.Name, x.ParameterName))); return(nodes); }
//-------------------------------------------------------------------------------- // Table //-------------------------------------------------------------------------------- public static string GetTableName(IGeneratorOption option, MethodInfo mi) { var parameter = mi.GetParameters() .FirstOrDefault(x => ParameterHelper.IsSqlParameter(x) && ParameterHelper.IsNestedParameter(x)); if (parameter == null) { return(null); } return(GetTableNameOfType(option, parameter.ParameterType)); }
public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var tableName = table ?? BuildHelper.GetTableNameOfType(option, type); if (String.IsNullOrEmpty(tableName)) { throw new BuilderException($"Table name resolve failed. type=[{mi.DeclaringType.FullName}], method=[{mi.Name}]"); } return(new[] { new SqlNode($"TRUNCATE TABLE {tableName}") }); }
//-------------------------------------------------------------------------------- // Helper //-------------------------------------------------------------------------------- public static string MakeKeyColumns(IGeneratorOption option, Type type) { var naming = option.GetValue("FieldNaming"); var elementType = ParameterHelper.IsMultipleParameter(type) ? ParameterHelper.GetMultipleParameterElementType(type) : type; return(String.Join(", ", elementType.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(x => new { Property = x, Key = x.GetCustomAttribute <KeyAttribute>() }) .Where(x => x.Key != null) .OrderBy(x => x.Key.Order) .Select(x => { var name = x.Property.GetCustomAttribute <NameAttribute>(); return NormalizeName(name != null ? name.Name : x.Property.Name, naming); }))); }
public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var parameters = BuildHelper.GetParameters(option, mi); var order = BuildHelper.PickParameter <OrderAttribute>(parameters); var tableName = table ?? (type != null ? BuildHelper.GetTableNameOfType(option, type) : null) ?? BuildHelper.GetReturnTableName(option, mi); if (String.IsNullOrEmpty(tableName)) { throw new BuilderException($"Table name resolve failed. type=[{mi.DeclaringType.FullName}], method=[{mi.Name}]"); } var sql = new StringBuilder(); sql.Append("SELECT * FROM "); sql.Append(tableName); BuildHelper.AddCondition(sql, parameters); if (order != null) { sql.Append(" ORDER BY "); sql.Append($"/*# {order.Name} */dummy"); } else if (!String.IsNullOrEmpty(Order)) { sql.Append(" ORDER BY "); sql.Append(Order); } else { var columns = BuildHelper.MakeKeyColumns(option, mi.ReturnType); if (!String.IsNullOrEmpty(columns)) { sql.Append(" ORDER BY "); sql.Append(columns); } } var tokenizer = new SqlTokenizer(sql.ToString()); var builder = new NodeBuilder(tokenizer.Tokenize()); return(builder.Build()); }
public override IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi) { var parameters = BuildHelper.GetParameters(option, mi); var tableName = table ?? BuildHelper.GetTableNameOfType(option, type); if (String.IsNullOrEmpty(tableName)) { throw new BuilderException($"Table name resolve failed. type=[{mi.DeclaringType.FullName}], method=[{mi.Name}]"); } var sql = new StringBuilder(); sql.Append("SELECT "); sql.Append(field); sql.Append(" FROM "); sql.Append(tableName); BuildHelper.AddCondition(sql, parameters); var tokenizer = new SqlTokenizer(sql.ToString()); var builder = new NodeBuilder(tokenizer.Tokenize()); return(builder.Build()); }
public static string GetReturnTableName(IGeneratorOption option, MethodInfo mi) { var isAsync = mi.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null; if (isAsync && !mi.ReturnType.IsGenericType) { return(null); } var returnType = isAsync ? mi.ReturnType.GetGenericArguments()[0] : mi.ReturnType; var elementType = ParameterHelper.IsMultipleParameter(returnType) ? ParameterHelper.GetMultipleParameterElementType(returnType) : returnType; if (!ParameterHelper.IsNestedType(elementType)) { return(null); } return(GetTableNameOfType(option, elementType)); }
public abstract IReadOnlyList <INode> GetNodes(ISqlLoader loader, IGeneratorOption option, MethodInfo mi);
public TestFactory(ISqlLoader loader, IGeneratorOption option, ExecuteEngine engine) { this.loader = loader; this.option = option; Engine = engine; }
public DataAccessorGenerator(ISqlLoader sqlLoader, ISourceWriter sourceWriter, IGeneratorOption option) { this.sqlLoader = sqlLoader; this.sourceWriter = sourceWriter; this.option = option; }