Skip to content

Commit

Permalink
Fix Issue #55 Problem with GuessType in BulkSql/BulkOperation.cs
Browse files Browse the repository at this point in the history
This is also a fix to #61: SqlBulkAsync throws error System.ArgumentException: Value does not fall within the expected range.
  • Loading branch information
hel1e committed May 11, 2023
1 parent f2443ea commit 50718a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/Dapper.Oracle/BulkSql/BulkOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public static int SqlBulk<T>(this IDbConnection connection, string sql, IEnumera
return connection.Execute(sql, parameters, commandType: cmdType);
}

public static async Task<AsyncQueryResult> SqlBulkAsync<T>(this IDbConnection connection, string sql, IEnumerable<T> objects, IEnumerable<BulkMapping<T>> mapping, CommandType? cmdType = CommandType.Text, IDbTransaction transaction=null)
public static async Task<AsyncQueryResult> SqlBulkAsync<T>(this IDbConnection connection, string sql, IEnumerable<T> objects, IEnumerable<BulkMapping<T>> mapping, CommandType? cmdType = CommandType.Text, IDbTransaction transaction = null)
{
var parameters = CreateParameterFromObject(objects, mapping);
var result = await connection.ExecuteAsync(sql, parameters,transaction, commandType:cmdType);
var result = await connection.ExecuteAsync(sql, parameters, transaction, commandType: cmdType);
return new AsyncQueryResult
{
ExecuteResult = result,
Expand All @@ -76,7 +76,8 @@ private static OracleDynamicParameters CreateParameterFromObject<T>(IEnumerable<
foreach (var map in mapping)
{
var values = map.Property != null ? obj.Select(map.Property).ToArray() : null;
var dbType = map.DbType ?? OracleMapper.GuessType(obj.First().GetType());

var dbType = map.DbType ?? (values != null ? OracleMapper.GuessType(values.First().GetType()) : null);

parameters.Add(
Clean(map.Name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public BulkOperationTests(DatabaseFixture fixture)
new TableColumn {Name = "ADDRESS", DataType = OracleMappingType.Varchar2, Size = 60},
new TableColumn {Name = "POSTALCODE", DataType = OracleMappingType.Varchar2, Size = 40, Nullable=true},
new TableColumn {Name = "COUNTRY", DataType = OracleMappingType.Varchar2, Size = 40},
new TableColumn {Name = "PHONE", DataType = OracleMappingType.Varchar2, Size = 40},
new TableColumn {Name = "PHONE", DataType = OracleMappingType.Varchar2, Size = 40, Nullable=true},
new TableColumn {Name = "FAX", DataType = OracleMappingType.Varchar2, Size = 40, Nullable=true},
new NumberColumn {Name = "TIDSSTEMPEL", DataType = OracleMappingType.Int64},
new TableColumn {Name = "OPPRETTETAV", DataType = OracleMappingType.Varchar2, Size = 40},
new TableColumn {Name = "OPPRETTETTID", DataType = OracleMappingType.Date},
new TableColumn {Name = "SISTENDRETAV", DataType = OracleMappingType.Varchar2, Size=40},
new TableColumn {Name = "SISTENDRETTID", DataType = OracleMappingType.Date},
new NumberColumn {Name = "DIPSID", DataType = OracleMappingType.Int64},
new NumberColumn {Name = "TIDSSTEMPEL", DataType = OracleMappingType.Int64, Nullable=true},
new TableColumn {Name = "OPPRETTETAV", DataType = OracleMappingType.Varchar2, Size = 40, Nullable=true},
new TableColumn {Name = "OPPRETTETTID", DataType = OracleMappingType.Date, Nullable=true},
new TableColumn {Name = "SISTENDRETAV", DataType = OracleMappingType.Varchar2, Size=40, Nullable=true},
new TableColumn {Name = "SISTENDRETTID", DataType = OracleMappingType.Date, Nullable=true},
new NumberColumn {Name = "DIPSID", DataType = OracleMappingType.Int64, Nullable=true},
};

TableCreator.Create(Fixture.Connection, "BULKCUSTOMERS", columns);
Expand Down Expand Up @@ -143,15 +143,21 @@ public CustomerDAL(IDbConnection connection)

public void InsertCustomers(IEnumerable<Customer> customers)
{
string insertSql = "INSERT INTO CUSTOMERS(CUSTOMERID,NAME,ADDRESS,POSTALCODE,CITY) VALUES(:CUSTOMERID,:NAME,:POSTALCODE,:CITY)";
string insertSql = @"INSERT INTO BULKCUSTOMERS(CUSTOMERID,COMPANYNAME,ADDRESS,POSTALCODE,CITY, CONTACTNAME, CONTACTTITLE, Country, tidsstempel, opprettettid)
VALUES(:CUSTOMERID,:COMPANYNAME,:ADDRESS, :POSTALCODE,:CITY, :ContactName, :CONTACTTITLE, :Country, :tidsstempel, :opprettettid)";

var mapping = new BulkMapping<Customer>[]
{
new BulkMapping<Customer>("CUSTOMERID",c=>c.CustomerId),
new BulkMapping<Customer>("NAME",c=>c.ContactName),
new BulkMapping<Customer>("COMPANYNAME",c=>c.ContactName),
new BulkMapping<Customer>("ADDRESS",c=>c.Address),
new BulkMapping<Customer>("POSTALCODE",c=>c.PostalCode),
new BulkMapping<Customer>("CITY",c=>c.City),
new BulkMapping<Customer>("ContactName",c=>c.ContactName),
new BulkMapping<Customer>("ContactTitle",c=>c.ContactTitle),
new BulkMapping<Customer>("Country", c=> c.Country),
new BulkMapping<Customer>("Tidsstempel", c=> c.TidsStempel),
new BulkMapping<Customer>("Opprettettid", c=> c.OpprettetTid)
};


Expand Down

0 comments on commit 50718a6

Please sign in to comment.