public CompiledObjectMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, RowOptions rowOptions) : base(commandBuilder) { m_RowOptions = rowOptions; if (rowOptions.HasFlag(RowOptions.InferConstructor)) { throw new NotSupportedException("Compiled materializers do not support non-default constructors"); } }
TObject?ConstructObject(IReadOnlyDictionary <string, object?>?row, int?rowCount) { if (rowCount == 0 || row == null) { if (!m_RowOptions.HasFlag(RowOptions.PreventEmptyResults)) { return(null); } else { throw new MissingDataException($"No rows were returned and {nameof(RowOptions)}.{nameof(RowOptions.PreventEmptyResults)} was enabled."); } } else if (rowCount > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new UnexpectedDataException($"Expected 1 row but received {rowCount} rows. If this was expected, use `RowOptions.DiscardExtraRows`."); } return(MaterializerUtilities.ConstructObject <TObject>(row, Constructor)); }
private TObject ConstructObject(IReadOnlyDictionary <string, object> row, int?rowCount) { if (rowCount == 0) { if (m_RowOptions.HasFlag(RowOptions.AllowEmptyResults)) { return(null); } else { throw new MissingDataException("No rows were returned"); } } else if (rowCount > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new UnexpectedDataException($"Expected 1 row but received {rowCount} rows"); } return(MaterializerUtilities.ConstructObject <TObject>(row, ConstructorSignature)); }
/// <summary> /// Initializes a new instance of the <see /// cref="Tortuga.Chain.Materializers.ObjectMaterializer{TCommand, TParameter, TObject}"/> class. /// </summary> /// <param name="commandBuilder">The command builder.</param> /// <param name="rowOptions">The row options.</param> /// <exception cref="MappingException"> /// Type {typeof(TObject).Name} has does not have any non-default constructors. or Type /// {typeof(TObject).Name} has more than one non-default constructor. Please use the /// WithConstructor method to specify which one to use. /// </exception> public ObjectMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, RowOptions rowOptions) : base(commandBuilder) { m_RowOptions = rowOptions; if (m_RowOptions.HasFlag(RowOptions.InferConstructor)) { Constructor = InferConstructor(); } }
TObject ConstructObject(IReadOnlyDictionary <string, object?>?row, int?rowCount) { if (rowCount == 0 || row == null) { throw new MissingDataException($"No rows were returned. It was this expected, use `.ToObjectOrNull` instead of `.ToObject`."); } else if (rowCount > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new UnexpectedDataException($"Expected 1 row but received {rowCount} rows. If this was expected, use `RowOptions.DiscardExtraRows`."); } return(MaterializerUtilities.ConstructObject <TObject>(row, Constructor)); }
public override DataRow Execute(object state = null) { var executionToken = Prepare(); var ds = new DataSet() { EnforceConstraints = false /*needed for PostgreSql*/ }; var table = new DataTable(); ds.Tables.Add(table); executionToken.Execute(cmd => { using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { table.Load(reader); return(table.Rows.Count); } }, state); if (table.Rows.Count == 0) { if (m_RowOptions.HasFlag(RowOptions.AllowEmptyResults)) { return(null); } else { throw new MissingDataException("No rows were returned"); } } else if (table.Rows.Count > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new UnexpectedDataException("Expected 1 row but received " + table.Rows.Count + " rows"); } return(table.Rows[0]); }
public override DataRow?Execute(object?state = null) { var executionToken = Prepare(); var ds = new DataSet() { EnforceConstraints = false /*needed for PostgreSql*/ }; var table = new DataTable(); ds.Tables.Add(table); executionToken.Execute(cmd => { using (var reader = cmd.ExecuteReader(CommandBehavior)) { table.Load(reader); return(table.Rows.Count); } }, state); if (table.Rows.Count == 0) { if (!m_RowOptions.HasFlag(RowOptions.PreventEmptyResults)) { return(null); } else { throw new MissingDataException($"No rows were returned and {nameof(RowOptions)}.{nameof(RowOptions.PreventEmptyResults)} was enabled."); } } else if (table.Rows.Count > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new UnexpectedDataException("Expected 1 row but received " + table.Rows.Count + " rows. If this was expected, use `RowOptions.DiscardExtraRows`."); } return(table.Rows[0]); }
public override TObject?Execute(object?state = null) { var result = new List <TObject>(); var executionToken = Prepare(); executionToken.Execute(cmd => { using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { var factory = CompiledMaterializers.CreateBuilder <TObject>(DataSource, cmd.CommandText, reader, CommandBuilder.TryGetNonNullableColumns()); while (reader.Read()) { result.Add(factory(reader)); } return(result.Count); } }, state); if (result.Count == 0) { if (!m_RowOptions.HasFlag(RowOptions.PreventEmptyResults)) { return(null); } else { throw new MissingDataException($"No rows were returned and {nameof(RowOptions)}.{nameof(RowOptions.PreventEmptyResults)} was enabled."); } } else if (result.Count > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new UnexpectedDataException($"Expected 1 row but received {result.Count} rows. Use {nameof(RowOptions)}.{nameof(RowOptions.DiscardExtraRows)} to suppress this error."); } return(result.First()); }
public override TObject Execute(object state = null) { var result = new List <TObject>(); var executionToken = Prepare(); executionToken.Execute(cmd => { using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { var factory = CompiledMaterializers.CreateBuilder <TObject>(DataSource, cmd.CommandText, reader, CommandBuilder.TryGetNonNullableColumns()); while (reader.Read()) { result.Add(factory(reader)); } return(result.Count); } }, state); if (result.Count == 0) { if (m_RowOptions.HasFlag(RowOptions.AllowEmptyResults)) { return(null); } else { throw new DataException("No rows were returned"); } } else if (result.Count > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows)) { throw new DataException("Expected 1 row but received " + result.Count + " rows"); } return(result.First()); }
/// <summary> /// Initializes a new instance of the <see /// cref="Tortuga.Chain.Materializers.ObjectMaterializer{TCommand, TParameter, TObject}"/> class. /// </summary> /// <param name="commandBuilder">The command builder.</param> /// <param name="rowOptions">The row options.</param> /// <exception cref="MappingException"> /// Type {typeof(TObject).Name} has does not have any non-default constructors. or Type /// {typeof(TObject).Name} has more than one non-default constructor. Please use the /// WithConstructor method to specify which one to use. /// </exception> public ObjectOrNullMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, RowOptions rowOptions) : base(commandBuilder) { m_RowOptions = rowOptions; if (m_RowOptions.HasFlag(RowOptions.InferConstructor)) { var constructors = ObjectMetadata.Constructors.Where(x => x.Signature.Length > 0).ToList(); if (constructors.Count == 0) { throw new MappingException($"Type {typeof(TObject).Name} has does not have any non-default constructors."); } if (constructors.Count > 1) { throw new MappingException($"Type {typeof(TObject).Name} has more than one non-default constructor. Please use the WithConstructor method to specify which one to use."); } Constructor = constructors[0]; } }