Skip to content

Commit

Permalink
add BulkInsert UTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Apr 18, 2019
1 parent fcc9cb5 commit 47c2865
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/SmartSql.Bulk.MySql/BulkInsert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public override void Insert()
MySqlBulkLoader bulkLoader = GetBulkLoader(conn);
bulkLoader.Load();
}

public String SecureFilePriv { get; set; }
private string _fieldTerminator = ",";
private char _fieldQuotationCharacter = '"';
private char _escapeCharacter = '"';
Expand All @@ -32,7 +34,7 @@ public override async Task InsertAsync()
await DbSession.OpenAsync();
var conn = DbSession.Connection as MySqlConnection;
MySqlBulkLoader bulkLoader = GetBulkLoader(conn);

await bulkLoader.LoadAsync();
}

Expand All @@ -59,7 +61,7 @@ private string ToCSV()
foreach (DataColumn dataColumn in Table.Columns)
{
if (colIndex != 0) dataBuilder.Append(_fieldTerminator);

if (dataColumn.DataType == CommonType.String
&& !row.IsNull(dataColumn)
&& row[dataColumn].ToString().Contains(_fieldTerminator))
Expand All @@ -75,8 +77,10 @@ private string ToCSV()
}
dataBuilder.Append(_lineTerminator);
}
var fileName = Guid.NewGuid().ToString("N") + ".csv";
fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

var fileName = Guid.NewGuid().ToString("N")+".csv";
var fileDir = SecureFilePriv ?? AppDomain.CurrentDomain.BaseDirectory;
fileName = Path.Combine(fileDir, fileName);
File.WriteAllText(fileName, dataBuilder.ToString());
return fileName;
}
Expand Down
1 change: 1 addition & 0 deletions src/SmartSql.Bulk.PostgreSql/BulkInsert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public BulkInsert(IDbSession dbSession) : base(dbSession)
{

}

public const string DATA_TYPE_NAME = "DataTypeName";
public override void Insert()
{
Expand Down
3 changes: 2 additions & 1 deletion src/SmartSql.Bulk/BulkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static DataTable ToDataTable<TEntity>(this IEnumerable<TEntity> list)
var dataTable = new DataTable(tableName);
foreach (var columnIndex in EntityMetaDataCache<TEntity>.IndexColumnMaps)
{
dataTable.Columns.Add(columnIndex.Value.Name, columnIndex.Value.FieldType);
DataColumn dataColumn=new DataColumn(columnIndex.Value.Name, columnIndex.Value.FieldType);
dataTable.Columns.Add(dataColumn);
}
foreach (var entity in list)
{
Expand Down
39 changes: 39 additions & 0 deletions src/SmartSql.Test.Unit/Bulk/MySqlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using SmartSql.Bulk;
using SmartSql.Bulk.MySql;
using SmartSql.DataSource;
using SmartSql.Test.Entities;
using Xunit;

namespace SmartSql.Test.Unit.Bulk
{
public class MySqlTest
{
[Fact]
public void Insert()
{
var dbSessionFactory = new SmartSqlBuilder()
.UseDataSource(DbProvider.MYSQL, "Data Source=localhost;database=SmartSqlTestDB;uid=root;pwd=SmartSql.net")
.Build().GetDbSessionFactory();

var list = new List<User> {
new User {Id = 1, UserName = "1"}
, new User {Id = 2, UserName = "2"}
};
using (var dbSession = dbSessionFactory.Open())
{
var data = list.ToDataTable();
data.TableName = "t_user";
BulkInsert bulkInsert = new BulkInsert(dbSession)
{
SecureFilePriv = "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads",
Table = data
};
bulkInsert.Insert();
}
}
}

}
38 changes: 38 additions & 0 deletions src/SmartSql.Test.Unit/Bulk/PostgreSqlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
using SmartSql.Bulk;
using SmartSql.Bulk.PostgreSql;
using SmartSql.DataSource;
using SmartSql.Test.Entities;
using Xunit;

namespace SmartSql.Test.Unit.Bulk
{
public class PostgreSqlTest
{
[Fact]
public void Insert()
{
var dbSessionFactory = new SmartSqlBuilder()
.UseDataSource(DbProvider.POSTGRESQL, "Server=localhost;Database=SmartTestDB;Port=5432;User Id=postgres;Password=SmartSql.net;")
.Build().GetDbSessionFactory();

var list = new List<User> {
new User {Id = 1, UserName = "1"}
, new User {Id = 2, UserName = "2"}
};
using (var dbSession = dbSessionFactory.Open())
{
var data = list.ToDataTable();
data.Columns.RemoveAt(0);
data.Columns["UserName"].ColumnName = "user_name";
data.Columns["Status"].ColumnName = "status";
data.TableName = "t_user";
BulkInsert bulkInsert = new BulkInsert(dbSession);
bulkInsert.Table = data;
bulkInsert.Insert();
}
}
}
}
2 changes: 1 addition & 1 deletion src/SmartSql.Test/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public User(long id, string name, UserStatus status)
public String UserName { get; set; }
public UserStatus Status { get; set; }
}
public enum UserStatus
public enum UserStatus : Int16
{
Ok = 1
}
Expand Down
10 changes: 9 additions & 1 deletion src/SmartSql.Test/Maps/User.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
//*******************************-->

<SmartSqlMap Scope="User" xmlns="http://SmartSql.net/schemas/SmartSqlMap.xsd">

<!--<Caches >
<Cache Id="RedisCache" Type="${RedisCacheProvider}">
<Property Name="DatabaseId" Value="3"/>
<Property Name="ConnectionString" Value="${Redis}"/>
<FlushOnExecute Statement="Insert"/>
<FlushOnExecute Statement="Update"/>
<FlushOnExecute Statement="Delete"/>
</Cache>
</Caches>-->
<Statements>
<Statement Id="QueryParams">
<Where>
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/SmartSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />

<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
</ItemGroup>
</Project>
37 changes: 37 additions & 0 deletions src/SmartSql/TypeHandlers/TypeHandlerCacheBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using SmartSql.Configuration;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace SmartSql.TypeHandlers
{
public class TypeHandlerCacheBuilder
{
private AssemblyBuilder _assemblyBuilder;
private ModuleBuilder _moduleBuilder;
private void Init()
{
string assemblyName = "SmartSql.TypeHandlerCacheBuilder" + this.GetHashCode();
_assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName
{
Name = assemblyName
}, AssemblyBuilderAccess.Run);
_moduleBuilder = _assemblyBuilder.DefineDynamicModule(assemblyName + ".dll");
}

public string CreateNameTypeHandlerCacheClassName(string typeHandlerName)
{
return $"{typeHandlerName}_TypeHandler_{this.GetHashCode()}";
}

public Type CreateNameTypeHandler(TypeHandler typeHandler)
{
string className = CreateNameTypeHandlerCacheClassName(typeHandler.Name);
var typeBuilder = _moduleBuilder.DefineType(className, TypeAttributes.Public);

throw new NotImplementedException();
}
}
}

0 comments on commit 47c2865

Please sign in to comment.