internal static Result BuildReturnResult(SchemaProviderBase provider) { return(new Result { Name = returnResult, Type = "global::DbSharper.Library.Data.ReturnResult", Description = "Return result.", IsOutputParameter = true }); }
/// <summary> /// Create a mapping. /// </summary> /// <param name="mappingConfigFile">Mapping configuration file.</param> /// <param name="mappingConfigContent">Mapping configuration file content.</param> /// <returns>Mapping.</returns> public static Mapping CreateMapping(string mappingConfigFile, string mappingConfigContent, NamedCollection <Enumeration> enumerations) { if (string.IsNullOrEmpty(mappingConfigFile)) { throw new ArgumentNullException("mappingConfigFile"); } if (string.IsNullOrEmpty(mappingConfigContent)) { throw new ArgumentNullException("mappingConfigContent"); } if (!File.Exists(mappingConfigFile)) { // TODO: Embed string into resource file later. throw new FileNotFoundException( string.Format(CultureInfo.InvariantCulture, "{0} is not found.", mappingConfigFile), mappingConfigFile); } MappingConfiguration mappingConfig = new MappingConfiguration(mappingConfigContent); mappingRuleManager = new MappingRuleManager(mappingConfig); string configFile = Path.Combine(Path.GetDirectoryName(mappingConfigFile), mappingConfig.ConfigFile); string connectionStringName = MappingHelper.GetConnectionStringName(mappingConfigFile); ConnectionStringSettings settings = MappingHelper.GetConnectionStringSettings(configFile, connectionStringName); if (string.IsNullOrEmpty(settings.ConnectionString)) { // TODO: Embed string into resource file later. throw new DbSharperException(string.Format(CultureInfo.InvariantCulture, "Value of connection string named \"{0}\" is null or empty.", connectionStringName)); } if (string.IsNullOrEmpty(settings.ProviderName)) { // TODO: Embed string into resource file later. throw new DbSharperException(string.Format(CultureInfo.InvariantCulture, "ProviderName of connection string named \"{0}\" is null or empty.", connectionStringName)); } provider = SchemaProviderFactory.Create(settings.ProviderName); Database.Database database = provider.GetSchema(settings.ConnectionString); mapping = GetMapping(database, connectionStringName, provider.GetDatabaseType().FullName, enumerations); MappingExtender extender = new MappingExtender(mapping, provider); //extender.Extend(); return(mapping); }
internal static Parameter BuildReturnResultParameter(SchemaProviderBase provider) { return(new Parameter { Name = returnResult, CamelCaseName = returnResult.ToCamelCase(), SqlName = provider.BuildParameterSqlName(returnResult), Description = "Return result.", EnumType = "global::DbSharper.Library.Data.ReturnResult", DbType = DbType.Int32, Size = 0, Type = "global::System.Int32", Direction = ParameterDirection.ReturnValue, }); }
/// <summary> /// Transform a model property to a method parameter. /// </summary> /// <param name="property">Model property.</param> /// <param name="isList"></param> /// <returns>Method paramter.</returns> internal static Parameter PropertyToParameter(SchemaProviderBase provider, Property property, bool isList) { try { string name = isList ? property.Name + "List" : property.Name; return(new Parameter { Name = name, CamelCaseName = name.ToCamelCase(), Description = property.Description, Direction = ParameterDirection.Input, Size = property.Size, DbType = property.DbType, SqlName = provider.BuildParameterSqlName(property.Name), Type = isList ? string.Format(CultureInfo.InvariantCulture, "global::System.Collections.Generic.List<global::System.{0}>", property.Type) : property.Type }); } catch (ArgumentException) { return(null); } }
public void SetForeignKeyMemberNameTest() { var thisTable = new TableSchema { TableName = "Xxx", }; var otherTable = new TableSchema { TableName = "Zzz", }; var key = new ForeignKeySchema { KeyName = "FK_Xxx_YyyZzz", MemberName = "FK_Xxx_YyyZzz", ThisColumns = new List <ColumnSchema> { new ColumnSchema { MemberName = "XxxID", IsPrimaryKey = true }, new ColumnSchema { MemberName = "YyyZzzID" }, }, OtherColumns = new List <ColumnSchema> { new ColumnSchema { MemberName = "ZzzID" }, }, ThisTable = thisTable, OtherTable = otherTable, }; var key1 = new ForeignKeySchema { KeyName = "FK_Xxx_Zzz", MemberName = "FK_Xxx_Zzz", ThisColumns = new List <ColumnSchema> { new ColumnSchema { MemberName = "XxxID", IsPrimaryKey = true }, new ColumnSchema { MemberName = "ZzzID" }, }, OtherColumns = new List <ColumnSchema> { new ColumnSchema { MemberName = "ZzzID" }, }, ThisTable = thisTable, OtherTable = otherTable, }; key.ThisTable.ForeignKeys = new List <ForeignKeySchema> { key, key1 }; key.ThisTable.Columns = key.ThisColumns; key.BackReference = new ForeignKeySchema { KeyName = key.KeyName + "_BackReference", MemberName = key.MemberName + "_BackReference", AssociationType = AssociationType.Auto, OtherTable = key.ThisTable, ThisColumns = key.OtherColumns, OtherColumns = key.ThisColumns, }; SchemaProviderBase.SetForeignKeyMemberName(new GetSchemaOptions { }, key.ThisTable, key); Assert.That(key.MemberName, Is.EqualTo("YyyZzz")); }
public void ToValidNameTest() { Assert.AreEqual("_1", SchemaProviderBase.ToValidName("1")); Assert.AreEqual("_1", SchemaProviderBase.ToValidName(" 1 ")); Assert.AreEqual("_1", SchemaProviderBase.ToValidName("\t1\t")); }
internal MappingExtender(Mapping mapping, SchemaProviderBase provider) { this.mapping = mapping; this.provider = provider; }
void PgSqlFixRecordResultFunctions() { var mappings = new List <(string, IList <string>)>(); foreach (var proc in _schema.Procedures .Where(p => p.IsFunction && !p.IsAggregateFunction && !p.IsTableFunction && p.Parameters.Any(pr => pr.IsOut))) { if (proc.Parameters.Count(pr => pr.IsOut) > 1) { var result = new TableSchema() { TypeName = SchemaProviderBase.ToValidName(proc.ProcedureName + "Result"), Columns = new List <ColumnSchema>(), IsProcedureResult = true, ForeignKeys = new List <ForeignKeySchema>() }; _schema.Tables.Add(result); proc.ResultTable = result; proc.Parameters.Add(new ParameterSchema() { IsResult = true, ParameterType = result.TypeName }); var resultMappings = new List <string>(); mappings.Add((result.TypeName, resultMappings)); foreach (var outParam in proc.Parameters.Where(_ => _.IsOut)) { //outParam.ParameterType, outParam.ParameterName, null, null result.Columns.Add(new ColumnSchema() { MemberType = outParam.ParameterType, MemberName = outParam.ParameterName }); resultMappings.Add($"{outParam.ParameterName} = ({outParam.ParameterType})tuple[{resultMappings.Count}]"); if (outParam.IsIn) { outParam.IsOut = false; } } proc.Parameters = proc.Parameters.Where(_ => !_.IsOut).ToList(); } else // one parameter { var param = proc.Parameters.Single(_ => _.IsOut); proc.Parameters.Remove(param); proc.Parameters.Add(new ParameterSchema() { IsResult = true, ParameterType = param.ParameterType }); } } if (mappings.Count > 0) { Code .AppendLine(" protected override void InitMappingSchema()") .AppendLine(" {"); foreach (var(typeName, valueMappings) in mappings) { Code.AppendLine($" MappingSchema.SetConvertExpression<object[], {typeName}>(tuple => new {typeName}() {{ {string.Join(", ", valueMappings)} }});"); } Code .AppendLine(" }"); } }
internal static Parameter PropertyToParameter(SchemaProviderBase provider, Property property) { return(PropertyToParameter(provider, property, false)); }