mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Db.Fastest pgsql
This commit is contained in:
parent
3c706401c9
commit
48cd5570f7
62
Src/Asp.Net/PgSqlTest/Demo/DemoO_Fastest.cs
Normal file
62
Src/Asp.Net/PgSqlTest/Demo/DemoO_Fastest.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using OrmTest;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class TestFAST11
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsArray =true, ColumnDataType ="text []")]
|
||||
public string[] Array { get; set; }
|
||||
|
||||
public int Sex { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
|
||||
public string Id { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable = true)]
|
||||
public long X { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable = true,IsJson =true,ColumnDataType ="json")]
|
||||
public string [] json { get; set; }
|
||||
}
|
||||
|
||||
public class DemoO_Fastest
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Insertable Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.PostgreSQL,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
db.CodeFirst.InitTables<TestFAST11>();
|
||||
db.Fastest<TestFAST11>().BulkCopy(new List<TestFAST11>() {
|
||||
new TestFAST11(){ Array=new string[]{ "2"}, Date=DateTime.Now, Id=Guid.NewGuid()+"", Sex=1 , X=11,json=new string[]{ "x"} }
|
||||
});
|
||||
var data = new List<TestFAST11>() {
|
||||
new TestFAST11(){ Array=new string[]{ "2"}, Date=DateTime.Now, Id=Guid.NewGuid()+"", Sex=1 , X=11,json=new string[]{ "x"} }
|
||||
};
|
||||
//db.Updateable(data).ExecuteCommand();
|
||||
db.Fastest<TestFAST11>().BulkUpdate(data);
|
||||
var x = db.Queryable<TestFAST11>().ToList();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -85,6 +85,7 @@
|
||||
<Compile Include="Demo\DemoG_SimpleClient.cs" />
|
||||
<Compile Include="Demo\DemoJ_Report.cs" />
|
||||
<Compile Include="Demo\DemoN_SplitTable.cs" />
|
||||
<Compile Include="Demo\DemoO_Fastest.cs" />
|
||||
<Compile Include="Models\AttributeTable.cs" />
|
||||
<Compile Include="Models\CarType.cs" />
|
||||
<Compile Include="Models\Custom.cs" />
|
||||
|
@ -22,7 +22,7 @@ namespace SqlSugar
|
||||
case DbType.Oracle:
|
||||
break;
|
||||
case DbType.PostgreSQL:
|
||||
break;
|
||||
return new PostgreSQLFastBuilder(this.entityInfo);
|
||||
case DbType.Dm:
|
||||
break;
|
||||
case DbType.Kdbndp:
|
||||
@ -97,14 +97,14 @@ namespace SqlSugar
|
||||
{
|
||||
value = Convert.ToDateTime("1900-01-01");
|
||||
}
|
||||
else if (columnInfo.IsJson)
|
||||
{
|
||||
columnInfo.IsJson = true;
|
||||
}
|
||||
else if (columnInfo.IsArray)
|
||||
{
|
||||
columnInfo.IsArray = true;
|
||||
}
|
||||
//else if (columnInfo.IsJson)
|
||||
//{
|
||||
// columnInfo.IsJson = true;
|
||||
//}
|
||||
//else if (columnInfo.IsArray)
|
||||
//{
|
||||
// columnInfo.IsArray = true;
|
||||
//}
|
||||
else if (columnInfo.UnderType.IsEnum() )
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
|
@ -0,0 +1,123 @@
|
||||
using Npgsql;
|
||||
using NpgsqlTypes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class PostgreSQLFastBuilder : FastBuilder, IFastBuilder
|
||||
{
|
||||
public static Dictionary<string , NpgsqlDbType> PgSqlType = UtilMethods.EnumToDictionary<NpgsqlDbType>();
|
||||
private EntityInfo entityInfo;
|
||||
|
||||
public PostgreSQLFastBuilder(EntityInfo entityInfo)
|
||||
{
|
||||
this.entityInfo = entityInfo;
|
||||
}
|
||||
|
||||
public override string UpdateSql { get; set; } = @"UPDATE {1} SET {0} FROM {2} AS TE WHERE {3}
|
||||
";
|
||||
|
||||
//public virtual async Task<int> UpdateByTempAsync(string tableName, string tempName, string[] updateColumns, string[] whereColumns)
|
||||
//{
|
||||
// Check.ArgumentNullException(!updateColumns.Any(), "update columns count is 0");
|
||||
// Check.ArgumentNullException(!whereColumns.Any(), "where columns count is 0");
|
||||
// var sets = string.Join(",", updateColumns.Select(it => $"TM.{it}=TE.{it}"));
|
||||
// var wheres = string.Join(",", whereColumns.Select(it => $"TM.{it}=TE.{it}"));
|
||||
// string sql = string.Format(UpdateSql, sets, tableName, tempName, wheres);
|
||||
// return await this.Context.Ado.ExecuteCommandAsync(sql);
|
||||
//}
|
||||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
|
||||
{
|
||||
List<string> lsColNames = new List<string>();
|
||||
for (int i = 0; i < dt.Columns.Count; i++)
|
||||
{
|
||||
lsColNames.Add($"\"{dt.Columns[i].ColumnName}\"");
|
||||
}
|
||||
string copyString = $"COPY {dt.TableName} ( {string.Join(",", lsColNames) } ) FROM STDIN (FORMAT BINARY)";
|
||||
NpgsqlConnection conn = (NpgsqlConnection)this.Context.Ado.Connection;
|
||||
var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(this.entityInfo.DbTableName);
|
||||
try
|
||||
{
|
||||
var identityColumnInfo = this.entityInfo.Columns.FirstOrDefault(it => it.IsIdentity);
|
||||
if (identityColumnInfo!=null)
|
||||
{
|
||||
throw new Exception("PgSql bulkcopy no support identity");
|
||||
}
|
||||
BulkCopy(dt, copyString, conn, columns);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
base.CloseDb();
|
||||
throw ex;
|
||||
}
|
||||
return await Task.FromResult(dt.Rows.Count);
|
||||
}
|
||||
|
||||
private static void BulkCopy(DataTable dt, string copyString, NpgsqlConnection conn, List<DbColumnInfo> columns)
|
||||
{
|
||||
if (conn.State == ConnectionState.Closed)
|
||||
conn.Open();
|
||||
using (var writer = conn.BeginBinaryImport(copyString))
|
||||
{
|
||||
foreach (DataRow row in dt.Rows)
|
||||
{
|
||||
writer.StartRow();
|
||||
foreach (DataColumn kvp in dt.Columns)
|
||||
{
|
||||
var first = columns.FirstOrDefault(it => it.DbColumnName.EqualCase(kvp.ColumnName));
|
||||
var key = first.DataType.ToLower();
|
||||
if (PgSqlType.ContainsKey(key))
|
||||
{
|
||||
WriterRow(writer, row, first);
|
||||
}
|
||||
else if (first.DataType.First() == '_')
|
||||
{
|
||||
var type = PgSqlType[key.Substring(1)];
|
||||
writer.Write(row[kvp.ColumnName], NpgsqlDbType.Array | type);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write(row[kvp.ColumnName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
writer.Complete();
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriterRow(NpgsqlBinaryImporter writer, DataRow row, DbColumnInfo colInfo)
|
||||
{
|
||||
if (PgSqlType.ContainsKey(colInfo.DataType.ToLower()))
|
||||
{
|
||||
if (row[colInfo.DbColumnName] == null || row[colInfo.DbColumnName] == DBNull.Value)
|
||||
{
|
||||
writer.Write(DBNull.Value, NpgsqlDbType.Integer);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write(row[colInfo.DbColumnName], PgSqlType[colInfo.DataType.ToLower()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
public override async Task<int> UpdateByTempAsync(string tableName, string tempName, string[] updateColumns, string[] whereColumns)
|
||||
{
|
||||
var sqlquerybulder= this.Context.Queryable<object>().SqlBuilder;
|
||||
Check.ArgumentNullException(!updateColumns.Any(), "update columns count is 0");
|
||||
Check.ArgumentNullException(!whereColumns.Any(), "where columns count is 0");
|
||||
var sets = string.Join(",", updateColumns.Select(it => $"{sqlquerybulder.GetTranslationColumnName(it)}=TE.{sqlquerybulder.GetTranslationColumnName(it)}"));
|
||||
var wheres = string.Join(",", whereColumns.Select(it => $"{tableName}.{sqlquerybulder.GetTranslationColumnName(it)}=TE.{sqlquerybulder.GetTranslationColumnName(it)}"));
|
||||
string sql = string.Format(UpdateSql, sets, tableName, tempName, wheres);
|
||||
return await this.Context.Ado.ExecuteCommandAsync(sql);
|
||||
}
|
||||
public override async Task CreateTempAsync<T>(DataTable dt)
|
||||
{
|
||||
await this.Context.Queryable<T>().Where(it => false).AS(dt.TableName).Select(" * into temp mytemptable").ToListAsync();
|
||||
dt.TableName = "mytemptable";
|
||||
}
|
||||
}
|
||||
}
|
@ -96,6 +96,7 @@
|
||||
<Compile Include="Interface\IFastBuilder.cs" />
|
||||
<Compile Include="Interface\IFastest.cs" />
|
||||
<Compile Include="Realization\MySql\SqlBuilder\MySqlFastBuilder.cs" />
|
||||
<Compile Include="Realization\PostgreSQL\SqlBuilder\PostgreSQLFastBuilder.cs" />
|
||||
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerFastBuilder.cs" />
|
||||
<Compile Include="SpliteTable\SplitTableAttribute.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
|
@ -411,5 +411,22 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, T> EnumToDictionary<T>()
|
||||
{
|
||||
Dictionary<string, T> dic = new Dictionary<string, T>();
|
||||
if (!typeof(T).IsEnum)
|
||||
{
|
||||
return dic;
|
||||
}
|
||||
string desc = string.Empty;
|
||||
foreach (var item in Enum.GetValues(typeof(T)))
|
||||
{
|
||||
var key = item.ToString().ToLower();
|
||||
if (!dic.ContainsKey(key))
|
||||
dic.Add(key, (T)item);
|
||||
}
|
||||
return dic;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user